Sha256: fdc8a76703053e4a98df238deba89a6c7e122d41cbe9424dc0daa0f801b53862

Contents?: true

Size: 1.62 KB

Versions: 4

Compression:

Stored size: 1.62 KB

Contents

# frozen_string_literal: true

module AjaxDatatablesRails
  module ORM
    module ActiveRecord

      def fetch_records
        get_raw_records
      end

      def filter_records(records)
        records.where(build_conditions)
      end

      # rubocop:disable Style/EachWithObject
      def sort_records(records)
        sort_by = datatable.orders.inject([]) do |queries, order|
          column = order.column
          queries << order.query(column.sort_query) if column && column.orderable?
          queries
        end
        records.order(Arel.sql(sort_by.join(', ')))
      end
      # rubocop:enable Style/EachWithObject

      def paginate_records(records)
        records.offset(datatable.offset).limit(datatable.per_page)
      end

      # ----------------- SEARCH HELPER METHODS --------------------

      def build_conditions
        if datatable.searchable?
          build_conditions_for_datatable
        else
          build_conditions_for_selected_columns
        end
      end

      def build_conditions_for_datatable
        criteria = search_for.inject([]) do |crit, atom|
          search = Datatable::SimpleSearch.new(value: atom, regex: datatable.search.regexp?)
          crit << searchable_columns.map do |simple_column|
            simple_column.search = search
            simple_column.search_query
          end.reduce(:or)
        end.compact.reduce(:and)
        criteria
      end

      def build_conditions_for_selected_columns
        search_columns.map(&:search_query).compact.reduce(:and)
      end

      def search_for
        datatable.search.value.split(global_search_delimiter)
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ajax-datatables-rails-1.0.0 lib/ajax-datatables-rails/orm/active_record.rb
ajax-datatables-rails-0.4.3 lib/ajax-datatables-rails/orm/active_record.rb
ajax-datatables-rails-0.4.2 lib/ajax-datatables-rails/orm/active_record.rb
ajax-datatables-rails-0.4.1 lib/ajax-datatables-rails/orm/active_record.rb