lib/para/markup/resources_table.rb in para-0.4.0 vs lib/para/markup/resources_table.rb in para-0.5.0

- old
+ new

@@ -9,13 +9,11 @@ if !options.key?(:orderable) || options.delete(:orderable) @orderable = model.orderable? end - if !options.key?(:actions) || options.delete(:actions) - @actions = true - end + @actions = options.fetch(:actions, true) merge_class!(options, 'table') merge_class!(options, 'para-component-relation-table') merge_class!(options, 'table-hover') if options.fetch(:hover, true) @@ -70,14 +68,27 @@ cells << actions_cell(resource) if actions cells.join("\n").html_safe end - def header_for(field_name, sort: field_name) - label = model.human_attribute_name(field_name) + def header_for(field_name = nil, options = {}, &block) + if Hash === field_name + options = field_name + field_name = nil + end - content_tag(:th) do + sort = options.delete(:sort) || field_name + + label = if Symbol === field_name + model.human_attribute_name(field_name) + elsif block + capture { block.call } + else + field_name + end + + content_tag(:th, options) do if sort != field_name view.sort_link(search, *sort, label, hide_indicator: true) elsif searchable?(field_name) view.sort_link(search, field_name, label, hide_indicator: true) else @@ -92,14 +103,16 @@ # the field_value_for helper # # - a single value : The value to display in the cell directly # which will be processed to be shorter than 100 chars # - def data_for(*args) + def data_for(*args, &block) value = if args.length >= 2 resource, field_name, type = args view.field_value_for(resource, field_name, type).to_s + elsif block + capture { block.call } else view.excerpt_value_for(args.first) end content_tag(:td) do @@ -117,51 +130,58 @@ end def actions_cell(resource) content_tag(:td) do content_tag(:div, class: 'pull-right btn-group') do - edit_button(resource) + - clone_button(resource) + - delete_button(resource) + edit = edit_button(resource) if action?(:edit) + clone = clone_button(resource) if action?(:clone) + delete = delete_button(resource) if action?(:delete) + + edit + clone + delete end end end def clone_button(resource) return unless resource.class.cloneable? - view.link_to( - component.relation_path( - resource, action: :clone, return_to: view.request.fullpath - ), + path = component.relation_path( + resource, action: :clone, return_to: view.request.fullpath + ) + + options = { method: :post, class: 'btn btn-info' - ) do + } + + view.link_to(path, options) do content_tag(:i, '', class: 'fa fa-refresh') end end def edit_button(resource) - view.link_to( - component.relation_path( - resource, action: :edit, return_to: view.request.fullpath - ), - class: 'btn btn-primary' - ) do + path = component.relation_path( + resource, action: :edit, return_to: view.request.fullpath + ) + + view.link_to(path, class: 'btn btn-primary') do content_tag(:i, '', class: 'fa fa-pencil') end end def delete_button(resource) - view.link_to( - component.relation_path(resource), + path = component.relation_path(resource) + + options = { method: :delete, data: { confirm: I18n.t('para.list.delete_confirmation') }, class: 'btn btn-danger' - ) do + } + + view.link_to(path, options) do content_tag(:i, '', class: 'fa fa-trash') end end private @@ -170,9 +190,13 @@ @search ||= view.instance_variable_get(:@q) end def searchable?(field_name) model.columns_hash.keys.include?(field_name.to_s) + end + + def action?(type) + actions == true || actions.include?(type) end end end end