Method: Alchemy::ResourcesHelper#render_attribute

Defined in:
app/helpers/alchemy/resources_helper.rb

#render_attribute(resource, attribute, options = {}) ⇒ String

Returns the value from resource attribute

If the attribute has a relation, the related object’s attribute value will be returned.

The output will be truncated after 50 chars. Pass another number to truncate then and pass false to disable this completely.

Parameters:

  • resource (Alchemy::Resource)
  • attribute (Hash)
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :truncate (Hash) — default: 50

    The length of the value returned.

  • :datetime_format (Hash) — default: alchemy.default

    The format of timestamps.

  • :time_format (Hash) — default: alchemy.time

    The format of time values.

Returns:

  • (String)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/helpers/alchemy/resources_helper.rb', line 74

def render_attribute(resource, attribute, options = {})
  attribute_value = resource.send(attribute[:name])
  if attribute[:relation]
    record = resource.send(attribute[:relation][:name])
    value = record.present? ? record.send(attribute[:relation][:attr_method]) : Alchemy.t(:not_found)
  elsif attribute_value && attribute[:type].to_s =~ /(date|time)/
    localization_format = if attribute[:type] == :datetime
      options[:datetime_format] || :"alchemy.default"
    elsif attribute[:type] == :date
      options[:date_format] || :"alchemy.default"
    else
      options[:time_format] || :"alchemy.time"
    end
    value = l(attribute_value, format: localization_format)
  elsif attribute[:type] == :boolean
    value = attribute_value ? '<alchemy-icon name="check"></alchemy-icon>'.html_safe : nil
  else
    value = attribute_value
  end

  options.reverse_merge!(truncate: 50)
  if options[:truncate]
    value.to_s.truncate(options.fetch(:truncate, 50))
  else
    value
  end
end