Sha256: 5bd091fe786ee51e97dafddf9b6b3f522ba89efdc5eb2370576430ab2dc3d67e

Contents?: true

Size: 919 Bytes

Versions: 2

Compression:

Stored size: 919 Bytes

Contents

# frozen_string_literal: true

require_relative 'model/updater'
require_relative 'model/proxy'
require_relative 'model/dsl'
require_relative 'model/updates_deprecator'

module Logux
  module Model
    class InsecureUpdateError < StandardError; end

    def self.included(base)
      base.extend(DSL)

      base.before_update :touch_logux_order_for_changes,
                         unless: -> { changes.key?('logux_fields_updated_at') }
    end

    def logux
      Proxy.new(self)
    end

    private

    def touch_logux_order_for_changes
      attributes = changed.each_with_object({}) do |attr, res|
        res[attr] = send(attr)
      end

      updater = Updater.new(model: self, attributes: attributes)
      self.logux_fields_updated_at = updater.updated_attributes

      ActiveSupport::Notifications.instrument(
        Logux::Model::UpdatesDeprecator::EVENT,
        model: self
      )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
logux_rails-0.2.0 lib/logux/model.rb
logux_rails-0.1.0 lib/logux/model.rb