Permalink
Browse files

Override rails g resource to generate Ember stuff

* disable asset generators
* generate a properly annotated model
* generate a singular view/controller
* generate a plural view/controller
  • Loading branch information...
wycats committed Jun 5, 2012
1 parent 31e2ddf commit 82d35eb582d4a0a73613bba61b92613484a03a62
View
@@ -8,6 +8,16 @@ module Rails
class Railtie < ::Rails::Railtie
config.ember = ActiveSupport::OrderedOptions.new
generators do |app|
app ||= ::Rails.application # Rails 3.0.x does not yield `app`
app.config.generators.assets = false
::Rails::Generators.configure!(app.config.generators)
::Rails::Generators.hidden_namespaces.uniq!
require "generators/ember/resource_override"
end
initializer "ember_rails.setup_vendor", :after => "ember_rails.setup", :group => :all do |app|
# Add the gem's vendored ember to the end of the asset search path
variant = app.config.ember.variant
@@ -5,22 +5,22 @@ module Generators
class ModelGenerator < ::Rails::Generators::NamedBase
source_root File.expand_path("../../templates", __FILE__)
argument :attributes, :type => :array, :default => [], :banner => "field[:type] field[:type] ..."
desc "Creates a new Ember.js model"
def create_model_files
template 'model.js', File.join('app/assets/javascripts/models', class_path, "#{file_name}.js")
end
private
private
def parse_attributes!
self.attributes = (attributes || []).map do |attr|
attr = attr.split(':')
{ name: attr[0], type: attr[1] }
name, type = attr.split(':')
type = 'string' if type == 'text'
{ name: name, type: type }
end
end
end
end
end
@@ -0,0 +1,32 @@
require "rails/generators"
require "rails/generators/rails/resource/resource_generator"
require "generators/ember/controller_generator"
require "generators/ember/view_generator"
module Rails
module Generators
ResourceGenerator.class_eval do
def add_ember
say_status :invoke, "ember:model", :white
with_padding do
invoke "ember:model"
end
say_status :invoke, "ember controller and view (singular)", :white
with_padding do
invoke "ember:view"
end
@_invocations[Ember::Generators::ControllerGenerator].delete "create_controller_files"
@_invocations[Ember::Generators::ViewGenerator].delete "create_view_files"
say_status :invoke, "ember controller and view (plural)", :white
with_padding do
invoke "ember:view", [plural_name], :array => true
end
end
end
end
end
@@ -11,8 +11,7 @@ class ViewGenerator < ::Rails::Generators::NamedBase
def create_view_files
template 'view.js', File.join('app/assets/javascripts/views', class_path, "#{file_name}_view.js")
template 'view.handlebars', File.join('app/assets/javascripts/templates', class_path, "#{file_name}.handlebars")
generate 'ember:controller', file_name, "--array" if options[:array]
generate 'ember:controller', file_name unless options[:array]
invoke('ember:controller', [ file_name ], options)
end
end

0 comments on commit 82d35eb

Please sign in to comment.