Class: Kadmin::Finder

Inherits:
Object
  • Object
show all
Includes:
Presentable
Defined in:
app/components/kadmin/finder.rb,
app/components/kadmin/finder/presenter.rb

Defined Under Namespace

Classes: Filter, Presenter

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Presentable

#present

Constructor Details

#initialize(scope) ⇒ Finder

Returns a new instance of Finder

Parameters:

  • scope (ActiveRecord::Relation)

    base relation to page/filter on



19
20
21
22
23
24
25
# File 'app/components/kadmin/finder.rb', line 19

def initialize(scope)
  @scope = scope
  @pager = nil
  @filters = {}
  @results = nil
  @filtering = false
end

Instance Attribute Details

#filtersArray<Kadmin::Finder::Filter> (readonly)

Returns array of filters applied to the finder

Returns:



10
11
12
# File 'app/components/kadmin/finder.rb', line 10

def filters
  @filters
end

#pagerKadmin::Pager (readonly)

Returns the pager to use (if any)

Returns:



7
8
9
# File 'app/components/kadmin/finder.rb', line 7

def pager
  @pager
end

#scopeActiveRecord::Relation (readonly)

Returns the base relation to find items from

Returns:

  • (ActiveRecord::Relation)

    the base relation to find items from



13
14
15
# File 'app/components/kadmin/finder.rb', line 13

def scope
  @scope
end

Instance Method Details

#filter(name:, column:, value:) ⇒ Object

Parameters:

  • name (String)

    the filter name (should be unique)

  • column (String, Array<String>)

    the column(s) name to filter on

  • value (String, Array<String>)

    the value or values to look for (OR'd)



30
31
32
33
34
35
36
37
38
39
40
# File 'app/components/kadmin/finder.rb', line 30

def filter(name:, column:, value:)
  if column.present? && !@filters.key?(name)
    @filters[name] = Kadmin::Finder::Filter.new(column, value)
    if value.present?
      @scope = @scope.where("#{@scope.table_name}.`#{column}` LIKE ?", "%#{value}%".squeeze('%'))
      @filtering = true
    end
  end

  return self
end

#filtering?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'app/components/kadmin/finder.rb', line 42

def filtering?
  return @filtering
end

#find!Object



72
73
74
75
76
# File 'app/components/kadmin/finder.rb', line 72

def find!
  @total_found = 0
  @results = nil
  return results
end

#paginate(offset: nil, size: nil) ⇒ Kadmin::Finder

Returns itself

Parameters:

  • offset (Integer)

    optional; offset/index for the current page

  • size (Integer)

    optional; size of a page

Returns:



49
50
51
52
53
54
55
56
57
58
# File 'app/components/kadmin/finder.rb', line 49

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

#resultsActiveRecord::Relation

Returns the filtered (and optionally paginated) results

Returns:

  • (ActiveRecord::Relation)

    the filtered (and optionally paginated) results



61
62
63
64
65
66
67
68
69
70
# File 'app/components/kadmin/finder.rb', line 61

def results
  return @results ||= begin
    results = @scope
    results = @pager.paginate(results) unless @pager.nil?
    Rails.logger.info('Right before loading')
    results.load
    Rails.logger.info('Right after loading')
    results
  end
end