Skip to content

Add new table row helper to base helper #9

@itschn

Description

@itschn

The table row helper should accept blocks as well.

So stuff can be done like:

= tr(:project_id) do
  = link_to resource.project.name, [current_namespace, resource.project]

This makes the code more readable?!

  def table_row(*attr, &block)
    options = attr.extract_options!

    object, field, value = if attr.first.kind_of?(Symbol)
      value = if block_given?
         capture(&block)
      else
        attr.size > 1 ? attr.last : nil
      end

      [resource, attr.first, value]
    else
      attr
    end

    defaults = { resource_class: object.class }
    options = defaults.merge(options)
    value = l(object.send(field), format: :number) if !value && field.to_s =~ /_at\Z/
    value = true_or_false(object.send(field)) if !value && [TrueClass, FalseClass].include?(object.send(field).class)

    content_tag :tr, id: dom_id(object, [:tr, field].join('_')) do
      concat content_tag(:td, options[:resource_class].human_attribute_name(field))
      value ? concat(content_tag(:td, value)) : concat(content_tag(:td, object.send(field)))
    end
  end

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions