alfred 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1,9 +1,6 @@
1
1
  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
2
2
 
3
- Travis is an attempt to create an open-source, distributed build system for the Ruby community that:
4
-
5
- 1. allows open-source projects to register their repository and have their test-suites run on demand
6
- 2. allows users to contribute build capacities by connecting a VM that runs a build agent somewhere on their underused servers
3
+ Alfred provides better attr_accessor handling on your application.
7
4
 
8
5
  h2. Contact
9
6
  * "Github":http://github.com/parcydo
data/lib/alfred.rb CHANGED
@@ -5,6 +5,10 @@ module Alfred
5
5
  mattr_accessor :auto_password_confirmation
6
6
  @@auto_password_confirmation = true
7
7
 
8
+ # Automaticly override orm to support action based mass assignment security.
9
+ mattr_accessor :override_orm
10
+ @@override_orm = true
11
+
8
12
  # Default way to setup Alfred. Run rails generate alfred:install to create
9
13
  # a fresh initializer with all configuration values.
10
14
  def self.setup
@@ -113,28 +113,29 @@ module Alfred
113
113
  end
114
114
 
115
115
  private
116
+ def role(as = nil, action = nil)
117
+ role = (as || :default).to_s
118
+ role << "_#{action}" if action
119
+ role.to_sym
120
+ end
121
+
116
122
  def alfred(method, *args)
117
123
  options = args.extract_options!
118
124
 
119
125
  if args.include?(:password) && Alfred.auto_password_confirmation
120
- args = args + [:password_confirmation]
126
+ args = [:password_confirmation] + args
121
127
  end
122
128
 
123
129
  [nil, :create, :update].each do |action|
124
- if options[:on]
125
- next unless options[:on] == action
126
- end
130
+ next if !options[:on] && options[:on] != action
127
131
 
128
132
  action_args = args.dup
133
+ role = role(options[:as], action)
129
134
 
130
- role = (options[:as] || :default).to_s
131
- role << "_#{action}" if action
132
- role = role.to_sym
133
-
135
+ action_args = send("#{method}_attributes", role(:default, action)).to_a.compact.reject { |s| s.blank? } + action_args
136
+ action_args = send("#{method}_attributes", role(options[:inherit], action)).to_a.compact.reject { |s| s.blank? } + action_args if options[:inherit]
134
137
  action_args = action_args + [{ as: role }]
135
138
 
136
- action_args = action_args + send("#{method}_attributes", role).to_a.compact.reject { |s| s.blank? }
137
-
138
139
  send("attr_#{method}", *action_args)
139
140
  end
140
141
  end
@@ -27,6 +27,8 @@ module Alfred
27
27
  end
28
28
  end
29
29
 
30
- class ActiveRecord::Base
31
- include Alfred::ActiveRecord::Base
30
+ if Alfred.override_orm
31
+ class ActiveRecord::Base
32
+ include Alfred::ActiveRecord::Base
33
+ end
32
34
  end
@@ -1,3 +1,3 @@
1
1
  module Alfred
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -2,11 +2,11 @@ require 'spec_helper'
2
2
 
3
3
  class AlfredModel
4
4
  include ActiveModel::MassAssignmentSecurity
5
- extend Alfred::Models
5
+ extend Alfred::ActiveModel::MassAssignmentSecurity
6
6
  attr_accessor :a, :b, :c, :d, :e, :f
7
7
  end
8
8
 
9
- describe Alfred::Models do
9
+ describe Alfred::ActiveModel::MassAssignmentSecurity::ClassMethods do
10
10
 
11
11
  describe "accessible" do
12
12
 
@@ -41,23 +41,18 @@ describe Alfred::Models do
41
41
  ADModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
42
42
  end
43
43
 
44
- it "add's attr_accessible to event" do
44
+ it "add's attr_accessible to action" do
45
45
  class AEModel < AlfredModel
46
46
  alfred_accessible :a, :b, on: :create
47
47
  end
48
- AEModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
49
- AEModel.before_create
50
- AEModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
48
+ AEModel.accessible_attributes(:default_create).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
51
49
  end
52
50
 
53
51
  it "appends attr_accessible with custom role to event" do
54
52
  class AFModel < AlfredModel
55
53
  alfred_accessible :a, :b, as: :custom, on: :create
56
54
  end
57
- AFModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
58
- AFModel.before_create
59
- AFModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([].map(&:to_s)))
60
- AFModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
55
+ AFModel.accessible_attributes(:custom_create).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
61
56
  end
62
57
  end
63
58
 
@@ -78,6 +73,7 @@ describe Alfred::Models do
78
73
  alfred_accessible :a, :b
79
74
  alfred_accessible :c, :d, as: :custom
80
75
  alfred_accessible :e, :f, as: :custom2, inherit: :custom
76
+ alfred_accessible :e, :f, as: :custom2, inherit: :custom
81
77
  end
82
78
  AHModel.accessible_attributes.should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b].map(&:to_s)))
83
79
  AHModel.accessible_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::WhiteList.new([:a, :b, :c, :d].map(&:to_s)))
@@ -140,23 +136,18 @@ describe Alfred::Models do
140
136
  PDModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b, :c, :d].map(&:to_s)))
141
137
  end
142
138
 
143
- it "add's attr_protected to event" do
139
+ it "add's attr_protected to action" do
144
140
  class PEModel < AlfredModel
145
141
  alfred_protected :a, :b, on: :create
146
142
  end
147
- PEModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
148
- PEModel.before_create
149
- PEModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
143
+ PEModel.protected_attributes(:default_create).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
150
144
  end
151
145
 
152
- it "appends attr_protected with custom role to event" do
146
+ it "appends attr_protected with custom role to action" do
153
147
  class PFModel < AlfredModel
154
148
  alfred_protected :a, :b, as: :custom, on: :create
155
149
  end
156
- PFModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
157
- PFModel.before_create
158
- PFModel.protected_attributes.should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([].map(&:to_s)))
159
- PFModel.protected_attributes(:custom).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
150
+ PFModel.protected_attributes(:custom_create).should eq(ActiveModel::MassAssignmentSecurity::BlackList.new([:a, :b].map(&:to_s)))
160
151
  end
161
152
  end
162
153
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alfred
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -112,7 +112,7 @@ files:
112
112
  - lib/alfred/mass_assignment_security.rb
113
113
  - lib/alfred/orm/active_record.rb
114
114
  - lib/alfred/version.rb
115
- - spec/models_spec.rb
115
+ - spec/mass_assignment_security_spec.rb
116
116
  - spec/spec_helper.rb
117
117
  homepage: http://rubygems.org/gems/alfred
118
118
  licenses: []
@@ -139,5 +139,5 @@ signing_key:
139
139
  specification_version: 3
140
140
  summary: Alfred is the unobtrusive butler who takes care of the uninvited guests.
141
141
  test_files:
142
- - spec/models_spec.rb
142
+ - spec/mass_assignment_security_spec.rb
143
143
  - spec/spec_helper.rb