Class: ActiveRecord::SearchService

Inherits:
Object
  • Object
show all
Defined in:
app/services/active_record/search_service.rb

Defined Under Namespace

Classes: Filter, Order, Paginate, Search

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, original_params, options = {}) ⇒ SearchService

Returns a new instance of SearchService.



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/services/active_record/search_service.rb', line 97

def initialize(model, original_params, options = {})
  @model = model
  @params = original_params.deep_dup.with_indifferent_access
  @options = options

  _order_str = options.delete(:order_str) || params.delete(:order_str)
  _order = options.delete(:order) || params.delete(:order) || 'id'
  _direction = options.delete(:direction) || params.delete(:direction) || 'desc'
  _per_page = options.delete(:per_page) || params.delete(:per_page)

  @search = Search.new(params, options.delete(:search) || {})
  @filters = Filter.new(params.delete(:filters) || {})
  @order = Order.new(_order, _direction, _order_str)
  @paginate = Paginate.new(params.delete(:page), _per_page)
end

Instance Attribute Details

#filtersObject (readonly)

Returns the value of attribute filters.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def filters
  @filters
end

#modelObject (readonly)

Returns the value of attribute model.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def model
  @model
end

#optionsObject (readonly)

Returns the value of attribute options.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def options
  @options
end

#orderObject (readonly)

Returns the value of attribute order.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def order
  @order
end

#paginateObject (readonly)

Returns the value of attribute paginate.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def paginate
  @paginate
end

#paramsObject (readonly)

Returns the value of attribute params.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def params
  @params
end

#searchObject (readonly)

Returns the value of attribute search.



95
96
97
# File 'app/services/active_record/search_service.rb', line 95

def search
  @search
end

Instance Method Details

#runObject



113
114
115
116
117
118
119
# File 'app/services/active_record/search_service.rb', line 113

def run
  resources = model.where(params.slice(*model.column_names.collect(&:to_sym))).where(options)
  [search, filters, order, paginate].each do |operation|
    resources = operation.apply(resources)
  end
  resources
end