Class: ActiveRecordCompose::Model
- Inherits:
-
Object
- Object
- ActiveRecordCompose::Model
- Includes:
- ActiveModel::Attributes, ActiveModel::Model, ActiveModel::Validations::Callbacks, DelegateAttribute, TransactionSupport
- Defined in:
- lib/active_record_compose/model.rb
Instance Method Summary collapse
- #create(attributes = {}) ⇒ Object deprecated Deprecated.
- #create!(attributes = {}) ⇒ Object deprecated Deprecated.
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
-
#persisted? ⇒ Boolean
Returns true if model is persisted.
-
#save ⇒ Boolean
Save the models that exist in models.
-
#save! ⇒ Object
Save the models that exist in models.
-
#update(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_update` and `after_update` hooks fires.
-
#update!(attributes = {}) ⇒ Object
Behavior is same to ‘#update`, but raises an exception prematurely on failure.
Methods included from TransactionSupport
#id, #restore_transaction_record_state, #trigger_transactional_callbacks?
Methods included from DelegateAttribute
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
64 65 66 |
# File 'lib/active_record_compose/model.rb', line 64 def initialize(attributes = {}) super end |
Instance Method Details
#create(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_create` and `after_create` hooks fires.
class ComposedModel < ActiveRecordCompose::Model
# ...
before_save { puts 'before_save called!' }
before_create { puts 'before_create called!' }
before_update { puts 'before_update called!' }
after_save { puts 'after_save called!' }
after_create { puts 'after_create called!' }
after_update { puts 'after_update called!' }
end
model = ComposedModel.new
model.save
# before_save called!
# after_save called!
model.create
# before_save called!
# before_create called!
# after_create called!
# after_save called!
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/active_record_compose/model.rb', line 147 def create(attributes = {}) if self.class.persisted_flag_callback_control raise '`#create` cannot be called. The context for creation or update is determined by the `#persisted` flag.' end # steep:ignore:start deprecator.warn( 'The behavior with `persisted_flag_callback_control` set to `false` will be removed in 0.9.0. ' \ 'Use `self.persisted_flag_callback_control = true` set to `true`. ' \ '(Alternatively, exclude statements that set `false`) ' \ 'cf. https://github.com/hamajyotan/active_record_compose/blob/v0.8.1/UPGRADE.md ', ) # steep:ignore:end assign_attributes(attributes) return false if invalid? with_transaction_returning_status do with_callbacks(context: :create) { save_models(bang: false) } rescue ActiveRecord::RecordInvalid false end end |
#create!(attributes = {}) ⇒ Object
Behavior is same to ‘#create`, but raises an exception prematurely on failure.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/active_record_compose/model.rb', line 174 def create!(attributes = {}) if self.class.persisted_flag_callback_control raise '`#create` cannot be called. The context for creation or update is determined by the `#persisted` flag.' end # steep:ignore:start deprecator.warn( 'The behavior with `persisted_flag_callback_control` set to `false` will be removed in 0.9.0. ' \ 'Use `self.persisted_flag_callback_control = true` set to `true`. ' \ '(Alternatively, exclude statements that set `false`) ' \ 'cf. https://github.com/hamajyotan/active_record_compose/blob/v0.8.1/UPGRADE.md ', ) # steep:ignore:end assign_attributes(attributes) valid? || raise_validation_error with_transaction_returning_status do with_callbacks(context: :create) { save_models(bang: true) } end || raise_on_save_error end |
#persisted? ⇒ Boolean
Returns true if model is persisted.
By overriding this definition, you can control the callbacks that are triggered when a save is made. For example, returning false will trigger before_create, around_create and after_create, and returning true will trigger before_update, around_update and after_update.
274 |
# File 'lib/active_record_compose/model.rb', line 274 def persisted? = super |
#save ⇒ Boolean
Save the models that exist in models. Returns false if any of the targets fail, true if all succeed.
The save is performed within a single transaction.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/active_record_compose/model.rb', line 74 def save return false if invalid? with_transaction_returning_status do if self.class.persisted_flag_callback_control with_callbacks { save_models(bang: false) } else # steep:ignore:start deprecator.warn( 'The behavior with `persisted_flag_callback_control` set to `false` will be removed in 0.9.0. ' \ 'Use `self.persisted_flag_callback_control = true` set to `true`. ' \ '(Alternatively, exclude statements that set `false`) ' \ 'cf. https://github.com/hamajyotan/active_record_compose/blob/v0.8.1/UPGRADE.md ', ) # steep:ignore:end run_callbacks(:save) { save_models(bang: false) } end rescue ActiveRecord::RecordInvalid false end end |
#save! ⇒ Object
Save the models that exist in models. Unlike #save, an exception is raises on failure.
Saving, like ‘#save`, is performed within a single transaction.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/active_record_compose/model.rb', line 101 def save! valid? || raise_validation_error with_transaction_returning_status do if self.class.persisted_flag_callback_control with_callbacks { save_models(bang: true) } else # steep:ignore:start deprecator.warn( 'The behavior with `persisted_flag_callback_control` set to `false` will be removed in 0.9.0. ' \ 'Use `self.persisted_flag_callback_control = true` set to `true`. ' \ '(Alternatively, exclude statements that set `false`) ' \ 'cf. https://github.com/hamajyotan/active_record_compose/blob/v0.8.1/UPGRADE.md ', ) # steep:ignore:end run_callbacks(:save) { save_models(bang: true) } end end || raise_on_save_error end |
#update(attributes = {}) ⇒ Object
Behavior is same to ‘#save`, but `before_update` and `after_update` hooks fires.
class ComposedModel < ActiveRecordCompose::Model
# ...
before_save { puts 'before_save called!' }
before_create { puts 'before_create called!' }
before_update { puts 'before_update called!' }
after_save { puts 'after_save called!' }
after_create { puts 'after_create called!' }
after_update { puts 'after_update called!' }
end
model = ComposedModel.new
model.save
# before_save called!
# after_save called!
model.update
# before_save called!
# before_update called!
# after_update called!
# after_save called!
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/active_record_compose/model.rb', line 221 def update(attributes = {}) assign_attributes(attributes) return false if invalid? with_transaction_returning_status do if self.class.persisted_flag_callback_control with_callbacks { save_models(bang: false) } else # steep:ignore:start deprecator.warn( 'The behavior with `persisted_flag_callback_control` set to `false` will be removed in 0.9.0. ' \ 'Use `self.persisted_flag_callback_control = true` set to `true`. ' \ '(Alternatively, exclude statements that set `false`) ' \ 'cf. https://github.com/hamajyotan/active_record_compose/blob/v0.8.1/UPGRADE.md ', ) # steep:ignore:end with_callbacks(context: :update) { save_models(bang: false) } end rescue ActiveRecord::RecordInvalid false end end |
#update!(attributes = {}) ⇒ Object
Behavior is same to ‘#update`, but raises an exception prematurely on failure.
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/active_record_compose/model.rb', line 246 def update!(attributes = {}) assign_attributes(attributes) valid? || raise_validation_error with_transaction_returning_status do if self.class.persisted_flag_callback_control with_callbacks { save_models(bang: true) } else # steep:ignore:start deprecator.warn( 'The behavior with `persisted_flag_callback_control` set to `false` will be removed in 0.9.0. ' \ 'Use `self.persisted_flag_callback_control = true` set to `true`. ' \ '(Alternatively, exclude statements that set `false`) ' \ 'cf. https://github.com/hamajyotan/active_record_compose/blob/v0.8.1/UPGRADE.md ', ) # steep:ignore:end with_callbacks(context: :update) { save_models(bang: true) } end end || raise_on_save_error end |