class Jcompiler def self.b(brand) %w(id name description).map do |attr| v = eval("brand.#{attr}") "\"#{attr}\":#{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