lib/tableficate/helper.rb in tableficate-0.1.3 vs lib/tableficate/helper.rb in tableficate-0.2.0
- old
+ new
@@ -5,11 +5,11 @@
yield(t)
t.render
end
def tableficate_header_tag(column)
- render partial: Tableficate::Utils::template_path('column_header', column.table.options[:theme]), locals: {column: column}
+ render partial: Tableficate::Utils::template_path('header', column.table.options[: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}
end
@@ -21,17 +21,82 @@
def tableficate_filter_tag(filter)
render partial: Tableficate::Utils::template_path(filter.template, filter.table.options[: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.options[:label_options])
end
def tableficate_text_field_tag(filter)
- text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.options[:field_options] || {})
+ text_field_tag(filter.field_name, filter.field_value(params[filter.table.as]), filter.options)
end
def tableficate_select_tag(filter)
- select_tag(filter.field_name, filter.choices, filter.options)
+ field_value = filter.field_value(params[filter.table.as])
+
+ if field_value.present? and filter.options[: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"')
+ else
+ filter.options[: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(' ') : ''
+ 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
+
+ select_tag(filter.field_name, filter.options.delete(:collection), filter.options)
+ end
+
+ def tableficate_radio_tags(filter, &block)
+ 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(
+ radio_button_tag(filter.field_name, choice.value, choice.checked?, choice.options),
+ label_tag("#{filter.field_name}[#{choice.value}]", choice.name),
+ '<br/>'
+ )
+ end
+ end
+
+ html.join("\n").html_safe
+ end
+
+ def tableficate_check_box_tags(filter, &block)
+ 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
+ end
+
+ html.join("\n").html_safe
end
end
end