sudo_attributes 0.5.2 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.2
data/Gemfile CHANGED
@@ -1,9 +1,10 @@
1
1
  source :rubygems
2
2
 
3
+ gem 'rails'
4
+
3
5
  group :test do
4
6
  gem 'rake'
5
- gem 'jeweler', '1.4.0'
6
- gem 'rspec', '~> 2.0.0'
7
- gem 'sqlite3-ruby', :require => 'sqlite3'
8
- gem 'rails', '~> 3.0.0'
7
+ gem 'jeweler', '~> 1.6.4'
8
+ gem 'rspec', '~> 2.6.0'
9
+ gem 'sqlite3'
9
10
  end
data/Gemfile.lock CHANGED
@@ -1,96 +1,106 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- abstract (1.0.0)
5
- actionmailer (3.0.3)
6
- actionpack (= 3.0.3)
7
- mail (~> 2.2.9)
8
- actionpack (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
11
- builder (~> 2.1.2)
12
- erubis (~> 2.6.6)
13
- i18n (~> 0.4)
14
- rack (~> 1.2.1)
15
- rack-mount (~> 0.6.13)
16
- rack-test (~> 0.5.6)
17
- tzinfo (~> 0.3.23)
18
- activemodel (3.0.3)
19
- activesupport (= 3.0.3)
20
- builder (~> 2.1.2)
21
- i18n (~> 0.4)
22
- activerecord (3.0.3)
23
- activemodel (= 3.0.3)
24
- activesupport (= 3.0.3)
25
- arel (~> 2.0.2)
26
- tzinfo (~> 0.3.23)
27
- activeresource (3.0.3)
28
- activemodel (= 3.0.3)
29
- activesupport (= 3.0.3)
30
- activesupport (3.0.3)
31
- arel (2.0.6)
32
- builder (2.1.2)
33
- diff-lcs (1.1.2)
34
- erubis (2.6.6)
35
- abstract (>= 1.0.0)
36
- gemcutter (0.6.1)
4
+ actionmailer (3.1.0)
5
+ actionpack (= 3.1.0)
6
+ mail (~> 2.3.0)
7
+ actionpack (3.1.0)
8
+ activemodel (= 3.1.0)
9
+ activesupport (= 3.1.0)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ i18n (~> 0.6)
13
+ rack (~> 1.3.2)
14
+ rack-cache (~> 1.0.3)
15
+ rack-mount (~> 0.8.2)
16
+ rack-test (~> 0.6.1)
17
+ sprockets (~> 2.0.0)
18
+ activemodel (3.1.0)
19
+ activesupport (= 3.1.0)
20
+ bcrypt-ruby (~> 3.0.0)
21
+ builder (~> 3.0.0)
22
+ i18n (~> 0.6)
23
+ activerecord (3.1.0)
24
+ activemodel (= 3.1.0)
25
+ activesupport (= 3.1.0)
26
+ arel (~> 2.2.1)
27
+ tzinfo (~> 0.3.29)
28
+ activeresource (3.1.0)
29
+ activemodel (= 3.1.0)
30
+ activesupport (= 3.1.0)
31
+ activesupport (3.1.0)
32
+ multi_json (~> 1.0)
33
+ arel (2.2.1)
34
+ bcrypt-ruby (3.0.0)
35
+ builder (3.0.0)
36
+ diff-lcs (1.1.3)
37
+ erubis (2.7.0)
37
38
  git (1.2.5)
38
- i18n (0.5.0)
39
- jeweler (1.4.0)
40
- gemcutter (>= 0.1.0)
39
+ hike (1.2.1)
40
+ i18n (0.6.0)
41
+ jeweler (1.6.4)
42
+ bundler (~> 1.0)
41
43
  git (>= 1.2.5)
42
- rubyforge (>= 2.0.0)
43
- json_pure (1.4.6)
44
- mail (2.2.14)
45
- activesupport (>= 2.3.6)
44
+ rake
45
+ mail (2.3.0)
46
46
  i18n (>= 0.4.0)
47
47
  mime-types (~> 1.16)
48
48
  treetop (~> 1.4.8)
49
49
  mime-types (1.16)
50
- polyglot (0.3.1)
51
- rack (1.2.1)
52
- rack-mount (0.6.13)
50
+ multi_json (1.0.3)
51
+ polyglot (0.3.2)
52
+ rack (1.3.2)
53
+ rack-cache (1.0.3)
54
+ rack (>= 0.4)
55
+ rack-mount (0.8.3)
53
56
  rack (>= 1.0.0)
54
- rack-test (0.5.7)
57
+ rack-ssl (1.3.2)
58
+ rack
59
+ rack-test (0.6.1)
55
60
  rack (>= 1.0)
56
- rails (3.0.3)
57
- actionmailer (= 3.0.3)
58
- actionpack (= 3.0.3)
59
- activerecord (= 3.0.3)
60
- activeresource (= 3.0.3)
61
- activesupport (= 3.0.3)
61
+ rails (3.1.0)
62
+ actionmailer (= 3.1.0)
63
+ actionpack (= 3.1.0)
64
+ activerecord (= 3.1.0)
65
+ activeresource (= 3.1.0)
66
+ activesupport (= 3.1.0)
62
67
  bundler (~> 1.0)
63
- railties (= 3.0.3)
64
- railties (3.0.3)
65
- actionpack (= 3.0.3)
66
- activesupport (= 3.0.3)
68
+ railties (= 3.1.0)
69
+ railties (3.1.0)
70
+ actionpack (= 3.1.0)
71
+ activesupport (= 3.1.0)
72
+ rack-ssl (~> 1.3.2)
67
73
  rake (>= 0.8.7)
68
- thor (~> 0.14.4)
69
- rake (0.8.7)
70
- rspec (2.0.1)
71
- rspec-core (~> 2.0.1)
72
- rspec-expectations (~> 2.0.1)
73
- rspec-mocks (~> 2.0.1)
74
- rspec-core (2.0.1)
75
- rspec-expectations (2.0.1)
76
- diff-lcs (>= 1.1.2)
77
- rspec-mocks (2.0.1)
78
- rspec-core (~> 2.0.1)
79
- rspec-expectations (~> 2.0.1)
80
- rubyforge (2.0.4)
81
- json_pure (>= 1.1.7)
82
- sqlite3-ruby (1.3.1)
74
+ rdoc (~> 3.4)
75
+ thor (~> 0.14.6)
76
+ rake (0.9.2)
77
+ rdoc (3.9.4)
78
+ rspec (2.6.0)
79
+ rspec-core (~> 2.6.0)
80
+ rspec-expectations (~> 2.6.0)
81
+ rspec-mocks (~> 2.6.0)
82
+ rspec-core (2.6.4)
83
+ rspec-expectations (2.6.0)
84
+ diff-lcs (~> 1.1.2)
85
+ rspec-mocks (2.6.0)
86
+ sprockets (2.0.0)
87
+ hike (~> 1.2)
88
+ rack (~> 1.0)
89
+ tilt (~> 1.1, != 1.3.0)
90
+ sqlite3 (1.3.4)
83
91
  thor (0.14.6)
84
- treetop (1.4.9)
92
+ tilt (1.3.3)
93
+ treetop (1.4.10)
94
+ polyglot
85
95
  polyglot (>= 0.3.1)
86
- tzinfo (0.3.23)
96
+ tzinfo (0.3.29)
87
97
 
88
98
  PLATFORMS
89
99
  ruby
90
100
 
91
101
  DEPENDENCIES
92
- jeweler (= 1.4.0)
93
- rails (~> 3.0.0)
102
+ jeweler (~> 1.6.4)
103
+ rails
94
104
  rake
95
- rspec (~> 2.0.0)
96
- sqlite3-ruby
105
+ rspec (~> 2.6.0)
106
+ sqlite3
data/README.textile CHANGED
@@ -12,7 +12,7 @@ The gem is hosted at "rubygems.org":https://rubygems.org/gems/sudo_attributes an
12
12
 
13
13
  h2. The Problem
14
14
 
15
- ActiveRecord (ActiveModel in Rails 3) provides a convenient way to make your application more secure by using "protected" attributes. Protected attributes are assigned using either @attr_protected@ or @attr_accessible@. This adds security by preventing mass assignment of attributes when doing things like @user.update_attributes(params[:user])@. The issue is that it can be tedious to always manually assign protected attributes in an administrative area of your application. You may find yourself doing things like:
15
+ ActiveModel provides a convenient way to make your application more secure by using "protected" attributes. Protected attributes are assigned using either @attr_protected@ or @attr_accessible@. This adds security by preventing mass assignment of attributes when doing things like @user.update_attributes(params[:user])@. The issue is that it can be tedious to always manually assign protected attributes in an administrative area of your application. You may find yourself doing things like:
16
16
 
17
17
  <pre>
18
18
  user = User.find(params[:id])
@@ -22,15 +22,20 @@ user.something_else = true
22
22
  user.save
23
23
  </pre>
24
24
 
25
+ or the alternative in Rails 3.1:
26
+
27
+ <pre>
28
+ user.assign_attributes(params[:user], :without_protection => true)
29
+ user.save
30
+ </pre>
31
+
25
32
  h2. The Solution
26
33
 
27
34
  SudoAttributes adds a few 'sudo' methods to your models, allowing you to override the protected attributes **when you know the input can be trusted**.
28
35
 
29
- It's as easy as adding one method call to your models like so:
30
-
31
36
  <pre>
32
37
  class User < ActiveRecord::Base
33
- sudo_attr_protected :admin
38
+ attr_protected :admin
34
39
  end
35
40
 
36
41
  user = User.find(params[:id])
@@ -39,18 +44,6 @@ user.sudo_update_attributes(params[:user])
39
44
 
40
45
  h2. Class Methods
41
46
 
42
- The class methods *sudo_attr_protected* and *sudo_attr_accessible* will be available to all ActiveRecord models. When called, it adds numerous 'sudo' methods to the class. You may still use the default methods @attr_protected@ or @attr_accessible@ provided by rails, but you must still call @has_sudo_attributes@ in order to gain access to the sudo methods.
43
-
44
- Here are four different ways it can be used:
45
-
46
- @sudo_attr_protected :attribute1, :attribute2@ - Defines protected attributes
47
-
48
- @sudo_attr_accessible :attribute1, :attribute2@ - Defines accessible attributes
49
-
50
- @sudo_attr_protected@ or @sudo_attr_accessible@ - With no arguments, it will rely on calls to @attr_protected@ or @attr_accessible@
51
-
52
- Any model that calls @sudo_attr_*@ will also be able to create new instances that override protected attributes using the following methods:
53
-
54
47
  @Model.sudo_create@ - Uses same syntax as @Model.create@ to instantiate and save an object with protected attributes
55
48
 
56
49
  @Model.sudo_create!@ - Similar to @Model.sudo_create@, but it raises an ActiveRecord::RecordInvalid exception if there are invalid attributes
@@ -59,10 +52,9 @@ Any model that calls @sudo_attr_*@ will also be able to create new instances tha
59
52
 
60
53
  h2. Instance Methods
61
54
 
62
- The following instance method is available to any ActiveRecord model that calls @has_sudo_attributes@
55
+ @sudo_update_attributes@ - Uses identical syntax to @update_attributes@, but overrides protected attributes.
63
56
 
64
- * @sudo_update_attributes@ - Uses identical syntax to @update_attributes@, but overrides protected attributes.
65
- * @sudo_update_attributes!@ - Same as sudo_update_attributes, but raises ActiveRecord errors. Same as @update_attributes!@
57
+ @sudo_update_attributes!@ - Same as sudo_update_attributes, but raises ActiveRecord errors. Same as @update_attributes!@
66
58
 
67
59
  h2. Examples
68
60
 
@@ -70,7 +62,7 @@ h2. Examples
70
62
 
71
63
  <pre>
72
64
  class User < ActiveRecord::Base
73
- sudo_attr_protected :admin
65
+ attr_protected :admin
74
66
  end
75
67
  </pre>
76
68
 
@@ -90,4 +82,4 @@ params[:user] = {:admin => false, :name => "Pete"}
90
82
 
91
83
  h2. Copyright
92
84
 
93
- Copyright (c) 2010 Peter Brown. See LICENSE for details.
85
+ Copyright (c) 2011 Peter Brown. See LICENSE for details.
data/Rakefile CHANGED
@@ -1,52 +1,34 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'rubygems'
2
4
  require 'rake'
3
5
 
4
6
  begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "sudo_attributes"
8
- gem.summary = %Q{Override ActiveRecord protected attributes with mass assignment}
9
- gem.description = %Q{Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment}
10
- gem.email = "github@lette.us"
11
- gem.homepage = "http://github.com/beerlington/sudo_attributes"
12
- gem.authors = ["Peter Brown"]
13
- gem.add_development_dependency "rspec", "~> 2.0.0"
14
- gem.add_development_dependency "rails", "~> 3.0.0"
15
- gem.add_development_dependency "sqlite3-ruby"
16
- gem.add_dependency "rails", ">= 2.3.0"
17
- end
18
- Jeweler::GemcutterTasks.new
19
- rescue LoadError
20
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
21
12
  end
13
+ require 'rake'
22
14
 
23
- require 'spec/rake/spectask'
24
- Spec::Rake::SpecTask.new(:spec) do |spec|
25
- spec.libs << 'lib' << 'spec'
26
- spec.spec_files = FileList['spec/**/*_spec.rb']
15
+ require 'jeweler'
16
+ Jeweler::Tasks.new do |gem|
17
+ gem.name = "sudo_attributes"
18
+ gem.summary = %Q{Override ActiveRecord protected attributes with mass assignment}
19
+ gem.description = %Q{Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment}
20
+ gem.email = "github@lette.us"
21
+ gem.homepage = "http://github.com/beerlington/sudo_attributes"
22
+ gem.authors = ["Peter Brown"]
23
+ gem.license = "MIT"
24
+ # dependencies defined in Gemfile
27
25
  end
26
+ Jeweler::RubygemsDotOrgTasks.new
28
27
 
29
- begin
30
- require 'rcov/rcovtask'
31
- Rcov::RcovTask.new do |test|
32
- test.libs << 'test'
33
- test.pattern = 'test/**/test_*.rb'
34
- test.verbose = true
35
- end
36
- rescue LoadError
37
- task :rcov do
38
- abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
- end
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
40
32
  end
41
33
 
42
34
  task :default => :spec
43
-
44
- require 'rake/rdoctask'
45
- Rake::RDocTask.new do |rdoc|
46
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
-
48
- rdoc.rdoc_dir = 'rdoc'
49
- rdoc.title = "sudo_attributes #{version}"
50
- rdoc.rdoc_files.include('README*')
51
- rdoc.rdoc_files.include('lib/**/*.rb')
52
- end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 1.0.1
@@ -1,59 +1,38 @@
1
1
  module SudoAttributes
2
+ extend ActiveSupport::Concern
2
3
 
3
- module Base
4
-
5
- # Protect attributes using ActiveRecord's built in <tt>attr_protected</tt> class macro.
6
- # When invoked, it also adds other sudo_attributes class and instance methods to model such as +sudo_create+
7
- #
8
- # ==== Example
9
- # # Define attributes which are protected from mass assignment
10
- # class User < ActiveRecord::Base
11
- # sudo_attr_protected :admin
12
- # end
13
- def sudo_attr_protected(*attrs)
14
- Private::set_attributes(self, attrs, :protected)
15
- end
16
-
17
- # Protect attributes using ActiveRecord's built in <tt>attr_accessible</tt> class macro.
18
- # When invoked, it also adds other sudo_attributes class and instance methods to model such as +sudo_create+
19
- #
20
- # ==== Example
21
- # # Define attributes which are not protected from mass assignment
22
- # class User < ActiveRecord::Base
23
- # sudo_attr_accessible :admin
24
- # end
25
- def sudo_attr_accessible(*attrs)
26
- Private::set_attributes(self, attrs, :accessible)
27
- end
28
- end
29
-
30
- module Private # :nodoc: all
31
-
32
- # Used internally to assign protected attributes and include additional sudo_attributes functionality
33
- def self.set_attributes(klass, attrs, type)
34
- # Call attr_(accessible|protected) if attributes are passed in
35
- klass.send("attr_#{type}", *attrs) unless attrs.empty?
36
-
37
- klass.extend SudoAttributes::ClassMethods
38
- klass.send :include, SudoAttributes::InstanceMethods
39
- end
40
- end
41
-
42
- # Added to ActiveRecord model only if sudo_attr_(accessible|protected) is called
43
4
  module ClassMethods
44
- # Creates an object with protected attributes and saves it to the database, if validations pass.
5
+ # Creates an object (or multiple objects) with protected attributes and saves it to the database, if validations pass.
45
6
  # The resulting object is returned whether the object was saved successfully to the database or not.
46
7
  #
47
- # Unlike ActiveRecord::Base.create, the <tt>attributes</tt> parameter can only be a Hash. This Hash describes the
8
+ # The +attributes+ parameter can be either be a Hash or an Array of Hashes. These Hashes describe the
48
9
  # attributes on the objects that are to be created.
49
10
  #
50
- # ==== Example
51
- # # Create a single new object where admin is a protected attribute
52
- # User.sudo_create(:first_name => 'Pete', :admin => true)
53
- def sudo_create(attributes=nil)
54
- instance = sudo_new(attributes)
55
- instance.save
56
- instance
11
+ # ==== Examples
12
+ # # Create a single new object
13
+ # User.sudo_create(:first_name => 'Pete')
14
+ #
15
+ # # Create an Array of new objects
16
+ # User.sudo_create([{ :first_name => 'Pete' }, { :first_name => 'Sebastian' }])
17
+ #
18
+ # # Create a single object and pass it into a block to set other attributes.
19
+ # User.sudo_create(:first_name => 'Pete') do |u|
20
+ # u.is_admin = false
21
+ # end
22
+ #
23
+ # # Creating an Array of new objects using a block, where the block is executed for each object:
24
+ # User.sudo_create([{ :first_name => 'Pete' }, { :first_name => 'Sebastian' }]) do |u|
25
+ # u.is_admin = false
26
+ # end
27
+ def sudo_create(attributes = nil, &block)
28
+ if attributes.is_a?(Array)
29
+ attributes.collect { |attr| sudo_create(attr, &block) }
30
+ else
31
+ object = sudo_new(attributes)
32
+ yield(object) if block_given?
33
+ object.save
34
+ object
35
+ end
57
36
  end
58
37
 
59
38
  # Creates an object just like sudo_create but calls save! instead of save so an exception is raised if the record is invalid
@@ -61,10 +40,15 @@ module SudoAttributes
61
40
  # ==== Example
62
41
  # # Create a single new object where admin is a protected attribute
63
42
  # User.sudo_create!(:first_name => 'Pete', :admin => true)
64
- def sudo_create!(attributes=nil)
65
- instance = sudo_new(attributes)
66
- instance.save!
67
- instance
43
+ def sudo_create!(attributes = nil, &block)
44
+ if attributes.is_a?(Array)
45
+ attributes.collect { |attr| sudo_create!(attr, &block) }
46
+ else
47
+ object = sudo_new(attributes)
48
+ yield(object) if block_given?
49
+ object.save!
50
+ object
51
+ end
68
52
  end
69
53
 
70
54
  # Instantiates an object just like ActiveRecord::Base.new, but allowing mass assignment of protected attributes
@@ -72,9 +56,9 @@ module SudoAttributes
72
56
  # ==== Example
73
57
  # # Instantiate an object where admin is a protected attribute
74
58
  # User.sudo_new(:first_name => 'Pete', :admin => true)
75
- def sudo_new(attributes=nil)
59
+ def sudo_new(attributes = nil)
76
60
  instance = new(nil)
77
- instance.send(:attributes=, attributes, false)
61
+ instance.sudo_assign_attributes(attributes)
78
62
  instance
79
63
  end
80
64
 
@@ -82,7 +66,6 @@ module SudoAttributes
82
66
 
83
67
  end
84
68
 
85
- # Added to ActiveRecord model only if sudo_attr_(accessible|protected) is called
86
69
  module InstanceMethods
87
70
 
88
71
  # Updates attributes of a model, including protected ones, from the passed-in hash and saves the
@@ -93,7 +76,7 @@ module SudoAttributes
93
76
  # @user = User.find(params[:id])
94
77
  # @user.sudo_update_attributes(params[:user])
95
78
  def sudo_update_attributes(new_attributes)
96
- self.send(:attributes=, new_attributes, false)
79
+ sudo_assign_attributes(new_attributes)
97
80
  save
98
81
  end
99
82
 
@@ -105,10 +88,19 @@ module SudoAttributes
105
88
  # @user = User.find(params[:id])
106
89
  # @user.sudo_update_attributes!(params[:user])
107
90
  def sudo_update_attributes!(new_attributes)
108
- self.send(:attributes=, new_attributes, false)
91
+ sudo_assign_attributes(new_attributes)
109
92
  save!
110
93
  end
94
+
95
+ # Used by sudo_attributes internally as a common API between Rails 3 and 3.1
96
+ def sudo_assign_attributes(attributes)
97
+ if respond_to? :assign_attributes
98
+ assign_attributes(attributes, :without_protection => true)
99
+ else
100
+ self.send(:attributes=, attributes, false)
101
+ end
102
+ end
111
103
  end
112
104
  end
113
105
 
114
- ActiveRecord::Base.extend SudoAttributes::Base
106
+ ActiveRecord::Base.send(:include, SudoAttributes)
data/spec/spec_helper.rb CHANGED
@@ -2,9 +2,9 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
 
4
4
  require 'rubygems'
5
+ require 'rspec'
5
6
  require 'active_record'
6
7
  require 'sudo_attributes'
7
- require 'spec'
8
8
 
9
9
  ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
10
10
 
@@ -16,25 +16,11 @@ ActiveRecord::Schema.define(:version => 1) do
16
16
  end
17
17
  end
18
18
 
19
- module SudoAttributesTest
20
- ARGUMENTS = [
21
- "sudo_attr_protected :name",
22
- "sudo_attr_accessible :color, :age",
23
- "attr_protected :name; sudo_attr_protected"
24
- ]
25
-
26
- def self.build_cat_class(argument)
27
-
28
- # Remove the Cat class if it's already been defined in previous run
29
- Object.class_eval { remove_const "Cat" if const_defined? "Cat" }
30
-
31
- # Create a new Cat class and evaluate 'has_sudo_attributes :arguments
32
- klass = Class.new(ActiveRecord::Base)
33
- Object.const_set("Cat", klass)
19
+ RSpec.configure do |config|
20
+ config.color_enabled = true
21
+ end
34
22
 
35
- Cat.class_eval %{
36
- validates_presence_of :color
37
- #{argument}
38
- }
39
- end
23
+ class Cat < ActiveRecord::Base
24
+ attr_protected :name
25
+ validates_presence_of :color
40
26
  end
@@ -1,124 +1,148 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
-
4
3
  describe "Cat" do
5
-
6
- SudoAttributesTest::ARGUMENTS.each do |arguments|
7
-
8
- SudoAttributesTest::build_cat_class(arguments)
9
-
10
- context "calling has_sudo_attributes #{arguments.inspect}" do
11
-
12
- before(:all) do
13
- @attributes = {:name => "Smiles", :color => "gray", :age => 6}
14
- end
4
+ let(:attributes) { {:name => "Smiles", :color => "gray", :age => 6} }
5
+
6
+ context "default rails initializer" do
7
+ let(:cat) { Cat.new(attributes) }
8
+
9
+ context 'baseline check' do
10
+ subject { cat }
11
+ its(:name) { should be_nil }
12
+ its(:color) { should eql('gray') }
13
+ its(:age) { should eql(6) }
14
+ end
15
+
16
+ it "should not set the name with update_attributes" do
17
+ cat.update_attributes(:name => "Smiles")
18
+ cat.name.should be_nil
19
+ end
20
+
21
+ it "should set the name with sudo_update_attributes" do
22
+ cat.sudo_update_attributes(:name => "Smiles")
23
+ cat.name.should == "Smiles"
24
+ end
25
+
26
+ it "should not raise an error with sudo_update_attributes" do
27
+ lambda { cat.sudo_update_attributes(:color => "") }.should_not raise_error(ActiveRecord::RecordInvalid)
28
+ end
29
+
30
+ it "should set the name with sudo_update_attributes!" do
31
+ cat.sudo_update_attributes!(:name => "Smiles")
32
+ cat.name.should == "Smiles"
33
+ end
34
+
35
+ it "should raise an error with sudo_update_attributes!" do
36
+ lambda { cat.sudo_update_attributes!(:color => "") }.should raise_error(ActiveRecord::RecordInvalid)
37
+ end
38
+ end
39
+
40
+ # Tests for sudo_new and sudo_build, aliases of each other
41
+ [:sudo_new, :sudo_build].each do |sudo_method|
42
+
43
+ context "#{sudo_method} initializer" do
44
+ let(:cat) { Cat.send(sudo_method, attributes) }
15
45
 
16
- context "that is built using" do
17
-
18
- context "default rails initializer" do
19
- before(:each) { @cat = Cat.new @attributes}
20
-
21
- it "should not have a name" do
22
- @cat.name.should be_nil
23
- end
24
-
25
- it "should not set the name with update_attributes" do
26
- @cat.update_attributes(:name => "Smiles")
27
- @cat.name.should be_nil
28
- end
29
-
30
- it "should set the name with sudo_update_attributes" do
31
- @cat.sudo_update_attributes(:name => "Smiles")
32
- @cat.name.should == "Smiles"
33
- end
34
-
35
- it "should not raise an error with sudo_update_attributes" do
36
- lambda { @cat.sudo_update_attributes(:color => "") }.should_not raise_error(ActiveRecord::RecordInvalid)
37
- end
38
-
39
- it "should set the name with sudo_update_attributes!" do
40
- @cat.sudo_update_attributes!(:name => "Smiles")
41
- @cat.name.should == "Smiles"
42
- end
43
-
44
- it "should raise an error with sudo_update_attributes!" do
45
- lambda { @cat.sudo_update_attributes!(:color => "") }.should raise_error(ActiveRecord::RecordInvalid)
46
- end
47
-
48
- it "should have a color" do
49
- @cat.color.should == @attributes[:color]
50
- end
51
-
52
- it "should have an age" do
53
- @cat.age.should == @attributes[:age]
54
- end
55
-
56
- end
57
-
58
- # Tests for sudo_new and sudo_build, aliases of each other
59
- [:sudo_new, :sudo_build].each do |sudo_method|
60
-
61
- context "SudoAttributes #{sudo_method} initializer" do
62
- before(:each) { @cat = Cat.send(sudo_method, @attributes)}
63
-
64
- it "should have a name" do
65
- @cat.name.should == @attributes[:name]
66
- end
67
-
68
- it "should set the name with sudo_update_attributes" do
69
- @cat.sudo_update_attributes(:name => "Portia")
70
- @cat.name.should == "Portia"
71
- end
72
-
73
- it "should have a color" do
74
- @cat.color.should == @attributes[:color]
75
- end
76
-
77
- it "should not have an id" do
78
- @cat.id.should be_nil
79
- end
80
-
81
- it "should have an age" do
82
- @cat.age.should == @attributes[:age]
83
- end
84
- end
85
- end
86
-
87
- context "SudoAttributes sudo_create initializer" do
88
- before(:each) { @cat = Cat.sudo_create @attributes}
89
-
90
- it "should have a name" do
91
- @cat.name.should == @attributes[:name]
92
- end
93
-
94
- it "should set the name with sudo_update_attributes" do
95
- @cat.sudo_update_attributes(:name => "Portia")
96
- @cat.name.should == "Portia"
97
- end
98
-
99
- it "should have a color" do
100
- @cat.color.should == @attributes[:color]
101
- end
102
-
103
- it "should have an id" do
104
- @cat.id.should_not be_nil
105
- end
106
-
107
- it "should have an age" do
108
- @cat.age.should == @attributes[:age]
109
- end
110
- end
46
+ subject { cat }
47
+ its(:name) { should eql('Smiles') }
48
+ its(:color) { should eql('gray') }
49
+ its(:id) { should be_nil }
50
+ its(:age) { should eql(6) }
51
+
52
+ it "should set the name with sudo_update_attributes" do
53
+ cat.sudo_update_attributes(:name => "Portia")
54
+ cat.name.should == "Portia"
111
55
  end
56
+
112
57
  end
113
58
  end
114
- end
115
59
 
116
- describe "A Cat" do
60
+ context "sudo_create initializer for single object" do
61
+ let(:cat) { Cat.sudo_create(attributes) }
117
62
 
118
- before(:each) do
119
- SudoAttributesTest::build_cat_class("sudo_attr_protected :name")
63
+ subject { cat }
64
+ its(:name) { should eql('Smiles') }
65
+ its(:color) { should eql('gray') }
66
+ its(:id) { should_not be_nil }
67
+ its(:age) { should eql(6) }
120
68
  end
121
69
 
70
+ context "sudo_create initializer w/block for single object" do
71
+ let(:cat) { Cat.sudo_create(attributes) {|u| u.color = 'white' } }
72
+
73
+ subject { cat }
74
+ its(:name) { should eql('Smiles') }
75
+ its(:color) { should eql('white') }
76
+ its(:id) { should_not be_nil }
77
+ its(:age) { should eql(6) }
78
+ end
79
+
80
+ context "sudo_create initializer for multiple objects" do
81
+ let(:attributes2) { {:name => "Portia", :color => "black", :age => 4} }
82
+ let(:cats) { Cat.sudo_create([attributes, attributes2]) }
83
+
84
+ context 'first object' do
85
+ subject { cats[0] }
86
+ its(:name) { should eql('Smiles') }
87
+ its(:color) { should eql('gray') }
88
+ its(:id) { should_not be_nil }
89
+ its(:age) { should eql(6) }
90
+ end
91
+
92
+ context 'second object' do
93
+ subject { cats[1] }
94
+ its(:name) { should eql('Portia') }
95
+ its(:color) { should eql('black') }
96
+ its(:id) { should_not be_nil }
97
+ its(:age) { should eql(4) }
98
+ end
99
+ end
100
+
101
+ context "sudo_create! initializer for single object" do
102
+ let(:cat) { Cat.sudo_create!(attributes) }
103
+
104
+ subject { cat }
105
+ its(:name) { should eql('Smiles') }
106
+ its(:color) { should eql('gray') }
107
+ its(:id) { should_not be_nil }
108
+ its(:age) { should eql(6) }
109
+ end
110
+
111
+ context "sudo_create! initializer w/block for single object" do
112
+ let(:cat) { Cat.sudo_create!(attributes) {|u| u.color = 'white' } }
113
+
114
+ subject { cat }
115
+ its(:name) { should eql('Smiles') }
116
+ its(:color) { should eql('white') }
117
+ its(:id) { should_not be_nil }
118
+ its(:age) { should eql(6) }
119
+ end
120
+
121
+ context "sudo_create! initializer for multiple objects" do
122
+ let(:attributes2) { {:name => "Portia", :color => "black", :age => 4} }
123
+ let(:cats) { Cat.sudo_create!([attributes, attributes2]) }
124
+
125
+ context 'first object' do
126
+ subject { cats[0] }
127
+ its(:name) { should eql('Smiles') }
128
+ its(:color) { should eql('gray') }
129
+ its(:id) { should_not be_nil }
130
+ its(:age) { should eql(6) }
131
+ end
132
+
133
+ context 'second object' do
134
+ subject { cats[1] }
135
+ its(:name) { should eql('Portia') }
136
+ its(:color) { should eql('black') }
137
+ its(:id) { should_not be_nil }
138
+ its(:age) { should eql(4) }
139
+ end
140
+ end
141
+
142
+ end
143
+
144
+ describe "A Cat" do
145
+
122
146
  context "when initialized with invalid params using sudo_create!" do
123
147
 
124
148
  it "should raise an ActiveRecord exception" do
@@ -142,8 +166,8 @@ describe "A Cat" do
142
166
  end
143
167
 
144
168
  it "should have a name" do
145
- @cat = Cat.sudo_create! :name => "Smiles", :color => "gray", :age => 12
146
- @cat.name.should == "Smiles"
169
+ cat = Cat.sudo_create! :name => "Smiles", :color => "gray", :age => 12
170
+ cat.name.should == "Smiles"
147
171
  end
148
172
  end
149
173
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sudo_attributes}
8
- s.version = "0.5.2"
8
+ s.version = "1.0.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Peter Brown"]
12
- s.date = %q{2011-02-19}
11
+ s.authors = [%q{Peter Brown}]
12
+ s.date = %q{2011-09-06}
13
13
  s.description = %q{Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment}
