Merge branch 'install_generator' of https://github.com/angelim/draper-1 into generators

This commit is contained in:
Jeff Casimir 2012-01-16 07:54:06 -05:00
commit f3f32233cd
11 changed files with 95 additions and 38 deletions

View File

@ -1,16 +1,18 @@
module Draper
class DecoratorGenerator < Rails::Generators::NamedBase
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
source_root File.expand_path('../templates', __FILE__)
DECORATORS_ROOT = 'app/decorators/'
APPLICATION_DECORATOR = 'application_decorator.rb'
APPLICATION_DECORATOR_PATH = DECORATORS_ROOT + APPLICATION_DECORATOR
def build_model_and_application_decorators
empty_directory "app/decorators"
unless File.exists?(APPLICATION_DECORATOR_PATH)
template APPLICATION_DECORATOR, APPLICATION_DECORATOR_PATH
end
def build_model_decorator
template 'decorator.rb', "#{DECORATORS_ROOT}#{singular_name}_decorator.rb"
end

View File

@ -0,0 +1,39 @@
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

View File

@ -3,14 +3,8 @@ module Rspec
source_root File.expand_path('../templates', __FILE__)
SPEC_ROOT = 'spec/decorators/'
APPLICATION_DECORATOR_SPEC = 'application_decorator_spec.rb'
APPLICATION_DECORATOR_SPEC_PATH = SPEC_ROOT + APPLICATION_DECORATOR_SPEC
def build_model_and_application_decorator_specs
empty_directory SPEC_ROOT
unless File.exists?(APPLICATION_DECORATOR_SPEC_PATH)
template APPLICATION_DECORATOR_SPEC, APPLICATION_DECORATOR_SPEC_PATH
end
template 'decorator_spec.rb', "#{SPEC_ROOT}#{singular_name}_decorator_spec.rb"
end
end

View File

@ -3,14 +3,8 @@ module TestUnit
source_root File.expand_path('../templates', __FILE__)
TEST_ROOT = 'test/decorators/'
APPLICATION_DECORATOR_TEST = 'application_decorator_test.rb'
APPLICATION_DECORATOR_TEST_PATH = TEST_ROOT + APPLICATION_DECORATOR_TEST
def build_model_and_application_decorator_tests
empty_directory TEST_ROOT
unless File.exists?(APPLICATION_DECORATOR_TEST_PATH)
template APPLICATION_DECORATOR_TEST, APPLICATION_DECORATOR_TEST_PATH
end
def build_model_decorator_tests
template 'decorator_test.rb', "#{TEST_ROOT}#{singular_name}_decorator_test.rb"
end
end

View File

@ -12,12 +12,6 @@ describe Draper::DecoratorGenerator do
describe 'no arguments' do
before { run_generator %w(products) }
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
describe 'app/decorators/products_decorator.rb' do
subject { file('app/decorators/products_decorator.rb') }
it { should exist }

View File

@ -0,0 +1,46 @@
require 'spec_helper'
# Generators are not automatically loaded by Rails
require 'generators/draper/install/install_generator'
describe Draper::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 }
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
context "using test_unit" do
before { run_generator ["", "-t=test_unit"] }
it_should_behave_like "ApplicationDecoratorGenerator"
describe 'spec/decorators/application_decorator_spec.rb' do
subject { file('spec/decorators/application_decorator_spec.rb') }
it { should_not exist }
end
describe 'spec/decorators/application_decorator_test.rb' do
subject { file('test/decorators/application_decorator_test.rb') }
it { should exist }
end
end
end

View File

@ -12,12 +12,6 @@ describe Rspec::DecoratorGenerator do
describe 'no arguments' do
before { run_generator %w(products) }
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
describe 'spec/decorators/products_decorator_spec.rb' do
subject { file('spec/decorators/products_decorator_spec.rb') }
it { should exist }

View File

@ -12,12 +12,6 @@ describe TestUnit::DecoratorGenerator do
describe 'no arguments' do
before { run_generator %w(products) }
describe 'test/decorators/application_decorator_test.rb' do
subject { file('test/decorators/application_decorator_test.rb') }
it { should exist }
it { should contain "class ApplicationDecoratorTest < ActiveSupport::TestCase" }
end
describe 'test/decorators/products_decorator_test.rb' do
subject { file('test/decorators/products_decorator_test.rb') }
it { should exist }