Class: Sortiri::Generators::OrderByGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/sortiri/generators/order_by_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sortable_columns:, whitelisted_columns:) ⇒ OrderByGenerator

Returns a new instance of OrderByGenerator.



8
9
10
11
12
13
# File 'lib/sortiri/generators/order_by_generator.rb', line 8

def initialize(sortable_columns:, whitelisted_columns:)
  @sortable_columns = sortable_columns # array of Sortiri::ActiveRecord::Column
  @whitelisted_columns = whitelisted_columns # array of Sortiri::Column
  @columns_hash = map_columns_and_active_record_columns
  @foreign_columns = select_unique_foreign_columns
end

Instance Attribute Details

#columns_hashObject (readonly)

Returns the value of attribute columns_hash.



6
7
8
# File 'lib/sortiri/generators/order_by_generator.rb', line 6

def columns_hash
  @columns_hash
end

#foreign_columnsObject (readonly)

Returns the value of attribute foreign_columns.



6
7
8
# File 'lib/sortiri/generators/order_by_generator.rb', line 6

def foreign_columns
  @foreign_columns
end

#sortable_columnsObject (readonly)

Returns the value of attribute sortable_columns.



6
7
8
# File 'lib/sortiri/generators/order_by_generator.rb', line 6

def sortable_columns
  @sortable_columns
end

#whitelisted_columnsObject (readonly)

Returns the value of attribute whitelisted_columns.



6
7
8
# File 'lib/sortiri/generators/order_by_generator.rb', line 6

def whitelisted_columns
  @whitelisted_columns
end

Instance Method Details

#sort(model, reorder: false) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/sortiri/generators/order_by_generator.rb', line 15

def sort(model, reorder: false)
  query = if reorder
            model.reorder(generate_order_by_clauses)
          else
            model.order(generate_order_by_clauses)
          end
  query = query.joins(generate_joins(model)) if needs_join?
  query
end