Module: PowerResource::BaseHelper

Included in:
BaseController
Defined in:
app/helpers/power_resource/base_helper.rb

Instance Method Summary collapse

Instance Method Details

#attribute_value_for(resource, attribute_name, truncation = 50) ⇒ Object

Returns preformatted attribute value of a specific resource



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/power_resource/base_helper.rb', line 56

def attribute_value_for(resource, attribute_name, truncation = 50)
  value = resource.send(attribute_name).to_s.truncate(truncation)
  if attribute_name.to_s.match(/_id$/)
    model_name = attribute_name.gsub(/_id$/, '').classify
    begin
      value = eval(model_name).find(value).to_s
    rescue ActiveRecord::RecordNotFound
      value = ""
    end
  end
  value
end

Returns association links for a resource



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/helpers/power_resource/base_helper.rb', line 80

def resource_association_links_for(resource, options = {})
  output = Array.new
  resource_associations(:has_many).each do |association|
    text = resource_human_name_for(association, 2)
    if controller_path.include?('/')
      namespace = controller.class.parent.name.underscore
      path = [namespace, resource, association.to_s.tableize]
    else
      path = [resource, association.to_s.tableize]
    end
    options.merge!(class: collection_table_button_classes)
    output << link_to(text, path, options)
  end
  output.join(' ').html_safe
end

#resource_associations(association_type) ⇒ Object

Returns associations for a resource



75
76
77
# File 'app/helpers/power_resource/base_helper.rb', line 75

def resource_associations(association_type)
  resource_class.reflect_on_all_associations(association_type).map(&:name)
end

#resource_attribute_human_name_for(attribute_name) ⇒ Object

Returns humanized and localized attribute name



49
50
51
52
53
# File 'app/helpers/power_resource/base_helper.rb', line 49

def resource_attribute_human_name_for(attribute_name)
  attribute_name = attribute_name.to_s
  I18n.t("activerecord.attributes.#{resource_name}.#{attribute_name}",
    default: attribute_name.humanize)
end

#resource_attributesObject

Returns all attributes for a resource



26
27
28
# File 'app/helpers/power_resource/base_helper.rb', line 26

def resource_attributes
  resource_class.attribute_names
end

#resource_form_pathObject

Returns a form path for a resource



70
71
72
# File 'app/helpers/power_resource/base_helper.rb', line 70

def resource_form_path
  resource.new_record? ? collection_path : resource_path
end

#resource_human_attributesObject

Returns attributes for a resource without non-human attributes



36
37
38
39
40
41
42
43
44
45
46
# File 'app/helpers/power_resource/base_helper.rb', line 36

def resource_human_attributes
  human_attributes = resource_attributes - resource_non_human_attributes

  # If resource has a belongs_to relationship, remove reference attribute
  if respond_to?('parent?')
    parent_attribute = "#{parent.class.name.underscore}_id"
    human_attributes = human_attributes - [parent_attribute]
  end

  human_attributes
end

#resource_human_nameObject

Returns humanized and localized name for a current resource



9
10
11
# File 'app/helpers/power_resource/base_helper.rb', line 9

def resource_human_name
  resource_class.model_name.human
end

#resource_human_name_for(resource_class_name, count = 1) ⇒ Object

Returns humanized and localized name for a specific resource



14
15
16
17
# File 'app/helpers/power_resource/base_helper.rb', line 14

def resource_human_name_for(resource_class_name, count = 1)
  class_name = resource_class_name.to_s.classify
  eval(class_name).model_name.human(count: count)
end

#resource_nameObject

Returns a name for a current resource



4
5
6
# File 'app/helpers/power_resource/base_helper.rb', line 4

def resource_name
  resource_class.to_s.tableize.singularize
end

#resource_non_human_attributesObject

Returns attributes that should be invisible for end-users



31
32
33
# File 'app/helpers/power_resource/base_helper.rb', line 31

def resource_non_human_attributes
  %w(id created_at updated_at)
end

#resource_title(options = {}) ⇒ Object

Returns an unique title for a current resource



20
21
22
23
# File 'app/helpers/power_resource/base_helper.rb', line 20

def resource_title(options = {})
  I18n.t("power_resource.titles.#{resource_name}.resource",
    { default: "#{resource_human_name} #{resource.id}" }.merge(options) )
end