Class: ActiveSet::ActiveRecordSetInstruction

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

Direct Known Subclasses

Filtering::ActiveRecord::SetInstruction

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



21
22
23
24
25
26
27
28
# File 'lib/active_set/active_record_set_instruction.rb', line 21

def arel_column
  return @arel_column if defined? @arel_column

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

  @arel_column = arel_column
end

#arel_column_nameObject



30
31
32
# File 'lib/active_set/active_record_set_instruction.rb', line 30

def arel_column_name
  arel_table[@attribute_instruction.attribute].name
end

#arel_tableObject



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

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

#attribute_modelObject



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

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

#initial_relationObject



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

def initial_relation
  return @initial_relation if defined? @initial_relation

  @initial_relation = if @attribute_instruction.associations_array.empty?
                        @set
                      else
                        @set.eager_load(@attribute_instruction.associations_hash)
                      end
end