Class: Space

Inherits:
ApplicationRecord show all
Includes:
AffiliatableParent, AlgoliaSearch, AttrJson::Record, Avatarable, Bannerable, Boardable, Feedable, Linkable, Mediumable, Membership, NotificationsHelpers, RecsysHelpers, RelationHelpers, Serviceable, Utils
Defined in:
app/models/space.rb

Overview

Umbrella object that holds this hierarchy: spaces -> program -> challenges -> projects -> needs Users can become affiliated to the space

Instance Method Summary collapse

Methods included from Utils

#obj_type, #reindex, #sanitize, #sanitize_content, #sanitize_description

Methods included from Serviceable

serviceable?

Methods included from RelationHelpers

#clappers, #claps_count, #followers, #followers_count, #reviews_count, #saves_count

Methods included from RecsysHelpers

#add_edge, #remove_edge

Methods included from NotificationsHelpers

#notif_new_follower, #notif_new_member, #notif_pending_member

Methods included from Membership

#admins, #has_privacy?, #owners, #pending_members, #user_is_member?, #users, #users_sm

Methods included from Feedable

#posts_count

Methods included from Bannerable

#banner_url, #banner_url_sm

Methods included from Avatarable

#logo_url, #logo_url_sm

Methods included from AffiliatableParent

#add_affiliate, #remove_affiliate

Instance Method Details

#activities_countObject



140
141
142
# File 'app/models/space.rb', line 140

def activities_count
  activities.count + peer_reviews.count
end

#all_owners_admins_membersObject



166
167
168
169
170
171
172
173
174
# File 'app/models/space.rb', line 166

def all_owners_admins_members
  owners
    .or(admins)
    .or(members)
    .active
    .order(Arel.sql("users.id, roles.name='owner' DESC, roles.name='admin' DESC, roles.name='member' DESC"))
    .except(:select)
    .select('DISTINCT ON (users.id) users.*, roles.name AS role')
end

#challenge_participantsObject



114
115
116
# File 'app/models/space.rb', line 114

def challenge_participants
  User.select("users.*, 'participant' as relation").joins('LEFT JOIN users_roles ON users.id = users_roles.user_id').joins('LEFT JOIN roles ON roles.id = users_roles.role_id').where("roles.name != 'pending' AND roles.resource_type = 'Challenge' AND roles.resource_id IN (SELECT id FROM challenges WHERE space_id = :space_id)", space_id: self.id)
end

#challenges_countObject



136
137
138
# File 'app/models/space.rb', line 136

def challenges_count
  challenges.accepted_challenge.count
end


144
145
146
# File 'app/models/space.rb', line 144

def frontend_link
  "/space/#{short_title}"
end

#membersObject



109
110
111
112
# File 'app/models/space.rb', line 109

def members
  User.with_role(:member, self)
  # User.select("users.*, 'member' as relation").joins('LEFT JOIN users_roles ON users.id = users_roles.user_id').joins('LEFT JOIN roles ON roles.id = users_roles.role_id').where("roles.name != 'pending' AND roles.resource_type = 'Space' AND roles.resource_id = :space_id", space_id: self.id)
end

#members_countObject

change members_count depending on if show_associated_users is true or false



132
133
134
# File 'app/models/space.rb', line 132

def members_count
  self.show_associated_users? ? ((members + participants).uniq).count : members.uniq.count
end

#needs_countObject



97
98
99
# File 'app/models/space.rb', line 97

def needs_count
  (Need.where(project_id: Affiliation.where(parent: self, affiliate_type: 'Project', status: 'accepted').pluck(:affiliate_id)) + Need.where(project_id: ::Project.select("projects.*, 'associated' AS relation").joins(:challenges).where(challenges: { space: self }).pluck(:id))).uniq(&:id).count
end

#notif_pending_join_request(requestor) ⇒ Object



148
149
150
151
152
153
154
155
# File 'app/models/space.rb', line 148

def notif_pending_join_request(requestor)
  Notification.create(
    target: requestor,
    category: :membership,
    type: 'pending_join_request',
    object: self
  )
end

#notif_pending_join_request_approved(requestor) ⇒ Object



157
158
159
160
161
162
163
164
# File 'app/models/space.rb', line 157

def notif_pending_join_request_approved(requestor)
  Notification.create(
    target: requestor,
    category: :membership,
    type: 'pending_join_request_approved',
    object: self
  )
end

#participantsObject



122
123
124
# File 'app/models/space.rb', line 122

def participants
  self.show_associated_users? ? challenge_participants + project_participants : []
end

#project_participantsObject



118
119
120
# File 'app/models/space.rb', line 118

def project_participants
  User.select("users.*, 'participant' as relation").joins('LEFT JOIN users_roles ON users.id = users_roles.user_id').joins('LEFT JOIN roles ON roles.id = users_roles.role_id').where("roles.name != 'pending' AND roles.resource_type = 'Project' AND roles.resource_id IN (SELECT id FROM projects WHERE space_id = :space_id)", space_id: self.id)
end

#projects_affiliatedObject



101
102
103
# File 'app/models/space.rb', line 101

def projects_affiliated
  Project.where(id: Affiliation.where(parent: self, affiliate_type: 'Project', status: 'accepted').pluck(:affiliate_id)).where.not(status: "draft")
end

#projects_associatedObject



105
106
107
# File 'app/models/space.rb', line 105

def projects_associated
  Project.joins(:challenges).where(challenges: { space: self })
end

#projects_countObject

change projects_count depending on if show_associated_projects is true or false



127
128
129
# File 'app/models/space.rb', line 127

def projects_count
  self.show_associated_projects? ? ((projects_affiliated + projects_associated).uniq).count : projects_affiliated.count
end