Sha256: ccbdc3a1a72c5a6ac73411218443eef7fd5a1c71ec4ecb9ec60cb772480f58fd

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

module AxleAttributes
  module Provided
    extend ActiveSupport::Concern
    include ActiveModel::AttributeMethods

    included do
      attribute_method_suffix '_provided?'
    end

    module ClassMethods
      def tracking_disabled?
        Thread.current[:disable_provided_tracking]
      end

      def without_provided_tracking
        Thread.current[:disable_provided_tracking] = true
        yield
      ensure
        Thread.current[:disable_provided_tracking] = false
      end
    end

    def provided
      provided_set.to_a
    end

    def provided_set
      @provided_set ||= Set.new
    end

    def provided?(attr)
      provided_set.include? attr
    end

    def save(*)
      if status = super
        provided_set.clear
      end
      status
    end

    # Attempts to <tt>save!</tt> the record and clears changed attributes if successful.
    def save!(*)
      super.tap do
        provided_set.clear
      end
    end

    # <tt>reload</tt> the record and clears changed attributes.
    def reload(*)
      super.tap do
        provided_set.clear
      end
    end

    private
      def attribute_provided?(attr)
        provided_set.include?(attr)
      end

      def write_attribute(attr, value)
        provided_set << attr.to_s unless self.class.tracking_disabled?

        super(attr, value)
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
axle_attributes-1.13.2 lib/axle_attributes/provided.rb