Class: Kadmin::Finder
- Inherits:
-
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
#present
Constructor Details
#initialize(scope) ⇒ Finder
19
20
21
22
23
24
25
|
# File 'app/components/kadmin/finder.rb', line 19
def initialize(scope)
@scope = scope
= nil
@filters = {}
@results = nil
@filtering = false
end
|
Instance Attribute Details
10
11
12
|
# File 'app/components/kadmin/finder.rb', line 10
def filters
@filters
end
|
7
8
9
|
# File 'app/components/kadmin/finder.rb', line 7
def
end
|
#scope ⇒ ActiveRecord::Relation
13
14
15
|
# File 'app/components/kadmin/finder.rb', line 13
def scope
@scope
end
|
Instance Method Details
#filter(name:, column:, value:) ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'app/components/kadmin/finder.rb', line 30
def filter(name:, column:, value:)
if column.present?
@filters[name] = Kadmin::Finder::Filter.new(column, value)
if value.present?
search_value = ActiveRecord::Base.sanitize("%#{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
46
47
48
|
# File 'app/components/kadmin/finder.rb', line 46
def filtering?
return @filtering
end
|
#find! ⇒ Object
Forces to refetch/recalculate the find operation results
75
76
77
78
79
|
# File 'app/components/kadmin/finder.rb', line 75
def find!
@total_found = 0
@results = nil
return results
end
|
#paginate(offset: nil, size: nil) ⇒ Kadmin::Finder
53
54
55
56
57
58
59
60
61
62
|
# File 'app/components/kadmin/finder.rb', line 53
def paginate(offset: nil, size: nil)
offset = offset.to_i
size = size.to_i
if size.positive? && offset >= 0
= Kadmin::.new(size: size, offset: offset)
end
return self
end
|
#results ⇒ ActiveRecord::Relation
65
66
67
68
69
70
71
72
|
# File 'app/components/kadmin/finder.rb', line 65
def results
return @results ||= begin
results = @scope
results = .paginate(results) unless .nil?
results.load
results
end
end
|