Module: CanTango::Model::Filter::ClassMethods

Defined in:
lib/cantango/model/filter.rb

Instance Method Summary collapse

Instance Method Details

#tango_account_filter(*method_names) ⇒ Object

def



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cantango/model/filter.rb', line 61

def  *method_names
  method_names.flatten.each do |name|
    case name
    when String, Symbol
      case name.to_sym
      when :REST, :MANAGE
         :create => :OPTS, :create! => :OPTS, :update_attributes => :OPTS, :update_attributes! => :OPTS
         :destroy, :destroy!
      when :CREATE, :NEW
         :create => :OPTS, :create! => :OPTS
      when :UPDATE, :EDIT
         :update_attributes => :OPTS, :update_attributes! => :OPTS
      when :DELETE, :DESTROY
         :destroy, :destroy!
      else
        meth_name = name.to_s
        method_name = CanTango::Model::Filter.clean_meth_name meth_name
        define_method :"#{method_name}" do |user|
          send(name) if (user).can? meth_name.to_sym, self
        end
      end
    when Hash
      base = self
      name.each_pair do |meth_name, args|
        norm_args = CanTango::Model::Filter.normalize_args args
        args = norm_args.map{|a| a == 'OPTS' ? 'options = {}' : a}.join(',')
        args_call = norm_args.map{|a| a == 'OPTS' ? 'options' : a}.join(',')

        method_name = CanTango::Model::Filter.clean_meth_name meth_name

        base.class_eval %{
          def #{method_name} the_user, #{args}
            send(:#{meth_name}, #{args_call}) if account_ability(the_user).can? :#{meth_name}, self
          end
        }
      end
    end
  end
end

#tango_filter(*method_names) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/cantango/model/filter.rb', line 21

def tango_filter *method_names
  method_names.flatten.each do |name|
    case name
    when String, Symbol
      case name.to_sym
      when :REST, :MANAGE
        tango_filter :create => :OPTS, :create! => :OPTS, :update_attributes => :OPTS, :update_attributes! => :OPTS
        tango_filter :destroy, :destroy!
      when :CREATE, :NEW
        tango_filter :create => :OPTS, :create! => :OPTS
      when :UPDATE, :EDIT
        tango_filter :update_attributes => :OPTS, :update_attributes! => :OPTS
      when :DELETE, :DESTROY
        tango_filter :destroy, :destroy!
      else
        meth_name = name.to_s
        method_name = CanTango::Model::Filter.clean_meth_name meth_name
        define_method :"#{method_name}" do |user|
          send(name) if user_ability(user).can? meth_name.to_sym, self
        end
      end
    when Hash
      base = self
      name.each_pair do |meth_name, args|
        norm_args = CanTango::Model::Filter.normalize_args args
        args = norm_args.map{|a| a == 'OPTS' ? 'options = {}' : a}.join(',')
        args_call = norm_args.map{|a| a == 'OPTS' ? 'options' : a}.join(',')

        method_name = CanTango::Model::Filter.clean_meth_name meth_name

        base.class_eval %{
          def #{method_name} the_user, #{args}
            send(:#{meth_name}, #{args_call}) if user_ability(the_user).can? :#{meth_name}, self
          end
        }
      end
    end
  end
end