Module: RailsOr

Defined in:
lib/rails_or.rb,
lib/rails_or/version.rb

Defined Under Namespace

Classes: WhereBindingMixs

Constant Summary collapse

IS_RAILS3_FLAG =
Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0')
IS_RAILS5_FLAG =
Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('5.0.0')
FROM_VALUE_METHOD =
%i[from_value from_clause].find{|s| ActiveRecord::Relation.method_defined?(s) }
ASSIGN_FROM_VALUE =
:"#{FROM_VALUE_METHOD}="
VERSION =
'1.1.9'

Class Method Summary collapse

Class Method Details

.get_current_scope(relation) ⇒ Object



29
30
31
32
33
34
# File 'lib/rails_or.rb', line 29

def get_current_scope(relation)
  return relation.clone if IS_RAILS3_FLAG
  # ref: https://github.com/rails/rails/blob/17ef58db1776a795c9f9e31a1634db7bcdc3ecdf/activerecord/lib/active_record/scoping/named.rb#L26
  # return relation.all # <- cannot use this because some gem changes this method's behavior
  return (relation.current_scope || relation.default_scoped).clone
end

.parse_parameter(relation, *other) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/rails_or.rb', line 36

def parse_parameter(relation, *other)
  other = other.first if other.size == 1
  case other
  when Hash   ; spawn_relation(relation, :where, other)
  when Array  ; spawn_relation(relation, :where, other)
  when String ; spawn_relation(relation, :where, other)
  else        ; other
  end
end

.spawn_relation(relation, method, condition) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/rails_or.rb', line 19

def spawn_relation(relation, method, condition)
  new_relation = relation.klass.send(method, condition)

  from_value = relation.send(FROM_VALUE_METHOD)
  new_relation.send(ASSIGN_FROM_VALUE, from_value) if from_value.present?

  copy_values(new_relation, relation) if IS_RAILS5_FLAG
  return new_relation
end

.values_to_arel(values) ⇒ Object



14
15
16
17
# File 'lib/rails_or.rb', line 14

def values_to_arel(values)
  values.map!{|x| wrap_arel(x) }
  return (values.size > 1 ? Arel::Nodes::And.new(values) : values)
end