Created
August 23, 2012 15:46
-
-
Save equivalent/3437893 to your computer and use it in GitHub Desktop.
FactoryGirl attributes_for giving only accessible attributes
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
describe SomeModel do | |
it do | |
p accessible_attributes_for(:contact) | |
end | |
end |
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 assessable_attributes_for(resource) | |
if resource.is_a? Symbol | |
(FactoryGirl.build resource).attributes.symbolize_keys.reject { |key, value| !resource.to_s.humanize.constantize.attr_accessible[:default].collect { |attribute| attribute.to_sym }.include?(key) } | |
else | |
resource.attributes.symbolize_keys.reject { |key, value| !resource.class.attr_accessible[:default].collect { |attribute| attribute.to_sym }.include?(key) } | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Inspired by http://stackoverflow.com/questions/5103572/factorygirl-attributes-for-not-giving-me-associated-attributes