module CanField::ControllerAdditions::InstanceMethods

Public Instance Methods

allowed_fields_for(action, subject) click to toggle source
# 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) click to toggle source
# 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