Module: Countrizable

Defined in:
lib/countrizable/active_record/attributes.rb,
lib/countrizable.rb,
lib/countrizable/i18n.rb,
lib/countrizable/railtie.rb,
lib/countrizable/version.rb,
lib/countrizable/active_record.rb,
lib/countrizable/interpolation.rb,
lib/countrizable/i18n/country_code.rb,
lib/patches/active_record/relation.rb,
lib/patches/active_record/persistence.rb,
lib/countrizable/active_record/adapter.rb,
lib/countrizable/active_record/act_macro.rb,
lib/countrizable/active_record/migration.rb,
lib/countrizable/active_record/exceptions.rb,
lib/countrizable/active_record/adapter_dirty.rb,
lib/countrizable/active_record/class_methods.rb,
lib/countrizable/active_record/country_value.rb,
lib/patches/active_record/rails4/serialization.rb,
lib/countrizable/active_record/instance_methods.rb,
lib/patches/active_record/rails5_1/serialization.rb,
lib/patches/active_record/xml_attribute_serializer.rb,
lib/patches/active_record/rails4/uniqueness_validator.rb,
lib/patches/active_record/rails5/uniqueness_validator.rb,
lib/countrizable/active_record/country_attributes_query.rb,
lib/patches/active_record/rails5_1/uniqueness_validator.rb

Overview

Helper class for storing values per country_codes. Used by Countrizable::Adapter to stash and cache attribute values.

Defined Under Namespace

Modules: ActiveRecord, AttributeMethods, I18n, Interpolation, Persistence, Relation, UniquenessValidatorOverride, Validations, XmlSerializer Classes: Railtie

Constant Summary collapse

VERSION =
'0.1.1'

Class Method Summary collapse

Class Method Details

.country_codeObject



16
17
18
# File 'lib/countrizable.rb', line 16

def country_code
  read_country_code || 'es'
end

.country_code=(country_code) ⇒ Object



20
21
22
# File 'lib/countrizable.rb', line 20

def country_code=(country_code)
  set_country_code(country_code)
end

.default_fallbacks(for_country_code = self.country_code) ⇒ Object



49
50
51
# File 'lib/countrizable.rb', line 49

def default_fallbacks(for_country_code = self.country_code)
  [for_country_code.to_sym]
end

.fallbacks(for_country_code = self.country_code) ⇒ Object



45
46
47
# File 'lib/countrizable.rb', line 45

def fallbacks(for_country_code = self.country_code)
  read_fallbacks[for_country_code] || default_fallbacks(for_country_code)
end

.fallbacks=(country_codes) ⇒ Object



41
42
43
# File 'lib/countrizable.rb', line 41

def fallbacks=(country_codes)
  set_fallbacks(country_codes)
end

.rails_52?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/countrizable.rb', line 62

def rails_52?
  ::ActiveRecord.version >= Gem::Version.new('5.2.0')
end

.rails_5?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/countrizable.rb', line 58

def rails_5?
  ::ActiveRecord.version >= Gem::Version.new('5.1.0')
end

.storageObject

Thread-safe global storage



54
55
56
# File 'lib/countrizable.rb', line 54

def storage
  RequestStore.store
end

.with_country_code(country_code, &block) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/countrizable.rb', line 24

def with_country_code(country_code, &block)
  previous_country_code = read_country_code
  begin
    set_country_code(country_code)
    result = yield(country_code)
  ensure
    set_country_code(previous_country_code)
  end
  result
end

.with_country_codes(*country_codes, &block) ⇒ Object



35
36
37
38
39
# File 'lib/countrizable.rb', line 35

def with_country_codes(*country_codes, &block)
  country_codes.flatten.map do |country_code|
    with_country_code(country_code, &block)
  end
end