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.
-
#current_location_params ⇒ Object
Returns all the params necessary to get you back from where you where before: the Ransack query and the current page.
- #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 ⇒ 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_filter_select ⇒ Object
- #resource_has_filters ⇒ Object
- #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_header ⇒ Object
Renders the human model name with a count as h1 header.
- #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
117 118 119 |
# File 'lib/alchemy/resources_helper.rb', line 117 def contains_relations? resource_handler.resource_relations.present? end |
#current_location_params ⇒ Object
Returns all the params necessary to get you back from where you where before: the Ransack query and the current page.
165 166 167 168 169 170 171 172 |
# File 'lib/alchemy/resources_helper.rb', line 165 def current_location_params { q: params[:q], page: params[:page], tagged_with: params[:tagged_with], filter: params[:filter] } end |
#edit_resource_path(resource = nil, options = {}) ⇒ Object
46 47 48 49 |
# File 'lib/alchemy/resources_helper.rb', line 46 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
42 43 44 |
# File 'lib/alchemy/resources_helper.rb', line 42 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.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/alchemy/resources_helper.rb', line 72 def render_attribute(resource, attribute, = {}) .reverse_merge!(truncate: 50) value = resource.send(attribute[:name]) if (relation = attribute[:relation]) && value.present? record = relation[:model_association].klass.find(value) value = record.send(relation[:attr_method]) elsif attribute[:type] == :datetime && value.present? value = l(value) end if [:truncate] value = value.to_s.truncate([:truncate]) end value rescue ActiveRecord::RecordNotFound => e warning e Alchemy.t(:not_found) end |
#render_resources ⇒ 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
156 157 158 159 160 |
# File 'lib/alchemy/resources_helper.rb', line 156 def render_resources render partial: resource_name, collection: resources_instance_variable rescue ActionView::MissingTemplate render partial: 'resource', collection: resources_instance_variable end |
#resource_attribute_field_options(attribute) ⇒ Object
Returns a options hash for simple_form input fields.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/alchemy/resources_helper.rb', line 91 def (attribute) = {hint: resource_handler.help_text_for(attribute)} case attribute[:type].to_s when 'boolean' when 'date', 'datetime' .merge as: 'string', input_html: { type: 'date', value: l(resource_instance_variable.send(attribute[:name]) || Time.current, format: :datepicker) } when 'time' .merge(as: 'time') when 'text' .merge(as: 'text', input_html: {rows: 4}) else .merge(as: 'string') end end |
#resource_filter_select ⇒ Object
182 183 184 185 186 187 188 189 |
# File 'lib/alchemy/resources_helper.rb', line 182 def resource_filter_select resource_model.alchemy_resource_filters.map do |filter_scope| [ Alchemy.t(filter_scope.to_sym, scope: ['resources', resource_name, 'filters']), filter_scope ] end end |
#resource_has_filters ⇒ Object
178 179 180 |
# File 'lib/alchemy/resources_helper.rb', line 178 def resource_has_filters resource_model.respond_to?(:alchemy_resource_filters) end |
#resource_has_tags ⇒ Object
174 175 176 |
# File 'lib/alchemy/resources_helper.rb', line 174 def resource_model.respond_to?(:tag_counts) && resource_model.tag_counts.any? end |
#resource_instance_variable ⇒ Object
14 15 16 |
# File 'lib/alchemy/resources_helper.rb', line 14 def resource_instance_variable instance_variable_get("@#{resource_handler.resource_name}") end |
#resource_model ⇒ Object
55 56 57 |
# File 'lib/alchemy/resources_helper.rb', line 55 def resource_model resource_handler.model end |
#resource_name ⇒ Object
51 52 53 |
# File 'lib/alchemy/resources_helper.rb', line 51 def resource_name resource_handler.resource_name end |
#resource_path(resource = resource_handler.namespaced_resource_name, options = {}) ⇒ Object
38 39 40 |
# File 'lib/alchemy/resources_helper.rb', line 38 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
122 123 124 |
# File 'lib/alchemy/resources_helper.rb', line 122 def resource_relations_names resource_handler.resource_relations.collect { |_k, v| v[:name].to_sym } end |
#resource_scope ⇒ Object
30 31 32 |
# File 'lib/alchemy/resources_helper.rb', line 30 def resource_scope @_resource_scope ||= [resource_url_proxy].concat(resource_handler.namespace_for_scope) end |
#resource_url_proxy ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/alchemy/resources_helper.rb', line 22 def resource_url_proxy if resource_handler.in_engine? eval(resource_handler.engine_name) 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
10 11 12 |
# File 'lib/alchemy/resources_helper.rb', line 10 def resource_window_size @resource_window_size ||= "420x#{100 + resource_handler.attributes.length * 40}" end |
#resources_header ⇒ Object
Renders the human model name with a count as h1 header
112 113 114 |
# File 'lib/alchemy/resources_helper.rb', line 112 def resources_header content_tag :h1, "#{resources_instance_variable.total_count} #{resource_model.model_name.human(count: resources_instance_variable.total_count)}", class: 'resources-header' end |
#resources_instance_variable ⇒ Object
18 19 20 |
# File 'lib/alchemy/resources_helper.rb', line 18 def resources_instance_variable instance_variable_get("@#{resource_handler.resources_name}") end |
#resources_path(resource_or_name = resource_handler.namespaced_resources_name, options = {}) ⇒ Object
34 35 36 |
# File 'lib/alchemy/resources_helper.rb', line 34 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.
130 131 132 133 134 135 136 |
# File 'lib/alchemy/resources_helper.rb', line 130 def sortable_resource_header_column(attribute) if relation = attribute[:relation] "#{relation[:model_association].name}_#{relation[:attr_method]}" else attribute[:name] end end |