Sha256: 004cfdb32a698c2752556703dace839e066ed0dfb95456d6295917f5b7baf2d7

Contents?: true

Size: 1.97 KB

Versions: 51

Compression:

Stored size: 1.97 KB

Contents

module Effective
  # The collection is an Array of Arrays
  class ArrayDatatableTool
    attr_accessor :table_columns

    delegate :order_name, :order_direction, :page, :per_page, :search_column, :display_table_columns, :to => :@datatable

    def initialize(datatable, table_columns)
      @datatable = datatable
      @table_columns = table_columns
    end

    def search_terms
      @search_terms ||= @datatable.search_terms.select { |name, search_term| table_columns.key?(name) }
    end

    def order_column
      @order_column ||= table_columns[order_name]
    end

    def order(collection)
      if order_column.present?
        index = display_index(order_column)

        if order_direction == 'ASC'
          collection.sort! { |x, y| x[index] <=> y[index] }
        else
          collection.sort! { |x, y| y[index] <=> x[index] }
        end
      end

      collection
    end

    def search(collection)
      search_terms.each do |name, search_term|
        column_search = search_column(collection, table_columns[name], search_term)
        raise 'search_column must return an Array object' unless column_search.kind_of?(Array)
        collection = column_search
      end
      collection
    end

    def search_column_with_defaults(collection, table_column, search_term)
      search_term = search_term.downcase
      index = display_index(table_column)

      collection.select! do |row|
        value = row[index].to_s.downcase

        if table_column[:filter][:type] == :select && table_column[:filter][:fuzzy] != true
          value == search_term
        else
          value.include?(search_term)
        end
      end || collection
    end

    def paginate(collection)
      Kaminari.paginate_array(collection).page(page).per(per_page)
    end

    private

    def display_index(column)
      display_table_columns.present? ? display_table_columns.keys.index(column[:name]) : column[:array_index]
    end

  end
end

# [
#   [1, 'title 1'],
#   [2, 'title 2'],
#   [3, 'title 3']
# ]

Version data entries

51 entries across 51 versions & 1 rubygems

Version Path
effective_datatables-2.3.4 app/models/effective/array_datatable_tool.rb
effective_datatables-2.3.3 app/models/effective/array_datatable_tool.rb
effective_datatables-2.3.2 app/models/effective/array_datatable_tool.rb
effective_datatables-2.3.1 app/models/effective/array_datatable_tool.rb
effective_datatables-2.3.0 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.11 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.10 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.9 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.8 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.7 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.6 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.5 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.4 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.3 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.2 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.1 app/models/effective/array_datatable_tool.rb
effective_datatables-2.2.0 app/models/effective/array_datatable_tool.rb
effective_datatables-2.1.20 app/models/effective/array_datatable_tool.rb
effective_datatables-2.1.19 app/models/effective/array_datatable_tool.rb
effective_datatables-2.1.18 app/models/effective/array_datatable_tool.rb