Class: Countrizable::ActiveRecord::Adapter
- Inherits:
-
Object
- Object
- Countrizable::ActiveRecord::Adapter
- Includes:
- AdapterDirty
- Defined in:
- lib/countrizable/active_record/adapter.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
-
#stash ⇒ Object
readonly
The cache caches attributes that already were looked up for read access.
Attributes included from AdapterDirty
Instance Method Summary collapse
- #fetch(country_code, name) ⇒ Object
- #fetch_stash(country_code, name) ⇒ Object
-
#initialize(record) ⇒ Adapter
constructor
A new instance of Adapter.
- #reset ⇒ Object
- #save_country_values! ⇒ Object
Methods included from AdapterDirty
#_reset_attribute, #clear_dirty, #store_old_value, #write
Constructor Details
#initialize(record) ⇒ Adapter
Returns a new instance of Adapter.
11 12 13 14 |
# File 'lib/countrizable/active_record/adapter.rb', line 11 def initialize(record) @record = record @stash = Attributes.new end |
Instance Attribute Details
#record ⇒ Object
The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.
6 7 8 |
# File 'lib/countrizable/active_record/adapter.rb', line 6 def record @record end |
#stash ⇒ Object
The cache caches attributes that already were looked up for read access. The stash keeps track of new or changed values that need to be saved.
6 7 8 |
# File 'lib/countrizable/active_record/adapter.rb', line 6 def stash @stash end |
Instance Method Details
#fetch(country_code, name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/countrizable/active_record/adapter.rb', line 23 def fetch(country_code, name) record.countrizable_fallbacks(country_code).each do |fallback| value = stash.contains?(fallback, name) ? fetch_stash(fallback, name) : fetch_attribute(fallback, name) unless fallbacks_for?(value) (value, :country_code => fallback, :requested_country_code => country_code) return value end end return nil end |
#fetch_stash(country_code, name) ⇒ Object
16 17 18 |
# File 'lib/countrizable/active_record/adapter.rb', line 16 def fetch_stash(country_code, name) stash.read(country_code, name) end |
#reset ⇒ Object
54 55 56 |
# File 'lib/countrizable/active_record/adapter.rb', line 54 def reset stash.clear end |
#save_country_values! ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/countrizable/active_record/adapter.rb', line 36 def save_country_values! stash.each do |country_code, attrs| next if attrs.empty? country_value = record.country_values_by_country_code[country_code] || record.country_values.build(country_code: country_code.to_s) attrs.each do |name, value| value = value.val if value.is_a?(Arel::Nodes::Casted) country_value[name] = value end ensure_foreign_key_for(country_value) country_value.save! end reset end |