Class: KB::BaseModel
- Inherits:
-
Object
- Object
- KB::BaseModel
- Includes:
- ActiveModel::Attributes, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Serializers::JSON, Inspectionable
- Defined in:
- lib/kb/models/base_model.rb
Direct Known Subclasses
Assessment, Breed, HubspotRelationship, Pet, PetContract, PetParent, Plan, Product, Referral
Instance Attribute Summary collapse
-
#persisted ⇒ Object
Returns the value of attribute persisted.
Class Method Summary collapse
- .define_attribute_methods(*fields) ⇒ Object
- .define_attributes(attributes, cast_type = nil) ⇒ Object
Instance Method Summary collapse
-
#==(other) ⇒ Object
(also: #eql?)
Copy-paste of ActiveRecord equality logic github.com/rails/rails/blob/main/activerecord/lib/active_record/core.rb.
-
#initialize(attributes = {}) {|_self| ... } ⇒ BaseModel
constructor
A new instance of BaseModel.
- #persist! ⇒ Object
- #persisted? ⇒ Boolean
Methods included from Inspectionable
Constructor Details
#initialize(attributes = {}) {|_self| ... } ⇒ BaseModel
Returns a new instance of BaseModel.
35 36 37 38 39 |
# File 'lib/kb/models/base_model.rb', line 35 def initialize(attributes = {}) super @persisted = false yield self if block_given? end |
Instance Attribute Details
#persisted ⇒ Object
Returns the value of attribute persisted.
9 10 11 |
# File 'lib/kb/models/base_model.rb', line 9 def persisted @persisted end |
Class Method Details
.define_attribute_methods(*fields) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/kb/models/base_model.rb', line 17 def define_attribute_methods(*fields) super fields.each do |field| define_method :"#{field}=" do |value| super(value).tap do public_send "#{field}_will_change!" if public_send("#{field}_changed?") end end end end |
.define_attributes(attributes, cast_type = nil) ⇒ Object
28 29 30 31 32 |
# File 'lib/kb/models/base_model.rb', line 28 def define_attributes(attributes, cast_type = nil) attributes.each do |attribute| attribute attribute, cast_type&.to_sym end end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
Copy-paste of ActiveRecord equality logic github.com/rails/rails/blob/main/activerecord/lib/active_record/core.rb
52 53 54 55 56 57 |
# File 'lib/kb/models/base_model.rb', line 52 def ==(other) super || (other.instance_of?(self.class) && !key.nil? && other.key == key) end |
#persist! ⇒ Object
45 46 47 48 |
# File 'lib/kb/models/base_model.rb', line 45 def persist! changes_applied @persisted = true end |
#persisted? ⇒ Boolean
41 42 43 |
# File 'lib/kb/models/base_model.rb', line 41 def persisted? @persisted end |