module NestedStrongParameters::ClassMethods

Public Instance Methods

strong_fields(*args) click to toggle source
# File lib/nested_strong_parameters/nested_strong_parameters.rb, line 13
def strong_fields(*args)
  options = extract_option_as(args)
  roles = options[:as] || :default

  self._strong_fields = self._strong_fields.deep_dup
  Array(roles).each do |role|
    self._strong_fields[role] ||= []
    self._strong_fields[role] += args
  end
end
whitelist(role = nil) click to toggle source
# File lib/nested_strong_parameters/nested_strong_parameters.rb, line 24
def whitelist(role = nil)
  map_params(self, role)
end

Private Instance Methods

extract_option_as(args) click to toggle source
# File lib/nested_strong_parameters/nested_strong_parameters.rb, line 30
def extract_option_as(args)
  if args.last.is_a?(Hash) && args.last[:as]
    args.pop
  else
    {}
  end
end
map_params(model, role = nil) click to toggle source
# File lib/nested_strong_parameters/nested_strong_parameters.rb, line 38
def map_params(model, role = nil)
  fields = model._strong_fields[:default]
  fields += model._strong_fields.fetch(role) { [] }

  fields.uniq.map do |field|
    if field.to_s.match /_attributes$/
      { field => map_params(nested_model(field), role) }
    else
      field
    end
  end
end
nested_model(field) click to toggle source
# File lib/nested_strong_parameters/nested_strong_parameters.rb, line 51
def nested_model(field)
  field.to_s.gsub(/_attributes$/, '').classify.constantize
end