Sha256: a04574e8ea68f2792c557f8e2f6a677ad72e6282a739b382cde195d209209b25
Contents?: true
Size: 1.42 KB
Versions: 12
Compression:
Stored size: 1.42 KB
Contents
module DiscoApp::Concerns::CanBeLiquified extend ActiveSupport::Concern SPLIT_ARRAY_SEPARATOR = '@!@' included do # Return this model as a hash for use with `to_liquid`. Returns `as_json` by default but is provided here as a hook # for potential overrides. def as_liquid as_json end # Render this model as a series of concatenated Liquid {%- assign -%} statements. def to_liquid as_liquid.map { |k, v| "{%- assign #{liquid_model_name}_#{k} = #{as_liquid_value(k, v)} -%}" }.join("\n") end # Method to allow override of the model name in Liquid. Useful for models # residing in namespaces that would otherwise have very long prefixes. def liquid_model_name model_name.param_key end private # Return given value as a string expression that will be evaluated in Liquid to result in the correct value type. def as_liquid_value(key, value) if value.is_a? Numeric or (!!value == value) return value.to_s end if value.nil? return 'nil' end if value.is_a? Array return "'#{value.map { |e| (e.is_a? String) ? CGI::escapeHTML(e) : e }.join(SPLIT_ARRAY_SEPARATOR)}' | split: '#{SPLIT_ARRAY_SEPARATOR}'" end if value.is_a? String and key.end_with? '_html' return "'#{value.to_s.gsub("'", "'")}'" end "'#{CGI::escapeHTML(value.to_s)}'" end end end
Version data entries
12 entries across 12 versions & 1 rubygems