Sha256: bb18d595a3b2e725108863c849ccef8dcdf8d88f969a61b5122acc46d00c08a3

Contents?: true

Size: 1.15 KB

Versions: 7

Compression:

Stored size: 1.15 KB

Contents

module KB
  class BaseModel
    include Inspectionable
    include ActiveModel::Model
    include ActiveModel::Attributes
    include ActiveModel::Serializers::JSON
    include ActiveModel::Dirty

    attr_accessor :persisted

    define_model_callbacks :save
    after_save :persist!

    class << self
      delegate :clear_cache_for, to: :kb_client
    end

    def initialize(attributes = {})
      super
      @persisted = false
      yield self if block_given?
    end

    def persisted?
      @persisted
    end

    def persist!
      changes_applied
      @persisted = true
    end

    # Copy-paste of ActiveRecord equality logic
    # https://github.com/rails/rails/blob/main/activerecord/lib/active_record/core.rb
    def ==(other)
      super ||
        (other.instance_of?(self.class) &&
        !key.nil? &&
        other.key == key)
    end
    alias eql? ==

    def self.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
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
barkibu-kb-0.25.0 lib/kb/models/base_model.rb
barkibu-kb-0.24.1 lib/kb/models/base_model.rb
barkibu-kb-0.24.0 lib/kb/models/base_model.rb
barkibu-kb-0.23.0 lib/kb/models/base_model.rb
barkibu-kb-0.22.0 lib/kb/models/base_model.rb
barkibu-kb-0.21.0 lib/kb/models/base_model.rb
barkibu-kb-0.20.0 lib/kb/models/base_model.rb