Module: Shared::AlternateValues::ClassMethods

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

Instance Method Summary (collapse)

Instance Method Details

- (Scope) with_alternate_value_on(a, b)

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)


39
40
41
# File 'app/models/concerns/shared/alternate_values.rb', line 39

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

- (Scope) with_any_value_for(attribute, value)

Use

Source.with_any_value_for(:title, 'foo')

Parameters:

  • the (:symbol)

    column name/attribute

  • the (String, Integer, etc)

    value to look for

Returns:

  • (Scope)


48
49
50
51
52
53
54
55
56
# File 'app/models/concerns/shared/alternate_values.rb', line 48

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