lib/tableficate/helper.rb in tableficate-0.2.1 vs lib/tableficate/helper.rb in tableficate-0.3.0

- old
+ new

@@ -1,102 +1,99 @@ module Tableficate module Helper def table_for(rows, options = {}) t = Tableficate::Table.new(self, rows, options, rows.tableficate_get_data) yield(t) - t.render + t.template.render(partial: Tableficate::Utils::template_path(t.template, 'table_for', t.theme), locals: {table: t}) end + def tableficate_table_tag(table) + render partial: Tableficate::Utils::template_path(table.template, 'table', table.theme), locals: {table: table} + end + def tableficate_header_tag(column) - render partial: Tableficate::Utils::template_path('header', column.table.options[:theme]), locals: {column: column} + table = column.table + render partial: Tableficate::Utils::template_path(table.template, 'header', table.theme), locals: {column: column} end def tableficate_data_tag(row, column) - render partial: Tableficate::Utils::template_path('data', column.table.options[:theme]), locals: {row: row, column: column} + table = column.table + render partial: Tableficate::Utils::template_path(table.template, 'data', table.theme), locals: {row: row, column: column} end def tableficate_row_tag(row, columns) - render partial: Tableficate::Utils::template_path('row', columns.first.table.options[:theme]), locals: {row: row, columns: columns} + table = columns.first.table + render partial: Tableficate::Utils::template_path(table.template, 'row', table.theme), locals: {row: row, columns: columns} end + def tableficate_filter_form_tag(table) + render partial: Tableficate::Utils::template_path(table.template, 'filters/form', table.theme), locals: {table: table} + end + def tableficate_filter_tag(filter) - render partial: Tableficate::Utils::template_path(filter.template, filter.table.options[:theme]), locals: {filter: filter} + table = filter.table + render partial: Tableficate::Utils::template_path(table.template, filter.template, table.theme), locals: {filter: filter} end def tableficate_label_tag(filter) - label_tag(filter.field_name, filter.label, filter.options[:label_options]) + label_tag(filter.field_name, filter.label, filter.label_options) end def tableficate_text_field_tag(filter) - text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.options) + text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.attrs) end def tableficate_select_tag(filter) field_value = filter.field_value(params[filter.table.as]) - if field_value.present? and filter.options[:collection].is_a?(String) + collection = filter.collection + + if field_value.present? and collection.is_a?(String) Array.wrap(field_value).each do |fv| - if filter.options[:collection].match(/<option[^>]*value\s*=/) - filter.options[:collection].gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"') + if collection.match(/<option[^>]*value\s*=/) + collection.gsub!(/(<option[^>]*value\s*=\s*['"]?#{fv}[^>]*)/, '\1 selected="selected"') else - filter.options[:collection].gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<") + collection.gsub!(/>#{fv}</, " selected=\"selected\">#{fv}<") end end - elsif not filter.options[:collection].is_a?(String) - filter.options[:collection] = Tableficate::Filter::Collection.new(filter.options[:collection], selected: field_value).map {|choice| - html_attributes = choice.options.length > 0 ? ' ' + choice.options.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : '' + elsif not collection.is_a?(String) + collection = Tableficate::Filter::Collection.new(collection, selected: field_value).map {|choice| + html_attributes = choice.attrs.length > 0 ? ' ' + choice.attrs.map {|k, v| %(#{k.to_s}="#{v}")}.join(' ') : '' selected_attribute = choice.selected? ? ' selected="selected"' : '' %(<option value="#{ERB::Util.html_escape(choice.value)}"#{selected_attribute}#{html_attributes}>#{ERB::Util.html_escape(choice.name)}</option>) }.join("\n") end - filter.options[:collection] = filter.options[:collection].html_safe + collection = collection.html_safe - select_tag(filter.field_name, filter.options.delete(:collection), filter.options) + select_tag(filter.field_name, collection, filter.attrs) end - def tableficate_radio_tags(filter, &block) - field_value = filter.field_value(params[filter.table.as]) + def tableficate_radio_tags(filter) + tableficate_collection_of_tags(filter) + end - collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as])) - - html = [] - if block_given? - html = collection.map {|choice| capture(choice, &block)} + def tableficate_check_box_tags(filter) + if filter.collection.empty? + check_box_tag(filter.field_name, true, filter.field_value(params[filter.table.as]) == 'true', filter.attrs) else - collection.each do |choice| - html.push( - radio_button_tag(filter.field_name, choice.value, choice.checked?, choice.options), - label_tag("#{filter.field_name}[#{choice.value}]", choice.name), - '<br/>' - ) - end + tableficate_collection_of_tags(filter) end - - html.join("\n").html_safe end - def tableficate_check_box_tags(filter, &block) + def tableficate_collection_of_tags(filter) + table = filter.table field_value = filter.field_value(params[filter.table.as]) - collection = Tableficate::Filter::Collection.new(filter.options[:collection], selected: filter.field_value(params[filter.table.as])) - html = [] - if block_given? - html = collection.map {|choice| capture(choice, &block)} - else - collection.each do |choice| - html.push( - check_box_tag( - "#{filter.field_name}[#{choice.value}]", choice.value, choice.checked?, choice.options.reverse_merge(name: "#{filter.field_name}[]") - ), - label_tag("#{filter.field_name}[#{choice.value}]", choice.name), - '<br/>' - ) - end + Tableficate::Filter::Collection.new(filter.collection, selected: field_value).each do |choice| + html.push( + render(partial: Tableficate::Utils::template_path(table.template, filter.template + '_choice', table.theme), locals: {filter: filter, choice: choice}) + ) end html.join("\n").html_safe end + private :tableficate_collection_of_tags end end