Class: Iquest::SimpleTable::TableBuilder
- Inherits:
-
Object
- Object
- Iquest::SimpleTable::TableBuilder
- 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
-
#actions ⇒ Object
readonly
Returns the value of attribute actions.
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#collection_actions ⇒ Object
readonly
Returns the value of attribute collection_actions.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#search_form ⇒ Object
readonly
Returns the value of attribute search_form.
-
#table_id ⇒ Object
readonly
Returns the value of attribute table_id.
Instance Method Summary collapse
- #action(*args, &block) ⇒ Object
- #collection_action(*args) ⇒ Object
- #column(*args, &block) ⇒ Object
-
#initialize(parent, collection_or_search, options = {}) ⇒ TableBuilder
constructor
A new instance of TableBuilder.
- #new_link(*_args) ⇒ Object
- #reset_link(*_args, &block) ⇒ Object
- #search_link(*_args, &block) ⇒ Object
- #to_s ⇒ Object
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, = {}) @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 [: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? || [:class]) @collection = collection_or_search @klass = [: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) [:search_url] ||= begin polymorphic_path(@klass) rescue NoMethodError nil end @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
#actions ⇒ Object (readonly)
Returns the value of attribute actions.
6 7 8 |
# File 'lib/iquest/simple_table/table_builder.rb', line 6 def actions @actions end |
#collection ⇒ Object (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_actions ⇒ Object (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 |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
6 7 8 |
# File 'lib/iquest/simple_table/table_builder.rb', line 6 def columns @columns end |
#parent ⇒ Object (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_form ⇒ Object (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_id ⇒ Object (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 = args. [:proc] = block if block_given? @actions << 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 = args. search = .delete(:search) @columns[attr] = @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 |
#new_link(*_args) ⇒ Object
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 |
#reset_link(*_args, &block) ⇒ Object
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 |
#search_link(*_args, &block) ⇒ Object
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_s ⇒ Object
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 |