Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Created March 5, 2012 18:16
Show Gist options
  • Save jamesgecko/1980103 to your computer and use it in GitHub Desktop.
Save jamesgecko/1980103 to your computer and use it in GitHub Desktop.
Attribute visibility task
# TODO: this will fail when run on a project that uses namespaced models
desc "Show visibility of attributes"
task :attr => :environment do
require 'set'
models = Dir['**/models/**/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize }
models.each do |m|
all = Set.new m.attribute_names
accessible = Set.new m.accessible_attributes.to_a
protected_ = Set.new m.protected_attributes.to_a
if accessible.empty?
is_accessible = (all - protected_).to_a
else
is_accessible = (accessible - protected_).to_a
end
non_accessible = ((all - is_accessible) + protected_).to_a
is_accessible = "None" if is_accessible.empty?
print m.name, "\n"
print " Accessible: ", is_accessible, "\n"
print " Nonaccessible: " , non_accessible , "\n\n"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment