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



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

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

Instance Attribute Details

#filtersHash<String, Kadmin::Finder::Filter> (readonly)



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

def filters
  @filters
end

#pagerKadmin::Pager (readonly)



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

def pager
  @pager
end

#scopeActiveRecord::Relation (readonly)



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

def scope
  @scope
end

Instance Method Details

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/kadmin/finder.rb', line 31

def filter(name:, column:, value:)
  if column.present?
    @filters[name] = Kadmin::Finder::Filter.new(column, value)
    if value.present?
      search_value = ActiveRecord::Base.connection.quote("%#{value}%".squeeze('%'))
      filters = Array.wrap(column).map do |column_name|
        %(`#{@scope.table_name}`.`#{column_name}` LIKE #{search_value})
      end
      @scope = @scope.where(filters.join(' OR '))
      @filtering = true
    end
  end

  return self
end

#filtering?Boolean



47
48
49
# File 'app/components/kadmin/finder.rb', line 47

def filtering?
  return @filtering
end

#find!Object

Forces to refetch/recalculate the find operation results



76
77
78
79
80
# File 'app/components/kadmin/finder.rb', line 76

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

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



54
55
56
57
58
59
60
61
62
63
# File 'app/components/kadmin/finder.rb', line 54

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



66
67
68
69
70
71
72
73
# File 'app/components/kadmin/finder.rb', line 66

def results
  return @results ||= begin
    results = @scope
    results = @pager.paginate(results) unless @pager.nil?
    results.load
    results
  end
end