Class: Manage::Fields::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/manage/fields/reader.rb

Class Method Summary collapse

Class Method Details

.field_title(resource_class, field_data, prefix = '') ⇒ Object



20
21
22
23
24
25
# File 'lib/manage/fields/reader.rb', line 20

def field_title(resource_class, field_data, prefix='')
  current_field, rest_field_parts = _parse_field_data(field_data)
  title = resource_class.human_attribute_name(current_field)

  rest_field_parts.empty? ? prefix + title : field_title(resource_class, rest_field_parts.join('.'), prefix + title + ' ')
end

.field_value(scope, field_data) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/manage/fields/reader.rb', line 5

def field_value(scope, field_data)
  current_field, rest_field_parts, custom_query, custom_format = _parse_field_data(field_data)
  value = scope.respond_to?(current_field) ? scope.public_send(current_field) : "missing index field '#{current_field}'"

  if _is_field_relation?(value)
    value = custom_query.present? ? custom_query.call(value) : value

    value.map do |entity|
      ("<a href=\"#{current_field}/#{entity.id}\">#{custom_format.present? ? custom_format.call(entity) : entity.id }</a>")
    end.join(', ').html_safe
  else
    rest_field_parts.empty? ? value : field_value(value, rest_field_parts.join('.'))
  end
end