Module: PowerResource::BaseHelper
- Defined in:
- app/helpers/power_resource/base_helper.rb
Instance Method Summary collapse
-
#attribute_human_name(attribute_name) ⇒ Object
Returns humanized and localized attribute name.
-
#attribute_value(resource, attribute_name, truncation = 50) ⇒ Object
Returns truncated attribute value.
-
#collection_title ⇒ Object
Returns a title for a collection.
-
#non_human_attributes ⇒ Object
Returns attributes that should be invisible for end-users.
-
#render_actions_for(resource) ⇒ Object
Renders action links for a resource.
-
#render_collection_table(custom_attributes = nil) ⇒ Object
Renders collection table.
-
#render_form(form_builder = "formtastic") ⇒ Object
Renders form using selected form builder.
-
#resource_action(action_name) ⇒ Object
Returns a text based on action and resource names.
-
#resource_attributes ⇒ Object
Returns all attributes for a resource.
-
#resource_form_path ⇒ Object
Returns form path for a resource.
-
#resource_human_attributes ⇒ Object
Returns attributes for a resource without non-human attributes.
-
#resource_human_name ⇒ Object
Returns humanized and localized name for a current resource model.
-
#resource_human_name_for(resource_class_name) ⇒ Object
Returns humanized and localized name for a specified resource class.
-
#resource_link(action_name) ⇒ Object
Returns a link for a resource.
-
#resource_title ⇒ Object
Returns an unique title for a resource.
Instance Method Details
#attribute_human_name(attribute_name) ⇒ Object
Returns humanized and localized attribute name
45 46 47 48 49 |
# File 'app/helpers/power_resource/base_helper.rb', line 45 def attribute_human_name(attribute_name) attribute_name = attribute_name.to_s I18n.t("activerecord.attributes.#{controller_name.singularize}.#{attribute_name}", default: attribute_name.humanize) end |
#attribute_value(resource, attribute_name, truncation = 50) ⇒ Object
Returns truncated attribute value
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'app/helpers/power_resource/base_helper.rb', line 52 def attribute_value(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 |
#collection_title ⇒ Object
Returns a title for a collection
19 20 21 22 |
# File 'app/helpers/power_resource/base_helper.rb', line 19 def collection_title I18n.t("activemodel.models.#{controller_name.singularize}.other", default: controller_name.humanize) end |
#non_human_attributes ⇒ Object
Returns attributes that should be invisible for end-users
30 31 32 |
# File 'app/helpers/power_resource/base_helper.rb', line 30 def non_human_attributes %w(id updated_at created_at) end |
#render_actions_for(resource) ⇒ Object
Renders action links for a resource
126 127 128 |
# File 'app/helpers/power_resource/base_helper.rb', line 126 def render_actions_for(resource) render "actions", resource: resource end |
#render_collection_table(custom_attributes = nil) ⇒ Object
Renders collection table
119 120 121 122 123 |
# File 'app/helpers/power_resource/base_helper.rb', line 119 def render_collection_table(custom_attributes = nil) render "collection", collection: collection, attributes: custom_attributes || resource_human_attributes end |
#render_form(form_builder = "formtastic") ⇒ Object
Renders form using selected form builder
110 111 112 113 114 115 116 |
# File 'app/helpers/power_resource/base_helper.rb', line 110 def render_form(form_builder = "formtastic") fields = resource_human_attributes fields.map! do |arg| arg.to_s.sub("_id", "").to_sym end render "power_resource/builders/#{form_builder}", fields: fields end |
#resource_action(action_name) ⇒ Object
Returns a text based on action and resource names
Example:
resource_action(:new)
# => "New Product"
71 72 73 74 75 |
# File 'app/helpers/power_resource/base_helper.rb', line 71 def resource_action(action_name) I18n.t("power_resource.resource_actions.#{action_name}", resource_name: resource_human_name, default: "#{action_name.to_s.titleize} #{resource_human_name}") end |
#resource_attributes ⇒ Object
Returns all attributes for a resource
25 26 27 |
# File 'app/helpers/power_resource/base_helper.rb', line 25 def resource_attributes resource_class.attribute_names end |
#resource_form_path ⇒ Object
Returns form path for a resource
105 106 107 |
# File 'app/helpers/power_resource/base_helper.rb', line 105 def resource_form_path resource.new_record? ? collection_path : resource_path end |
#resource_human_attributes ⇒ Object
Returns attributes for a resource without non-human attributes
35 36 37 38 39 40 41 42 |
# File 'app/helpers/power_resource/base_helper.rb', line 35 def resource_human_attributes human_attributes = resource_attributes - non_human_attributes if respond_to?("parent?") parent_attribute = "#{parent.class.name.underscore}_id" human_attributes = human_attributes - ["#{parent_attribute}"] end human_attributes end |
#resource_human_name ⇒ Object
Returns humanized and localized name for a current resource model
4 5 6 |
# File 'app/helpers/power_resource/base_helper.rb', line 4 def resource_human_name resource_class.model_name.human end |
#resource_human_name_for(resource_class_name) ⇒ Object
Returns humanized and localized name for a specified resource class
9 10 11 |
# File 'app/helpers/power_resource/base_helper.rb', line 9 def resource_human_name_for(resource_class_name) eval("#{resource_class_name.to_s.classify}.model_name.human") end |
#resource_link(action_name) ⇒ Object
Returns a link for a resource
Examples:
resource_link(:new)
# => <a href="/products/new">New Product</a>
resource_link(:edit)
# => <a href="/products/1/edit">Edit Product</a>
resource_link(:edit, row)
# => <a href="/products/1/edit">Edit</a>
resource_link(:edit, text: "Make changes")
# => <a href="/products/1/edit">Make changes</a>
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'app/helpers/power_resource/base_helper.rb', line 89 def resource_link(action_name) text ||= resource_action(action_name) eval("resource_link_for_#{action_name.to_s}(text)") case action_name.to_sym when :new link_to(text, new_resource_path) when :edit link_to(text, edit_resource_path(resource)) when :delete link_to end end |
#resource_title ⇒ Object
Returns an unique title for a resource
14 15 16 |
# File 'app/helpers/power_resource/base_helper.rb', line 14 def resource_title "#{resource_human_name} #{resource.id}" end |