diff --git a/lib/protector/adapters/active_record/association.rb b/lib/protector/adapters/active_record/association.rb index 14c1624..03e1afa 100644 --- a/lib/protector/adapters/active_record/association.rb +++ b/lib/protector/adapters/active_record/association.rb @@ -29,8 +29,20 @@ def scope_with_protector(*args) # Forwards protection subject to the new instance def build_record_with_protector(*args) return build_record_without_protector(*args) unless protector_subject? + + protector_permit_strong_params(args) build_record_without_protector(*args).restrict!(protector_subject) end + + private + + def protector_meta(subject=protector_subject) + klass.protector_meta.evaluate(subject) + end + + def protector_permit_strong_params(args) + Protector::ActiveRecord::Adapters::StrongParameters.sanitize! args, true, protector_meta + end end end end diff --git a/lib/protector/adapters/active_record/relation.rb b/lib/protector/adapters/active_record/relation.rb index 06dc6e5..951573d 100644 --- a/lib/protector/adapters/active_record/relation.rb +++ b/lib/protector/adapters/active_record/relation.rb @@ -231,9 +231,7 @@ def protector_expand_inclusion(inclusion, results=[], base=[], klass=@klass) def protector_permit_strong_params(args) # strong_parameters integration - if Protector.config.strong_parameters? && args.first.respond_to?(:permit) - Protector::ActiveRecord::Adapters::StrongParameters.sanitize! args, true, protector_meta - end + Protector::ActiveRecord::Adapters::StrongParameters.sanitize! args, true, protector_meta end diff --git a/lib/protector/adapters/active_record/strong_parameters.rb b/lib/protector/adapters/active_record/strong_parameters.rb index a3dfb73..b772dcc 100644 --- a/lib/protector/adapters/active_record/strong_parameters.rb +++ b/lib/protector/adapters/active_record/strong_parameters.rb @@ -3,6 +3,7 @@ module ActiveRecord module Adapters module StrongParameters def self.sanitize!(args, is_new, meta) + return unless Protector.config.strong_parameters? && args.first.respond_to?(:permit) return if args[0].permitted? if is_new args[0] = args[0].permit(*meta.access[:create].keys) if meta.access.include? :create @@ -16,9 +17,7 @@ def sanitize_for_mass_assignment(*args) # We check only for updation here since the creation will be handled by relation # (see Protector::Adapters::ActiveRecord::Relation#new_with_protector and # Protector::Adapters::ActiveRecord::Relation#create_with_protector) - if Protector.config.strong_parameters? && args.first.respond_to?(:permit) \ - && !new_record? && protector_subject? - + if !new_record? && protector_subject? StrongParameters.sanitize! args, false, protector_meta end