active_attr 0.4.1 → 0.5.0.alpha1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of active_attr might be problematic. Click here for more details.
- data/.travis.yml +7 -0
- data/CHANGELOG.md +14 -0
- data/README.md +57 -25
- data/Rakefile +2 -0
- data/active_attr.gemspec +3 -2
- data/gemfiles/rails_3_1.gemfile +6 -0
- data/lib/active_attr.rb +3 -2
- data/lib/active_attr/attribute_defaults.rb +120 -0
- data/lib/active_attr/attribute_definition.rb +25 -2
- data/lib/active_attr/attributes.rb +44 -33
- data/lib/active_attr/logger.rb +8 -8
- data/lib/active_attr/mass_assignment.rb +2 -1
- data/lib/active_attr/matchers/have_attribute_matcher.rb +28 -10
- data/lib/active_attr/model.rb +2 -0
- data/lib/active_attr/query_attributes.rb +2 -5
- data/lib/active_attr/typecasted_attributes.rb +77 -0
- data/lib/active_attr/typecasting.rb +60 -0
- data/lib/active_attr/typecasting/boolean.rb +6 -0
- data/lib/active_attr/typecasting/boolean_typecaster.rb +21 -0
- data/lib/active_attr/typecasting/date_time_typecaster.rb +14 -0
- data/lib/active_attr/typecasting/date_typecaster.rb +14 -0
- data/lib/active_attr/typecasting/float_typecaster.rb +11 -0
- data/lib/active_attr/typecasting/integer_typecaster.rb +12 -0
- data/lib/active_attr/typecasting/object_typecaster.rb +9 -0
- data/lib/active_attr/typecasting/string_typecaster.rb +11 -0
- data/lib/active_attr/version.rb +1 -1
- data/spec/functional/active_attr/attribute_defaults_spec.rb +136 -0
- data/spec/functional/active_attr/attributes_spec.rb +19 -5
- data/spec/functional/active_attr/model_spec.rb +34 -4
- data/spec/functional/active_attr/typecasted_attributes_spec.rb +116 -0
- data/spec/support/mass_assignment_shared_examples.rb +1 -1
- data/spec/unit/active_attr/attribute_defaults_spec.rb +43 -0
- data/spec/unit/active_attr/attribute_definition_spec.rb +12 -8
- data/spec/unit/active_attr/attributes_spec.rb +22 -10
- data/spec/unit/active_attr/mass_assignment_spec.rb +1 -0
- data/spec/unit/active_attr/matchers/have_attribute_matcher_spec.rb +94 -15
- data/spec/unit/active_attr/query_attributes_spec.rb +1 -1
- data/spec/unit/active_attr/typecasted_attributes_spec.rb +76 -0
- data/spec/unit/active_attr/typecasting/boolean_spec.rb +10 -0
- data/spec/unit/active_attr/typecasting/boolean_typecaster_spec.rb +147 -0
- data/spec/unit/active_attr/typecasting/date_time_typecaster_spec.rb +67 -0
- data/spec/unit/active_attr/typecasting/date_typecaster_spec.rb +55 -0
- data/spec/unit/active_attr/typecasting/float_typecaster_spec.rb +27 -0
- data/spec/unit/active_attr/typecasting/integer_typecaster_spec.rb +35 -0
- data/spec/unit/active_attr/typecasting/object_typecaster_spec.rb +15 -0
- data/spec/unit/active_attr/typecasting/string_typecaster_spec.rb +24 -0
- data/spec/unit/active_attr/typecasting_spec.rb +87 -0
- data/spec/unit/active_attr/version_spec.rb +11 -2
- metadata +75 -29
- data/lib/active_attr/strict_mass_assignment.rb +0 -44
- data/lib/active_attr/unknown_attributes_error.rb +0 -18
- data/spec/unit/active_attr/strict_mass_assignment_spec.rb +0 -38
- data/spec/unit/active_attr/unknown_attributes_error_spec.rb +0 -9
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_attr/typecasting/string_typecaster"
|
3
|
+
require "active_support/basic_object"
|
4
|
+
|
5
|
+
module ActiveAttr
|
6
|
+
module Typecasting
|
7
|
+
describe StringTypecaster do
|
8
|
+
describe "#call" do
|
9
|
+
it "returns the original string for a String" do
|
10
|
+
value = "abc"
|
11
|
+
subject.call(value).should equal value
|
12
|
+
end
|
13
|
+
|
14
|
+
it "casts nil to an empty String" do
|
15
|
+
subject.call(nil).should eql ""
|
16
|
+
end
|
17
|
+
|
18
|
+
it "returns the string version of a Symbol" do
|
19
|
+
subject.call(:value).should eql "value"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "active_attr/typecasting"
|
3
|
+
|
4
|
+
module ActiveAttr
|
5
|
+
describe Typecasting do
|
6
|
+
subject { model_class.new }
|
7
|
+
|
8
|
+
let :model_class do
|
9
|
+
Class.new do
|
10
|
+
include Typecasting
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#typecast_attribute" do
|
15
|
+
let(:type) { String }
|
16
|
+
|
17
|
+
it "raises an ArgumentError when a nil type is given" do
|
18
|
+
expect { subject.typecast_attribute(nil, "foo") }.to raise_error(ArgumentError, "a Class must be given")
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when there is no way to typecast the value" do
|
22
|
+
let(:value) { mock("SomeRandomValue") }
|
23
|
+
|
24
|
+
it "returns nil" do
|
25
|
+
subject.stub(:typecast_value).and_return(nil)
|
26
|
+
subject.typecast_attribute(type, value).should be_nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when the value is nil" do
|
31
|
+
let(:name) { nil }
|
32
|
+
let(:model) { model_class.new }
|
33
|
+
subject { model.typecast_attribute(type, name) }
|
34
|
+
|
35
|
+
it "returns the original value" do
|
36
|
+
should eql name
|
37
|
+
end
|
38
|
+
|
39
|
+
it "does not call try to convert the value" do
|
40
|
+
model.should_not_receive(:typecast_value)
|
41
|
+
subject
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#typecast_value" do
|
47
|
+
let(:model) { model_class.new }
|
48
|
+
subject { model.typecast_value(type, value) }
|
49
|
+
let(:value) { mock }
|
50
|
+
|
51
|
+
it "calls BooleanTypecaster when typecasting to Boolean" do
|
52
|
+
Typecasting::BooleanTypecaster.any_instance.should_receive(:call).with(value)
|
53
|
+
model.typecast_value(Typecasting::Boolean, value)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "calls DateTypecaster when typecasting to Date" do
|
57
|
+
Typecasting::DateTypecaster.any_instance.should_receive(:call).with(value)
|
58
|
+
model.typecast_value(Date, value)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "calls DateTypecaster when typecasting to Date" do
|
62
|
+
Typecasting::DateTimeTypecaster.any_instance.should_receive(:call).with(value)
|
63
|
+
model.typecast_value(DateTime, value)
|
64
|
+
end
|
65
|
+
|
66
|
+
it "calls FloatTypecaster when typecasting to Float" do
|
67
|
+
Typecasting::FloatTypecaster.any_instance.should_receive(:call).with(value)
|
68
|
+
model.typecast_value(Float, value)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "calls IntegerTypecaster when typecasting to Integer" do
|
72
|
+
Typecasting::IntegerTypecaster.any_instance.should_receive(:call).with(value)
|
73
|
+
model.typecast_value(Integer, value)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "calls StringTypecaster when typecasting to String" do
|
77
|
+
Typecasting::StringTypecaster.any_instance.should_receive(:call).with(value)
|
78
|
+
model.typecast_value(String, value)
|
79
|
+
end
|
80
|
+
|
81
|
+
it "calls ObjectTypecaster when typecasting to Object" do
|
82
|
+
Typecasting::ObjectTypecaster.any_instance.should_receive(:call).with(value)
|
83
|
+
model.typecast_value(Object, value)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -11,7 +11,8 @@ module ActiveAttr
|
|
11
11
|
let(:gem_version) { Gem::Version.new VERSION }
|
12
12
|
subject { gem_version }
|
13
13
|
|
14
|
-
it { subject.should
|
14
|
+
it { subject.should have_at_least(3).segments }
|
15
|
+
it { subject.should have_at_most(5).segments }
|
15
16
|
|
16
17
|
describe "major version" do
|
17
18
|
subject { gem_version.segments[0] }
|
@@ -28,7 +29,15 @@ module ActiveAttr
|
|
28
29
|
describe "patch version" do
|
29
30
|
subject { gem_version.segments[2] }
|
30
31
|
|
31
|
-
it {
|
32
|
+
it { should be_a_kind_of Fixnum }
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "pre-release version" do
|
36
|
+
subject { VERSION.split(".")[3] }
|
37
|
+
|
38
|
+
it "is nil or starts with a letter and is alphanumeric" do
|
39
|
+
(subject.nil? || subject =~ /^[A-Za-z][0-9A-Za-z]*?/).should be_true
|
40
|
+
end
|
32
41
|
end
|
33
42
|
end
|
34
43
|
end
|
metadata
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_attr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.5.0.alpha1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Chris Griego
|
@@ -10,33 +10,39 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2012-01-30 00:00:00.000000000Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activemodel
|
17
|
-
requirement: &
|
17
|
+
requirement: &70156170094480 !ruby/object:Gem::Requirement
|
18
18
|
none: false
|
19
19
|
requirements:
|
20
|
-
- -
|
20
|
+
- - ! '>='
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '3.1'
|
23
|
+
- - <
|
24
|
+
- !ruby/object:Gem::Version
|
25
|
+
version: '4.1'
|
23
26
|
type: :runtime
|
24
27
|
prerelease: false
|
25
|
-
version_requirements: *
|
28
|
+
version_requirements: *70156170094480
|
26
29
|
- !ruby/object:Gem::Dependency
|
27
30
|
name: activesupport
|
28
|
-
requirement: &
|
31
|
+
requirement: &70156170093260 !ruby/object:Gem::Requirement
|
29
32
|
none: false
|
30
33
|
requirements:
|
31
|
-
- -
|
34
|
+
- - ! '>='
|
32
35
|
- !ruby/object:Gem::Version
|
33
36
|
version: '3.1'
|
37
|
+
- - <
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '4.1'
|
34
40
|
type: :runtime
|
35
41
|
prerelease: false
|
36
|
-
version_requirements: *
|
42
|
+
version_requirements: *70156170093260
|
37
43
|
- !ruby/object:Gem::Dependency
|
38
44
|
name: bundler
|
39
|
-
requirement: &
|
45
|
+
requirement: &70156170091500 !ruby/object:Gem::Requirement
|
40
46
|
none: false
|
41
47
|
requirements:
|
42
48
|
- - ~>
|
@@ -44,10 +50,10 @@ dependencies:
|
|
44
50
|
version: '1.0'
|
45
51
|
type: :development
|
46
52
|
prerelease: false
|
47
|
-
version_requirements: *
|
53
|
+
version_requirements: *70156170091500
|
48
54
|
- !ruby/object:Gem::Dependency
|
49
55
|
name: factory_girl
|
50
|
-
requirement: &
|
56
|
+
requirement: &70156170090500 !ruby/object:Gem::Requirement
|
51
57
|
none: false
|
52
58
|
requirements:
|
53
59
|
- - ~>
|
@@ -55,10 +61,10 @@ dependencies:
|
|
55
61
|
version: '2.2'
|
56
62
|
type: :development
|
57
63
|
prerelease: false
|
58
|
-
version_requirements: *
|
64
|
+
version_requirements: *70156170090500
|
59
65
|
- !ruby/object:Gem::Dependency
|
60
66
|
name: rake
|
61
|
-
requirement: &
|
67
|
+
requirement: &70156170089280 !ruby/object:Gem::Requirement
|
62
68
|
none: false
|
63
69
|
requirements:
|
64
70
|
- - ~>
|
@@ -66,10 +72,10 @@ dependencies:
|
|
66
72
|
version: 0.9.0
|
67
73
|
type: :development
|
68
74
|
prerelease: false
|
69
|
-
version_requirements: *
|
75
|
+
version_requirements: *70156170089280
|
70
76
|
- !ruby/object:Gem::Dependency
|
71
77
|
name: rspec
|
72
|
-
requirement: &
|
78
|
+
requirement: &70156170087680 !ruby/object:Gem::Requirement
|
73
79
|
none: false
|
74
80
|
requirements:
|
75
81
|
- - ~>
|
@@ -77,7 +83,18 @@ dependencies:
|
|
77
83
|
version: '2.6'
|
78
84
|
type: :development
|
79
85
|
prerelease: false
|
80
|
-
version_requirements: *
|
86
|
+
version_requirements: *70156170087680
|
87
|
+
- !ruby/object:Gem::Dependency
|
88
|
+
name: tzinfo
|
89
|
+
requirement: &70156170086620 !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ~>
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: 0.3.29
|
95
|
+
type: :development
|
96
|
+
prerelease: false
|
97
|
+
version_requirements: *70156170086620
|
81
98
|
description: Create plain old ruby models without reinventing the wheel.
|
82
99
|
email:
|
83
100
|
- cgriego@gmail.com
|
@@ -98,8 +115,10 @@ files:
|
|
98
115
|
- README.md
|
99
116
|
- Rakefile
|
100
117
|
- active_attr.gemspec
|
118
|
+
- gemfiles/rails_3_1.gemfile
|
101
119
|
- gemfiles/rails_head.gemfile
|
102
120
|
- lib/active_attr.rb
|
121
|
+
- lib/active_attr/attribute_defaults.rb
|
103
122
|
- lib/active_attr/attribute_definition.rb
|
104
123
|
- lib/active_attr/attributes.rb
|
105
124
|
- lib/active_attr/basic_model.rb
|
@@ -116,18 +135,29 @@ files:
|
|
116
135
|
- lib/active_attr/query_attributes.rb
|
117
136
|
- lib/active_attr/railtie.rb
|
118
137
|
- lib/active_attr/rspec.rb
|
119
|
-
- lib/active_attr/
|
138
|
+
- lib/active_attr/typecasted_attributes.rb
|
139
|
+
- lib/active_attr/typecasting.rb
|
140
|
+
- lib/active_attr/typecasting/boolean.rb
|
141
|
+
- lib/active_attr/typecasting/boolean_typecaster.rb
|
142
|
+
- lib/active_attr/typecasting/date_time_typecaster.rb
|
143
|
+
- lib/active_attr/typecasting/date_typecaster.rb
|
144
|
+
- lib/active_attr/typecasting/float_typecaster.rb
|
145
|
+
- lib/active_attr/typecasting/integer_typecaster.rb
|
146
|
+
- lib/active_attr/typecasting/object_typecaster.rb
|
147
|
+
- lib/active_attr/typecasting/string_typecaster.rb
|
120
148
|
- lib/active_attr/unknown_attribute_error.rb
|
121
|
-
- lib/active_attr/unknown_attributes_error.rb
|
122
149
|
- lib/active_attr/version.rb
|
150
|
+
- spec/functional/active_attr/attribute_defaults_spec.rb
|
123
151
|
- spec/functional/active_attr/attributes_spec.rb
|
124
152
|
- spec/functional/active_attr/chainable_initialization_spec.rb
|
125
153
|
- spec/functional/active_attr/model_spec.rb
|
126
154
|
- spec/functional/active_attr/query_attributes_spec.rb
|
155
|
+
- spec/functional/active_attr/typecasted_attributes_spec.rb
|
127
156
|
- spec/spec_helper.rb
|
128
157
|
- spec/support/active_model_lint.rb
|
129
158
|
- spec/support/initialization_verifier.rb
|
130
159
|
- spec/support/mass_assignment_shared_examples.rb
|
160
|
+
- spec/unit/active_attr/attribute_defaults_spec.rb
|
131
161
|
- spec/unit/active_attr/attribute_definition_spec.rb
|
132
162
|
- spec/unit/active_attr/attributes_spec.rb
|
133
163
|
- spec/unit/active_attr/basic_model_spec.rb
|
@@ -139,9 +169,17 @@ files:
|
|
139
169
|
- spec/unit/active_attr/mass_assignment_spec.rb
|
140
170
|
- spec/unit/active_attr/matchers/have_attribute_matcher_spec.rb
|
141
171
|
- spec/unit/active_attr/query_attributes_spec.rb
|
142
|
-
- spec/unit/active_attr/
|
172
|
+
- spec/unit/active_attr/typecasted_attributes_spec.rb
|
173
|
+
- spec/unit/active_attr/typecasting/boolean_spec.rb
|
174
|
+
- spec/unit/active_attr/typecasting/boolean_typecaster_spec.rb
|
175
|
+
- spec/unit/active_attr/typecasting/date_time_typecaster_spec.rb
|
176
|
+
- spec/unit/active_attr/typecasting/date_typecaster_spec.rb
|
177
|
+
- spec/unit/active_attr/typecasting/float_typecaster_spec.rb
|
178
|
+
- spec/unit/active_attr/typecasting/integer_typecaster_spec.rb
|
179
|
+
- spec/unit/active_attr/typecasting/object_typecaster_spec.rb
|
180
|
+
- spec/unit/active_attr/typecasting/string_typecaster_spec.rb
|
181
|
+
- spec/unit/active_attr/typecasting_spec.rb
|
143
182
|
- spec/unit/active_attr/unknown_attribute_error_spec.rb
|
144
|
-
- spec/unit/active_attr/unknown_attributes_error_spec.rb
|
145
183
|
- spec/unit/active_attr/version_spec.rb
|
146
184
|
homepage: https://github.com/cgriego/active_attr
|
147
185
|
licenses: []
|
@@ -157,16 +195,13 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
157
195
|
version: '0'
|
158
196
|
segments:
|
159
197
|
- 0
|
160
|
-
hash:
|
198
|
+
hash: -4291119495453604965
|
161
199
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
200
|
none: false
|
163
201
|
requirements:
|
164
|
-
- - ! '
|
202
|
+
- - ! '>'
|
165
203
|
- !ruby/object:Gem::Version
|
166
|
-
version:
|
167
|
-
segments:
|
168
|
-
- 0
|
169
|
-
hash: 3302118214315982046
|
204
|
+
version: 1.3.1
|
170
205
|
requirements: []
|
171
206
|
rubyforge_project: active_attr
|
172
207
|
rubygems_version: 1.8.10
|
@@ -174,14 +209,17 @@ signing_key:
|
|
174
209
|
specification_version: 3
|
175
210
|
summary: What ActiveModel left out
|
176
211
|
test_files:
|
212
|
+
- spec/functional/active_attr/attribute_defaults_spec.rb
|
177
213
|
- spec/functional/active_attr/attributes_spec.rb
|
178
214
|
- spec/functional/active_attr/chainable_initialization_spec.rb
|
179
215
|
- spec/functional/active_attr/model_spec.rb
|
180
216
|
- spec/functional/active_attr/query_attributes_spec.rb
|
217
|
+
- spec/functional/active_attr/typecasted_attributes_spec.rb
|
181
218
|
- spec/spec_helper.rb
|
182
219
|
- spec/support/active_model_lint.rb
|
183
220
|
- spec/support/initialization_verifier.rb
|
184
221
|
- spec/support/mass_assignment_shared_examples.rb
|
222
|
+
- spec/unit/active_attr/attribute_defaults_spec.rb
|
185
223
|
- spec/unit/active_attr/attribute_definition_spec.rb
|
186
224
|
- spec/unit/active_attr/attributes_spec.rb
|
187
225
|
- spec/unit/active_attr/basic_model_spec.rb
|
@@ -193,8 +231,16 @@ test_files:
|
|
193
231
|
- spec/unit/active_attr/mass_assignment_spec.rb
|
194
232
|
- spec/unit/active_attr/matchers/have_attribute_matcher_spec.rb
|
195
233
|
- spec/unit/active_attr/query_attributes_spec.rb
|
196
|
-
- spec/unit/active_attr/
|
234
|
+
- spec/unit/active_attr/typecasted_attributes_spec.rb
|
235
|
+
- spec/unit/active_attr/typecasting/boolean_spec.rb
|
236
|
+
- spec/unit/active_attr/typecasting/boolean_typecaster_spec.rb
|
237
|
+
- spec/unit/active_attr/typecasting/date_time_typecaster_spec.rb
|
238
|
+
- spec/unit/active_attr/typecasting/date_typecaster_spec.rb
|
239
|
+
- spec/unit/active_attr/typecasting/float_typecaster_spec.rb
|
240
|
+
- spec/unit/active_attr/typecasting/integer_typecaster_spec.rb
|
241
|
+
- spec/unit/active_attr/typecasting/object_typecaster_spec.rb
|
242
|
+
- spec/unit/active_attr/typecasting/string_typecaster_spec.rb
|
243
|
+
- spec/unit/active_attr/typecasting_spec.rb
|
197
244
|
- spec/unit/active_attr/unknown_attribute_error_spec.rb
|
198
|
-
- spec/unit/active_attr/unknown_attributes_error_spec.rb
|
199
245
|
- spec/unit/active_attr/version_spec.rb
|
200
246
|
has_rdoc:
|
@@ -1,44 +0,0 @@
|
|
1
|
-
require "active_attr/mass_assignment"
|
2
|
-
require "active_attr/unknown_attributes_error"
|
3
|
-
require "active_support/concern"
|
4
|
-
|
5
|
-
module ActiveAttr
|
6
|
-
# StrictMassAssignment allows mass assignment of attributes, but raises an
|
7
|
-
# exception when assigning unknown attributes
|
8
|
-
#
|
9
|
-
# Attempting to assign any unknown or private attribute through any of the
|
10
|
-
# mass assignment methods ({#assign_attributes}, {#attributes=}, and
|
11
|
-
# {#initialize}) will raise an {ActiveAttr::UnknownAttributesError}
|
12
|
-
# exception.
|
13
|
-
#
|
14
|
-
# @example Usage
|
15
|
-
# class Person
|
16
|
-
# include ActiveAttr::StrictMassAssignment
|
17
|
-
# end
|
18
|
-
#
|
19
|
-
# @since 0.2.0
|
20
|
-
module StrictMassAssignment
|
21
|
-
extend ActiveSupport::Concern
|
22
|
-
include MassAssignment
|
23
|
-
|
24
|
-
# Mass update a model's attributes, but raise an error if an attempt is
|
25
|
-
# made to assign an unknown attribute
|
26
|
-
#
|
27
|
-
# @param (see MassAssignment#assign_attributes)
|
28
|
-
#
|
29
|
-
# @raise [ActiveAttr::UnknownAttributesError]
|
30
|
-
#
|
31
|
-
# @since 0.2.0
|
32
|
-
def assign_attributes(new_attributes, options={})
|
33
|
-
unknown_attribute_names = (new_attributes || {}).reject do |name, value|
|
34
|
-
respond_to? "#{name}="
|
35
|
-
end.map { |name, value| name.to_s }.sort
|
36
|
-
|
37
|
-
if unknown_attribute_names.any?
|
38
|
-
raise UnknownAttributesError, "unknown attribute(s): #{unknown_attribute_names.join(", ")}"
|
39
|
-
else
|
40
|
-
super
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
require "active_attr/error"
|
2
|
-
|
3
|
-
module ActiveAttr
|
4
|
-
# This exception is raised if attempting to mass assign unknown attributes
|
5
|
-
# when using {StrictMassAssignment}
|
6
|
-
#
|
7
|
-
# @example Rescuing an UnknownAttributesError error
|
8
|
-
# begin
|
9
|
-
# Person.new(attributes)
|
10
|
-
# rescue ActiveAttr::UnknownAttributesError
|
11
|
-
# Person.new
|
12
|
-
# end
|
13
|
-
#
|
14
|
-
# @since 0.2.0
|
15
|
-
class UnknownAttributesError < NoMethodError
|
16
|
-
include Error
|
17
|
-
end
|
18
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require "spec_helper"
|
2
|
-
require "active_attr/strict_mass_assignment"
|
3
|
-
|
4
|
-
module ActiveAttr
|
5
|
-
describe StrictMassAssignment, :mass_assignment do
|
6
|
-
subject do
|
7
|
-
model_class.class_eval do
|
8
|
-
include StrictMassAssignment
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
shared_examples "strict mass assignment method", :strict_mass_assignment_method => true do
|
13
|
-
include_examples "mass assignment method"
|
14
|
-
|
15
|
-
it "raises when assigning an unknown attribute" do
|
16
|
-
expect do
|
17
|
-
mass_assign_attributes(:middle_initial => "J")
|
18
|
-
end.to raise_error UnknownAttributesError, "unknown attribute(s): middle_initial"
|
19
|
-
end
|
20
|
-
|
21
|
-
it "raises when trying to assign a private attribute" do
|
22
|
-
expect do
|
23
|
-
mass_assign_attributes(:middle_name => "J")
|
24
|
-
end.to raise_error UnknownAttributesError, "unknown attribute(s): middle_name"
|
25
|
-
end
|
26
|
-
|
27
|
-
it "raises when assigning multiple unknown attributes with a message referencing both in alphabetical order" do
|
28
|
-
expect do
|
29
|
-
mass_assign_attributes(:middle_name => "J", :middle_initial => "J")
|
30
|
-
end.to raise_error UnknownAttributesError, "unknown attribute(s): middle_initial, middle_name"
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
describe "#assign_attributes", :assign_attributes, :strict_mass_assignment_method
|
35
|
-
describe "#attributes=", :attributes=, :strict_mass_assignment_method
|
36
|
-
describe "#initialize", :initialize, :strict_mass_assignment_method
|
37
|
-
end
|
38
|
-
end
|