Class: Babik::Selection::ForeignSelection

Inherits:
Path::ForeignPath show all
Defined in:
lib/babik/queryset/lib/selection/foreign_selection.rb

Overview

Foreign selection

Constant Summary

Constants inherited from Path::ForeignPath

Path::ForeignPath::OPERATOR_SEPARATOR, Path::ForeignPath::RELATIONSHIP_SEPARATOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Path::ForeignPath

#_initialize_associations

Constructor Details

#initialize(model, selection_path, value) ⇒ ForeignSelection

Create a foreign selection, that is, a filter that is based on a foreign field condition.

Parameters:

  • model (ActiveRecord::Base)

    Model

  • selection_path (String, Symbol)

    selection path used only to raise errors. e.g.: posts::category__in author::posts::tags creation_at__date__gte

  • value (String, Integer, ActiveRecord::Base)

    value that will be used in the filter



21
22
23
24
25
26
27
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 21

def initialize(model, selection_path, value)
  super(model, selection_path)
  # If the value is an ActiveRecord model, get its id
  @value = value
  @value = @value.id if @value.is_a?(ActiveRecord::Base)
  _init_sql_where_condition
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



10
11
12
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 10

def model
  @model
end

#operatorObject (readonly)

Returns the value of attribute operator.



10
11
12
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 10

def operator
  @operator
end

#selected_fieldObject (readonly)

Returns the value of attribute selected_field.



10
11
12
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 10

def selected_field
  @selected_field
end

#selection_pathObject (readonly)

Returns the value of attribute selection_path.



10
11
12
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 10

def selection_path
  @selection_path
end

#sql_where_conditionObject (readonly)

Returns the value of attribute sql_where_condition.



10
11
12
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 10

def sql_where_condition
  @sql_where_condition
end

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 10

def value
  @value
end

Instance Method Details

#_init_sql_where_conditionObject

Initialize the SQL condition that will be used on the SQL SELECT



30
31
32
33
34
35
36
# File 'lib/babik/queryset/lib/selection/foreign_selection.rb', line 30

def _init_sql_where_condition
  last_association_model = @association_chain.target_model
  actual_field = Babik::Table::Field.new(last_association_model, @selected_field).real_field
  @sql_where_condition = Babik::Selection::Operation::Base.factory(
    "#{target_alias}.#{actual_field}", @operator, @value
  )
end