module Countrizable::ActiveRecord::InstanceMethods
Public Instance Methods
[](attr_name)
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 48 def [](attr_name) if country_attributed?(attr_name) read_attribute(attr_name) else read_attribute(attr_name) { |n| missing_attribute(n, caller) } end end
_assign_attributes(new_attributes)
click to toggle source
In Rails 5.2 we need to override _assign_attributes as it's called earlier in the stack (before assign_attributes) See github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L11
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 25 def _assign_attributes(new_attributes) attributes = new_attributes.stringify_keys with_given_country_code(attributes) { super(attributes.except("country_code")) } end
_read_attribute(attr_name, options = {}, &block)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 68 def _read_attribute(attr_name, options = {}, &block) country_value = read_country_attribute(attr_name, options, &block) country_value.nil? ? super(attr_name, &block) : country_value end
assign_attributes(new_attributes, *options)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 32 def assign_attributes(new_attributes, *options) super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present? attributes = new_attributes.stringify_keys with_given_country_code(attributes) { super(attributes.except("country_code"), *options) } end
attribute_names()
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 73 def attribute_names country_attribute_names.map(&:to_s) + super end
attributes()
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 10 def attributes super.merge(country_attributed_attributes) end
attributes=(new_attributes, *options)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 14 def attributes=(new_attributes, *options) super unless new_attributes.respond_to?(:stringify_keys) && new_attributes.present? attributes = new_attributes.stringify_keys with_given_country_code(attributes) { super(attributes.except("country_code"), *options) } end
available_country_codes()
click to toggle source
Get available country_codes from country_value
association, without a separate distinct query
# File lib/countrizable/active_record/instance_methods.rb, line 153 def available_country_codes country_values.map(&:country_code).uniq end
cache_key()
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 180 def cache_key [super, country_value.cache_key].join("/") end
changed?()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 184 def changed? changed_attributes.present? || country_values.any?(&:changed?) end
column_for_attribute(name)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 174 def column_for_attribute name return super if country_attribute_names.exclude?(name) countrizable.send(:column_for_attribute, name) end
countrizable()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 6 def countrizable @countrizable ||= Adapter.new(self) end
countrizable_fallbacks(country_code)
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 157 def countrizable_fallbacks(country_code) Countrizable.fallbacks(country_code) end
country_attribute_by_country_code(name)
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 148 def country_attribute_by_country_code(name) country_values_by_country_code(&:"#{name}") end
country_attributed_attributes()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 79 def country_attributed_attributes country_attribute_names.inject({}) do |attributes, name| attributes.merge(name.to_s => send(name)) end end
country_value()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 123 def country_value country_value_for(::Countrizable.country_code) end
country_value_caches()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 138 def country_value_caches @country_value_caches ||= {} end
country_value_for(country_code, build_if_missing = true)
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 127 def country_value_for(country_code, build_if_missing = true) unless country_value_caches[country_code] # Fetch values from database as those in the country values collection may be incomplete _country_value = country_values.detect{|t| t.country_code.to_s == country_code.to_s} _country_value ||= country_values.with_country_code(country_code).first unless country_values.loaded? _country_value ||= country_values.build(:country_code => country_code) if build_if_missing country_value_caches[country_code] = _country_value if _country_value end country_value_caches[country_code] end
country_values_by_country_code() { |t| ... }
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 142 def country_values_by_country_code country_values.each_with_object(HashWithIndifferentAccess.new) do |t, hash| hash[t.country_code] = block_given? ? yield(t) : t end end
initialize_dup(other)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 114 def initialize_dup(other) @countrizable = nil @country_value_caches = nil super other.each_country_code_and_country_attribute do |country_code, name| countrizable.write(country_code, name, other.countrizable.fetch(country_code, name) ) end end
original_changed_attributes()
click to toggle source
need to access instance variable directly since changed_attributes is frozen as of Rails 4.2
# File lib/countrizable/active_record/instance_methods.rb, line 190 def original_changed_attributes @changed_attributes end
read_attribute(attr_name, options = {}, &block)
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 56 def read_attribute(attr_name, options = {}, &block) name = if self.class.attribute_alias?(attr_name) self.class.attribute_alias(attr_name).to_s else attr_name.to_s end name = self.class.primary_key if name == "id".freeze && self.class.primary_key _read_attribute(name, options, &block) end
reload(options = nil)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 107 def reload(options = nil) country_value_caches.clear country_attribute_names.each { |name| @attributes.reset(name.to_s) } countrizable.reset super(options) end
save(*)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 161 def save(*) result = Countrizable.with_country_code(country_value.country_code || I18n.default_country_code) do without_fallbacks do super end end if result countrizable.clear_dirty end result end
set_country_values(options)
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 93 def set_country_values(options) options.keys.each do |country_code| country_value = country_value_for(country_code) || country_values.build(:country_code => country_code.to_s) options[country_code].each do |key, value| country_value.send :"#{key}=", value country_value.countrizable_model.send :"#{key}=", value end country_value.save if persisted? end countrizable.reset end
uncountry_attributes()
click to toggle source
This method is basically the method built into Rails but we have to pass {:country_attributed => false}
# File lib/countrizable/active_record/instance_methods.rb, line 87 def uncountry_attributes attribute_names.inject({}) do |attrs, name| attrs[name] = read_attribute(name, {:country_attributed => false}); attrs end end
write_attribute(name, value, *args, &block)
click to toggle source
Calls superclass method
# File lib/countrizable/active_record/instance_methods.rb, line 40 def write_attribute(name, value, *args, &block) return super(name, value, *args, &block) unless country_attributed?(name) options = {:country_code => Countrizable.country_code}.merge(args.first || {}) countrizable.write(options[:country_code], name, value) end
Protected Instance Methods
each_country_code_and_country_attribute() { |country_code, name| ... }
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 196 def each_country_code_and_country_attribute used_country_codes.each do |country_code| country_attribute_names.each do |name| yield country_code, name end end end
read_country_attribute(name, options) { |value| ... }
click to toggle source
nil or value
# File lib/countrizable/active_record/instance_methods.rb, line 234 def read_country_attribute(name, options) options = {:country_attributed => true, :country_code => nil}.merge(options) #:translated => true return nil unless options[:country_attributed] #translated return nil unless country_attributed?(name) value = countrizable.fetch(options[:country_code] || Countrizable.country_code, name) return nil if value.nil? block_given? ? yield(value) : value end
save_country_values!()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 210 def save_country_values! countrizable.save_country_values! country_value_caches.clear end
used_country_codes()
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 204 def used_country_codes country_codes = countrizable.stash.keys.concat(countrizable.stash.keys).concat(country_values.valued_country_codes) country_codes.uniq! country_codes end
with_given_country_code(_attributes) { || ... }
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 215 def with_given_country_code(_attributes, &block) attributes = _attributes.stringify_keys if country_code = attributes.try(:delete, "country_code") Countrizable.with_country_code(country_code, &block) else yield end end
without_fallbacks() { || ... }
click to toggle source
# File lib/countrizable/active_record/instance_methods.rb, line 225 def without_fallbacks before = self.fallbacks_for_empty_country_values self.fallbacks_for_empty_country_values = false yield ensure self.fallbacks_for_empty_country_values = before end