Commit 13a79c3d by ivan Lan

Add spec generator into controller generator

parent b0dc8cc4
......@@ -54,12 +54,29 @@ module Shotengai
"app/controllers/#{@namespace}/#{@klass_name.underscore.pluralize}_controller.rb"
end
create_routes
create_factory
create_request_spec
require 'irb'
binding.irb
end
def create_routes
route (@role == 'merchant' ? merchant_routes : customer_routes)
end
def create_factory
Dir["#{self.class.source_root}/../spec/factories/*.rb"].each do |path|
template path, "spec/shotengai/#{path.match(/(.*)\/spec\/(.*)/)[2]}"
end
end
def create_request_spec
Dir["#{self.class.source_root}/../spec/requests/#{@role}/*.rb"].each do |path|
template path, "spec/shotengai/#{path.match(/(.*)\/spec\/(.*)/)[2]}"
end
end
def merchant_routes
product, order = @product.underscore, @order.underscore
"
......
module Shotengai
module Generators
class SpecGenerator < Rails::Generators::Base
source_root File.expand_path("../../templates/spec", __FILE__)
desc "Copy swagger spec of tempalte MVC to your application."
class_option :customer, type: :string, required: true,
desc: "Customer class name"
class_option :merchant, type: :string, required: true,
desc: "Merchant class name"
class_option :product, type: :string, required: true,
desc: "Product class name"
class_option :order, type: :string, required: true,
desc: "Order class name"
def copy_spec
@customer_class_name, @merchant_class_name, @product_class_name, @order_class_name =
options.values_at(:customer, :merchant, :product, :order)
Dir["#{self.class.source_root}/**/*.rb"].each do |path|
relative_path = path.gsub(self.class.source_root, '')
template path, "spec/shotengai/#{relative_path}"
end
end
end
end
end
FactoryGirl.define do
factory :merchant, '<%= @merhcant_class_name %>' do
factory :merchant, class_name: '' do
end
end
......@@ -28,7 +28,7 @@
#
FactoryGirl.define do
factory :order, class_name: '<%= @order_class_name %>' do
factory :order, class_name: '<%= @order %>' do
end
end
......@@ -26,7 +26,7 @@
#
FactoryGirl.define do
factory :product, class: '<%= @product_class_name %>' do
factory :product, class: '<%= @product %>' do
title 'Test Product Title'
# status
spec {
......
......@@ -20,7 +20,7 @@
#
FactoryGirl.define do
factory :product_series, class: '<%= "#{@product_class_name}Series" %>' do
factory :product_series, class: '<%= "#{@product}Series" %>' do
original_price 100
price 80
stock 10
......
......@@ -27,7 +27,7 @@
FactoryGirl.define do
factory :product_snapshot, class: '<%= "#{@product_class_name}Snapshot" %>' do
factory :product_snapshot, class: '<%= "#{@product}Snapshot" %>' do
original_price 100
price 80
count 2
......
FactoryGirl.define do
factory :user, class_name: '<%= @customer_class_name %>' do
factory :user, class_name: '' do
end
end
require 'swagger_helper'
namespace = '<%= @customer_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, tags: ["#{namespace} API", "order"] do
before do
@user = create(:user)
......
require 'swagger_helper'
namespace = '<%= @customer_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/products/:product_id/product_series", type: :request, capture_examples: true, tags: ["#{namespace} API", "product_series"] do
before do
@products = create_list(:product, 3)
......
require 'swagger_helper'
namespace = '<%= @customer_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/product_snapshots", type: :request, capture_examples: true, tags: ["#{namespace} API", "product_snapshots"] do
before do
@products = create_list(:product, 3)
......
require 'swagger_helper'
namespace = '<%= @customer_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/products", type: :request, capture_examples: true, tags: ["#{namespace} API", "product"] do
before do
class Catalog < Shotengai::Catalog; end
......
require 'swagger_helper'
namespace = '<%= @merchant_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/orders", type: :request, capture_examples: true, tags: ["#{namespace} API", "order"] do
before do
@products = create_list(:product, 3)
......
require 'swagger_helper'
namespace = '<%= @merchant_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/products/:product_id/product_series", type: :request, capture_examples: true, tags: ["#{namespace} API", "product_series"] do
before do
@clothes = Catalog.create!(name: '衣服')
......
require 'swagger_helper'
namespace = '<%= @merchant_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/product_snapshots", type: :request, capture_examples: true, tags: ["#{namespace} API", "product_snapshots"] do
before do
@products = create_list(:product, 3)
......
require 'swagger_helper'
namespace = '<%= @merchant_class_name.underscore %>'
namespace = '<%= @namespace %>'
RSpec.describe "#{namespace}/products", type: :request, capture_examples: true, tags: ["#{namespace} API", "product"] do
before do
@merchant = create(:merchant)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment