Class: IndexFor::WiceBuilder

Inherits:
Builder
  • Object
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

Constructor Details

This class inherits a constructor from IndexFor::Builder

Instance Attribute Details

#directionObject

Returns the value of attribute direction.



5
6
7
# File 'lib/index_for/builders/wice_builder.rb', line 5

def direction
  @direction
end

#orderObject

Returns the value of attribute order.



5
6
7
# File 'lib/index_for/builders/wice_builder.rb', line 5

def order
  @order
end

#pageObject

Returns the value of attribute page.



5
6
7
# File 'lib/index_for/builders/wice_builder.rb', line 5

def page
  @page
end

#per_pageObject

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

#searchObject

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



15
16
17
18
19
20
# File 'lib/index_for/builders/wice_builder.rb', line 15

def attribute attribute_name, options = {}
  model_class = options[:model] || html_options[:model] || @object
  sortable_fields << (options[:sortable] || "#{model_class.table_name}.#{attribute_name}")
  searchable_fields << "#{model_class.table_name}.#{attribute_name}" if options[:searchable]
  nil
end

#fields_for(attribute_name, options = {}, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/index_for/builders/wice_builder.rb', line 22

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

#per_pagesObject



46
47
48
49
50
51
# File 'lib/index_for/builders/wice_builder.rb', line 46

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

#resultObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/index_for/builders/wice_builder.rb', line 30

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 = params[:order] if sortable_fields.include? params[:order]
  self.direction = params[:direction] || "asc"
  self.search = params[:search]

  collection = @object
  collection = collection.page(page).per(per_page)
  collection = collection.order("#{order} #{direction.upcase}") if order
  collection = collection.where(searchable_fields.map{|f|"#{f} LIKE '%#{search}%'"}.join(" OR ")) if search
  collection
end

#searchable_fieldsObject



11
12
13
# File 'lib/index_for/builders/wice_builder.rb', line 11

def searchable_fields
  @searchable_fields ||= []
end

#sortable_fieldsObject



7
8
9
# File 'lib/index_for/builders/wice_builder.rb', line 7

def sortable_fields
  @sortable_fields ||= []
end