Permalink
Browse files

Built specs

  • Loading branch information...
1 parent d321743 commit a7bd5a462cd19639606924838a4adae737298dd2 Daniel Spangenberg committed Apr 10, 2012
View
@@ -1,9 +1,6 @@
h1. Alfred - the unobtrusive butler who takes care of the uninvited guests "!https://secure.travis-ci.org/parcydo/alfred.png?branch=master!":http://travis-ci.org/parcydo/alfred
-Travis is an attempt to create an open-source, distributed build system for the Ruby community that:
-
-1. allows open-source projects to register their repository and have their test-suites run on demand
-2. allows users to contribute build capacities by connecting a VM that runs a build agent somewhere on their underused servers
+Alfred provides better attr_accessor handling on your application.
h2. Contact
* "Github":http://github.com/parcydo
View
@@ -5,6 +5,10 @@ module Alfred
mattr_accessor :auto_password_confirmation
@@auto_password_confirmation = true
+ # Automaticly override orm to support action based mass assignment security.
+ mattr_accessor :override_orm
+ @@override_orm = true
+
# Default way to setup Alfred. Run rails generate alfred:install to create
# a fresh initializer with all configuration values.
def self.setup
@@ -113,28 +113,29 @@ def alfred_accessible(*args)
end
private
+ def role(as = nil, action = nil)
+ role = (as || :default).to_s
+ role << "_#{action}" if action
+ role.to_sym
+ end
+
def alfred(method, *args)
options = args.extract_options!
if args.include?(:password) && Alfred.auto_password_confirmation
- args = args + [:password_confirmation]
+ args = [:password_confirmation] + args
end
[nil, :create, :update].each do |action|
- if options[:on]
- next unless options[:on] == action
- end
+ next if !options[:on] && options[:on] != action
action_args = args.dup
+ role = role(options[:as], action)
- role = (options[:as] || :default).to_s
- role << "_#{action}" if action
- role = role.to_sym
-
+ action_args = send("#{method}_attributes", role(:default, action)).to_a.compact.reject { |s| s.blank? } + action_args
+ action_args = send("#{method}_attributes", role(options[:inherit], action)).to_a.compact.reject { |s| s.blank? } + action_args if options[:inherit]
action_args = action_args + [{ as: role }]
- action_args = action_args + send("#{method}_attributes", role).to_a.compact.reject { |s| s.blank? }
-
send("attr_#{method}", *action_args)
end
end
@@ -27,6 +27,8 @@ def update_attributes(attributes, options = {})
end
end
-class ActiveRecord::Base
- include Alfred::ActiveRecord::Base
+if Alfred.override_orm
+ class ActiveRecord::Base
+ include Alfred::ActiveRecord::Base
+ end
end
@@ -2,11 +2,11 @@
class AlfredModel
include ActiveModel::MassAssignmentSecurity
- extend Alfred::Models
+ extend Alfred::ActiveModel::MassAssignmentSecurity
attr_accessor :a, :b, :c, :d, :e, :f
end
-describe Alfred::Models do
+describe Alfred::ActiveModel::MassAssignmentSecurity::ClassMethods do
describe "accessible" do
@@ -41,23 +41,18 @@ class ADModel < AlfredModel
ADModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
end
- it "add's attr_accessible to event" do
+ it "add's attr_accessible to action" do
class AEModel < AlfredModel
alfred_accessible :a, :b, on: :create
end
- AEModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
- AEModel.before_create
- AEModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
+ AEModel.accessible_attributes(:default_create).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
end
it "appends attr_accessible with custom role to event" do
class AFModel < AlfredModel
alfred_accessible :a, :b, as: :custom, on: :create
end
- AFModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
- AFModel.before_create
- AFModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
- AFModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
+ AFModel.accessible_attributes(:custom_create).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
end
end
@@ -78,6 +73,7 @@ class AHModel < AlfredModel
alfred_accessible :a, :b
alfred_accessible :c, :d, as: :custom
alfred_accessible :e, :f, as: :custom2, inherit: :custom
+ alfred_accessible :e, :f, as: :custom2, inherit: :custom
end
AHModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
AHModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
@@ -140,23 +136,18 @@ class PDModel < AlfredModel
PDModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d].map(&:to_s)))
end
- it "add's attr_protected to event" do
+ it "add's attr_protected to action" do
class PEModel < AlfredModel
alfred_protected :a, :b, on: :create
end
- PEModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
- PEModel.before_create
- PEModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
+ PEModel.protected_attributes(:default_create).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
end
- it "appends attr_protected with custom role to event" do
+ it "appends attr_protected with custom role to action" do
class PFModel < AlfredModel
alfred_protected :a, :b, as: :custom, on: :create
end
- PFModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
- PFModel.before_create
- PFModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
- PFModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
+ PFModel.protected_attributes(:custom_create).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
end
end

0 comments on commit a7bd5a4

Please sign in to comment.