Module: Alchemy::ResourcesHelper
- Included in:
- Admin::ResourcesController
- Defined in:
- lib/alchemy/resources_helper.rb
Instance Method Summary collapse
-
#contains_relations? ⇒ Boolean
Returns true if the resource contains any relations.
- #edit_resource_path(resource = nil, options = {}) ⇒ Object
- #new_resource_path(options = {}) ⇒ Object
-
#render_attribute(resource, attribute, options = {}) ⇒ String
Returns the value from resource attribute.
-
#render_resources(icon: nil) ⇒ Object
Renders the row for a resource record in the resources table.
-
#resource_attribute_field_options(attribute) ⇒ Object
Returns a options hash for simple_form input fields.
- #resource_has_tags ⇒ Object
- #resource_instance_variable ⇒ Object
- #resource_model ⇒ Object
- #resource_name ⇒ Object
- #resource_path(resource = resource_handler.namespaced_resource_name, options = {}) ⇒ Object
-
#resource_relations_names ⇒ Object
Returns an array of all resource_relations names.
- #resource_scope ⇒ Object
- #resource_url_proxy ⇒ Object
-
#resource_window_size ⇒ Object
Alchemy::ResourceHelper.
- #resources_instance_variable ⇒ Object
- #resources_path(resource_or_name = resource_handler.namespaced_resources_name, options = {}) ⇒ Object
-
#sortable_resource_header_column(attribute) ⇒ Object
Returns the attribute’s column for sorting.
Instance Method Details
#contains_relations? ⇒ Boolean
Returns true if the resource contains any relations
121 122 123 |
# File 'lib/alchemy/resources_helper.rb', line 121 def contains_relations? resource_handler.resource_relations.present? end |
#edit_resource_path(resource = nil, options = {}) ⇒ Object
48 49 50 51 |
# File 'lib/alchemy/resources_helper.rb', line 48 def edit_resource_path(resource = nil, = {}) path_segments = resource_scope + [resource] || resource_handler.resource_array edit_polymorphic_path path_segments, end |
#new_resource_path(options = {}) ⇒ Object
44 45 46 |
# File 'lib/alchemy/resources_helper.rb', line 44 def new_resource_path( = {}) new_polymorphic_path (resource_scope + [resource_handler.namespaced_resource_name]), end |
#render_attribute(resource, attribute, options = {}) ⇒ String
Returns the value from resource attribute
If the attribute has a relation, the related object’s attribute value will be returned.
The output will be truncated after 50 chars. Pass another number to truncate then and pass false to disable this completely.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/alchemy/resources_helper.rb', line 76 def render_attribute(resource, attribute, = {}) attribute_value = resource.send(attribute[:name]) if attribute[:relation] record = resource.send(attribute[:relation][:name]) value = record.present? ? record.send(attribute[:relation][:attr_method]) : Alchemy.t(:not_found) elsif attribute_value && attribute[:type].to_s =~ /(date|time)/ localization_format = if attribute[:type] == :datetime [:datetime_format] || :"alchemy.default" elsif attribute[:type] == :date [:date_format] || :"alchemy.default" else [:time_format] || :"alchemy.time" end value = l(attribute_value, format: localization_format) elsif attribute[:type] == :boolean value = attribute_value ? '<alchemy-icon name="check"></alchemy-icon>'.html_safe : nil else value = attribute_value end .reverse_merge!(truncate: 50) if [:truncate] value.to_s.truncate(.fetch(:truncate, 50)) else value end end |
#render_resources(icon: nil) ⇒ Object
Renders the row for a resource record in the resources table.
This helper has a nice fallback. If you create a partial for your record then this partial will be rendered.
Otherwise the default app/views/alchemy/admin/resources/_resource.html.erb
partial gets rendered.
Example
For a resource named Comment
you can create a partial named _comment.html.erb
# app/views/admin/comments/_comment.html.erb
<tr>
<td><%= comment.title %></td>
<td><%= comment.body %></td>
</tr>
NOTE: Alchemy gives you a local variable named like your resource
160 161 162 163 164 |
# File 'lib/alchemy/resources_helper.rb', line 160 def render_resources(icon: nil) render partial: resource_name, collection: resources_instance_variable, locals: {icon: icon} rescue ActionView::MissingTemplate render partial: "resource", collection: resources_instance_variable, locals: {icon: icon} end |
#resource_attribute_field_options(attribute) ⇒ Object
Returns a options hash for simple_form input fields.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/alchemy/resources_helper.rb', line 105 def (attribute) = {hint: resource_handler.help_text_for(attribute)} input_type = attribute[:type].to_s case input_type when "boolean" when "date", "time", "datetime" .merge(as: input_type) when "text" .merge(as: "text", input_html: {rows: 4}) else .merge(as: "string") end end |
#resource_has_tags ⇒ Object
166 167 168 |
# File 'lib/alchemy/resources_helper.rb', line 166 def resource_model.respond_to?(:tag_counts) && resource_model.tag_counts.any? end |
#resource_instance_variable ⇒ Object
16 17 18 |
# File 'lib/alchemy/resources_helper.rb', line 16 def resource_instance_variable instance_variable_get(:"@#{resource_handler.resource_name}") end |
#resource_model ⇒ Object
57 58 59 |
# File 'lib/alchemy/resources_helper.rb', line 57 def resource_model resource_handler.model end |
#resource_name ⇒ Object
53 54 55 |
# File 'lib/alchemy/resources_helper.rb', line 53 def resource_name resource_handler.resource_name end |
#resource_path(resource = resource_handler.namespaced_resource_name, options = {}) ⇒ Object
40 41 42 |
# File 'lib/alchemy/resources_helper.rb', line 40 def resource_path(resource = resource_handler.namespaced_resource_name, = {}) resources_path(resource, ) end |
#resource_relations_names ⇒ Object
Returns an array of all resource_relations names
126 127 128 |
# File 'lib/alchemy/resources_helper.rb', line 126 def resource_relations_names resource_handler.resource_relations.collect { |_k, v| v[:name].to_sym } end |
#resource_scope ⇒ Object
32 33 34 |
# File 'lib/alchemy/resources_helper.rb', line 32 def resource_scope @_resource_scope ||= [resource_url_proxy].concat(resource_handler.namespace_for_scope) end |
#resource_url_proxy ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/alchemy/resources_helper.rb', line 24 def resource_url_proxy if resource_handler.in_engine? eval(resource_handler.engine_name) # rubocop:disable Security/Eval else main_app end end |
#resource_window_size ⇒ Object
Alchemy::ResourceHelper
Used to DRY up resource like structures in Alchemy’s admin backend in combination with Alchemy::Resource
See Alchemy::Resource for examples how to initialize a resource_handler
12 13 14 |
# File 'lib/alchemy/resources_helper.rb', line 12 def resource_window_size @resource_window_size ||= "480x#{100 + resource_handler.attributes.length * 40}" end |
#resources_instance_variable ⇒ Object
20 21 22 |
# File 'lib/alchemy/resources_helper.rb', line 20 def resources_instance_variable instance_variable_get(:"@#{resource_handler.resources_name}") end |
#resources_path(resource_or_name = resource_handler.namespaced_resources_name, options = {}) ⇒ Object
36 37 38 |
# File 'lib/alchemy/resources_helper.rb', line 36 def resources_path(resource_or_name = resource_handler.namespaced_resources_name, = {}) polymorphic_path (resource_scope + [resource_or_name]), end |
#sortable_resource_header_column(attribute) ⇒ Object
Returns the attribute’s column for sorting
If the attribute contains a resource_relation, then the table and column for related model will be returned.
134 135 136 137 138 139 140 |
# File 'lib/alchemy/resources_helper.rb', line 134 def sortable_resource_header_column(attribute) if (relation = attribute[:relation]) "#{relation[:model_association].name}_#{relation[:attr_method]}" else attribute[:name] end end |