提交 570cc89b 编写于 作者: C Carlos Antonio da Silva 提交者: Santiago Pastorino

Generate special controller and functional test templates for http apps

The main goal is to not generate the format.html block in scaffold
controller, and to generate a different functional test as we don't rely
on redirects anymore, we should test for http responses.

In addition to that, the :edit action is removed from the http
controller and the edit route is not generated by default, as they
usually do not make sense in this scenario.

[Carlos Antonio da Silva & Santiago Pastorino]
上级 3e138df9
......@@ -142,6 +142,5 @@ def method_missing(method, *args)
end
end
end
end
end
......@@ -46,6 +46,7 @@ module Generators
:assets => true,
:force_plural => false,
:helper => true,
:http => false,
:integration_tool => nil,
:javascripts => true,
:javascript_engine => :js,
......@@ -115,6 +116,7 @@ def self.http_only!
options[:rails].merge!(
:assets => false,
:helper => false,
:http => true,
:javascripts => false,
:javascript_engine => nil,
:stylesheets => false,
......
......@@ -14,10 +14,14 @@ class ResourceGenerator < ModelGenerator #metagenerator
class_option :actions, :type => :array, :banner => "ACTION ACTION", :default => [],
:desc => "Actions for the resource controller"
class_option :http, :type => :boolean, :default => false,
:desc => "Generate resource with HTTP actions only"
def add_resource_route
return if options[:actions].present?
route_config = regular_class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ")
route_config = regular_class_path.collect{ |namespace| "namespace :#{namespace} do " }.join(" ")
route_config << "resources :#{file_name.pluralize}"
route_config << ", except: :edit" if options.http?
route_config << " end" * regular_class_path.size
route route_config
end
......
......@@ -10,8 +10,12 @@ class ScaffoldControllerGenerator < NamedBase
class_option :orm, :banner => "NAME", :type => :string, :required => true,
:desc => "ORM to generate the controller for"
class_option :http, :type => :boolean, :default => false,
:desc => "Generate controller with HTTP actions only"
def create_controller_files
template 'controller.rb', File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
template_file = options.http? ? "http_controller.rb" : "controller.rb"
template template_file, File.join('app/controllers', class_path, "#{controller_file_name}_controller.rb")
end
hook_for :template_engine, :test_framework, :as => :scaffold
......
<% module_namespacing do -%>
class <%= controller_class_name %>Controller < ApplicationController
# GET <%= route_url %>
# GET <%= route_url %>.json
def index
@<%= plural_table_name %> = <%= orm_class.all(class_name) %>
render json: @<%= plural_table_name %>
end
# GET <%= route_url %>/1
# GET <%= route_url %>/1.json
def show
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
render json: @<%= singular_table_name %>
end
# GET <%= route_url %>/new
# GET <%= route_url %>/new.json
def new
@<%= singular_table_name %> = <%= orm_class.build(class_name) %>
render json: @<%= singular_table_name %>
end
# POST <%= route_url %>
# POST <%= route_url %>.json
def create
@<%= singular_table_name %> = <%= orm_class.build(class_name, "params[:#{singular_table_name}]") %>
if @<%= orm_instance.save %>
render json: @<%= singular_table_name %>, status: :created, location: @<%= singular_table_name %>
else
render json: @<%= orm_instance.errors %>, status: :unprocessable_entity
end
end
# PATCH/PUT <%= route_url %>/1
# PATCH/PUT <%= route_url %>/1.json
def update
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
if @<%= orm_instance.update_attributes("params[:#{singular_table_name}]") %>
head :no_content
else
render json: @<%= orm_instance.errors %>, status: :unprocessable_entity
end
end
# DELETE <%= route_url %>/1
# DELETE <%= route_url %>/1.json
def destroy
@<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
@<%= orm_instance.destroy %>
head :no_content
end
end
<% end -%>
......@@ -10,8 +10,13 @@ class ScaffoldGenerator < Base
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
class_option :http, :type => :boolean, :default => false,
:desc => "Generate functional test with HTTP actions only"
def create_test_files
template 'functional_test.rb',
template_file = options.http? ? "http_functional_test.rb" : "functional_test.rb"
template template_file,
File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb")
end
......
require 'test_helper'
<% module_namespacing do -%>
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
setup do
@<%= singular_table_name %> = <%= table_name %>(:one)
@request.accept = "application/json"
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:<%= table_name %>)
end
test "should get new" do
get :new
assert_response :success
end
test "should create <%= singular_table_name %>" do
assert_difference('<%= class_name %>.count') do
post :create, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
end
assert_response 201
assert_not_nil assigns(:<%= singular_table_name %>)
end
test "should show <%= singular_table_name %>" do
get :show, id: @<%= singular_table_name %>
assert_response :success
end
test "should update <%= singular_table_name %>" do
put :update, id: @<%= singular_table_name %>, <%= "#{singular_table_name}: { #{attributes_hash} }" %>
assert_response 204
assert_not_nil assigns(:<%= singular_table_name %>)
end
test "should destroy <%= singular_table_name %>" do
assert_difference('<%= class_name %>.count', -1) do
delete :destroy, id: @<%= singular_table_name %>
end
assert_response 204
assert_not_nil assigns(:<%= singular_table_name %>)
end
end
<% end -%>
......@@ -86,4 +86,10 @@ def test_route_is_removed_on_revoke
assert_no_match(/resources :accounts$/, route)
end
end
def test_http_only_does_not_generate_edit_route
run_generator ["Account", "--http"]
assert_file "config/routes.rb", /resources :accounts, except: :edit$/
end
end
......@@ -142,4 +142,41 @@ def test_new_hash_style
assert_match(/\{ render action: "new" \}/, content)
end
end
def test_http_only_generates_controller_without_respond_to_block_and_html_format
run_generator ["User", "--http"]
assert_file "app/controllers/users_controller.rb" do |content|
assert_match(/render json: @user/, content)
assert_no_match(/respond_to/, content)
assert_no_match(/format\.html/, content)
end
end
def test_http_only_generates_functional_tests_with_json_format_and_http_status_assertions
run_generator ["User", "--http"]
assert_file "test/functional/users_controller_test.rb" do |content|
assert_match(/class UsersControllerTest < ActionController::TestCase/, content)
assert_match(/@request\.accept = "application\/json"/, content)
assert_match(/test "should get index"/, content)
assert_match(/assert_response 201/, content)
assert_no_match(/assert_redirected_to/, content)
end
end
def test_http_only_does_not_generate_edit_action
run_generator ["User", "--http"]
assert_file "app/controllers/users_controller.rb" do |content|
assert_match(/def index/, content)
assert_no_match(/def edit/, content)
end
assert_file "test/functional/users_controller_test.rb" do |content|
assert_match(/test "should get index"/, content)
assert_no_match(/test "should get edit"/, content)
end
end
end
......@@ -229,6 +229,16 @@ def test_http_only_hides_generators
end
end
def test_http_only_enables_http_option
options = Rails::Generators.options[:rails]
assert !options[:http], "http option should be disabled by default"
with_http_only! do
assert options[:http], "http only should enable generator http option"
end
end
def test_http_only_disables_template_and_helper_and_assets_options
options = Rails::Generators.options[:rails]
disable_options = [:assets, :helper, :javascripts, :javascript_engine,
......@@ -251,7 +261,6 @@ def with_http_only!
Rails::Generators.http_only!
yield
ensure
Rails::Generators.instance_variable_set(:@http_only, false)
Rails::Generators.instance_variable_set(:@hidden_namespaces, nil)
Rails::Generators.instance_variable_set(:@options, nil)
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册