Module: Countrizable::ActiveRecord::ClassMethods

Defined in:
lib/countrizable/active_record/class_methods.rb

Instance Method Summary collapse

Instance Method Details

#columns_hashObject



7
8
9
# File 'lib/countrizable/active_record/class_methods.rb', line 7

def columns_hash
  super.except(*country_attribute_names.map(&:to_s))
end

#country_attributed?(name) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/countrizable/active_record/class_methods.rb', line 37

def country_attributed?(name)
  country_attribute_names.include?(name.to_sym)
end

#country_column_name(name) ⇒ Object



72
73
74
# File 'lib/countrizable/active_record/class_methods.rb', line 72

def country_column_name(name)
  "#{country_value_class.table_name}.#{name}"
end

#country_value_classObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/countrizable/active_record/class_methods.rb', line 51

def country_value_class
  @country_value_class ||= begin
    if self.const_defined?(:CountryValue, false)
      klass = self.const_get(:CountryValue, false)
    else
      klass = self.const_set(:CountryValue, Class.new(Countrizable::ActiveRecord::CountryValue))
    end

    klass.belongs_to :countrizable_model,
      class_name: self.name,
      foreign_key: country_value_options[:foreign_key],
      inverse_of: :country_values,
      touch: country_value_options.fetch(:touch, false)
    klass
  end
end

#country_values_table_nameObject



68
69
70
# File 'lib/countrizable/active_record/class_methods.rb', line 68

def country_values_table_name
  country_value_class.table_name
end

#required_attributesObject



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

def required_attributes
  warn 'required_attributes is deprecated and will be removed in the next release of Countrizable.'
  validators.map { |v| v.attributes if v.is_a?(ActiveModel::Validations::PresenceValidator) }.flatten
end

#required_country_attributesObject



46
47
48
49
# File 'lib/countrizable/active_record/class_methods.rb', line 46

def required_country_attributes
  warn 'required_country_attributes is deprecated and will be removed in the next release of Countrizable.'
  country_attribute_names & required_attributes
end

#with_country_attribute(name, value, country = Countrizable.fallbacks) ⇒ Object



30
31
32
33
34
35
# File 'lib/countrizable/active_record/class_methods.rb', line 30

def with_country_attribute(name, value, country = Countrizable.fallbacks)
  with_country_values.where(
    country_column_name(name)    => value,
    country_column_name(:country_code) => Array(country_codes).map(&:to_s)
  )
end

#with_country_codes(*country_code) ⇒ Object



12
13
14
# File 'lib/countrizable/active_record/class_methods.rb', line 12

def with_country_codes(*country_code)
  all.merge country_value_class.with_country_codes(*country_codes)
end

#with_country_values(*country_codes) ⇒ Object



16
17
18
19
20
21
# File 'lib/countrizable/active_record/class_methods.rb', line 16

def with_country_values(*country_codes)
  country_codes = values_country_codes if country_codes.empty?
  preload(:country_values).joins(:country_values).readonly(false).with_country_codes(country_codes).tap do |query|
    query.distinct! unless country_codes.flatten.one?
  end
end

#with_required_attributesObject



23
24
25
26
27
28
# File 'lib/countrizable/active_record/class_methods.rb', line 23

def with_required_attributes
  warn 'with_required_attributes is deprecated and will be removed in the next release of Countrizable.'
  required_country_attributes.inject(all) do |scope, name|
    scope.where("#{country_column_name(name)} IS NOT NULL")
  end
end