aboutsummaryrefslogtreecommitdiffstats
path: root/railties/lib
diff options
context:
space:
mode:
Diffstat (limited to 'railties/lib')
-rw-r--r--railties/lib/rails/configuration.rb1
-rw-r--r--railties/lib/rails/generators.rb2
-rw-r--r--railties/lib/rails/generators/rails/resource/resource_generator.rb6
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb6
-rw-r--r--railties/lib/rails/generators/rails/scaffold_controller/templates/http_controller.rb60
-rw-r--r--railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb7
-rw-r--r--railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb50
7 files changed, 128 insertions, 4 deletions
diff --git a/railties/lib/rails/configuration.rb b/railties/lib/rails/configuration.rb
index bce696c8f9..0fb463c44d 100644
--- a/railties/lib/rails/configuration.rb
+++ b/railties/lib/rails/configuration.rb
@@ -142,6 +142,5 @@ module Rails
end
end
end
-
end
end
diff --git a/railties/lib/rails/generators.rb b/railties/lib/rails/generators.rb
index 8d66cd2fd3..3965e05823 100644
--- a/railties/lib/rails/generators.rb
+++ b/railties/lib/rails/generators.rb
@@ -46,6 +46,7 @@ module Rails
:assets => true,
:force_plural => false,
:helper => true,
+ :http => false,
:integration_tool => nil,
:javascripts => true,
:javascript_engine => :js,
@@ -115,6 +116,7 @@ module Rails
options[:rails].merge!(
:assets => false,
:helper => false,
+ :http => true,
:javascripts => false,
:javascript_engine => nil,
:stylesheets => false,
diff --git a/railties/lib/rails/generators/rails/resource/resource_generator.rb b/railties/lib/rails/generators/rails/resource/resource_generator.rb
index c7345f3cfb..11326388b4 100644
--- a/railties/lib/rails/generators/rails/resource/resource_generator.rb
+++ b/railties/lib/rails/generators/rails/resource/resource_generator.rb
@@ -14,10 +14,14 @@ module Rails
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
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
index 2271c6f9c1..17d462fa40 100644
--- a/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
+++ b/railties/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb
@@ -10,8 +10,12 @@ module Rails
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
diff --git a/railties/lib/rails/generators/rails/scaffold_controller/templates/http_controller.rb b/railties/lib/rails/generators/rails/scaffold_controller/templates/http_controller.rb
new file mode 100644
index 0000000000..3f44ac18a4
--- /dev/null
+++ b/railties/lib/rails/generators/rails/scaffold_controller/templates/http_controller.rb
@@ -0,0 +1,60 @@
+<% 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 -%>
diff --git a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
index 37b9f7ef7d..e875c81340 100644
--- a/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
+++ b/railties/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb
@@ -10,8 +10,13 @@ module TestUnit
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
diff --git a/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb b/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb
new file mode 100644
index 0000000000..5bb61cb263
--- /dev/null
+++ b/railties/lib/rails/generators/test_unit/scaffold/templates/http_functional_test.rb
@@ -0,0 +1,50 @@
+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 -%>