Created
May 21, 2018 16:28
-
-
Save TrumpClone/9af270c781d9377cafe5872a6897f660 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
# @see https://github.com/rails/rails/blob/4-2-stable/activerecord/lib/active_record/store.rb | |
module ActiveRecord::Store::ClassMethods | |
def multi_store_accessor(store_attributes, *keys, store_host: nil) | |
store_host = store_attributes.first unless store_host | |
keys = keys.flatten | |
_store_accessors_module.module_eval do | |
keys.each do |key| | |
define_method("#{key}=") do |value| | |
store_attributes.each do |store_attribute| | |
write_store_attribute(store_attribute, key, value) | |
end | |
end | |
define_method(key) do | |
read_store_attribute(store_host, key) | |
end | |
end | |
end | |
self.local_stored_attributes ||= {} | |
store_attributes.each do |store_attribute| | |
self.local_stored_attributes[store_attribute] ||= [] | |
self.local_stored_attributes[store_attribute] |= keys | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment