Created
July 8, 2015 16:38
-
-
Save duderman/f794443e322304d81eb0 to your computer and use it in GitHub Desktop.
method returns accessible attributes with inheritance and descendants
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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