Skip to content

Commit

Permalink
Merge pull request drapergem#131 from yakko/master
Browse files Browse the repository at this point in the history
draper:decorator defaults to rspec too
  • Loading branch information
steveklabnik committed Feb 14, 2012
2 parents 56ce55a + 7e45a1f commit 49952ff
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 74 deletions.
28 changes: 25 additions & 3 deletions lib/generators/draper/decorator/decorator_generator.rb
@@ -1,5 +1,5 @@
module Draper
class DecoratorGenerator < Rails::Generators::NamedBase
class DecoratorGenerator < Rails::Generators::Base
desc <<-DESC
Description:
Generate a decorator for the given model.
Expand All @@ -8,14 +8,36 @@ class DecoratorGenerator < Rails::Generators::NamedBase
"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}#{singular_name}_decorator.rb"
template 'decorator.rb', "#{DECORATORS_ROOT}#{resource_name.singularize}_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', "#{resource_name.singularize}_decorator_spec.rb")
end

def build_decorator_test
empty_directory 'test/decorators/'
template 'decorator_test.rb', File.join('test/decorators', "#{resource_name.singularize}_decorator_test.rb")
end

hook_for :test_framework
end
end
4 changes: 2 additions & 2 deletions lib/generators/draper/decorator/templates/decorator.rb
@@ -1,5 +1,5 @@
class <%= singular_name.camelize %>Decorator < ApplicationDecorator
decorates :<%= singular_name.to_sym %>
class <%= resource_name.singularize.camelize %>Decorator < ApplicationDecorator
decorates :<%= resource_name.singularize.to_sym %>

# Accessing Helpers
# You can access any helper via a proxy
Expand Down
@@ -1,5 +1,5 @@
require 'spec_helper'

describe <%= singular_name.camelize %>Decorator do
describe <%= resource_name.singularize.camelize %>Decorator do
before { ApplicationController.new.set_current_view_context }
end
@@ -1,6 +1,6 @@
require 'test_helper'

class <%= singular_name.camelize %>DecoratorTest < ActiveSupport::TestCase
class <%= resource_name.singularize.camelize %>DecoratorTest < ActiveSupport::TestCase
def setup
ApplicationController.new.set_current_view_context
end
Expand Down
11 changes: 0 additions & 11 deletions lib/generators/rspec/decorator_generator.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/generators/test_unit/decorator_generator.rb

This file was deleted.

83 changes: 82 additions & 1 deletion spec/generators/draper/decorator/decorator_generator_spec.rb
Expand Up @@ -9,6 +9,50 @@

before { prepare_destination }

context 'decorator context' do
before { run_generator ["product"] }

describe 'app/decorators/product_decorator.rb' do
subject { file('app/decorators/product_decorator.rb') }
it { should exist }
it { should contain "class ProductDecorator < ApplicationDecorator" }
end
end

context 'default test framework' do
before { run_generator ["product"] }

describe 'spec/decorators/product_decorator_spec.rb' do
subject { file('spec/decorators/product_decorator_spec.rb') }
it { should exist }
it { should contain "describe ProductDecorator" }
end
end

context 'using rspec' do
before { run_generator ["product", "-t=rspec"] }

describe 'spec/decorators/product_decorator_spec.rb' do
subject { file('spec/decorators/product_decorator_spec.rb') }
it { should exist }
it { should contain "describe ProductDecorator" }
end
end

context 'using rspec' do
before { run_generator ["product", "-t=test_unit"] }

describe 'test/decorators/product_decorator_test.rb' do
subject { file('test/decorators/product_decorator_test.rb') }
it { should exist }
it { should contain "class ProductDecoratorTest < ActiveSupport::TestCase" }
end
end

end


=begin
describe 'no arguments' do
before { run_generator %w(products) }
Expand All @@ -17,6 +61,43 @@
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
=end
22 changes: 0 additions & 22 deletions spec/generators/rspec/decorator_generator_spec.rb

This file was deleted.

22 changes: 0 additions & 22 deletions spec/generators/test_unit/decorator_generator_spec.rb

This file was deleted.

0 comments on commit 49952ff

Please sign in to comment.