Module: Countrizable::ActiveRecord::CountryAttributesQuery
- Defined in:
- lib/countrizable/active_record/country_attributes_query.rb
Defined Under Namespace
Classes: WhereChain
Instance Method Summary collapse
- #calculate(*args) ⇒ Object
- #exists?(conditions = :none) ⇒ Boolean
- #group(*columns) ⇒ Object
- #having(opts, *rest) ⇒ Object
- #join_country_values(relation = self) ⇒ Object
- #order(opts, *rest) ⇒ Object
- #parse_country_conditions(opts) ⇒ Object
- #pluck(*column_names) ⇒ Object
- #reorder(opts, *rest) ⇒ Object
- #select(*columns) ⇒ Object
- #where(opts = :chain, *rest) ⇒ Object
- #where_values_hash(*args) ⇒ Object
- #with_country_values_in_fallbacks ⇒ Object
Instance Method Details
#calculate(*args) ⇒ Object
72 73 74 75 76 77 78 79 80 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 72 def calculate(*args) column_name = args[1] if respond_to?(:country_attribute_names) && country_column?(column_name) args[1] = country_column_name(column_name) join_country_values.calculate(*args) else super end end |
#exists?(conditions = :none) ⇒ Boolean
64 65 66 67 68 69 70 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 64 def exists?(conditions = :none) if parsed = parse_country_conditions(conditions) with_country_values_in_fallbacks.exists?(parsed) else super end end |
#group(*columns) ⇒ Object
48 49 50 51 52 53 54 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 48 def group(*columns) if respond_to?(:country_attribute_names) && parsed = parse_countries_columns(columns) join_country_values super(parsed) else super end end |
#having(opts, *rest) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 24 def having(opts, *rest) if parsed = parse_country_conditions(opts) join_country_values(super(parsed, *rest)) else super end end |
#join_country_values(relation = self) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 119 def join_country_values(relation = self) if relation.joins_values.include?(:country_values) relation else relation.with_country_values_in_fallbacks end end |
#order(opts, *rest) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 32 def order(opts, *rest) if respond_to?(:country_attribute_names) && parsed = parse_countries_order(opts) join_country_values super(parsed) else super end end |
#parse_country_conditions(opts) ⇒ Object
94 95 96 97 98 99 100 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 94 def parse_country_conditions(opts) if opts.is_a?(Hash) && respond_to?(:country_attribute_names) && (keys = opts.symbolize_keys.keys & country_attribute_names).present? opts = opts.dup keys.each { |key| opts[country_column_name(key)] = opts.delete(key) || opts.delete(key.to_s) } opts end end |
#pluck(*column_names) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 82 def pluck(*column_names) if respond_to?(:country_values_attribute_names) && parsed = parse_countries_columns(column_names) join_country_values.pluck(*parsed) else super end end |
#reorder(opts, *rest) ⇒ Object
40 41 42 43 44 45 46 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 40 def reorder(opts, *rest) if respond_to?(:country_attribute_names) && parsed = parse_countries_order(opts) join_country_values super(parsed) else super end end |
#select(*columns) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 56 def select(*columns) if respond_to?(:country_attribute_names) && parsed = parse_countries_columns(columns) join_country_values super(parsed) else super end end |
#where(opts = :chain, *rest) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 14 def where(opts = :chain, *rest) if opts == :chain WhereChain.new(spawn) elsif parsed = parse_country_conditions(opts) join_country_values(super(parsed, *rest)) else super end end |
#where_values_hash(*args) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 103 def where_values_hash(*args) return super unless respond_to?(:country_values_table_name) equalities = respond_to?(:with_default_scope) ? with_default_scope.where_values : where_values equalities = equalities.grep(Arel::Nodes::Equality).find_all { |node| node.left.relation.name == country_values_table_name } binds = Hash[bind_values.find_all(&:first).map { |column, v| [column.name, v] }] super.merge(Hash[equalities.map { |where| name = where.left.name [name, binds.fetch(name.to_s) { right = where.right; right.is_a?(Arel::Nodes::Casted) ? right.val : right }] }]) end |
#with_country_values_in_fallbacks ⇒ Object
90 91 92 |
# File 'lib/countrizable/active_record/country_attributes_query.rb', line 90 def with_country_values_in_fallbacks with_country_values(Countrizable.fallbacks) end |