Module: Joinable::ActsAsJoinableComponent::ActMethod

Defined in:
lib/joinable/acts_as_joinable_component.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_joinable_component(options = {}) ⇒ Object

Inherits permissions of an a target object through the permission_links table An entry in the permission_links table is calculated by tracing the proxy object through to the target Takes a hash of params that specify which attached object the proxy should inherit its permissions from



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/joinable/acts_as_joinable_component.rb', line 7

def acts_as_joinable_component(options = {})
  extend ClassMethods unless (class << self; included_modules; end).include?(ClassMethods)
  include InstanceMethods unless included_modules.include?(InstanceMethods)
  
  options.assert_valid_keys :polymorphic, :parent, :view_permission
  
  class << self
    attr_accessor :view_permission
  end

  self.view_permission = options[:view_permission]
  
  # If we inherit permissions from multiple types of objects (polymorphic)
  if options[:polymorphic]
    parent_klass = options[:parent] + '_type.constantize'
    parent_id = options[:parent] + '_id'
  # Else if we are not, and inherit permissions from only one type of object
  else
    parent_klass = options[:parent].camelize
    parent_id = options[:parent] + '_id'
  end
  
  # Rescue in case we haven't got a parent 
  # TODO: this could probably be done better
  class_eval <<-EOV
    def next_link
      begin
        #{parent_klass}.find_by_id(#{parent_id})
      rescue
        nil
      end
    end
  EOV
end