RailsCasts Pro episodes are now free!

Learn more or hide this

Svensk Lånemarknad's Profile

GitHub User: Svensklanemarknad

Site: lanemarknad.se

Comments by Svensk Lånemarknad

Avatar

Wouldn't it make more sense to create a $JsProduct class object in an initializer and then call new on $JsProduct in the for_mustache method?

config/initializers/js_product.rb

ruby
coffee = File.read(Rails.root.join("app/assets/javascripts/product.js.coffee"))
javascript = CoffeeScript.compile(coffee)
c = V8::Context.new
c.eval(javascript)
$JsProduct = c.eval("Product")

models/product.rb

ruby
def for_mustache
  $JsProduct.new(to_json)
end