# frozen_string_literal: true
module Alchemy
module ResourcesHelper
# = 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
#
def resource_window_size
@resource_window_size ||= "480x#{100 + resource_handler.attributes.length * 40}"
end
def resource_instance_variable
instance_variable_get(:"@#{resource_handler.resource_name}")
end
def resources_instance_variable
instance_variable_get(:"@#{resource_handler.resources_name}")
end
def resource_url_proxy
if resource_handler.in_engine?
eval(resource_handler.engine_name) # rubocop:disable Security/Eval
else
main_app
end
end
def resource_scope
@_resource_scope ||= [resource_url_proxy].concat(resource_handler.namespace_for_scope)
end
def resources_path(resource_or_name = resource_handler.namespaced_resources_name, options = {})
polymorphic_path (resource_scope + [resource_or_name]), options
end
def resource_path(resource = resource_handler.namespaced_resource_name, options = {})
resources_path(resource, options)
end
def new_resource_path(options = {})
new_polymorphic_path (resource_scope + [resource_handler.namespaced_resource_name]), options
end
def edit_resource_path(resource = nil, options = {})
path_segments = resource_scope + [resource] || resource_handler.resource_array
edit_polymorphic_path path_segments, options
end
def resource_name
resource_handler.resource_name
end
def resource_model
resource_handler.model
end
# 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.
#
# @param [Alchemy::Resource] resource
# @param [Hash] attribute
# @option options [Hash] :truncate (50) The length of the value returned.
# @option options [Hash] :datetime_format (alchemy.default) The format of timestamps.
# @option options [Hash] :time_format (alchemy.time) The format of time values.
#
# @return [String]
#
def render_attribute(resource, attribute, options = {})
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
options[:datetime_format] || :"alchemy.default"
elsif attribute[:type] == :date
options[:date_format] || :"alchemy.default"
else
options[:time_format] || :"alchemy.time"
end
value = l(attribute_value, format: localization_format)
elsif attribute[:type] == :boolean
value = attribute_value ? '