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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 63d70aecd10656dd557d28b44362db1a814c81e47df383045a3d54925239c91b
4
- data.tar.gz: 379998b3f9fab3270f0555a88c2590bc3d6521663b1e04ca0283545a102ee01b
3
+ metadata.gz: 8f8f6dbda24a133cb3d85c64c34c1aeb120babbf12f8efb6273fb191dee677a4
4
+ data.tar.gz: 03a411981c4aefd4aefbc9063490b4513f1befc973e442850bfe0eeabf4e64ea
5
5
  SHA512:
6
- metadata.gz: b8fc4e1502511f42394dd3db15471897579a457d958f0c149e455991e175c0675b3d7d6a7afbbacc20385d59fe2b6968d526e683e68245b07b291c1b0b817bd9
7
- data.tar.gz: 2d741c905a880ab2550d0bb5f74344f15f60dcf0e83d1e7cfbd4ee065f884f9054f6ae0a5eaee4a37e4ccbb5ac4a906028007c975653f9fb684d569d2762dd61
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
- This is a gem for using Rails ActiveModel functionalities without having an actual database table.
3
+ PassiveModel provides lightweight model objects for Rails applications when you
4
+ want ActiveModel-style behavior without a database table.
4
5
 
5
- Supports validation, callbacks etc. And behaves like a model object, so can be replaced with actual model objects.
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 'passive_model'
15
+ gem "passive_model"
13
16
  ```
14
17
 
15
18
  And then execute:
16
19
 
17
- $ bundle install
20
+ ```sh
21
+ bundle install
22
+ ```
18
23
 
19
- Or install it yourself as:
24
+ Or install it yourself:
20
25
 
21
- $ gem install passive_model
26
+ ```sh
27
+ gem install passive_model
28
+ ```
22
29
 
23
- ## Usage
30
+ PassiveModel depends on ActiveModel 7.0 or newer. It does not depend on Rails or
31
+ ActiveRecord.
24
32
 
25
- Create a class from `PassiveModel::Base`.
33
+ ## Usage
26
34
 
27
- For example:
35
+ Create a class that inherits from `PassiveModel::Base` and define the attributes
36
+ your object exposes.
28
37
 
29
- ```erbruby
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 if first_name.downcase != 'test'
49
+ return unless first_name.to_s.downcase == "test"
41
50
 
42
- errors.add(:first_name, 'is not valid')
51
+ errors.add(:first_name, "is not valid")
43
52
  end
44
53
  end
45
54
  ```
46
55
 
47
- #### Create new objects
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
- `form = ContactForm.new(first_name: 'John', last_name: 'Doe')`
81
+ `PassiveModel::Base` includes ActiveModel validations, so standard Rails
82
+ validators work:
50
83
 
51
- #### Set attributes
84
+ ```ruby
85
+ form = ContactForm.new(last_name: "Doe")
52
86
 
53
- `form.attributes = { first_name: 'Lewis' }`
87
+ form.valid? # => false
88
+ form.errors[:first_name] # => ["can't be blank"]
89
+ ```
54
90
 
55
- #### Emulate a save
91
+ ActiveModel validation callbacks such as `before_validation` and
92
+ `after_validation` are also available.
56
93
 
57
- It runs the validations and also calls `before_save` callbacks if the object is valid
94
+ ### Save
58
95
 
59
- `form.save # returns true if valid?`
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
- #### Emulate a save!
99
+ ```ruby
100
+ invalid_form = ContactForm.new(last_name: "Doe")
101
+ valid_form = ContactForm.new(first_name: "John", last_name: "Doe")
62
102
 
63
- Raise an error if cannot be saved
103
+ invalid_form.save # => false
104
+ valid_form.save # => true
105
+ ```
64
106
 
65
- `form.save!`
107
+ ### Save Bang
66
108
 
67
- #### persisted?
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
- Return if save has been called
113
+ ```ruby
114
+ form = ContactForm.new
70
115
 
71
- `form.persisted?`
116
+ form.save! # raises PassiveModel::ValidationError
117
+ ```
72
118
 
73
- #### before_save callback
119
+ ### before_save Callback
74
120
 
75
- Add a callback as follows:
121
+ Register a `before_save` callback with the class method:
76
122
 
77
- ```erbruby
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 will be executed only if validations are clear.
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
- If you need more functionality, for bug reports raise a request at https://github.com/RocketApex/passive_model
167
+ For bug reports or feature requests, open an issue at
168
+ https://github.com/RocketApex/passive_model.
@@ -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
- def initialize(hash = {})
18
- set_attributes(hash)
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 attributes=(hash = {})
22
- set_attributes(hash)
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(@@before_save_callbacks)
45
+ execute_callbacks(before_save_callbacks)
29
46
  true
30
47
  end
31
48
 
32
49
  def save!
33
- raise(ActiveRecord::RecordInvalid) unless self.save
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 self.before_save(name)
49
- @@before_save_callbacks ||= []
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
@@ -1,3 +1,3 @@
1
1
  module PassiveModel
2
- VERSION = "1.0.3"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/passive_model.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require "passive_model/version"
2
- require "passive_model/base"
2
+ require "active_model"
3
3
 
4
4
  module PassiveModel
5
5
  class Error < StandardError; end
6
+ class ValidationError < ActiveModel::ValidationError; end
6
7
  end
8
+
9
+ require "passive_model/base"
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: 1.0.3
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: 2024-11-14 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: rails
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.3.0
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.3.7
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: []