Sha256: 1c02b6e93d7fb1bcab97cf4b9ab8f52536736de87b91c042e7f21c1b25161571
Contents?: true
Size: 1.83 KB
Versions: 3
Compression:
Stored size: 1.83 KB
Contents
module CanField module ControllerAdditions module ClassMethods def permit_can_field_constraints resource_name = self.to_s.sub("Controller", "").underscore.split('/').last.singularize model = Kernel.const_get(resource_name.camelcase) [:update, :create].each do |action| prepend_before_filter(only: action) do if params.has_key?(resource_name) keys = allowed_fields_for action, model if keys == [:all] self.params[resource_name] = params[resource_name].permit! else self.params[resource_name] = params[resource_name].permit *keys end else self.params = params.permit, *keys end end end end end module InstanceMethods 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 def canf?(action, subject, field) allowed_fields_for(action, subject).include?(field.to_sym) or allowed_fields_for(action, subject).include?(:all) end end def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods receiver.helper_method :allowed_fields_for, :canf? end end end if defined? ActionController::Base ActionController::Base.class_eval do include CanField::ControllerAdditions end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
can_field-0.0.3 | lib/can_field/controller_additions.rb |
can_field-0.0.2 | lib/can_field/controller_additions.rb |
can_field-0.0.1 | lib/can_field/controller_additions.rb |