Class: Iquest::SimpleTable::TableBuilder

Inherits:
Object
  • Object
show all
Includes:
RansackSimpleForm::FormHelper
Defined in:
lib/iquest/simple_table/table_builder.rb

Constant Summary collapse

CLASS_DELIMITER =
' '.freeze
EMPTY_STRING =
''.freeze
DEFAULT_FORMATTER =
->(value) do
  case value
  when Time
    l(value)
  when Date
    l(value)
  else
    value
  end
end
WRAPPER_TEMPLATE =
'<div class="filter-table-block">
<%= content %>
<div class="paginate-block"><%= paginate @collection if @collection.respond_to?(:current_page) %></div>
<div class="totals-block"><%= page_entries_info @collection, entry_name: @klass.model_name.human if @collection.respond_to?(:current_page) %></div>
</div>'.freeze
WRAPPER_ERB =
ERB.new(WRAPPER_TEMPLATE)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, collection_or_search, options = {}) ⇒ TableBuilder

Returns a new instance of TableBuilder.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/iquest/simple_table/table_builder.rb', line 25

def initialize(parent, collection_or_search, options = {})
  @parent = parent
  if collection_or_search.is_a? Ransack::Search
    @collection = collection_or_search.result
    @search = collection_or_search
    @klass = @search.klass
  elsif collection_or_search.is_a?(ActiveRecord::Relation) || collection_or_search.is_a?(ActiveRecord::AssociationRelation)
    @collection = collection_or_search
    @klass = @collection.klass
  elsif collection_or_search.is_a?(Array) && (search = collection_or_search.detect { |o| o.is_a?(Ransack::Search) })
    @search = search
    @collection = search.result
    @klass = @collection.klass
    options[:search_url] ||= polymorphic_path(collection_or_search.map { |o| o.is_a?(Ransack::Search) ? o.klass : o })
  elsif collection_or_search.is_a?(Array) && (collection = collection_or_search.detect { |o| o.is_a?(ActiveRecord::Relation) || o.is_a?(ActiveRecord::AssociationRelation) })
    @collection = collection
    @klass = @collection.klass
  elsif collection_or_search.is_a?(Array) && (collection_or_search.any? || options[:class])
    @collection = collection_or_search
    @klass = options[:class] || collection_or_search.first.class
  else
    raise TypeError, 'ActiveRecord::Relation, ActiveRecord::AssociationRelation, Ransack::Search or Array of ActiveModel like objects expected'
  end
  apply_pagination
  # draper
  @collection = @collection.decorate if @collection.respond_to?(:decorate)
  options[:search_url] ||= begin
                             polymorphic_path(@klass)
                           rescue NoMethodError
                             nil
                           end
  @options = options
  @table_id = "table_#{@klass}".pluralize.parameterize
  @columns = {}.with_indifferent_access
  @actions = []
  @collection_actions = []
  @search_input_default_options = { label: false, placeholder: false }.with_indifferent_access
  @attr_classes = {}
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def actions
  @actions
end

#collectionObject (readonly)

Returns the value of attribute collection.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def collection
  @collection
end

#collection_actionsObject (readonly)

Returns the value of attribute collection_actions.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def collection_actions
  @collection_actions
end

#columnsObject (readonly)

Returns the value of attribute columns.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def columns
  @columns
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def parent
  @parent
end

#search_formObject (readonly)

Returns the value of attribute search_form.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def search_form
  @search_form
end

#table_idObject (readonly)

Returns the value of attribute table_id.



6
7
8
# File 'lib/iquest/simple_table/table_builder.rb', line 6

def table_id
  @table_id
end

Instance Method Details

#action(*args, &block) ⇒ Object



86
87
88
89
90
91
# File 'lib/iquest/simple_table/table_builder.rb', line 86

def action(*args, &block)
  _action = args.first
  options = args.extract_options!
  options[:proc] = block if block_given?
  @actions << options
end

#collection_action(*args) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/iquest/simple_table/table_builder.rb', line 93

def collection_action(*args)
  action = args.first
  if action.is_a? String
    @collection_actions << action
  elsif block_given?
    @collection_actions << yield
  end
end

#column(*args, &block) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/iquest/simple_table/table_builder.rb', line 65

def column(*args, &block)
  attr = args.first
  options = args.extract_options!
  search = options.delete(:search)
  @columns[attr] = options
  @columns[attr][:label] ||= column_label(attr)
  # iniciaizce search options
  if search.is_a?(Symbol) || search.is_a?(String)
    @columns[attr][:search] = { search.to_sym => {} }
  elsif search.is_a? Array
    @columns[attr][:search] = search.each_with_object({}) { |s, h| h[s.to_sym] = {}; }
  elsif search.is_a? Hash
    @columns[attr][:search] = search
  end
  @columns[attr][:formatter] ||= block_given? ? block : DEFAULT_FORMATTER
  @columns[attr][:sort] ||= attr.to_s.tr('.', '_') unless @columns[attr][:sort] == false # sort link attr
  @columns[attr][:html] ||= {}
  @columns[attr][:html][:class] = @columns[attr][:html][:class].join(CLASS_DELIMITER) if @columns[attr][:html][:class].is_a?(Array)
  @columns[attr][:html][:class] ||= ''
end


102
103
104
# File 'lib/iquest/simple_table/table_builder.rb', line 102

def new_link(*_args)
  ActiveSupport::Deprecation.warn("Iquest::SimpleTable#new_link does nothing. Use collection_action")
end


110
111
112
# File 'lib/iquest/simple_table/table_builder.rb', line 110

def reset_link(*_args, &block)
  @reset_button = block if block_given?
end


106
107
108
# File 'lib/iquest/simple_table/table_builder.rb', line 106

def search_link(*_args, &block)
  @search_button = block if block_given?
end

#to_sObject



121
122
123
124
# File 'lib/iquest/simple_table/table_builder.rb', line 121

def to_s
  content = render_table
  WRAPPER_ERB.result(binding).html_safe
end