active_attr 0.12.0 → 0.15.1
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.
- checksums.yaml +5 -5
- data/.github/workflows/test.yaml +160 -0
- data/.gitignore +1 -0
- data/CHANGELOG.md +33 -0
- data/Gemfile +5 -8
- data/README.md +142 -117
- data/active_attr.gemspec +13 -6
- data/gemfiles/rails_3_0.gemfile +0 -4
- data/gemfiles/rails_3_1.gemfile +1 -8
- data/gemfiles/rails_3_2.gemfile +1 -12
- data/gemfiles/rails_4_0.gemfile +1 -1
- data/gemfiles/rails_4_1.gemfile +1 -1
- data/gemfiles/rails_4_2.gemfile +1 -1
- data/gemfiles/rails_5_0.gemfile +1 -1
- data/gemfiles/rails_5_1.gemfile +1 -1
- data/gemfiles/rails_5_2.gemfile +9 -0
- data/gemfiles/rails_6_0.gemfile +9 -0
- data/gemfiles/rails_head.gemfile +1 -1
- data/lib/active_attr.rb +1 -1
- data/lib/active_attr/attributes.rb +63 -9
- data/lib/active_attr/chainable_initialization.rb +2 -6
- data/lib/active_attr/logger.rb +1 -1
- data/lib/active_attr/mass_assignment.rb +2 -2
- data/lib/active_attr/matchers/have_attribute_matcher.rb +1 -1
- data/lib/active_attr/model.rb +1 -0
- data/lib/active_attr/query_attributes.rb +1 -1
- data/lib/active_attr/railtie.rb +5 -0
- data/lib/active_attr/typecasting/big_decimal_typecaster.rb +5 -2
- data/lib/active_attr/typecasting/boolean_typecaster.rb +8 -0
- data/lib/active_attr/typecasting/float_typecaster.rb +3 -1
- data/lib/active_attr/typecasting/integer_typecaster.rb +3 -1
- data/lib/active_attr/version.rb +1 -1
- data/spec/functional/active_attr/attributes_spec.rb +8 -40
- data/spec/functional/active_attr/mass_assignment_spec.rb +2 -2
- data/spec/functional/active_attr/matchers/have_attribute_matcher_spec.rb +48 -0
- data/spec/functional/active_attr/model_spec.rb +47 -0
- data/spec/functional/active_attr/typecasted_attributes_spec.rb +1 -1
- data/spec/unit/active_attr/attribute_defaults_spec.rb +8 -0
- data/spec/unit/active_attr/attributes_spec.rb +91 -0
- data/spec/unit/active_attr/query_attributes_spec.rb +4 -4
- data/spec/unit/active_attr/typecasting/big_decimal_typecaster_spec.rb +17 -9
- data/spec/unit/active_attr/typecasting/boolean_typecaster_spec.rb +7 -7
- data/spec/unit/active_attr/typecasting/float_typecaster_spec.rb +10 -2
- data/spec/unit/active_attr/typecasting/integer_typecaster_spec.rb +10 -2
- metadata +44 -29
- data/.ruby-version +0 -1
- data/.travis.yml +0 -83
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 643c6f5a8e69bd25697285baa018622a93aa3f6942a89dd2ffe0abc7dc6b6406
|
4
|
+
data.tar.gz: '081be1c5942fff694f85e31fa273a22e85d0e41e179f4321e3164ff6697f97cb'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 561d396771257d70ac4ac7e4d16d47c1c4cb1f59ee3da5d48bb933640253c022968120d1c53fc39cbb6fbf874d8b2531e5488922002711c8942b204d8478b0b4
|
7
|
+
data.tar.gz: ba853cbb6b519b66de4608f2614f0a12a50455e5f6eee23d6c62848d3682e69fb85c33c244a4b15d755f1c3dcda8ecf5334d0b76599c82c1ae31c6584a0bb4a7
|
@@ -0,0 +1,160 @@
|
|
1
|
+
name: Test
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches: [ master ]
|
6
|
+
pull_request:
|
7
|
+
branches: [ master ]
|
8
|
+
|
9
|
+
jobs:
|
10
|
+
test:
|
11
|
+
name: Ruby ${{ matrix.ruby }} with ${{ matrix.gemfile }}
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
strategy:
|
14
|
+
matrix:
|
15
|
+
ruby:
|
16
|
+
- 2.1.9
|
17
|
+
- 2.2.10
|
18
|
+
- 2.3.8
|
19
|
+
- 2.4.10
|
20
|
+
- 2.5.8
|
21
|
+
- 2.6.6
|
22
|
+
- 2.7.2
|
23
|
+
- truffleruby-head
|
24
|
+
gemfile:
|
25
|
+
- gemfiles/rails_3_0.gemfile
|
26
|
+
- gemfiles/rails_3_1.gemfile
|
27
|
+
- gemfiles/rails_3_2.gemfile
|
28
|
+
- gemfiles/rails_4_0.gemfile
|
29
|
+
- gemfiles/rails_4_1.gemfile
|
30
|
+
- gemfiles/rails_4_2.gemfile
|
31
|
+
- gemfiles/rails_5_0.gemfile
|
32
|
+
- gemfiles/rails_5_1.gemfile
|
33
|
+
- gemfiles/rails_5_2.gemfile
|
34
|
+
- gemfiles/rails_6_0.gemfile
|
35
|
+
- Gemfile
|
36
|
+
- gemfiles/rails_head.gemfile
|
37
|
+
exclude:
|
38
|
+
- ruby: 1.9.2
|
39
|
+
gemfile: gemfiles/rails_4_0.gemfile
|
40
|
+
- ruby: 1.9.2
|
41
|
+
gemfile: gemfiles/rails_4_1.gemfile
|
42
|
+
- ruby: 1.9.2
|
43
|
+
gemfile: gemfiles/rails_4_2.gemfile
|
44
|
+
- ruby: 1.9.2
|
45
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
46
|
+
- ruby: 1.9.2
|
47
|
+
gemfile: gemfiles/rails_5_1.gemfile
|
48
|
+
- ruby: 1.9.2
|
49
|
+
gemfile: gemfiles/rails_5_2.gemfile
|
50
|
+
- ruby: 1.9.2
|
51
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
52
|
+
- ruby: 1.9.2
|
53
|
+
gemfile: Gemfile
|
54
|
+
- ruby: 1.9.2
|
55
|
+
gemfile: gemfiles/rails_head.gemfile
|
56
|
+
- ruby: 1.9.3
|
57
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
58
|
+
- ruby: 1.9.3
|
59
|
+
gemfile: gemfiles/rails_5_1.gemfile
|
60
|
+
- ruby: 1.9.3
|
61
|
+
gemfile: gemfiles/rails_5_2.gemfile
|
62
|
+
- ruby: 1.9.3
|
63
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
64
|
+
- ruby: 1.9.3
|
65
|
+
gemfile: Gemfile
|
66
|
+
- ruby: 1.9.3
|
67
|
+
gemfile: gemfiles/rails_head.gemfile
|
68
|
+
- ruby: 2.0.0
|
69
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
70
|
+
- ruby: 2.0.0
|
71
|
+
gemfile: gemfiles/rails_5_1.gemfile
|
72
|
+
- ruby: 2.0.0
|
73
|
+
gemfile: gemfiles/rails_5_2.gemfile
|
74
|
+
- ruby: 2.0.0
|
75
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
76
|
+
- ruby: 2.0.0
|
77
|
+
gemfile: Gemfile
|
78
|
+
- ruby: 2.0.0
|
79
|
+
gemfile: gemfiles/rails_head.gemfile
|
80
|
+
- ruby: 2.1.9
|
81
|
+
gemfile: gemfiles/rails_5_0.gemfile
|
82
|
+
- ruby: 2.1.9
|
83
|
+
gemfile: gemfiles/rails_5_1.gemfile
|
84
|
+
- ruby: 2.1.9
|
85
|
+
gemfile: gemfiles/rails_5_2.gemfile
|
86
|
+
- ruby: 2.1.9
|
87
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
88
|
+
- ruby: 2.1.9
|
89
|
+
gemfile: Gemfile
|
90
|
+
- ruby: 2.1.9
|
91
|
+
gemfile: gemfiles/rails_head.gemfile
|
92
|
+
- ruby: 2.2.10
|
93
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
94
|
+
- ruby: 2.2.10
|
95
|
+
gemfile: Gemfile
|
96
|
+
- ruby: 2.2.10
|
97
|
+
gemfile: gemfiles/rails_head.gemfile
|
98
|
+
- ruby: 2.3.8
|
99
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
100
|
+
- ruby: 2.3.8
|
101
|
+
gemfile: Gemfile
|
102
|
+
- ruby: 2.3.8
|
103
|
+
gemfile: gemfiles/rails_head.gemfile
|
104
|
+
- ruby: 2.4.10
|
105
|
+
gemfile: gemfiles/rails_6_0.gemfile
|
106
|
+
- ruby: 2.4.10
|
107
|
+
gemfile: Gemfile
|
108
|
+
- ruby: 2.4.10
|
109
|
+
gemfile: gemfiles/rails_head.gemfile
|
110
|
+
- ruby: 2.5.8
|
111
|
+
gemfile: gemfiles/rails_3_0.gemfile
|
112
|
+
- ruby: 2.6.6
|
113
|
+
gemfile: gemfiles/rails_3_0.gemfile
|
114
|
+
- ruby: 2.7.2
|
115
|
+
gemfile: gemfiles/rails_3_0.gemfile
|
116
|
+
- ruby: 2.7.2
|
117
|
+
gemfile: gemfiles/rails_3_1.gemfile
|
118
|
+
- ruby: 2.7.2
|
119
|
+
gemfile: gemfiles/rails_3_2.gemfile
|
120
|
+
- ruby: 2.7.2
|
121
|
+
gemfile: gemfiles/rails_4_0.gemfile
|
122
|
+
- ruby: 2.7.2
|
123
|
+
gemfile: gemfiles/rails_4_1.gemfile
|
124
|
+
- ruby: 2.7.2
|
125
|
+
gemfile: gemfiles/rails_4_2.gemfile
|
126
|
+
- ruby: truffleruby-head
|
127
|
+
gemfile: gemfiles/rails_3_0.gemfile
|
128
|
+
- ruby: truffleruby-head
|
129
|
+
gemfile: gemfiles/rails_3_1.gemfile
|
130
|
+
- ruby: truffleruby-head
|
131
|
+
gemfile: gemfiles/rails_3_2.gemfile
|
132
|
+
- ruby: truffleruby-head
|
133
|
+
gemfile: gemfiles/rails_4_0.gemfile
|
134
|
+
- ruby: truffleruby-head
|
135
|
+
gemfile: gemfiles/rails_4_1.gemfile
|
136
|
+
- ruby: truffleruby-head
|
137
|
+
gemfile: gemfiles/rails_4_2.gemfile
|
138
|
+
env:
|
139
|
+
Rails: ${{ matrix.rails }}
|
140
|
+
BUNDLE_GEMFILE: ${{ matrix.gemfile }}
|
141
|
+
BUNDLER_VERSION: 1.17.3
|
142
|
+
|
143
|
+
steps:
|
144
|
+
- name: Checkout code
|
145
|
+
uses: actions/checkout@master
|
146
|
+
- name: Set up Ruby
|
147
|
+
uses: ruby/setup-ruby@v1
|
148
|
+
with:
|
149
|
+
ruby-version: ${{ matrix.ruby }}
|
150
|
+
- name: Set up Bundler cache
|
151
|
+
uses: actions/cache@v1
|
152
|
+
with:
|
153
|
+
path: vendor/bundle
|
154
|
+
key: ${{ runner.os }}-gems
|
155
|
+
- name: Build and run tests
|
156
|
+
run: |
|
157
|
+
gem install bundler --version ${{ env.BUNDLER_VERSION }} --no-document
|
158
|
+
bundle _${{ env.BUNDLER_VERSION }}_ config path vendor/bundle
|
159
|
+
bundle _${{ env.BUNDLER_VERSION }}_ install --jobs 4 --retry 3
|
160
|
+
bundle _${{ env.BUNDLER_VERSION }}_ exec rake --trace
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,36 @@
|
|
1
|
+
# ActiveAttr 0.15.1 (December 11, 2020) #
|
2
|
+
|
3
|
+
* ActiveAttr now supports Ruby 2.7
|
4
|
+
* ActiveAttr now supports Rails 6.1
|
5
|
+
* Drop support for Ruby versions below 2.1
|
6
|
+
|
7
|
+
# ActiveAttr 0.15.0 (June 12, 2019) #
|
8
|
+
|
9
|
+
* Add missing ActiveSupport require for Attributes
|
10
|
+
* Change numeric typecasters to cast nil and empty strings to nil
|
11
|
+
|
12
|
+
# ActiveAttr 0.14.0 (June 10, 2019) #
|
13
|
+
|
14
|
+
* Drop support for Ruby versions below 1.9.2
|
15
|
+
* #139 Changed Typecasting::BooleanTypecaster to cast nil and empty
|
16
|
+
strings to false (Corin Langosch)
|
17
|
+
* Attributes#inspect is now filtered based on filtered_attributes,
|
18
|
+
which defaults to `Rails.application.config.filter_parameters` in
|
19
|
+
Rails apps.
|
20
|
+
* #143 Changed Attributes to allocate less objects (Chris Grigg)
|
21
|
+
* #153 Changed HaveAttributeMatcher#with_default_value_of to support
|
22
|
+
Procs (Yoshiyuki Hirano)
|
23
|
+
* #145 Add ActiveModel::Validations::Callbacks to ActiveAttr::Model
|
24
|
+
(Kazuki BABA)
|
25
|
+
|
26
|
+
# ActiveAttr 0.13.1 (April 25, 2019) #
|
27
|
+
|
28
|
+
* ActiveAttr now supports Rails 6.0
|
29
|
+
|
30
|
+
# ActiveAttr 0.13.0 (April 6, 2019) #
|
31
|
+
|
32
|
+
* ActiveAttr now supports Ruby 2.6
|
33
|
+
|
1
34
|
# ActiveAttr 0.12.0 (August 6, 2018) #
|
2
35
|
|
3
36
|
* #167 Changed Typecasting::BooleanTypecaster to cast strings starting
|
data/Gemfile
CHANGED
@@ -2,15 +2,14 @@ source "https://rubygems.org"
|
|
2
2
|
|
3
3
|
gemspec :development_group => :test
|
4
4
|
|
5
|
-
gem "activemodel", ">= 5.2.0.beta1", "< 6.
|
6
|
-
gem "activesupport", ">= 5.2.0.beta1", "< 6.
|
7
|
-
gem "actionpack", ">= 5.2.0.beta1", "< 6.
|
5
|
+
gem "activemodel", ">= 5.2.0.beta1", "< 6.2"
|
6
|
+
gem "activesupport", ">= 5.2.0.beta1", "< 6.2"
|
7
|
+
gem "actionpack", ">= 5.2.0.beta1", "< 6.2"
|
8
8
|
gem "activemodel-serializers-xml", :group => :test
|
9
9
|
gem "protected_attributes_continued", :group => :test
|
10
10
|
|
11
11
|
group :development do
|
12
|
-
gem "
|
13
|
-
gem "byebug", :platforms => [:mri_20, :mri_21, :mri_22, :mri_23, :mri_24]
|
12
|
+
gem "byebug"
|
14
13
|
gem "growl"
|
15
14
|
gem "guard"
|
16
15
|
gem "guard-bundler"
|
@@ -18,8 +17,6 @@ group :development do
|
|
18
17
|
gem "rb-fsevent"
|
19
18
|
gem "rdiscount"
|
20
19
|
gem "rdoc"
|
21
|
-
gem "
|
22
|
-
gem "spec_coverage", :platforms => [:mri_19, :mri_20, :mri_21, :mri_22, :mri_23, :mri_24], :git => "https://github.com/getaroom/spec_coverage.git"
|
23
|
-
gem "travis"
|
20
|
+
gem "spec_coverage", :git => "https://github.com/getaroom/spec_coverage.git"
|
24
21
|
gem "yard"
|
25
22
|
end
|
data/README.md
CHANGED
@@ -1,15 +1,13 @@
|
|
1
1
|
# ActiveAttr #
|
2
2
|
|
3
|
-
[![Build History][travis badge]][travis]
|
4
3
|
[![Code Climate][codeclimate badge]][codeclimate]
|
5
4
|
|
6
|
-
ActiveAttr is a set of modules that makes it easy to create plain old
|
5
|
+
ActiveAttr is a set of modules that makes it easy to create plain old Ruby
|
7
6
|
models with functionality found in ORMs, like ActiveRecord, without
|
8
7
|
reinventing the wheel. Think of ActiveAttr as the stuff ActiveModel left out.
|
9
8
|
|
10
|
-
ActiveAttr is distributed as a
|
9
|
+
ActiveAttr is distributed as a Ruby gem [on rubygems.org][rubygems].
|
11
10
|
|
12
|
-
[![Models Models Every Where][speakerdeck slide]][speakerdeck]
|
13
11
|
[![ActiveAttr Railscast][railscast poster]][railscast]
|
14
12
|
|
15
13
|
* [Slides][speakerdeck]
|
@@ -26,10 +24,7 @@ ActiveAttr is distributed as a rubygem [on rubygems.org][rubygems].
|
|
26
24
|
[rubygems]: http://rubygems.org/gems/active_attr
|
27
25
|
[protected_attributes]: https://github.com/westonganger/protected_attributes_continued
|
28
26
|
[strong_parameters]: https://github.com/rails/strong_parameters
|
29
|
-
[speakerdeck slide]: https://speakerd.s3.amazonaws.com/presentations/4f31f1dec583b4001f008ec3/thumb_slide_0.jpg
|
30
27
|
[speakerdeck]: https://speakerdeck.com/u/cgriego/p/models-models-every-where
|
31
|
-
[travis badge]: https://secure.travis-ci.org/cgriego/active_attr.svg?branch=master
|
32
|
-
[travis]: http://travis-ci.org/cgriego/active_attr
|
33
28
|
|
34
29
|
## Modules ##
|
35
30
|
|
@@ -38,96 +33,108 @@ ActiveAttr is distributed as a rubygem [on rubygems.org][rubygems].
|
|
38
33
|
Including the Attributes module into your class gives you a DSL for defining
|
39
34
|
the attributes of your model.
|
40
35
|
|
41
|
-
|
42
|
-
|
36
|
+
```ruby
|
37
|
+
class Person
|
38
|
+
include ActiveAttr::Attributes
|
43
39
|
|
44
|
-
|
45
|
-
|
46
|
-
|
40
|
+
attribute :first_name
|
41
|
+
attribute :last_name
|
42
|
+
end
|
47
43
|
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
44
|
+
person = Person.new
|
45
|
+
person.first_name = "Chris"
|
46
|
+
person.last_name = "Griego"
|
47
|
+
person.attributes #=> {"first_name"=>"Chris", "last_name"=>"Griego"}
|
48
|
+
```
|
52
49
|
|
53
50
|
#### AttributeDefaults ####
|
54
51
|
|
55
52
|
Including the AttributeDefaults module into your class builds on Attributes by
|
56
53
|
allowing defaults to be declared with attributes.
|
57
54
|
|
58
|
-
|
59
|
-
|
55
|
+
```ruby
|
56
|
+
class Person
|
57
|
+
include ActiveAttr::AttributeDefaults
|
60
58
|
|
61
|
-
|
62
|
-
|
63
|
-
|
59
|
+
attribute :first_name, :default => "John"
|
60
|
+
attribute :last_name, :default => "Doe"
|
61
|
+
end
|
64
62
|
|
65
|
-
|
66
|
-
|
67
|
-
|
63
|
+
person = Person.new
|
64
|
+
person.first_name #=> "John"
|
65
|
+
person.last_name #=> "Doe"
|
66
|
+
```
|
68
67
|
|
69
68
|
#### QueryAttributes ####
|
70
69
|
|
71
70
|
Including the QueryAttributes module into your class builds on Attributes by
|
72
71
|
providing instance methods for querying your attributes.
|
73
72
|
|
74
|
-
|
75
|
-
|
73
|
+
```ruby
|
74
|
+
class Person
|
75
|
+
include ActiveAttr::QueryAttributes
|
76
76
|
|
77
|
-
|
78
|
-
|
79
|
-
|
77
|
+
attribute :first_name
|
78
|
+
attribute :last_name
|
79
|
+
end
|
80
80
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
81
|
+
person = Person.new
|
82
|
+
person.first_name = "Chris"
|
83
|
+
person.first_name? #=> true
|
84
|
+
person.last_name? #=> false
|
85
|
+
```
|
85
86
|
|
86
87
|
#### TypecastedAttributes ####
|
87
88
|
|
88
89
|
Including the TypecastedAttributes module into your class builds on Attributes
|
89
90
|
by providing type conversion for your attributes.
|
90
91
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
92
|
+
```ruby
|
93
|
+
class Person
|
94
|
+
include ActiveAttr::TypecastedAttributes
|
95
|
+
attribute :age, :type => Integer
|
96
|
+
end
|
95
97
|
|
96
|
-
|
97
|
-
|
98
|
-
|
98
|
+
person = Person.new
|
99
|
+
person.age = "29"
|
100
|
+
person.age #=> 29
|
101
|
+
```
|
99
102
|
|
100
103
|
### BasicModel ###
|
101
104
|
|
102
105
|
Including the BasicModel module into your class gives you the bare minimum
|
103
106
|
required for your model to meet the ActiveModel API requirements.
|
104
107
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
+
```ruby
|
109
|
+
class Person
|
110
|
+
include ActiveAttr::BasicModel
|
111
|
+
end
|
108
112
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
+
Person.model_name.plural #=> "people"
|
114
|
+
person = Person.new
|
115
|
+
person.valid? #=> true
|
116
|
+
person.errors.full_messages #=> []
|
117
|
+
```
|
113
118
|
|
114
119
|
### BlockInitialization ###
|
115
120
|
|
116
121
|
Including the BlockInitialization module into your class will yield the model
|
117
122
|
instance to a block passed to when creating a new instance.
|
118
123
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
124
|
+
```ruby
|
125
|
+
class Person
|
126
|
+
include ActiveAttr::BlockInitialization
|
127
|
+
attr_accessor :first_name, :last_name
|
128
|
+
end
|
123
129
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
130
|
+
person = Person.new do |p|
|
131
|
+
p.first_name = "Chris"
|
132
|
+
p.last_name = "Griego"
|
133
|
+
end
|
128
134
|
|
129
|
-
|
130
|
-
|
135
|
+
person.first_name #=> "Chris"
|
136
|
+
person.last_name #=> "Griego"
|
137
|
+
```
|
131
138
|
|
132
139
|
### Logger ###
|
133
140
|
|
@@ -137,18 +144,20 @@ be configured on an instance, subclass, class, parent class, and globally by
|
|
137
144
|
setting ActiveAttr::Logger.logger. When using Rails, the Rails framework
|
138
145
|
logger will be configured by default.
|
139
146
|
|
140
|
-
|
141
|
-
|
142
|
-
|
147
|
+
```ruby
|
148
|
+
class Person
|
149
|
+
include ActiveAttr::Logger
|
150
|
+
end
|
143
151
|
|
144
|
-
|
145
|
-
|
146
|
-
|
152
|
+
Person.logger = Logger.new(STDOUT)
|
153
|
+
Person.logger? #=> true
|
154
|
+
Person.logger.info "Logging an informational message"
|
147
155
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
156
|
+
person = Person.new
|
157
|
+
person.logger? #=> true
|
158
|
+
person.logger = Logger.new(STDERR)
|
159
|
+
person.logger.warn "Logging a warning message"
|
160
|
+
```
|
152
161
|
|
153
162
|
### MassAssignment ###
|
154
163
|
|
@@ -156,66 +165,76 @@ Including the MassAssignment module into your class gives you methods for bulk
|
|
156
165
|
initializing and updating the attributes of your model. Any unknown attributes
|
157
166
|
are silently ignored.
|
158
167
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
168
|
+
```ruby
|
169
|
+
class Person
|
170
|
+
include ActiveAttr::MassAssignment
|
171
|
+
attr_accessor :first_name, :last_name, :age
|
172
|
+
end
|
163
173
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
174
|
+
person = Person.new(:first_name => "Christopher", :last_name => "Griego")
|
175
|
+
person.attributes = { :first_name => "Chris", :age => 21 }
|
176
|
+
person.first_name #=> "Chris"
|
177
|
+
person.last_name #=> "Griego"
|
178
|
+
```
|
168
179
|
|
169
180
|
MassAssignment supports mass assignment security/sanitization if a sanitizer
|
170
181
|
is included in the model. If using Rails 4.0, include ActiveModel's forbidden
|
171
182
|
attributes protection module to get support for strong parameters.
|
172
183
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
184
|
+
```ruby
|
185
|
+
class Person
|
186
|
+
include ActiveAttr::MassAssignment
|
187
|
+
include ActiveModel::ForbiddenAttributesProtection
|
188
|
+
attr_accessor :first_name, :last_name
|
189
|
+
end
|
190
|
+
|
191
|
+
person = Person.new(ActionController::Parameters.new({
|
192
|
+
:first_name => "Chris",
|
193
|
+
:last_name => "Griego",
|
194
|
+
}).permit(:first_name))
|
195
|
+
person.first_name #=> "Chris"
|
196
|
+
person.last_name #=> nil
|
197
|
+
```
|
185
198
|
|
186
199
|
If using Rails 3.x or the [Protected Attributes gem][protected_attributes],
|
187
200
|
include ActiveModel's mass assignment security module to get support for
|
188
201
|
protected attributes, including support for mass assignment roles.
|
189
202
|
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
203
|
+
```ruby
|
204
|
+
class Person
|
205
|
+
include ActiveAttr::MassAssignment
|
206
|
+
include ActiveModel::MassAssignmentSecurity
|
207
|
+
attr_accessor :first_name, :last_name
|
208
|
+
attr_protected :last_name
|
209
|
+
end
|
196
210
|
|
197
|
-
|
198
|
-
|
199
|
-
|
211
|
+
person = Person.new(:first_name => "Chris", :last_name => "Griego")
|
212
|
+
person.first_name #=> "Chris"
|
213
|
+
person.last_name #=> nil
|
214
|
+
```
|
200
215
|
|
201
216
|
If using the [Strong Parameters gem][strong_parameters] with Rails 3.2,
|
202
217
|
include the forbidden attributes protection module after including
|
203
218
|
the mass assignment security module.
|
204
219
|
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
220
|
+
```ruby
|
221
|
+
class Person
|
222
|
+
include ActiveAttr::MassAssignment
|
223
|
+
include ActiveModel::MassAssignmentSecurity
|
224
|
+
include ActiveModel::ForbiddenAttributesProtection
|
225
|
+
end
|
226
|
+
```
|
210
227
|
|
211
228
|
### Serialization ###
|
212
229
|
|
213
230
|
The Serialization module is a shortcut for incorporating ActiveModel's
|
214
231
|
serialization functionality into your model with one include.
|
215
232
|
|
216
|
-
|
217
|
-
|
218
|
-
|
233
|
+
```ruby
|
234
|
+
class Person
|
235
|
+
include ActiveAttr::Serialization
|
236
|
+
end
|
237
|
+
```
|
219
238
|
|
220
239
|
### Model ###
|
221
240
|
|
@@ -223,9 +242,11 @@ The Model module is a shortcut for incorporating the most common model
|
|
223
242
|
functionality into your model with one include. All of the above modules
|
224
243
|
are included when you include Model.
|
225
244
|
|
226
|
-
|
227
|
-
|
228
|
-
|
245
|
+
```ruby
|
246
|
+
class Person
|
247
|
+
include ActiveAttr::Model
|
248
|
+
end
|
249
|
+
```
|
229
250
|
|
230
251
|
## Integrations ##
|
231
252
|
|
@@ -235,19 +256,23 @@ When using ActiveAttr inside a Rails application, ActiveAttr will configure
|
|
235
256
|
your models' default logger to use the Rails logger automatically. Just
|
236
257
|
include ActiveAttr in your Gemfile.
|
237
258
|
|
238
|
-
|
259
|
+
```ruby
|
260
|
+
gem "active_attr"
|
261
|
+
```
|
239
262
|
|
240
263
|
### RSpec ###
|
241
264
|
|
242
265
|
ActiveAttr comes with matchers and RSpec integration to assist you in testing
|
243
266
|
your models. The matchers also work with compatible frameworks like Shoulda.
|
244
267
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
268
|
+
```ruby
|
269
|
+
require "active_attr/rspec"
|
270
|
+
|
271
|
+
describe Person do
|
272
|
+
it do
|
273
|
+
should have_attribute(:first_name).
|
274
|
+
of_type(String).
|
275
|
+
with_default_value_of("John")
|
276
|
+
end
|
277
|
+
end
|
278
|
+
```
|