passive_model 1.0.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +24 -0
- data/README.md +110 -37
- data/lib/passive_model/base.rb +25 -15
- data/lib/passive_model/version.rb +1 -1
- data/lib/passive_model.rb +4 -1
- metadata +8 -10
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8f8f6dbda24a133cb3d85c64c34c1aeb120babbf12f8efb6273fb191dee677a4
|
|
4
|
+
data.tar.gz: 03a411981c4aefd4aefbc9063490b4513f1befc973e442850bfe0eeabf4e64ea
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 7f240b57d81a5a08ef79774a461dbb42c834d6ef987c22e1516c44fb383b59471d04c58694b3d8d17788aa116801768734c431afb49bbcef46dc7f96bb91f115
|
|
7
|
+
data.tar.gz: 838c1511b7e889cfa2fd1c134109f849deb37c2767de18a6c7243b5c0cc3389dc5d53fde34546e75345e310ba3e2535748a37fdc914d1a52307d5ae4675e1e04
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 2.0.0
|
|
4
|
+
|
|
5
|
+
- Use ActiveModel for assignment through `.new`, `attributes=`, and
|
|
6
|
+
`assign_attributes`. These methods call public setters.
|
|
7
|
+
- Raise `ActiveModel::UnknownAttributeError` by default when a field has no
|
|
8
|
+
public setter. Rails controller parameters must be permitted before
|
|
9
|
+
assignment.
|
|
10
|
+
- See [Upgrading to 2.0](README.md#upgrading-to-20) for migration steps.
|
|
11
|
+
|
|
12
|
+
## 1.1.0 - 2026-06-14
|
|
13
|
+
|
|
14
|
+
- Require `active_model` directly when loading `passive_model`, so the gem boots
|
|
15
|
+
without relying on the host Rails application to preload ActiveModel.
|
|
16
|
+
- Replace the broad `rails` runtime dependency with `activemodel`.
|
|
17
|
+
- Align the gem's Ruby requirement with ActiveModel 7.
|
|
18
|
+
- Change `save!` to raise `PassiveModel::ValidationError`, an
|
|
19
|
+
`ActiveModel::ValidationError` subclass, instead of
|
|
20
|
+
`ActiveRecord::RecordInvalid`.
|
|
21
|
+
- Store `before_save` callbacks per subclass so callbacks do not leak between
|
|
22
|
+
unrelated `PassiveModel::Base` subclasses.
|
|
23
|
+
- Add focused tests for independent boot, validations, `save`, `save!`, and
|
|
24
|
+
callback isolation.
|
data/README.md
CHANGED
|
@@ -1,95 +1,168 @@
|
|
|
1
1
|
# PassiveModel
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
PassiveModel provides lightweight model objects for Rails applications when you
|
|
4
|
+
want ActiveModel-style behavior without a database table.
|
|
4
5
|
|
|
5
|
-
|
|
6
|
+
It is useful for form objects, service inputs, or other plain Ruby objects that
|
|
7
|
+
need validations, naming, translation, conversion, and callback hooks similar to
|
|
8
|
+
Active Record models.
|
|
6
9
|
|
|
7
10
|
## Installation
|
|
8
11
|
|
|
9
12
|
Add this line to your application's Gemfile:
|
|
10
13
|
|
|
11
14
|
```ruby
|
|
12
|
-
gem
|
|
15
|
+
gem "passive_model"
|
|
13
16
|
```
|
|
14
17
|
|
|
15
18
|
And then execute:
|
|
16
19
|
|
|
17
|
-
|
|
20
|
+
```sh
|
|
21
|
+
bundle install
|
|
22
|
+
```
|
|
18
23
|
|
|
19
|
-
Or install it yourself
|
|
24
|
+
Or install it yourself:
|
|
20
25
|
|
|
21
|
-
|
|
26
|
+
```sh
|
|
27
|
+
gem install passive_model
|
|
28
|
+
```
|
|
22
29
|
|
|
23
|
-
|
|
30
|
+
PassiveModel depends on ActiveModel 7.0 or newer. It does not depend on Rails or
|
|
31
|
+
ActiveRecord.
|
|
24
32
|
|
|
25
|
-
|
|
33
|
+
## Usage
|
|
26
34
|
|
|
27
|
-
|
|
35
|
+
Create a class that inherits from `PassiveModel::Base` and define the attributes
|
|
36
|
+
your object exposes.
|
|
28
37
|
|
|
29
|
-
```
|
|
38
|
+
```ruby
|
|
30
39
|
class ContactForm < PassiveModel::Base
|
|
31
40
|
attr_accessor :first_name, :last_name
|
|
32
41
|
|
|
33
42
|
validates :first_name, presence: true
|
|
34
43
|
validates :last_name, presence: true
|
|
35
44
|
validate :first_name_is_not_spam
|
|
36
|
-
|
|
45
|
+
|
|
37
46
|
private
|
|
38
|
-
|
|
47
|
+
|
|
39
48
|
def first_name_is_not_spam
|
|
40
|
-
return
|
|
49
|
+
return unless first_name.to_s.downcase == "test"
|
|
41
50
|
|
|
42
|
-
errors.add(:first_name,
|
|
51
|
+
errors.add(:first_name, "is not valid")
|
|
43
52
|
end
|
|
44
53
|
end
|
|
45
54
|
```
|
|
46
55
|
|
|
47
|
-
|
|
56
|
+
### Creating Objects
|
|
57
|
+
|
|
58
|
+
Pass initial attributes to `.new`:
|
|
59
|
+
|
|
60
|
+
```ruby
|
|
61
|
+
form = ContactForm.new(first_name: "John", last_name: "Doe")
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Assigning Attributes
|
|
65
|
+
|
|
66
|
+
Assign multiple attributes with `attributes=`:
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
form.attributes = { first_name: "Lewis" }
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
You can also call `form.assign_attributes(first_name: "Lewis")`. Both forms and
|
|
73
|
+
`.new` use ActiveModel to call the public setter for each field.
|
|
74
|
+
|
|
75
|
+
By default, a field without a public setter raises
|
|
76
|
+
`ActiveModel::UnknownAttributeError`. If you pass Rails controller parameters,
|
|
77
|
+
permit the fields for that form first.
|
|
78
|
+
|
|
79
|
+
### Validations
|
|
48
80
|
|
|
49
|
-
`
|
|
81
|
+
`PassiveModel::Base` includes ActiveModel validations, so standard Rails
|
|
82
|
+
validators work:
|
|
50
83
|
|
|
51
|
-
|
|
84
|
+
```ruby
|
|
85
|
+
form = ContactForm.new(last_name: "Doe")
|
|
52
86
|
|
|
53
|
-
|
|
87
|
+
form.valid? # => false
|
|
88
|
+
form.errors[:first_name] # => ["can't be blank"]
|
|
89
|
+
```
|
|
54
90
|
|
|
55
|
-
|
|
91
|
+
ActiveModel validation callbacks such as `before_validation` and
|
|
92
|
+
`after_validation` are also available.
|
|
56
93
|
|
|
57
|
-
|
|
94
|
+
### Save
|
|
58
95
|
|
|
59
|
-
`
|
|
96
|
+
`save` validates the object. If the object is invalid, it returns `false`. If the
|
|
97
|
+
object is valid, it runs registered `before_save` callbacks and returns `true`.
|
|
60
98
|
|
|
61
|
-
|
|
99
|
+
```ruby
|
|
100
|
+
invalid_form = ContactForm.new(last_name: "Doe")
|
|
101
|
+
valid_form = ContactForm.new(first_name: "John", last_name: "Doe")
|
|
62
102
|
|
|
63
|
-
|
|
103
|
+
invalid_form.save # => false
|
|
104
|
+
valid_form.save # => true
|
|
105
|
+
```
|
|
64
106
|
|
|
65
|
-
|
|
107
|
+
### Save Bang
|
|
66
108
|
|
|
67
|
-
|
|
109
|
+
`save!` calls `save` and raises `PassiveModel::ValidationError` when the object
|
|
110
|
+
cannot be saved. `PassiveModel::ValidationError` inherits from
|
|
111
|
+
`ActiveModel::ValidationError`; it does not require ActiveRecord.
|
|
68
112
|
|
|
69
|
-
|
|
113
|
+
```ruby
|
|
114
|
+
form = ContactForm.new
|
|
70
115
|
|
|
71
|
-
|
|
116
|
+
form.save! # raises PassiveModel::ValidationError
|
|
117
|
+
```
|
|
72
118
|
|
|
73
|
-
|
|
119
|
+
### before_save Callback
|
|
74
120
|
|
|
75
|
-
|
|
121
|
+
Register a `before_save` callback with the class method:
|
|
76
122
|
|
|
77
|
-
```
|
|
123
|
+
```ruby
|
|
78
124
|
class ContactForm < PassiveModel::Base
|
|
125
|
+
attr_accessor :first_name
|
|
126
|
+
|
|
127
|
+
validates :first_name, presence: true
|
|
128
|
+
|
|
79
129
|
before_save :send_info_to_mailchimp
|
|
80
|
-
|
|
81
|
-
validate :first_name, presence: true
|
|
82
|
-
|
|
130
|
+
|
|
83
131
|
private
|
|
84
|
-
|
|
132
|
+
|
|
85
133
|
def send_info_to_mailchimp
|
|
86
|
-
# API call
|
|
134
|
+
# API call
|
|
87
135
|
end
|
|
88
136
|
end
|
|
89
137
|
```
|
|
90
138
|
|
|
91
|
-
The callback
|
|
139
|
+
The callback is executed only after validations pass.
|
|
140
|
+
|
|
141
|
+
`before_save` callbacks are stored per class, so callbacks registered on one
|
|
142
|
+
`PassiveModel::Base` subclass do not run for unrelated subclasses.
|
|
143
|
+
|
|
144
|
+
### persisted?
|
|
145
|
+
|
|
146
|
+
`persisted?` is intended to report whether `save` has been called successfully.
|
|
147
|
+
In the current implementation it returns `false` unless an instance sets
|
|
148
|
+
`@persisted` itself.
|
|
149
|
+
|
|
150
|
+
## Upgrading to 2.0
|
|
151
|
+
|
|
152
|
+
Review the fields you pass to `.new` and `attributes=` before upgrading.
|
|
153
|
+
|
|
154
|
+
- Give each field you intend to write a public setter. Use `attr_accessor` for
|
|
155
|
+
a simple field or define the setter yourself.
|
|
156
|
+
- Remove extra keys before passing a hash. In Rails controllers, pass only
|
|
157
|
+
parameters permitted for that form.
|
|
158
|
+
- Review any custom setters. PassiveModel now calls them during initialization
|
|
159
|
+
and later assignment.
|
|
160
|
+
|
|
161
|
+
If you are upgrading from a version earlier than 1.1.0, also check any code that
|
|
162
|
+
rescues errors from `save!`. Since 1.1.0, a validation failure raises
|
|
163
|
+
`PassiveModel::ValidationError`.
|
|
92
164
|
|
|
93
165
|
## Contributing
|
|
94
166
|
|
|
95
|
-
|
|
167
|
+
For bug reports or feature requests, open an issue at
|
|
168
|
+
https://github.com/RocketApex/passive_model.
|
data/lib/passive_model/base.rb
CHANGED
|
@@ -8,29 +8,46 @@ module PassiveModel
|
|
|
8
8
|
# Supports before_save (gets run only if valid) - this is a custom implementation
|
|
9
9
|
# Supports passing the attributes .new and .attributes
|
|
10
10
|
class Base
|
|
11
|
+
include ActiveModel::AttributeAssignment
|
|
11
12
|
include ActiveModel::Validations
|
|
12
13
|
include ActiveModel::Validations::Callbacks
|
|
13
14
|
include ActiveModel::Conversion
|
|
14
15
|
extend ActiveModel::Naming
|
|
15
16
|
extend ActiveModel::Translation
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
class << self
|
|
19
|
+
def before_save(name)
|
|
20
|
+
callback = name.to_sym
|
|
21
|
+
|
|
22
|
+
before_save_callbacks << callback unless before_save_callbacks.include?(callback)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def inherited(subclass)
|
|
26
|
+
super
|
|
27
|
+
|
|
28
|
+
subclass.instance_variable_set(:@before_save_callbacks, before_save_callbacks.dup)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def before_save_callbacks
|
|
34
|
+
@before_save_callbacks ||= []
|
|
35
|
+
end
|
|
19
36
|
end
|
|
20
37
|
|
|
21
|
-
def
|
|
22
|
-
|
|
38
|
+
def initialize(hash = {})
|
|
39
|
+
assign_attributes(hash)
|
|
23
40
|
end
|
|
24
41
|
|
|
25
42
|
def save
|
|
26
43
|
return false unless self.valid?
|
|
27
44
|
|
|
28
|
-
execute_callbacks(
|
|
45
|
+
execute_callbacks(before_save_callbacks)
|
|
29
46
|
true
|
|
30
47
|
end
|
|
31
48
|
|
|
32
49
|
def save!
|
|
33
|
-
raise(
|
|
50
|
+
raise(PassiveModel::ValidationError.new(self)) unless self.save
|
|
34
51
|
end
|
|
35
52
|
|
|
36
53
|
def persisted?
|
|
@@ -45,15 +62,8 @@ module PassiveModel
|
|
|
45
62
|
end
|
|
46
63
|
end
|
|
47
64
|
|
|
48
|
-
def
|
|
49
|
-
|
|
50
|
-
@@before_save_callbacks << name unless @@before_save_callbacks.include?(name)
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
def set_attributes(hash)
|
|
54
|
-
hash.keys.each do |key|
|
|
55
|
-
instance_variable_set('@' + key.to_s, hash[key])
|
|
56
|
-
end
|
|
65
|
+
def before_save_callbacks
|
|
66
|
+
self.class.send(:before_save_callbacks)
|
|
57
67
|
end
|
|
58
68
|
end
|
|
59
69
|
end
|
data/lib/passive_model.rb
CHANGED
metadata
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: passive_model
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jey Geethan
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: activemodel
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
16
|
- - ">="
|
|
@@ -32,6 +31,7 @@ executables: []
|
|
|
32
31
|
extensions: []
|
|
33
32
|
extra_rdoc_files: []
|
|
34
33
|
files:
|
|
34
|
+
- CHANGELOG.md
|
|
35
35
|
- MIT-LICENSE
|
|
36
36
|
- README.md
|
|
37
37
|
- Rakefile
|
|
@@ -44,9 +44,8 @@ licenses:
|
|
|
44
44
|
metadata:
|
|
45
45
|
allowed_push_host: https://rubygems.org
|
|
46
46
|
homepage_uri: https://github.com/RocketApex/passive_model
|
|
47
|
-
source_code_uri: https://github.com/RocketApex/passive_model
|
|
48
|
-
changelog_uri: https://github.com/RocketApex/passive_model
|
|
49
|
-
post_install_message:
|
|
47
|
+
source_code_uri: https://github.com/RocketApex/passive_model/tree/main
|
|
48
|
+
changelog_uri: https://github.com/RocketApex/passive_model/blob/main/CHANGELOG.md
|
|
50
49
|
rdoc_options: []
|
|
51
50
|
require_paths:
|
|
52
51
|
- lib
|
|
@@ -54,15 +53,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
54
53
|
requirements:
|
|
55
54
|
- - ">="
|
|
56
55
|
- !ruby/object:Gem::Version
|
|
57
|
-
version: 2.
|
|
56
|
+
version: 2.7.0
|
|
58
57
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
58
|
requirements:
|
|
60
59
|
- - ">="
|
|
61
60
|
- !ruby/object:Gem::Version
|
|
62
61
|
version: '0'
|
|
63
62
|
requirements: []
|
|
64
|
-
rubygems_version: 3.
|
|
65
|
-
signing_key:
|
|
63
|
+
rubygems_version: 3.6.9
|
|
66
64
|
specification_version: 4
|
|
67
65
|
summary: For using models without saving to the database
|
|
68
66
|
test_files: []
|