Sha256: 300301ac99df0c8609c49211f6479a1c81d75e63e5742a3cd510667e75579c9f

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

module RocketJobMissionControl
  class Query
    attr_reader :scope
    attr_accessor :search_term, :order_by, :start, :page_size,
                  :search_columns, :display_columns

    def initialize(scope, order_by = nil)
      @scope           = scope
      @order_by        = order_by
      @search_columns  = []
      @display_columns = []
    end

    # Returns the filtered query expression with the sort applied
    def query
      # Sort must be applied last
      order_by ? unsorted_query.sort(order_by) : unsorted_query
    end

    # Count after applying search_term.
    # Pagination settings do not affect this count.
    def count
      unsorted_query.count
    end

    # Count before applying search term
    # Pagination settings do not affect this count.
    def unfiltered_count
      scope.count
    end

    private

    # Returns the filtered query expression
    def unsorted_query
      records = scope
      # Text Search
      if search_term
        escaped = Regexp.escape(search_term)
        regexp  = Regexp.new(escaped, Regexp::IGNORECASE)
        if search_columns.size == 1
          records = records.where(search_columns.first => regexp)
        else
          cols    = search_columns.collect { |col| {col => regexp} }
          records = records.where('$or' => cols)
        end
      end

      # Pagination
      if start && page_size
        records = records.skip(start).limit(page_size)
      end
      records
    end

  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rocketjob_mission_control-4.3.0 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-4.2.1 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-4.2.0 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-4.1.0 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-4.0.0 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-3.2.0 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-3.1.0 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-3.0.3 app/models/rocket_job_mission_control/query.rb
rocketjob_mission_control-3.0.2 app/models/rocket_job_mission_control/query.rb