Class: ActiveSet::Sorting::ActiveRecordStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_set/sorting/active_record_strategy.rb

Instance Method Summary collapse

Constructor Details

#initialize(set, attribute_instructions) ⇒ ActiveRecordStrategy

Returns a new instance of ActiveRecordStrategy.



6
7
8
9
# File 'lib/active_set/sorting/active_record_strategy.rb', line 6

def initialize(set, attribute_instructions)
  @set = set
  @attribute_instructions = attribute_instructions
end

Instance Method Details

#executable_instructionsObject



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_set/sorting/active_record_strategy.rb', line 24

def executable_instructions
  return {} unless @set.respond_to? :to_sql

  @attribute_instructions.select do |attribute_instruction|
    attribute_model = attribute_model_for(attribute_instruction)
    next false unless attribute_model
    next false unless attribute_model.respond_to?(:attribute_names)
    next false unless attribute_model.attribute_names.include?(attribute_instruction.attribute)

    true
  end
end

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/active_set/sorting/active_record_strategy.rb', line 11

def execute
  return false unless @set.respond_to? :to_sql

  executable_instructions.reduce(set_with_eager_loaded_associations) do |set, attribute_instruction|
    statement = set.merge(order_operation_for(attribute_instruction))

    return false if throws?(ActiveRecord::StatementInvalid) { statement.load }

    attribute_instruction.processed = true
    statement
  end
end