ActiveRecord::Store::ClassMethods (original) (raw)
Methods
S
Instance Public methods
store(store_attribute, options = {})Link
def store(store_attribute, options = {}) coder = build_column_serializer(store_attribute, options[:coder], Object, options[:yaml]) serialize store_attribute, coder: IndifferentCoder.new(store_attribute, coder) store_accessor(store_attribute, options[:accessors], **options.slice(:prefix, :suffix)) if options.has_key? :accessors end
store_accessor(store_attribute, *keys, prefix: nil, suffix: nil)Link
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]
prev_store&.dig(key) != new_store&.dig(key)
end
define_method("#{accessor_key}_change") do
return unless attribute_changed?(store_attribute)
prev_store, new_store = changes[store_attribute]
[prev_store&.dig(key), new_store&.dig(key)]
end
define_method("#{accessor_key}_was") do
return unless attribute_changed?(store_attribute)
prev_store, _new_store = changes[store_attribute]
prev_store&.dig(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]
prev_store&.dig(key) != new_store&.dig(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]
[prev_store&.dig(key), new_store&.dig(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]
prev_store&.dig(key)
end
end
end
self.local_stored_attributes ||= {} self.local_stored_attributes[store_attribute] ||= [] self.local_stored_attributes[store_attribute] |= keys end
stored_attributes()Link
def stored_attributes parent = superclass.respond_to?(:stored_attributes) ? superclass.stored_attributes : {} if local_stored_attributes parent.merge!(local_stored_attributes) { |k, a, b| a | b } end parent end