提交 1f9d234a 编写于 作者: J José Valim

By default use ActiveModel API in controller generators, unless otherwise...

By default use ActiveModel API in controller generators, unless otherwise specified [#3123 status:resolved]
上级 03c5a0e5
......@@ -32,7 +32,7 @@ def initialize(name)
# GET index
def self.all(klass)
raise NotImplementedError
"#{klass}.all"
end
# GET show
......@@ -40,34 +40,38 @@ def self.all(klass)
# PUT update
# DELETE destroy
def self.find(klass, params=nil)
raise NotImplementedError
"#{klass}.find(#{params})"
end
# GET new
# POST create
def self.build(klass, params=nil)
raise NotImplementedError
if params
"#{klass}.new(#{params})"
else
"#{klass}.new"
end
end
# POST create
def save
raise NotImplementedError
"#{name}.save"
end
# PUT update
def update_attributes(params=nil)
raise NotImplementedError
"#{name}.update_attributes(#{params})"
end
# POST create
# PUT update
def errors
raise NotImplementedError
"#{name}.errors"
end
# DELETE destroy
def destroy
raise NotImplementedError
"#{name}.destroy"
end
end
end
......
......@@ -18,39 +18,5 @@ def next_migration_number(dirname) #:nodoc:
end
end
end
class ActiveModel < Rails::Generators::ActiveModel #:nodoc:
def self.all(klass)
"#{klass}.all"
end
def self.find(klass, params=nil)
"#{klass}.find(#{params})"
end
def self.build(klass, params=nil)
if params
"#{klass}.new(#{params})"
else
"#{klass}.new"
end
end
def save
"#{name}.save"
end
def update_attributes(params=nil)
"#{name}.update_attributes(#{params})"
end
def errors
"#{name}.errors"
end
def destroy
"#{name}.destroy"
end
end
end
end
require 'rails/generators/active_model'
module Rails
module Generators
# Deal with controller names on scaffold and add some helpers to deal with
......@@ -47,20 +49,11 @@ def orm_class
raise "You need to have :orm as class option to invoke orm_class and orm_instance"
end
active_model = "#{options[:orm].to_s.classify}::Generators::ActiveModel"
# If the orm was not loaded, try to load it at "generators/orm",
# for example "generators/active_record" or "generators/sequel".
begin
klass = active_model.constantize
rescue NameError
require "rails/generators/#{options[:orm]}"
"#{options[:orm].to_s.classify}::Generators::ActiveModel".constantize
rescue NameError => e
Rails::Generators::ActiveModel
end
# Try once again after loading the file with success.
klass ||= active_model.constantize
rescue Exception => e
raise Error, "Could not load #{active_model}, skipping controller. Error: #{e.message}."
end
end
......
......@@ -2,6 +2,11 @@
require 'generators/generators_test_helper'
require 'rails/generators/rails/scaffold_controller/scaffold_controller_generator'
module Unknown
module Generators
end
end
class ScaffoldControllerGeneratorTest < GeneratorsTestCase
def test_controller_skeleton_is_created
......@@ -97,10 +102,38 @@ def test_skip_layout_if_required
assert_no_file "app/views/layouts/users.html.erb"
end
def test_error_is_shown_if_orm_does_not_provide_interface
error = capture(:stderr){ run_generator ["User", "--orm=unknown"] }
assert_equal "Could not load Unknown::Generators::ActiveModel, skipping controller. " <<
"Error: no such file to load -- rails/generators/unknown.\n", error
def test_default_orm_is_used
run_generator ["User", "--orm=unknown"]
assert_file "app/controllers/users_controller.rb" do |content|
assert_match /class UsersController < ApplicationController/, content
assert_instance_method content, :index do |m|
assert_match /@users = User\.all/, m
end
end
end
def test_customized_orm_is_used
klass = Class.new(Rails::Generators::ActiveModel) do
def self.all(klass)
"#{klass}.find(:all)"
end
end
Unknown::Generators.const_set(:ActiveModel, klass)
run_generator ["User", "--orm=unknown"]
assert_file "app/controllers/users_controller.rb" do |content|
assert_match /class UsersController < ApplicationController/, content
assert_instance_method content, :index do |m|
assert_match /@users = User\.find\(:all\)/, m
assert_no_match /@users = User\.all/, m
end
end
ensure
Unknown::Generators.send :remove_const, :ActiveModel
end
protected
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册