Class: Kadmin::Finder
- Inherits:
-
Object
- Object
- Kadmin::Finder
- Includes:
- Presentable
- Defined in:
- app/components/kadmin/finder.rb,
app/components/kadmin/finder/filter.rb,
app/components/kadmin/finder/presenter.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#filters ⇒ Hash<String, Kadmin::Finder::Filter>
readonly
Array of filters applied to the finder.
-
#pager ⇒ Kadmin::Pager
readonly
The pager to use (if any).
-
#scope ⇒ ActiveRecord::Relation
readonly
The base relation to find items from.
-
#sort_asc ⇒ bool
readonly
True if sort order is ASC, false if DESC.
-
#sort_column ⇒ String
readonly
Name of column that is used in ORDER_BY clause.
Instance Method Summary collapse
- #filter(filter, value = nil) ⇒ Object
- #filtering? ⇒ Boolean
-
#find! ⇒ Object
Forces to refetch/recalculate the find operation results.
-
#initialize(scope) ⇒ Finder
constructor
A new instance of Finder.
-
#order(sort_column, sort_asc) ⇒ Object
sets sort order for scope, any existing order is overwritten.
-
#paginate(offset: nil, size: nil) ⇒ Kadmin::Finder
Itself.
-
#results ⇒ ActiveRecord::Relation
The filtered (and optionally paginated) results.
Methods included from Presentable
Constructor Details
#initialize(scope) ⇒ Finder
Returns a new instance of Finder.
23 24 25 26 27 28 29 30 |
# File 'app/components/kadmin/finder.rb', line 23 def initialize(scope) @scope = scope @pager = nil @filters = {} @results = nil @filtering = false @sort_asc = true end |
Instance Attribute Details
#filters ⇒ Hash<String, Kadmin::Finder::Filter> (readonly)
Returns array of filters applied to the finder.
11 12 13 |
# File 'app/components/kadmin/finder.rb', line 11 def filters @filters end |
#pager ⇒ Kadmin::Pager (readonly)
Returns the pager to use (if any).
8 9 10 |
# File 'app/components/kadmin/finder.rb', line 8 def pager @pager end |
#scope ⇒ ActiveRecord::Relation (readonly)
Returns the base relation to find items from.
14 15 16 |
# File 'app/components/kadmin/finder.rb', line 14 def scope @scope end |
#sort_asc ⇒ bool (readonly)
Returns true if sort order is ASC, false if DESC.
20 21 22 |
# File 'app/components/kadmin/finder.rb', line 20 def sort_asc @sort_asc end |
#sort_column ⇒ String (readonly)
Returns name of column that is used in ORDER_BY clause.
17 18 19 |
# File 'app/components/kadmin/finder.rb', line 17 def sort_column @sort_column end |
Instance Method Details
#filter(filter, value = nil) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'app/components/kadmin/finder.rb', line 35 def filter(filter, value = nil) @filters[filter.name] = filter if value.present? @filtering = true @scope = filter.apply(@scope, value) end return self end |
#filtering? ⇒ Boolean
54 55 56 |
# File 'app/components/kadmin/finder.rb', line 54 def filtering? return @filtering end |
#find! ⇒ Object
Forces to refetch/recalculate the find operation results
83 84 85 86 87 |
# File 'app/components/kadmin/finder.rb', line 83 def find! @total_found = 0 @results = nil return results end |
#order(sort_column, sort_asc) ⇒ Object
sets sort order for scope, any existing order is overwritten
48 49 50 51 52 |
# File 'app/components/kadmin/finder.rb', line 48 def order(sort_column, sort_asc) @sort_column = sort_column @sort_asc = sort_asc @scope = @scope.reorder("#{sort_column} #{sort_asc ? 'ASC' : 'DESC'}") end |
#paginate(offset: nil, size: nil) ⇒ Kadmin::Finder
Returns itself.
61 62 63 64 65 66 67 68 69 70 |
# File 'app/components/kadmin/finder.rb', line 61 def paginate(offset: nil, size: nil) offset = offset.to_i size = size.to_i if size.positive? && offset >= 0 @pager = Kadmin::Pager.new(size: size, offset: offset) end return self end |
#results ⇒ ActiveRecord::Relation
Returns the filtered (and optionally paginated) results.
73 74 75 76 77 78 79 80 |
# File 'app/components/kadmin/finder.rb', line 73 def results return @results ||= begin results = @scope results = @pager.paginate(results) unless @pager.nil? results.load results end end |