Class: Babik::Selection::LocalSelection

Inherits:
Path::LocalPath show all
Defined in:
lib/babik/queryset/lib/selection/local_selection.rb

Overview

Selection by a local field

Constant Summary

Constants inherited from Path::LocalPath

Path::LocalPath::OPERATOR_SEPARATOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Path::LocalPath

#left_joins_by_alias, #target_alias

Constructor Details

#initialize(model, selection_path, value) ⇒ LocalSelection

Construct a local field selector

Parameters:

  • model (ActiveRecord::Base)

    model whose field will be used.

  • selection_path (String)

    selection path. Of the form <field>__<operator>. e.g. first_name__equal, stars__gt If no operator is given (first_name), ‘equal’ will be used.

  • value (String, Integer, Float, ActiveRecord::Base, Babik::QuerySet::Base)

    anything that can be used to select objects.



20
21
22
23
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 20

def initialize(model, selection_path, value)
  super(model, selection_path)
  @value = value
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



12
13
14
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 12

def model
  @model
end

#operatorObject (readonly)

Returns the value of attribute operator.



12
13
14
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 12

def operator
  @operator
end

#secondary_operatorObject (readonly)

Returns the value of attribute secondary_operator.



12
13
14
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 12

def secondary_operator
  @secondary_operator
end

#selected_fieldObject (readonly)

Returns the value of attribute selected_field.



12
13
14
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 12

def selected_field
  @selected_field
end

#selection_pathObject (readonly)

Returns the value of attribute selection_path.



12
13
14
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 12

def selection_path
  @selection_path
end

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 12

def value
  @value
end

Instance Method Details

#sql_where_conditionBabik::Selection::Operation::Base

Return the SQL where condition

Returns:



27
28
29
30
31
32
33
34
35
36
# File 'lib/babik/queryset/lib/selection/local_selection.rb', line 27

def sql_where_condition
  actual_field = Babik::Table::Field.new(model, @selected_field).real_field
  # Return the condition
  operator = if @secondary_operator
               [@operator, @secondary_operator]
             else
               @operator
             end
  Babik::Selection::Operation::Base.factory("#{self.target_alias}.#{actual_field}", operator, @value)
end