Module: Noa::Resource::BaseHelper
- Defined in:
- app/helpers/noa/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 = "form_for") ⇒ 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
46 47 48 49 50 |
# File 'app/helpers/noa/resource/base_helper.rb', line 46 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
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/helpers/noa/resource/base_helper.rb', line 53 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
20 21 22 23 |
# File 'app/helpers/noa/resource/base_helper.rb', line 20 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
31 32 33 |
# File 'app/helpers/noa/resource/base_helper.rb', line 31 def non_human_attributes %w(id updated_at created_at) end |
#render_actions_for(resource) ⇒ Object
Renders action links for a resource
127 128 129 |
# File 'app/helpers/noa/resource/base_helper.rb', line 127 def render_actions_for(resource) render "actions", resource: resource end |
#render_collection_table(custom_attributes = nil) ⇒ Object
Renders collection table
120 121 122 123 124 |
# File 'app/helpers/noa/resource/base_helper.rb', line 120 def render_collection_table(custom_attributes = nil) render "collection", collection: collection, attributes: custom_attributes || resource_human_attributes end |
#render_form(form_builder = "form_for") ⇒ Object
Renders form using selected form builder
111 112 113 114 115 116 117 |
# File 'app/helpers/noa/resource/base_helper.rb', line 111 def render_form(form_builder = "form_for") fields = resource_human_attributes fields.map! do |arg| arg.to_s.sub("_id", "").to_sym end render "noa/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"
72 73 74 75 76 |
# File 'app/helpers/noa/resource/base_helper.rb', line 72 def resource_action(action_name) I18n.t("noa.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
26 27 28 |
# File 'app/helpers/noa/resource/base_helper.rb', line 26 def resource_attributes resource_class.attribute_names end |
#resource_form_path ⇒ Object
Returns form path for a resource
106 107 108 |
# File 'app/helpers/noa/resource/base_helper.rb', line 106 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
36 37 38 39 40 41 42 43 |
# File 'app/helpers/noa/resource/base_helper.rb', line 36 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
5 6 7 |
# File 'app/helpers/noa/resource/base_helper.rb', line 5 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
10 11 12 |
# File 'app/helpers/noa/resource/base_helper.rb', line 10 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>
90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'app/helpers/noa/resource/base_helper.rb', line 90 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
15 16 17 |
# File 'app/helpers/noa/resource/base_helper.rb', line 15 def resource_title "#{resource_human_name} #{resource.id}" end |