Class: IndexFor::WiceBuilder
- Inherits:
-
Builder
- Object
- Builder
- IndexFor::WiceBuilder
show all
- Defined in:
- lib/index_for/builders/wice_builder.rb
Instance Attribute Summary collapse
Attributes inherited from Builder
#html_options, #object, #template
Instance Method Summary
collapse
Methods inherited from Builder
#actions, #association, #attributes, #initialize
Instance Attribute Details
#direction ⇒ Object
Returns the value of attribute direction.
5
6
7
|
# File 'lib/index_for/builders/wice_builder.rb', line 5
def direction
@direction
end
|
#order ⇒ Object
Returns the value of attribute order.
5
6
7
|
# File 'lib/index_for/builders/wice_builder.rb', line 5
def order
@order
end
|
#page ⇒ Object
Returns the value of attribute page.
5
6
7
|
# File 'lib/index_for/builders/wice_builder.rb', line 5
def page
@page
end
|
#per_page ⇒ Object
Returns the value of attribute per_page.
5
6
7
|
# File 'lib/index_for/builders/wice_builder.rb', line 5
def per_page
@per_page
end
|
#search ⇒ Object
Returns the value of attribute search.
5
6
7
|
# File 'lib/index_for/builders/wice_builder.rb', line 5
def search
@search
end
|
Instance Method Details
#attribute(attribute_name, options = {}) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/index_for/builders/wice_builder.rb', line 27
def attribute attribute_name, options = {}
model_class = options[:model] || html_options[:model] || @object
field_name = "#{model_class.table_name}.#{attribute_name}"
sortable_field = parse_option(options[:sortable] || true, field_name)
sortable_fields.merge!(field_name => sortable_field) if sortable_field
searchable_field = parse_option(options[:searchable] || false, field_name)
searchable_fields.concat(Array.wrap(searchable_field)) if searchable_field
filterable_field = parse_option(options[:filterable] || false, field_name)
filterable_fields.merge! filterable_field if filterable_field
nil
end
|
#column_exists?(field_name) ⇒ Boolean
23
24
25
|
# File 'lib/index_for/builders/wice_builder.rb', line 23
def column_exists? field_name
ActiveRecord::Base.connection.column_exists? *field_name.split(".", 2)
end
|
#fields_for(attribute_name, options = {}, &block) ⇒ Object
39
40
41
42
43
44
45
|
# File 'lib/index_for/builders/wice_builder.rb', line 39
def fields_for attribute_name, options = {}, &block
cached_html_options = @html_options
@html_options = options
block.call self
@html_options = cached_html_options
nil
end
|
#filterable_fields ⇒ Object
15
16
17
|
# File 'lib/index_for/builders/wice_builder.rb', line 15
def filterable_fields
@filterable_fields ||= {}
end
|
#parse_option(option, default) ⇒ Object
19
20
21
|
# File 'lib/index_for/builders/wice_builder.rb', line 19
def parse_option option, default
option === true ? default : option if option
end
|
#per_pages ⇒ Object
64
65
66
67
68
69
|
# File 'lib/index_for/builders/wice_builder.rb', line 64
def per_pages
params = template.params.permit!
IndexFor.per_pages.each do |per_page|
yield per_page, @template.url_for(params.merge(page: nil, per_page: per_page)), per_page == self.per_page
end
end
|
#result ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/index_for/builders/wice_builder.rb', line 47
def result
params = @template.params
self.per_page = (params[:per_page] || IndexFor.per_page).to_i
self.page = (params[:page] || 1).to_i
self.order = sortable_fields[params[:order]] if sortable_fields.key? params[:order]
self.direction = params[:direction] || "asc"
self.search = params[:search]
collection = @object
collection = collection.page(page).per(per_page)
collection = collection.order(Array.wrap(order).select{|f|column_exists? f}.map{|f|"#{f} #{direction.upcase}"}.join(", ")) if order
collection = collection.where((searchable_fields.flatten.map{|f|"#{f} LIKE '%#{search}%'"} + filterable_fields.select{|k,v|v}.map{|k,v|"#{k} = #{v}"}).join(" OR ")) if search
collection
end
|
#searchable_fields ⇒ Object
11
12
13
|
# File 'lib/index_for/builders/wice_builder.rb', line 11
def searchable_fields
@searchable_fields ||= []
end
|
#sortable_fields ⇒ Object
7
8
9
|
# File 'lib/index_for/builders/wice_builder.rb', line 7
def sortable_fields
@sortable_fields ||= {}
end
|