Class: ActiveSet::ActiveRecordSetInstruction

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/active_set/active_record_set_instruction.rb

Instance Method Summary collapse

Constructor Details

#initialize(attribute_instruction, set) ⇒ ActiveRecordSetInstruction

Returns a new instance of ActiveRecordSetInstruction.



5
6
7
8
9
# File 'lib/active_set/active_record_set_instruction.rb', line 5

def initialize(attribute_instruction, set)
  @attribute_instruction = attribute_instruction
  @set = set
  super(@attribute_instruction)
end

Instance Method Details

#arel_columnObject

rubocop:disable Lint/UnderscorePrefixedVariableName



35
36
37
38
39
40
# File 'lib/active_set/active_record_set_instruction.rb', line 35

def arel_column
  _arel_column = arel_table[@attribute_instruction.attribute]
  return _arel_column.lower if case_insensitive_operation?

  _arel_column
end

#arel_operatorObject

rubocop:enable Lint/UnderscorePrefixedVariableName



43
44
45
# File 'lib/active_set/active_record_set_instruction.rb', line 43

def arel_operator
  @attribute_instruction.operator(default: :eq)
end

#arel_tableObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/active_set/active_record_set_instruction.rb', line 23

def arel_table
  # This is to work around an bug in ActiveRecord,
  # where BINARY fields aren't found properly when using
  # the `arel_table` class method to build an ARel::Node
  if arel_type == :binary
    Arel::Table.new(attribute_model.table_name)
  else
    attribute_model.arel_table
  end
end

#arel_typeObject



17
18
19
20
21
# File 'lib/active_set/active_record_set_instruction.rb', line 17

def arel_type
  attribute_model
    .columns_hash[@attribute_instruction.attribute]
    .type
end

#arel_valueObject

rubocop:disable Lint/UnderscorePrefixedVariableName



48
49
50
51
52
53
# File 'lib/active_set/active_record_set_instruction.rb', line 48

def arel_value
  _arel_value = @attribute_instruction.value
  return _arel_value.downcase if case_insensitive_operation?

  _arel_value
end

#attribute_modelObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/active_set/active_record_set_instruction.rb', line 60

def attribute_model
  return @set.klass if @attribute_instruction.associations_array.empty?
  return @attribute_model if defined? @attribute_model

  @attribute_model = @attribute_instruction
                     .associations_array
                     .reduce(@set) do |obj, assoc|
    obj.reflections[assoc.to_s]&.klass
  end
end

#case_insensitive_operation?Boolean

rubocop:enable Lint/UnderscorePrefixedVariableName

Returns:

  • (Boolean)


56
57
58
# File 'lib/active_set/active_record_set_instruction.rb', line 56

def case_insensitive_operation?
  @attribute_instruction.case_insensitive? && arel_type.presence_in(%i[string text])
end

#initial_relationObject



11
12
13
14
15
# File 'lib/active_set/active_record_set_instruction.rb', line 11

def initial_relation
  return @set if @attribute_instruction.associations_array.empty?

  @set.eager_load(@attribute_instruction.associations_hash)
end