Module: Alchemy::ResourcesHelper
- Includes:
- Admin::ResourceName
- Included in:
- Admin::ResourcesController
- Defined in:
- app/helpers/alchemy/resources_helper.rb
Instance Method Summary collapse
-
#contains_relations? ⇒ Boolean
Returns true if the resource contains any relations.
-
#csv_value(value) ⇒ String
Returns the value prepared for a cell of a CSV export.
- #edit_resource_path(resource = nil, options = {}) ⇒ Object
- #new_resource_path(options = {}) ⇒ Object
-
#render_attribute(resource, attribute, options = {}) ⇒ String
Returns the value from resource attribute.
-
#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_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
Methods included from Admin::ResourceName
#controller_path_array, #resource_array, #resource_model_name, #resource_name, #resources_name
Instance Method Details
#contains_relations? ⇒ Boolean
Returns true if the resource contains any relations
144 145 146 |
# File 'app/helpers/alchemy/resources_helper.rb', line 144 def contains_relations? resource_handler.resource_relations.present? end |
#csv_value(value) ⇒ String
Returns the value prepared for a cell of a CSV export.
Spreadsheet applications evaluate a cell starting with one of these characters
as a formula, turning exported content into code that runs on the machine
opening the file. The tab keeps the cell text and, unlike the more common
single quote, survives Excel saving and re-opening the file, which strips
quotes and would reactivate the formula. It only works inside a quoted field,
so the export generates with force_quotes.
A bare negative number is exempt, because spreadsheets read it as a number
instead of a formula, so escaping it would only break arithmetic on the
exported column. An expression like -2+3 is a formula and stays escaped.
120 121 122 123 124 125 |
# File 'app/helpers/alchemy/resources_helper.rb', line 120 def csv_value(value) value = value.to_s return value if value.match?(/\A-\d+(\.\d+)?\z/) value.match?(/\A[-=+@\t\r\n\uFF0D\uFF1D\uFF0B\uFF20]/) ? "\t#{value}" : value end |
#edit_resource_path(resource = nil, options = {}) ⇒ Object
50 51 52 53 |
# File 'app/helpers/alchemy/resources_helper.rb', line 50 def edit_resource_path(resource = nil, = {}) path_segments = resource_scope + [resource] || resource_array edit_polymorphic_path path_segments, end |
#new_resource_path(options = {}) ⇒ Object
46 47 48 |
# File 'app/helpers/alchemy/resources_helper.rb', line 46 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.
74 75 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 |
# File 'app/helpers/alchemy/resources_helper.rb', line 74 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 |
#resource_attribute_field_options(attribute) ⇒ Object
Returns a options hash for simple_form input fields.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'app/helpers/alchemy/resources_helper.rb', line 128 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
153 154 155 |
# File 'app/helpers/alchemy/resources_helper.rb', line 153 def resource_model.respond_to?(:tag_counts) && resource_model.tag_counts.any? end |
#resource_instance_variable ⇒ Object
18 19 20 |
# File 'app/helpers/alchemy/resources_helper.rb', line 18 def resource_instance_variable instance_variable_get(:"@#{resource_name}") end |
#resource_model ⇒ Object
55 56 57 |
# File 'app/helpers/alchemy/resources_helper.rb', line 55 def resource_model resource_handler.model end |
#resource_path(resource = resource_handler.namespaced_resource_name, options = {}) ⇒ Object
42 43 44 |
# File 'app/helpers/alchemy/resources_helper.rb', line 42 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
149 150 151 |
# File 'app/helpers/alchemy/resources_helper.rb', line 149 def resource_relations_names resource_handler.resource_relations.collect { |_k, v| v[:name].to_sym } end |
#resource_scope ⇒ Object
34 35 36 |
# File 'app/helpers/alchemy/resources_helper.rb', line 34 def resource_scope @_resource_scope ||= [resource_url_proxy].concat(resource_handler.namespace_for_scope) end |
#resource_url_proxy ⇒ Object
26 27 28 29 30 31 32 |
# File 'app/helpers/alchemy/resources_helper.rb', line 26 def resource_url_proxy if resource_handler.in_engine? public_send(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
14 15 16 |
# File 'app/helpers/alchemy/resources_helper.rb', line 14 def resource_window_size @resource_window_size ||= "480x#{100 + resource_handler.attributes.length * 40}" end |
#resources_instance_variable ⇒ Object
22 23 24 |
# File 'app/helpers/alchemy/resources_helper.rb', line 22 def resources_instance_variable instance_variable_get(:"@#{resources_name}") end |
#resources_path(resource_or_name = resource_handler.namespaced_resources_name, options = {}) ⇒ Object
38 39 40 |
# File 'app/helpers/alchemy/resources_helper.rb', line 38 def resources_path(resource_or_name = resource_handler.namespaced_resources_name, = {}) polymorphic_path (resource_scope + [resource_or_name]), end |