Module: Allows::ClassMethods

Defined in:
lib/allows.rb

Instance Method Summary collapse

Instance Method Details

#allow(*options, &block) ⇒ Object



71
72
73
74
# File 'lib/allows.rb', line 71

def allow(*options, &block)
  @allows ||= Array.new
  @allows << options
end

#clear_permissionsObject



111
112
113
# File 'lib/allows.rb', line 111

def clear_permissions
  @permissions = nil
end

#set_permissionsObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/allows.rb', line 76

def set_permissions
  # Permissions are calculated only once per class for performance considerations
  # This behaviour is overridden in development so permissions are always calculated
  if (!@permissions && @allows) || (defined?(RAILS_ENV) && RAILS_ENV == 'development')
    @permissions = Hash.new
    
    if @allows 
      @allows.each do |p|
        allowed = Hash.new
        hash = p.select{|o| o.is_a?(Hash)}
        
        allowed[:permissions] = p - hash

        actions = hash.first
        if hash.first
          allowed[:except] = actions[:except] 
          allowed[:only] = actions[:only]
        end

        if allowed[:only]                               # Only for the specified actions
          allowed_actions = Array(allowed[:only])
        elsif allowed[:except]                          # All available actions EXCEPT the ones specified
          allowed_actions = controller_actions - Array(allowed[:except])
        else                                            # All available actions
          allowed_actions = controller_actions
        end

        allowed_actions.each do |action|
          @permissions[action.to_sym] = (Array(@permissions[action.to_sym]) + allowed[:permissions]).uniq
        end
      end
    end
  end
end