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
- #access_model=(model) ⇒ Object
- #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.
-
#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_updated_at ⇒ Object
Returns the timestamp of the last 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.
191 192 193 |
# File 'lib/joinable/acts_as_joinable.rb', line 191 def cached_membership @cached_membership end |
#cached_membership_invitation ⇒ Object
Returns the value of attribute cached_membership_invitation.
191 192 193 |
# File 'lib/joinable/acts_as_joinable.rb', line 191 def cached_membership_invitation @cached_membership_invitation end |
#cached_membership_request ⇒ Object
Returns the value of attribute cached_membership_request.
191 192 193 |
# File 'lib/joinable/acts_as_joinable.rb', line 191 def cached_membership_request @cached_membership_request end |
#initiator ⇒ Object
Returns the value of attribute initiator.
191 192 193 |
# File 'lib/joinable/acts_as_joinable.rb', line 191 def initiator @initiator end |
Instance Method Details
#access_model=(model) ⇒ Object
264 265 266 |
# File 'lib/joinable/acts_as_joinable.rb', line 264 def access_model=(model) .access_model = model end |
#acts_like_joinable? ⇒ Boolean
187 188 189 |
# File 'lib/joinable/acts_as_joinable.rb', line 187 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
183 184 185 |
# File 'lib/joinable/acts_as_joinable.rb', line 183 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.
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 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/joinable/acts_as_joinable.rb', line 270 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 |
#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
244 245 246 247 248 249 250 |
# File 'lib/joinable/acts_as_joinable.rb', line 244 def membership_for(user) if cached_membership != nil cached_membership else memberships.where(:user_id => user.id).first end end |
#membership_for?(user) ⇒ Boolean
Find out whether this Joinable has a membership for a certain user and return true, else false
253 254 255 |
# File 'lib/joinable/acts_as_joinable.rb', line 253 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
227 228 229 230 231 232 233 |
# File 'lib/joinable/acts_as_joinable.rb', line 227 def membership_invitation_for(user) if cached_membership_invitation != nil cached_membership_invitation else membership_invitations.where(:user_id => user.id).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
236 237 238 |
# File 'lib/joinable/acts_as_joinable.rb', line 236 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.
209 210 211 212 213 214 215 |
# File 'lib/joinable/acts_as_joinable.rb', line 209 def membership_request_for(user) if cached_membership_request != nil cached_membership_request else membership_requests.where(:user_id => user.id).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
218 219 220 |
# File 'lib/joinable/acts_as_joinable.rb', line 218 def membership_request_for?(user) !membership_request_for(user).nil? end |
#memberships_updated_at ⇒ Object
Returns the timestamp of the last time the memberships were updated for this joinable
258 259 260 |
# File 'lib/joinable/acts_as_joinable.rb', line 258 def memberships_updated_at memberships.maximum(:updated_at) end |