Permalink
Browse files

copy gist

  • Loading branch information...
1 parent 1583130 commit 8c58b9470f60579baf69f44ba83389d14a6bb482 @eval committed Feb 2, 2012
Showing with 87 additions and 8 deletions.
  1. +0 −1 Gemfile
  2. +5 −0 Rakefile
  3. +73 −3 lib/sudo_attr_accessibility.rb
  4. +1 −1 lib/sudo_attr_accessibility/version.rb
  5. +8 −3 sudo_attr_accessibility.gemspec
View
@@ -1,4 +1,3 @@
source 'https://rubygems.org'
-# Specify your gem's dependencies in sudo_attr_accessibility.gemspec
gemspec
View
@@ -1,2 +1,7 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
+
+desc 'Removes trailing whitespace'
+task :whitespace do
+ sh %{find . -name '*.rb' -exec sed -i '' 's/ *$//g' {} \\;}
+end
@@ -1,5 +1,75 @@
-require "sudo_attr_accessibility/version"
-
module SudoAttrAccessibility
- # Your code goes here...
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ # Make all attributes of an AR-model accessible to some roles.
+ #
+ # @example
+ # class Person < ActiveRecord::Base
+ # belongs_to :account
+ #
+ # # attributes mass-assignable as role default
+ # attr_accessible :email
+ #
+ # # the admin-role can access all...
+ # sudo_attr_accessible_as :admin
+ #
+ # # ...even attributes defined later on
+ # attr_accessor :current_step
+ # end
+ #
+ # p1 = Person.new(:email => 'person1@example.org', :active => true)
+ # p1.email # => 'person1@example.org'
+ # p1.active # => nil
+ # p2 = Person.new({:email => 'person1@example.org', :active => true,
+ # :account => Account.first, :current_step => 1},
+ # :as => :admin)
+ # p2.email # => 'person1@example.org'
+ # p2.active # => true
+ # p2.current_step # => 2
+ # p2.account # => <Account ...>
+ #
+ # Alternatively the default-role is passed to sudo_attr_accessible_as and
+ # another role is used for attr_accessible. This is more convenient when
+ # working in the console for example (no ':as => :role' is needed) though
+ # is less secure of course.
+ #
+ # Enabling this behaviour by default for all subclasses of AR:
+ # class ActiveRecord::Base
+ # def self.inherited(child_class)
+ # child_class.class_eval{ sudo_attr_accessible_as :default }
+ # super
+ # end
+ # end
+ def sudo_attr_accessible_as(*roles)
+ re_method_filter = %r{(.+)=\z}
+
+ # take care of any future attribute
+ unless respond_to?(:method_added_with_sudo_attr_accessibility)
+ class_eval %{
+ def self.method_added_with_sudo_attr_accessibility(m)
+ if attribute = m.to_s[#{re_method_filter.inspect}, 1]
+ attr_accessible attribute, :as => #{roles.inspect}
+ end
+ method_added_without_sudo_attr_accessibility(m)
+ end
+
+ class << self
+ alias_method_chain :method_added, :sudo_attr_accessibility
+ end
+ }, __FILE__, __LINE__ + 1
+ end
+
+ # handle current attributes
+ attributes = [].tap do |a|
+ a.push *self.attribute_names
+ a.push *self.instance_methods(false).map do |m|
+ m.to_s[re_method_filter, 1]
+ end.compact
+ end.each do |attr|
+ attr_accessible attr, :as => roles
+ end
+ end
+ end
end
+ActiveRecord::Base.send(:include, SudoAttrAccessibility)
@@ -1,3 +1,3 @@
module SudoAttrAccessibility
- VERSION = "0.0.1"
+ VERSION = "0.4.2"
end
@@ -4,14 +4,19 @@ require File.expand_path('../lib/sudo_attr_accessibility/version', __FILE__)
Gem::Specification.new do |gem|
gem.authors = ["Gert Goet"]
gem.email = ["gert@thinkcreate.nl"]
- gem.description = %q{TODO: Write a gem description}
- gem.summary = %q{TODO: Write a gem summary}
- gem.homepage = ""
+ gem.description = %q{Make all attributes of an AR-model accessible to some roles}
+ gem.summary = %q{Make all attributes of an AR-model accessible to some roles}
+ gem.homepage = "https://github.com/eval/sudo_attr_accessibility"
+ gem.license = "MIT"
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.name = "sudo_attr_accessibility"
gem.require_paths = ["lib"]
gem.version = SudoAttrAccessibility::VERSION
+ gem.required_ruby_version = '>= 1.8.7'
+
+ gem.add_dependency "activesupport", ">= 3.0.0"
+ gem.add_dependency "activemodel", ">= 3.0.0"
end

0 comments on commit 8c58b94

Please sign in to comment.