Module: Manage::ResourceHelper

Defined in:
app/helpers/manage/resource_helper.rb

Instance Method Summary collapse

Instance Method Details

to customise the actions for a resource define a list of actions

example:

action_links  :posts, :tickets, ->(resource) {link_to "#{resource.name}"}

Parameters:

  • scope (type)
    description
  • link_data (type)
    description

Returns:

  • (type)
    description


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/manage/resource_helper.rb', line 34

def action_link(scope, link_data)
  value = nil
  case link_data

  when Proc
    value = link_data.call(scope)

  when Hash
    relation = link_data.keys.first
    entity = Fields::Reader.field_value(scope, relation)
    unless entity.present?
      return ''
    end
    if link_data[relation][:label_field].present?
      value = field_value(scope, link_data[relation][:label_field])
    elsif link_data[relation][:label].present?
      value = link_data[relation][:label]
    end

    path = entity.class.name.dasherize.pluralize.downcase
    return  link_to value, [scope.public_send(relation)]

  when *[Symbol, String]
    relation = link_data.to_s
    assocation = scope.class.reflect_on_association(link_data.to_sym)
    raise "assocation #{link_data} not found on #{scope.class}" unless assocation
    if assocation.options[:class_name].present?
      rel_name = scope.class.reflect_on_association(link_data.to_sym).options[:class_name]
      relation = rel_name.downcase.dasherize.pluralize
    end
    if scope.class.reflect_on_association(link_data.to_sym).options[:as].present?
      key = scope.class.reflect_on_association(link_data.to_sym).options[:as].to_s + '_id'
    else
      key = scope.class.name.downcase.dasherize + '_id'
    end
    return "<a href=\"#{relation}?f%5B#{key}%5D=#{scope.id}\">#{resource_class.human_attribute_name(link_data.to_s)}</a>".html_safe
  else
    raise 'Unsupported link data'
  end

end

#attributesObject



11
12
13
# File 'app/helpers/manage/resource_helper.rb', line 11

def attributes
  resource_class.attribute_names - %w(id created_at updated_at)
end

#edit_fieldsObject



7
8
9
# File 'app/helpers/manage/resource_helper.rb', line 7

def edit_fields
  list_edit_fields
end

#field_title(resource_class, field_data) ⇒ Object



19
20
21
# File 'app/helpers/manage/resource_helper.rb', line 19

def field_title(resource_class, field_data)
  Fields::Reader.field_title(resource_class, field_data)
end

#field_value(scope, field_data) ⇒ Object



15
16
17
# File 'app/helpers/manage/resource_helper.rb', line 15

def field_value(scope, field_data)
  Fields::Reader.field_value(scope, field_data).to_s
end

#index_fieldsObject



3
4
5
# File 'app/helpers/manage/resource_helper.rb', line 3

def index_fields
  list_index_fields
end