Skip to content

Instantly share code, notes, and snippets.

@lucascaton
Forked from anonymous/gist:5742311bc8bb4ac5285e
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucascaton/124421b621d9a9bb6a89 to your computer and use it in GitHub Desktop.
Save lucascaton/124421b621d9a9bb6a89 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