Module: Joinable::ActsAsJoinable::InstanceMethods
- Defined in:
- lib/joinable/acts_as_joinable.rb
Instance Attribute Summary collapse
-
#cached_membership ⇒ Object
Returns the value of attribute cached_membership.
-
#cached_membership_invitation ⇒ Object
Returns the value of attribute cached_membership_invitation.
-
#cached_membership_request ⇒ Object
Returns the value of attribute cached_membership_request.
-
#initiator ⇒ Object
Returns the value of attribute initiator.
Instance Method Summary collapse
- #acts_like_joinable? ⇒ Boolean
-
#attributes=(attributes_hash) ⇒ Object
Override attributes= to make sure that the user and initiator attributes are initialized before the membership_invitation and membership before_add callbacks are triggered since they reference these attributes.
-
#check_permission(user, permission_name) ⇒ Object
Returns true or false depending on whether or not the user has the specified permission for this object.
-
#default_permission_set ⇒ Object
Ensure the joinable has a set of default permissions (an empty set unless one already exists).
-
#default_permissions ⇒ Object
Convenience method for reading the default permission set’s access_model.
-
#default_permissions=(permissions) ⇒ Object
Convenience method for writing the default permission set’s access_model.
-
#membership_for(user) ⇒ Object
Get the membership (if any) for a specific user.
-
#membership_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership for a certain user and return true, else false.
-
#membership_invitation_for(user) ⇒ Object
Get the membership invitation (if any) for a specific user.
-
#membership_invitation_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership invitation for a certain user and return true, else false.
-
#membership_request_for(user) ⇒ Object
Get the membership request (if any) for a specific user.
-
#membership_request_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership request for a certain user and return true, else false.
-
#memberships_cache_key ⇒ Object
Returns a unique cache key any time the memberships were updated for this joinable.
Methods included from Joinable::ActsAsPermissable::InstanceMethods
#acts_like_permissable?, #who_can?
Instance Attribute Details
#cached_membership ⇒ Object
Returns the value of attribute cached_membership.
171 172 173 |
# File 'lib/joinable/acts_as_joinable.rb', line 171 def cached_membership @cached_membership end |
#cached_membership_invitation ⇒ Object
Returns the value of attribute cached_membership_invitation.
171 172 173 |
# File 'lib/joinable/acts_as_joinable.rb', line 171 def cached_membership_invitation @cached_membership_invitation end |
#cached_membership_request ⇒ Object
Returns the value of attribute cached_membership_request.
171 172 173 |
# File 'lib/joinable/acts_as_joinable.rb', line 171 def cached_membership_request @cached_membership_request end |
#initiator ⇒ Object
Returns the value of attribute initiator.
171 172 173 |
# File 'lib/joinable/acts_as_joinable.rb', line 171 def initiator @initiator end |
Instance Method Details
#acts_like_joinable? ⇒ Boolean
167 168 169 |
# File 'lib/joinable/acts_as_joinable.rb', line 167 def acts_like_joinable? true end |
#attributes=(attributes_hash) ⇒ Object
Override attributes= to make sure that the user and initiator attributes are initialized before the membership_invitation and membership before_add callbacks are triggered since they reference these attributes
163 164 165 |
# File 'lib/joinable/acts_as_joinable.rb', line 163 def attributes=(attributes_hash) super(ActiveSupport::OrderedHash[attributes_hash.symbolize_keys.sort_by {|a| [:user, :user_id, :initiator].include?(a.first) ? 0 : 1}]) end |
#check_permission(user, permission_name) ⇒ Object
Returns true or false depending on whether or not the user has the specified permission for this object. Will cache the result if uncached.
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/joinable/acts_as_joinable.rb', line 257 def (user, ) = .to_s.dup # Generate a cache path based on the factors that affect the user's permissions # If User has membership # - depends on permissions # Elsif User has been invited # - depends on existence of invitation and the default permissions (when checking the view permission) # Else User doesn't have any membership # - depends on default permissions of the joinable if membership = memberships.where(:user_id => user.id).first key = "membership_#{membership.updated_at.to_f}" elsif self.membership_invitations.where(:user_id => user.id).exists? key = "default_permissions_#{self..updated_at.to_f}_invitation_exists" else key = "default_permissions_#{self..updated_at.to_f}" end cache_path = "permissions/#{self.class.table_name}/#{self.id}/user_#{user.id}_#{key}" if defined?(Rails.cache) = Rails.cache.read(cache_path) if && (value = []) != nil return value end end # The permission isn't cached yet, so cache it value = self.class.(user, ).exists?(self.id) if defined?(Rails.cache) if = .dup [] = value else = { => value} end Rails.cache.write(cache_path, ) end return value end |
#default_permission_set ⇒ Object
Ensure the joinable has a set of default permissions (an empty set unless one already exists)
300 301 302 |
# File 'lib/joinable/acts_as_joinable.rb', line 300 def super || self. end |
#default_permissions ⇒ Object
Convenience method for reading the default permission set’s access_model
246 247 248 |
# File 'lib/joinable/acts_as_joinable.rb', line 246 def self.. end |
#default_permissions=(permissions) ⇒ Object
Convenience method for writing the default permission set’s access_model
251 252 253 |
# File 'lib/joinable/acts_as_joinable.rb', line 251 def () self.. = end |
#membership_for(user) ⇒ Object
Get the membership (if any) for a specific user. This method also supports caching of a membership request in order to facilitate eager loading. NOTE: See :membership_request_for documentation for an in depth example of this type of behaviour
224 225 226 227 228 229 230 |
# File 'lib/joinable/acts_as_joinable.rb', line 224 def membership_for(user) if cached_membership != nil cached_membership else memberships.where(:user_id => user).first end end |
#membership_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership for a certain user and return true, else false
233 234 235 |
# File 'lib/joinable/acts_as_joinable.rb', line 233 def membership_for?(user) !membership_for(user).nil? end |
#membership_invitation_for(user) ⇒ Object
Get the membership invitation (if any) for a specific user. This method also supports caching of a membership request in order to facilitate eager loading. NOTE: See :membership_request_for documentation for an in depth example of this type of behaviour
207 208 209 210 211 212 213 |
# File 'lib/joinable/acts_as_joinable.rb', line 207 def membership_invitation_for(user) if cached_membership_invitation != nil cached_membership_invitation else membership_invitations.where(:user_id => user).first end end |
#membership_invitation_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership invitation for a certain user and return true, else false
216 217 218 |
# File 'lib/joinable/acts_as_joinable.rb', line 216 def membership_invitation_for?(user) !membership_invitation_for(user).nil? end |
#membership_request_for(user) ⇒ Object
Get the membership request (if any) for a specific user. This method also supports caching of a membership request in order to facilitate eager loading.
eg. For all of the projects on the projects index page, we want to do something similar to Project.all(:include => :membership_requests).
We can’t do exactly that however because we only want the membership_requests related to the current_user, not all users.
Instead, we fake it by doing a separate query which gets all the user’s membership_requests related to all the projects being displayed. We then cache the request relevant to this project in the cached_membership_request instance variable for later use by the view.
189 190 191 192 193 194 195 |
# File 'lib/joinable/acts_as_joinable.rb', line 189 def membership_request_for(user) if cached_membership_request != nil cached_membership_request else membership_requests.where(:user_id => user).first end end |
#membership_request_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership request for a certain user and return true, else false
198 199 200 |
# File 'lib/joinable/acts_as_joinable.rb', line 198 def membership_request_for?(user) !membership_request_for(user).nil? end |
#memberships_cache_key ⇒ Object
Returns a unique cache key any time the memberships were updated for this joinable
238 239 240 |
# File 'lib/joinable/acts_as_joinable.rb', line 238 def memberships_cache_key "#{memberships.size}_#{memberships.maximum(:updated_at).to_f}" end |