14
14
  s.email = %q{github@lette.us}
15
15
  s.extra_rdoc_files = [
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ ".travis.yml",
21
22
  "Gemfile",
22
23
  "Gemfile.lock",
23
24
  "LICENSE",
@@ -32,33 +33,21 @@ Gem::Specification.new do |s|
32
33
  "sudo_attributes.gemspec"
33
34
  ]
34
35
  s.homepage = %q{http://github.com/beerlington/sudo_attributes}
35
- s.require_paths = ["lib"]
36
- s.rubygems_version = %q{1.5.2}
36
+ s.licenses = [%q{MIT}]
37
+ s.require_paths = [%q{lib}]
38
+ s.rubygems_version = %q{1.8.6}
37
39
  s.summary = %q{Override ActiveRecord protected attributes with mass assignment}
38
- s.test_files = [
39
- "spec/spec_helper.rb",
40
- "spec/sudo_attributes_spec.rb"
41
- ]
42
40
 
43
41
  if s.respond_to? :specification_version then
44
42
  s.specification_version = 3
45
43
 
46
44
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
- s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
48
- s.add_development_dependency(%q<rails>, ["~> 3.0.0"])
49
- s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
50
- s.add_runtime_dependency(%q<rails>, [">= 2.3.0"])
45
+ s.add_runtime_dependency(%q<rails>, [">= 0"])
51
46
  else
52
- s.add_dependency(%q<rspec>, ["~> 2.0.0"])
53
- s.add_dependency(%q<rails>, ["~> 3.0.0"])
54
- s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
55
- s.add_dependency(%q<rails>, [">= 2.3.0"])
47
+ s.add_dependency(%q<rails>, [">= 0"])
56
48
  end
57
49
  else
58
- s.add_dependency(%q<rspec>, ["~> 2.0.0"])
59
- s.add_dependency(%q<rails>, ["~> 3.0.0"])
60
- s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
61
- s.add_dependency(%q<rails>, [">= 2.3.0"])
50
+ s.add_dependency(%q<rails>, [">= 0"])
62
51
  end
63
52
  end
64
53
 
metadata CHANGED
@@ -1,96 +1,38 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: sudo_attributes
3
- version: !ruby/object:Gem::Version
4
- hash: 15
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 5
9
- - 2
10
- version: 0.5.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Peter Brown
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-02-19 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
22
- type: :development
23
- requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
- requirements:
26
- - - ~>
27
- - !ruby/object:Gem::Version
28
- hash: 15
29
- segments:
30
- - 2
31
- - 0
32
- - 0
33
- version: 2.0.0
34
- name: rspec
35
- version_requirements: *id001
36
- prerelease: false
37
- - !ruby/object:Gem::Dependency
38
- type: :development
39
- requirement: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ~>
43
- - !ruby/object:Gem::Version
44
- hash: 7
45
- segments:
46
- - 3
47
- - 0
48
- - 0
49
- version: 3.0.0
12
+ date: 2011-09-06 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
50
15
  name: rails
51
- version_requirements: *id002
52
- prerelease: false
53
- - !ruby/object:Gem::Dependency
54
- type: :development
55
- requirement: &id003 !ruby/object:Gem::Requirement
16
+ requirement: &70236626225500 !ruby/object:Gem::Requirement
56
17
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- hash: 3
61
- segments:
62
- - 0
63
- version: "0"
64
- name: sqlite3-ruby
65
- version_requirements: *id003
66
- prerelease: false
67
- - !ruby/object:Gem::Dependency
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
68
22
  type: :runtime
69
- requirement: &id004 !ruby/object:Gem::Requirement
70
- none: false
71
- requirements:
72
- - - ">="
73
- - !ruby/object:Gem::Version
74
- hash: 3
75
- segments:
76
- - 2
77
- - 3
78
- - 0
79
- version: 2.3.0
80
- name: rails
81
- version_requirements: *id004
82
23
  prerelease: false
83
- description: Adds 'sudo' methods to update protected ActiveRecord attributes with mass assignment
24
+ version_requirements: *70236626225500
25
+ description: Adds 'sudo' methods to update protected ActiveRecord attributes with
26
+ mass assignment
84
27
  email: github@lette.us
85
28
  executables: []
86
-
87
29
  extensions: []
88
-
89
- extra_rdoc_files:
30
+ extra_rdoc_files:
90
31
  - LICENSE
91
32
  - README.textile
92
- files:
33
+ files:
93
34
  - .document
35
+ - .travis.yml
94
36
  - Gemfile
95
37
  - Gemfile.lock
96
38
  - LICENSE
@@ -103,40 +45,32 @@ files:
103
45
  - spec/spec_helper.rb
104
46
  - spec/sudo_attributes_spec.rb
105
47
  - sudo_attributes.gemspec
106
- has_rdoc: true
107
48
  homepage: http://github.com/beerlington/sudo_attributes
108
- licenses: []
109
-
49
+ licenses:
50
+ - MIT
110
51
  post_install_message:
111
52
  rdoc_options: []
112
-
113
- require_paths:
53
+ require_paths:
114
54
  - lib
115
- required_ruby_version: !ruby/object:Gem::Requirement
55
+ required_ruby_version: !ruby/object:Gem::Requirement
116
56
  none: false
117
- requirements:
118
- - - ">="
119
- - !ruby/object:Gem::Version
120
- hash: 3
121
- segments:
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
122
62
  - 0
123
- version: "0"
124
- required_rubygems_version: !ruby/object:Gem::Requirement
63
+ hash: 1087971882480331192
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
65
  none: false
126
- requirements:
127
- - - ">="
128
- - !ruby/object:Gem::Version
129
- hash: 3
130
- segments:
131
- - 0
132
- version: "0"
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
133
70
  requirements: []
134
-
135
71
  rubyforge_project:
136
- rubygems_version: 1.5.2
72
+ rubygems_version: 1.8.6
137
73
  signing_key:
138
74
  specification_version: 3
139
75
  summary: Override ActiveRecord protected attributes with mass assignment
140
- test_files:
141
- - spec/spec_helper.rb
142
- - spec/sudo_attributes_spec.rb
76
+ test_files: []