class Jcompiler def self.b(brand) { id: 'brand.id', name: 'brand.name', description: 'brand.description', published_at: 'brand.published_at.utc.iso8601(3)', official_url: 'brand.blog_url', sex_group: 'brand.blog_sex_switch', # image_medium: 'absolute_url(brand.image_file.url(:medium))', # image_square: 'absolute_url(brand.image_file.url(:square))', # url: brand_ #labels: '"#{brand.new_arrival? ? \'["NEW"]\' : \'[]\'}"' }.map do |key, code| v = eval(code) "\"#{key}\":#{v.is_a?(String) ? ActiveSupport::JSON.encode(v) : v ? v.to_s : 'null'}" end end def self.a(brands) brands.map do |brand| "{#{b(brand).join(',')}}" end end def self.code(brands) "[#{a(brands).join(',')}]" end end class JcompilerHandler cattr_accessor :default_format self.default_format = Mime::JSON def self.call(template) "Jcompiler.code(@brands)" end end ActionView::Template.register_template_handler :jcompiler, JcompilerHandler