instance method store_accessor

Ruby on Rails 8.1.2

Since v3.2.22.5

Available in: v3.2.22.5 v4.0.13 v4.1.16 v4.2.9 v5.2.8.1 v6.0.6 v6.1.7.10 v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)

No documentation comment.

Parameters

store_attribute req
keys rest
prefix key = nil
suffix key = nil
Source
# File activerecord/lib/active_record/store.rb, line 112
      def store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)
        keys = keys.flatten

        accessor_prefix =
          case prefix
          when String, Symbol
            "#{prefix}_"
          when TrueClass
            "#{store_attribute}_"
          else
            ""
          end
        accessor_suffix =
          case suffix
          when String, Symbol
            "_#{suffix}"
          when TrueClass
            "_#{store_attribute}"
          else
            ""
          end

        _store_accessors_module.module_eval do
          keys.each do |key|
            accessor_key = "#{accessor_prefix}#{key}#{accessor_suffix}"

            define_method("#{accessor_key}=") do |value|
              write_store_attribute(store_attribute, key, value)
            end

            define_method(accessor_key) do
              read_store_attribute(store_attribute, key)
            end

            define_method("#{accessor_key}_changed?") do
              return false unless attribute_changed?(store_attribute)
              prev_store, new_store = changes[store_attribute]
              accessor = store_accessor_for(store_attribute)
              accessor.get(prev_store, key) != accessor.get(new_store, key)
            end

            define_method("#{accessor_key}_change") do
              return unless attribute_changed?(store_attribute)
              prev_store, new_store = changes[store_attribute]
              accessor = store_accessor_for(store_attribute)
              [accessor.get(prev_store, key), accessor.get(new_store, key)]
            end

            define_method("#{accessor_key}_was") do
              return unless attribute_changed?(store_attribute)
              prev_store, _new_store = changes[store_attribute]
              accessor = store_accessor_for(store_attribute)
              accessor.get(prev_store, key)
            end

            define_method("saved_change_to_#{accessor_key}?") do
              return false unless saved_change_to_attribute?(store_attribute)
              prev_store, new_store = saved_changes[store_attribute]
              accessor = store_accessor_for(store_attribute)
              accessor.get(prev_store, key) != accessor.get(new_store, key)
            end

            define_method("saved_change_to_#{accessor_key}") do
              return unless saved_change_to_attribute?(store_attribute)
              prev_store, new_store = saved_changes[store_attribute]
              accessor = store_accessor_for(store_attribute)
              [accessor.get(prev_store, key), accessor.get(new_store, key)]
            end

            define_method("#{accessor_key}_before_last_save") do
              return unless saved_change_to_attribute?(store_attribute)
              prev_store, _new_store = saved_changes[store_attribute]
              accessor = store_accessor_for(store_attribute)
              accessor.get(prev_store, key)
            end
          end
        end

        # assign new store attribute and create new hash to ensure that each class in the hierarchy
        # has its own hash of stored attributes.
        self.local_stored_attributes ||= {}
        self.local_stored_attributes[store_attribute] ||= []
        self.local_stored_attributes[store_attribute] |= keys
      end

Defined in activerecord/lib/active_record/store.rb line 112 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Store::ClassMethods

Type at least 2 characters to search.

↑↓ navigate · open · esc close