Skip to content

Instantly share code, notes, and snippets.

Created February 6, 2015 20:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5742311bc8bb4ac5285e to your computer and use it in GitHub Desktop.
Save anonymous/5742311bc8bb4ac5285e to your computer and use it in GitHub Desktop.
Rails::Generator::Commands::Create.class_eval do
def template(relative_source, relative_destination, template_options = {})
file(relative_source, relative_destination, template_options) do |file|
# Evaluate any assignments in a temporary, throwaway binding.
vars = template_options[:assigns] || {}
b = template_options[:binding] || binding
# this no longer works, eval throws "undefined local variable or method `vars'"
# vars.each { |k,v| eval "#{k} = vars[:#{k}] || vars['#{k}']", b }
vars.each { |k,v| b.local_variable_set(:"#{k}", v) }
# Render the source file with the temporary binding.
ERB.new(file.read, nil, '-').result(b)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment