Module: Shared::AlternateValues::ClassMethods

Defined in:
app/models/concerns/shared/alternate_values.rb

Instance Method Summary collapse

Instance Method Details

#with_alternate_value_on(a, b) ⇒ Scope

Use

Source.with_alternate_value_on(:title, 'f
oo')

Parameters:

  • the (:symbol)

    column name/attribute

  • the (String, Integer, etc)

    value to look for

Returns:

  • (Scope)


43
44
45
# File 'app/models/concerns/shared/alternate_values.rb', line 43

def with_alternate_value_on(a, b)
  joins(:alternate_values).where(alternate_values: {alternate_value_object_attribute: a, value: b})
end

#with_any_value_for(attribute, value) ⇒ Scope

Use

Source.with_any_value_for(:title, 'foo')

Parameters:

  • the (:symbol)

    column name/attribute

  • the (String, Integer, etc)

    value to look for

Returns:

  • (Scope)


52
53
54
55
56
57
58
59
60
# File 'app/models/concerns/shared/alternate_values.rb', line 52

def with_any_value_for(attribute, value)
  self_table = self.arel_table
  alternate_value_table = AlternateValue.arel_table

  a = alternate_value_table[:value].eq(value).and(alternate_value_table[:alternate_value_object_attribute].eq(attribute))
  b = self_table[attribute].eq(value)

  self.includes(:alternate_values).where(a.or(b).to_sql).references(:alternate_values)
end