mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
rework generators
This commit is contained in:
parent
5686874496
commit
49f01d9086
17 changed files with 76 additions and 174 deletions
|
@ -1,46 +0,0 @@
|
|||
module Draper
|
||||
class DecoratorGenerator < Rails::Generators::Base
|
||||
desc <<-DESC
|
||||
Description:
|
||||
Generate a decorator for the given model.
|
||||
Example: rails g draper:decorator Article
|
||||
generates: "app/decorators/article_decorator"
|
||||
"spec/decorators/article_decorator_spec"
|
||||
DESC
|
||||
|
||||
argument :resource_name, :type => :string
|
||||
class_option "test-framework", :type => :string, :default => "rspec", :aliases => "-t", :desc => "Test framework to be invoked"
|
||||
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
DECORATORS_ROOT = 'app/decorators/'
|
||||
|
||||
def build_model_decorator
|
||||
template 'decorator.rb', "#{DECORATORS_ROOT}#{decorator_name}_decorator.rb"
|
||||
end
|
||||
|
||||
def build_decorator_tests
|
||||
case options["test-framework"]
|
||||
when "rspec"
|
||||
build_decorator_spec
|
||||
when "test_unit"
|
||||
build_decorator_test
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def build_decorator_spec
|
||||
empty_directory 'spec/decorators'
|
||||
template 'decorator_spec.rb', File.join('spec/decorators', "#{decorator_name}_decorator_spec.rb")
|
||||
end
|
||||
|
||||
def build_decorator_test
|
||||
empty_directory 'test/decorators/'
|
||||
template 'decorator_test.rb', File.join('test/decorators', "#{decorator_name}_decorator_test.rb")
|
||||
end
|
||||
|
||||
def decorator_name
|
||||
resource_name.underscore.singularize
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,4 +0,0 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe <%= resource_name.singularize.camelize %>Decorator do
|
||||
end
|
21
lib/generators/draper/decorator_generator.rb
Normal file
21
lib/generators/draper/decorator_generator.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Draper
|
||||
module Generators
|
||||
class DecoratorGenerator < Rails::Generators::NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
desc <<-DESC
|
||||
Description:
|
||||
Generate a decorator for the given model.
|
||||
Example: rails g draper:decorator Article
|
||||
generates: "app/decorators/article_decorator"
|
||||
"spec/decorators/article_decorator_spec"
|
||||
DESC
|
||||
|
||||
def create_decorator_file
|
||||
template 'decorator.rb', File.join('app/decorators', "#{singular_name}_decorator.rb")
|
||||
end
|
||||
|
||||
hook_for :test_framework
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,39 +0,0 @@
|
|||
module Draper
|
||||
class InstallGenerator < Rails::Generators::Base
|
||||
|
||||
desc <<-DESC
|
||||
Description:
|
||||
Generate application and spec decorators in your application.
|
||||
DESC
|
||||
|
||||
class_option "test-framework", :type => :string, :default => "rspec", :aliases => "-t", :desc => "Test framework to be invoked"
|
||||
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def build_application_decorator
|
||||
empty_directory 'app/decorators'
|
||||
template 'application_decorator.rb', File.join('app/decorators', 'application_decorator.rb')
|
||||
end
|
||||
|
||||
def build_decorator_tests
|
||||
case options["test-framework"]
|
||||
when "rspec"
|
||||
build_application_decorator_spec
|
||||
when "test_unit"
|
||||
build_application_decorator_test
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def build_application_decorator_spec
|
||||
empty_directory 'spec/decorators'
|
||||
template 'application_decorator_spec.rb', File.join('spec/decorators', 'application_decorator_spec.rb')
|
||||
end
|
||||
|
||||
def build_application_decorator_test
|
||||
empty_directory 'test/decorators/'
|
||||
template 'application_decorator_test.rb', File.join('test/decorators', 'application_decorator_test.rb')
|
||||
end
|
||||
|
||||
end
|
||||
end
|
20
lib/generators/draper/install_generator.rb
Normal file
20
lib/generators/draper/install_generator.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
module Draper
|
||||
module Generators
|
||||
class InstallGenerator < Rails::Generators::Base
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
desc <<-DESC
|
||||
Description:
|
||||
Generate application and spec decorators in your application.
|
||||
DESC
|
||||
|
||||
def create_decorator_file
|
||||
template 'application_decorator.rb', File.join('app/decorators', 'application_decorator.rb')
|
||||
end
|
||||
|
||||
hook_for :test_framework, :as => :decorator do |test_framework|
|
||||
invoke test_framework, ['application']
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,5 +1,5 @@
|
|||
class <%= resource_name.singularize.camelize %>Decorator < ApplicationDecorator
|
||||
decorates :<%= resource_name.singularize.underscore.to_sym %>
|
||||
class <%= singular_name.camelize %>Decorator < ApplicationDecorator
|
||||
decorates :<%= singular_name %>
|
||||
|
||||
# Accessing Helpers
|
||||
# You can access any helper via a proxy
|
|
@ -1,15 +0,0 @@
|
|||
require File.expand_path('../../draper/decorator/decorator_generator.rb', __FILE__)
|
||||
class Rails::DecoratorGenerator < Draper::DecoratorGenerator
|
||||
|
||||
source_root File.expand_path('../../draper/decorator/templates', __FILE__)
|
||||
|
||||
class_option :invoke_after_finished, :type => :string, :description => "Generator to invoke when finished"
|
||||
|
||||
def build_model_and_application_decorators
|
||||
super
|
||||
if self.options[:invoke_after_finished]
|
||||
Rails::Generators.invoke(self.options[:invoke_after_finished], [@name, @_initializer.first[1..-1]])
|
||||
end
|
||||
end
|
||||
|
||||
end
|
9
lib/generators/rspec/decorator_generator.rb
Normal file
9
lib/generators/rspec/decorator_generator.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module Rspec
|
||||
class DecoratorGenerator < ::Rails::Generators::NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def create_spec_file
|
||||
template 'decorator_spec.rb', File.join('spec/decorators', "#{singular_name}_decorator_spec.rb")
|
||||
end
|
||||
end
|
||||
end
|
4
lib/generators/rspec/templates/decorator_spec.rb
Normal file
4
lib/generators/rspec/templates/decorator_spec.rb
Normal file
|
@ -0,0 +1,4 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe <%= singular_name.camelize %>Decorator do
|
||||
end
|
9
lib/generators/test_unit/decorator_generator.rb
Normal file
9
lib/generators/test_unit/decorator_generator.rb
Normal file
|
@ -0,0 +1,9 @@
|
|||
module TestUnit
|
||||
class DecoratorGenerator < ::Rails::Generators::NamedBase
|
||||
source_root File.expand_path('../templates', __FILE__)
|
||||
|
||||
def create_test_file
|
||||
template 'decorator_test.rb', File.join('test/decorators', "#{singular_name}_decorator_test.rb")
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,6 +1,6 @@
|
|||
require 'test_helper'
|
||||
|
||||
class <%= resource_name.singularize.camelize %>DecoratorTest < ActiveSupport::TestCase
|
||||
class <%= singular_name.camelize %>DecoratorTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
ApplicationController.new.set_current_view_context
|
||||
end
|
|
@ -1,9 +1,9 @@
|
|||
require 'spec_helper'
|
||||
|
||||
# Generators are not automatically loaded by Rails
|
||||
require 'generators/draper/decorator/decorator_generator'
|
||||
require 'generators/draper/decorator_generator'
|
||||
|
||||
describe Draper::DecoratorGenerator do
|
||||
describe Draper::Generators::DecoratorGenerator do
|
||||
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
||||
destination File.expand_path("../../../../../tmp", __FILE__)
|
||||
|
||||
|
@ -21,17 +21,7 @@ describe Draper::DecoratorGenerator do
|
|||
end
|
||||
|
||||
context 'decorator name' do
|
||||
before { run_generator ["YourModel"] }
|
||||
|
||||
describe 'spec/decorators/your_model_decorator_spec.rb' do
|
||||
subject { file('spec/decorators/your_model_decorator_spec.rb') }
|
||||
it { should exist }
|
||||
it { should contain "describe YourModelDecorator" }
|
||||
end
|
||||
end
|
||||
|
||||
context 'default test framework' do
|
||||
before { run_generator ["YourModel"] }
|
||||
before { run_generator ["YourModel", '-t=rspec'] }
|
||||
|
||||
describe 'spec/decorators/your_model_decorator_spec.rb' do
|
||||
subject { file('spec/decorators/your_model_decorator_spec.rb') }
|
||||
|
@ -60,54 +50,3 @@ describe Draper::DecoratorGenerator do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
=begin
|
||||
describe 'no arguments' do
|
||||
before { run_generator %w(products) }
|
||||
|
||||
describe 'app/decorators/products_decorator.rb' do
|
||||
subject { file('app/decorators/products_decorator.rb') }
|
||||
it { should exist }
|
||||
it { should contain "class ProductsDecorator < ApplicationDecorator" }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
context 'simple' do
|
||||
before { run_generator %w(products) }
|
||||
|
||||
describe 'app/decorators/products_decorator.rb' do
|
||||
subject { file('app/decorators/products_decorator.rb') }
|
||||
it { should exist }
|
||||
it { should contain "class ProductsDecorator < ApplicationDecorator" }
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
context 'using rspec' do
|
||||
|
||||
describe 'app/decorators/products_decorator.rb' do
|
||||
subject { file('app/decorators/products_decorator.rb') }
|
||||
it { should exist }
|
||||
it { should contain "class ProductsDecorator < ApplicationDecorator" }
|
||||
end
|
||||
|
||||
shared_examples_for "ApplicationDecoratorGenerator" do
|
||||
describe 'app/decorators/application_decorator.rb' do
|
||||
subject { file('app/decorators/application_decorator.rb') }
|
||||
it { should exist }
|
||||
it { should contain "class ApplicationDecorator < Draper::Base" }
|
||||
end
|
||||
end
|
||||
|
||||
describe 'spec/decorators/application_decorator_spec.rb' do
|
||||
subject { file('spec/decorators/application_decorator_spec.rb') }
|
||||
it { should exist }
|
||||
it { should contain "describe ApplicationDecorator do" }
|
||||
end
|
||||
end
|
||||
=end
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
require 'spec_helper'
|
||||
|
||||
# Generators are not automatically loaded by Rails
|
||||
require 'generators/draper/install/install_generator'
|
||||
require 'generators/draper/install_generator'
|
||||
|
||||
describe Draper::InstallGenerator do
|
||||
describe Draper::Generators::InstallGenerator do
|
||||
# Tell the generator where to put its output (what it thinks of as Rails.root)
|
||||
destination File.expand_path("../../../../../tmp", __FILE__)
|
||||
|
||||
before { prepare_destination }
|
||||
|
||||
context 'using rspec' do
|
||||
before { run_generator }
|
||||
before do
|
||||
run_generator ['', "-t=rspec"]
|
||||
end
|
||||
|
||||
shared_examples_for "ApplicationDecoratorGenerator" do
|
||||
describe 'app/decorators/application_decorator.rb' do
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
require 'rubygems'
|
||||
require 'bundler/setup'
|
||||
require 'rails'
|
||||
|
||||
Bundler.require
|
||||
|
||||
require './spec/support/samples/active_record.rb'
|
||||
|
|
Loading…
Add table
Reference in a new issue