Module: CanField::ControllerAdditions::InstanceMethods

Defined in:
lib/can_field/controller_additions.rb

Instance Method Summary collapse

Instance Method Details

#allowed_fields_for(action, subject) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/can_field/controller_additions.rb', line 26

def allowed_fields_for(action, subject)
  #TODO: gen cool rules
  abilities = Ability.new(current_user).abilities_for_subject(subject).map(&:to_s).group_by{|x| x[0..2] == '_cf' ? :field : :mass}

  re = /\A_cf_(?<action>\w+)_fl_(?<field>\w+)$/
  result = (abilities[:field] || []).map do |a|
    x = re.match(a)
    [x[:action], x[:field]]
  end.select{|a| a[0] == action.to_s}.map{|a| a[1]}.map(&:to_sym)

  result << :all if result == []

  result
end

#canf?(action, subject, field) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
# File 'lib/can_field/controller_additions.rb', line 41

def canf?(action, subject, field)
  allowed_fields_for(action, subject).include?(field.to_sym) or
    allowed_fields_for(action, subject).include?(:all)
end