Skip to content

Instantly share code, notes, and snippets.

@duderman
Created July 8, 2015 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duderman/f794443e322304d81eb0 to your computer and use it in GitHub Desktop.
Save duderman/f794443e322304d81eb0 to your computer and use it in GitHub Desktop.
method returns accessible attributes with inheritance and descendants
def permitted_params(klass)
self_attrs = klass.accessible_attributes.reject { |attr| attr =~ /_attributes$/ }.map(&:to_sym)
nested_attrs = klass.nested_attributes_options.keys.map do |nested_klass|
nested_const_klass = nested_klass.to_s.singularize.classify.constantize
{ "#{nested_klass}_attributes".to_sym => permitted_params(nested_const_klass) }
end
types_attrs = klass.descendants.map { |type_class| permitted_params(type_class) }
(self_attrs + (types_attrs || []) + nested_attrs).flatten.uniq.compact
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment