Sha256: 3376c02964199c64a94c9848d7a0380d4ea4c5efe019eb371430f7c59435de15

Contents?: true

Size: 1.23 KB

Versions: 35

Compression:

Stored size: 1.23 KB

Contents

module ActiveGraph
  # This mixin allows storage and update of undeclared properties in the included class
  module UndeclaredProperties
    extend ActiveSupport::Concern

    included do
      attr_accessor :undeclared_properties
    end

    def validate_attributes!(_)
    end

    def read_attribute(name)
      respond_to?(name) ? super(name) : read_undeclared_property(name.to_sym)
    end
    alias [] read_attribute

    def read_undeclared_property(name)
      _persisted_obj ? _persisted_obj.properties[name] : (undeclared_properties && undeclared_properties[name])
    end

    def write_attribute(name, value)
      if respond_to? "#{name}="
        super(name, value)
      else
        add_undeclared_property(name, value)
      end
    end
    alias []= write_attribute

    def skip_update?
      super && undeclared_properties.blank?
    end

    def props_for_create
      super.merge(undeclared_properties!)
    end

    def props_for_update
      super.merge(undeclared_properties!)
    end

    def undeclared_properties!
      undeclared_properties || {}
    ensure
      self.undeclared_properties = nil
    end

    def add_undeclared_property(name, value)
      (self.undeclared_properties ||= {})[name] = value
    end
  end
end

Version data entries

35 entries across 35 versions & 1 rubygems

Version Path
activegraph-12.0.0.beta.5 lib/active_graph/undeclared_properties.rb
activegraph-12.0.0.beta.4 lib/active_graph/undeclared_properties.rb
activegraph-11.5.0.beta.3 lib/active_graph/undeclared_properties.rb
activegraph-12.0.0.beta.3 lib/active_graph/undeclared_properties.rb
activegraph-12.0.0.beta.2 lib/active_graph/undeclared_properties.rb
activegraph-11.5.0.beta.2 lib/active_graph/undeclared_properties.rb
activegraph-12.0.0.beta.1 lib/active_graph/undeclared_properties.rb
activegraph-11.5.0.beta.1 lib/active_graph/undeclared_properties.rb
activegraph-11.5.0.alpha.1 lib/active_graph/undeclared_properties.rb
activegraph-11.4.0 lib/active_graph/undeclared_properties.rb
activegraph-11.3.1 lib/active_graph/undeclared_properties.rb
activegraph-11.3.0 lib/active_graph/undeclared_properties.rb
activegraph-11.2.0 lib/active_graph/undeclared_properties.rb
activegraph-11.1.0 lib/active_graph/undeclared_properties.rb
activegraph-11.1.0.beta.1 lib/active_graph/undeclared_properties.rb
activegraph-11.1.0.alpha.4 lib/active_graph/undeclared_properties.rb
activegraph-11.1.0.alpha.3 lib/active_graph/undeclared_properties.rb
activegraph-11.1.0.alpha.2 lib/active_graph/undeclared_properties.rb
activegraph-11.1.0.alpha.1 lib/active_graph/undeclared_properties.rb
activegraph-10.2.0.beta.1 lib/active_graph/undeclared_properties.rb