Module: Joinable::ActsAsJoinableComponent::InstanceMethods

Includes:
Joinable::ActsAsPermissable::InstanceMethods
Defined in:
lib/joinable/acts_as_joinable_component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Joinable::ActsAsPermissable::InstanceMethods

#acts_like_permissable?, #who_can?

Instance Attribute Details

#view_permissionObject



177
178
179
180
181
182
183
# File 'lib/joinable/acts_as_joinable_component.rb', line 177

def view_permission
  klass_view_permission = self.class.view_permission
  klass_view_permission = klass_view_permission.call(self) if klass_view_permission.respond_to?(:call)

  # Allow view_permission to be set at the instance level
  return @view_permission || klass_view_permission
end

Instance Method Details

#acts_like_joinable_component?Boolean

Returns:



113
114
115
# File 'lib/joinable/acts_as_joinable_component.rb', line 113

def acts_like_joinable_component?
  true
end

#check_permission(user, permission) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/joinable/acts_as_joinable_component.rb', line 127

def check_permission(user, permission)
  # You can't ask to join joinable_components so the find permission is actually the view permission
  permission = :view if permission == :find
  
  if new_record?
    if joinable.acts_like?(:joinable)
      permission = recurse_to_inherit_custom_view_permission if permission == :view
      joinable.check_permission(user, permission)
    else
      # The component isn't contained by a joinable so it is public.
      true
    end
  else
    self.class.with_permission(user, permission).exists?(id)
  end
end

#inherited_view_permissionObject

inherited_view_permission is calculated by ascending up the chain of joinable components while view permission only takes into account the current joinable component. inherited_view_permission is for external use while view_permission should only be used internally.



173
174
175
# File 'lib/joinable/acts_as_joinable_component.rb', line 173

def inherited_view_permission
  permission_link.try(:component_view_permission)
end

#joinableObject

Returns the object that we should inherit permissions from

Recurses until the target is reached, if we reach a target that does not act as a joinable, call method again if it is a joinable component, else fall out as the chain has no valid endpoint (eg. feed -> discussion -> item)



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/joinable/acts_as_joinable_component.rb', line 149

def joinable
  if permission_link.present?
    permission_link.joinable
  else
    parent = next_link
    
    # Our target is now joinable therefore our target is at the end (eg. feed -> discussion -> [project])
    if parent && parent.acts_like?(:joinable)
      return parent

    # Our target is a joinable_component therefore our target somewhere between the beginning and the end (eg. feed -> [discussion] -> ??? -> project)
    elsif parent && parent.acts_like?(:joinable_component)
      return parent.joinable

    # We've fallen out because there was either no target or the target was not joinable or a joinable_component
    else
      return parent
    end
  end
end

#who_will_be_able_to_view?Boolean

Used by unsaved joinable_components to return a list of users who will be able to view the component once it is saved. Useful for outputting information to the user while they are creating a new component.

Returns:



121
122
123
124
125
# File 'lib/joinable/acts_as_joinable_component.rb', line 121

def who_will_be_able_to_view?
  User.joins(:memberships)
    .where(:memberships => {:joinable => joinable})
    .where(permission_sql_condition('memberships.permissions', recurse_to_inherit_custom_view_permission))
end