Rename Base to Decorator

This commit is contained in:
Andrew Haines 2012-10-09 09:51:40 +01:00
parent 16140fed55
commit 025742cb3b
19 changed files with 64 additions and 64 deletions

View File

@ -23,7 +23,7 @@
If you need common methods in your decorators, create an `app/decorators/application_decorator.rb`: If you need common methods in your decorators, create an `app/decorators/application_decorator.rb`:
``` ruby ``` ruby
class ApplicationDecorator < Draper::Base class ApplicationDecorator < Draper::Decorator
# your methods go here # your methods go here
end end
``` ```
@ -45,7 +45,7 @@ Why hate normal helpers? In Ruby/Rails we approach everything from an Object-Ori
A decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then the decorator could override `.published_at` to use formatted output like this: A decorator wraps an object with presentation-related accessor methods. For instance, if you had an `Article` object, then the decorator could override `.published_at` to use formatted output like this:
```ruby ```ruby
class ArticleDecorator < Draper::Base class ArticleDecorator < Draper::Decorator
decorates :article decorates :article
def published_at def published_at
@ -149,7 +149,7 @@ rails generate decorator article
Open the decorator model (ex: `app/decorators/article_decorator.rb`) and add normal instance methods. To access the wrapped source object, use a method named after the `decorates` argument: Open the decorator model (ex: `app/decorators/article_decorator.rb`) and add normal instance methods. To access the wrapped source object, use a method named after the `decorates` argument:
```ruby ```ruby
class ArticleDecorator < Draper::Base class ArticleDecorator < Draper::Decorator
decorates :article decorates :article
def author_name def author_name
@ -163,7 +163,7 @@ end
You probably want to make use of Rails helpers and those defined in your application. Use the `helpers` or `h` method proxy: You probably want to make use of Rails helpers and those defined in your application. Use the `helpers` or `h` method proxy:
```ruby ```ruby
class ArticleDecorator < Draper::Base class ArticleDecorator < Draper::Decorator
decorates :article decorates :article
def published_at def published_at
@ -179,7 +179,7 @@ end
Hate seeing that `h.` proxy all over? Willing to mix a bazillion methods into your decorator? Then try lazy helpers: Hate seeing that `h.` proxy all over? Willing to mix a bazillion methods into your decorator? Then try lazy helpers:
```ruby ```ruby
class ArticleDecorator < Draper::Base class ArticleDecorator < Draper::Decorator
decorates :article decorates :article
include Draper::LazyHelpers include Draper::LazyHelpers
@ -298,7 +298,7 @@ Then within your views you can utilize both the normal data methods and your new
Ta-da! Object-oriented data formatting for your view layer. Below is the complete decorator with extra comments removed: Ta-da! Object-oriented data formatting for your view layer. Below is the complete decorator with extra comments removed:
```ruby ```ruby
class ArticleDecorator < Draper::Base class ArticleDecorator < Draper::Decorator
decorates :article decorates :article
def published_at def published_at
@ -314,12 +314,12 @@ end
Add a `decorates_association :association_name` to gain access to a decorated version of your target association. Add a `decorates_association :association_name` to gain access to a decorated version of your target association.
```ruby ```ruby
class ArticleDecorator < Draper::Base class ArticleDecorator < Draper::Decorator
decorates :article decorates :article
decorates_association :author # belongs_to :author association decorates_association :author # belongs_to :author association
end end
class AuthorDecorator < Draper::Base class AuthorDecorator < Draper::Decorator
decorates :author decorates :author
def fancy_name def fancy_name

View File

@ -3,7 +3,7 @@ require 'action_view'
require 'draper/version' require 'draper/version'
require 'draper/system' require 'draper/system'
require 'draper/active_model_support' require 'draper/active_model_support'
require 'draper/base' require 'draper/decorator'
require 'draper/lazy_helpers' require 'draper/lazy_helpers'
require 'draper/model_support' require 'draper/model_support'
require 'draper/helper_support' require 'draper/helper_support'

View File

@ -8,7 +8,7 @@ module Draper::ActiveModelSupport
proxies.each do |method_name| proxies.each do |method_name|
if base.model.respond_to?(method_name) if base.model.respond_to?(method_name)
base.singleton_class.class_eval do base.singleton_class.class_eval do
if !base.class.instance_methods.include?(method_name) || base.class.instance_method(method_name).owner === Draper::Base if !base.class.instance_methods.include?(method_name) || base.class.instance_method(method_name).owner === Draper::Decorator
define_method(method_name) do |*args, &block| define_method(method_name) do |*args, &block|
model.send(method_name, *args, &block) model.send(method_name, *args, &block)
end end

View File

@ -1,5 +1,5 @@
module Draper module Draper
class Base class Decorator
require 'active_support/core_ext/class/attribute' require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/array/extract_options' require 'active_support/core_ext/array/extract_options'
@ -20,7 +20,7 @@ module Draper
def initialize(input, options = {}) def initialize(input, options = {})
input.to_a if input.respond_to?(:to_a) # forces evaluation of a lazy query from AR input.to_a if input.respond_to?(:to_a) # forces evaluation of a lazy query from AR
self.class.model_class = input.class if model_class.nil? self.class.model_class = input.class if model_class.nil?
@model = input.kind_of?(Draper::Base) ? input.model : input @model = input.kind_of?(Draper::Decorator) ? input.model : input
self.options = options self.options = options
self.extend Draper::ActiveModelSupport::Proxies self.extend Draper::ActiveModelSupport::Proxies
end end

View File

@ -1,7 +1,7 @@
class MiniTest::Rails::ActiveSupport::TestCase class MiniTest::Rails::ActiveSupport::TestCase
# Use AS::TestCase for the base class when describing a decorator # Use AS::TestCase for the base class when describing a decorator
register_spec_type(self) do |desc| register_spec_type(self) do |desc|
desc < Draper::Base if desc.is_a?(Class) desc < Draper::Decorator if desc.is_a?(Class)
end end
register_spec_type(/Decorator( ?Test)?\z/i, self) register_spec_type(/Decorator( ?Test)?\z/i, self)
end end

View File

@ -20,7 +20,7 @@ module Rails
elsif defined?(ApplicationDecorator) elsif defined?(ApplicationDecorator)
"ApplicationDecorator" "ApplicationDecorator"
else else
"Draper::Base" "Draper::Decorator"
end end
end end
end end

View File

@ -1,5 +1,5 @@
require "./performance/models" require "./performance/models"
class ProductDecorator < Draper::Base class ProductDecorator < Draper::Decorator
decorates :product decorates :product
def awesome_title def awesome_title
@ -21,7 +21,7 @@ class ProductDecorator < Draper::Base
end end
class FastProductDecorator < Draper::Base class FastProductDecorator < Draper::Decorator
decorates :product decorates :product
def awesome_title def awesome_title

View File

@ -1,6 +1,6 @@
require 'spec_helper' require 'spec_helper'
describe Draper::Base do describe Draper::Decorator do
before(:each){ ApplicationController.new.view_context } before(:each){ ApplicationController.new.view_context }
subject{ Decorator.new(source) } subject{ Decorator.new(source) }
let(:source){ Product.new } let(:source){ Product.new }
@ -53,7 +53,7 @@ describe Draper::Base do
it "handle plural-like words properly'" do it "handle plural-like words properly'" do
class Business; end class Business; end
expect do expect do
class BusinessDecorator < Draper::Base class BusinessDecorator < Draper::Decorator
decorates:business decorates:business
end end
BusinessDecorator.model_class.should == Business BusinessDecorator.model_class.should == Business
@ -63,7 +63,7 @@ describe Draper::Base do
context("accepts ActiveRecord like :class_name option too") do context("accepts ActiveRecord like :class_name option too") do
it "accepts constants for :class" do it "accepts constants for :class" do
expect do expect do
class CustomDecorator < Draper::Base class CustomDecorator < Draper::Decorator
decorates :product, :class => Product decorates :product, :class => Product
end end
CustomDecorator.model_class.should == Product CustomDecorator.model_class.should == Product
@ -72,7 +72,7 @@ describe Draper::Base do
it "accepts constants for :class_name" do it "accepts constants for :class_name" do
expect do expect do
class CustomDecorator < Draper::Base class CustomDecorator < Draper::Decorator
decorates :product, :class_name => Product decorates :product, :class_name => Product
end end
CustomDecorator.model_class.should == Product CustomDecorator.model_class.should == Product
@ -81,7 +81,7 @@ describe Draper::Base do
it "accepts strings for :class" do it "accepts strings for :class" do
expect do expect do
class CustomDecorator < Draper::Base class CustomDecorator < Draper::Decorator
decorates :product, :class => 'Product' decorates :product, :class => 'Product'
end end
CustomDecorator.model_class.should == Product CustomDecorator.model_class.should == Product
@ -90,7 +90,7 @@ describe Draper::Base do
it "accepts strings for :class_name" do it "accepts strings for :class_name" do
expect do expect do
class CustomDecorator < Draper::Base class CustomDecorator < Draper::Decorator
decorates :product, :class_name => 'Product' decorates :product, :class_name => 'Product'
end end
CustomDecorator.model_class.should == Product CustomDecorator.model_class.should == Product
@ -230,7 +230,7 @@ describe Draper::Base do
describe "method selection" do describe "method selection" do
it "echos the methods of the wrapped class except default exclusions" do it "echos the methods of the wrapped class except default exclusions" do
source.methods.each do |method| source.methods.each do |method|
unless Draper::Base::DEFAULT_DENIED.include?(method) unless Draper::Decorator::DEFAULT_DENIED.include?(method)
subject.should respond_to(method.to_sym) subject.should respond_to(method.to_sym)
end end
end end
@ -249,16 +249,16 @@ describe Draper::Base do
context "when an ActiveModel descendant" do context "when an ActiveModel descendant" do
it "always proxy to_param if it is not defined on the decorator itself" do it "always proxy to_param if it is not defined on the decorator itself" do
source.stub(:to_param).and_return(1) source.stub(:to_param).and_return(1)
Draper::Base.new(source).to_param.should == 1 Draper::Decorator.new(source).to_param.should == 1
end end
it "always proxy id if it is not defined on the decorator itself" do it "always proxy id if it is not defined on the decorator itself" do
source.stub(:id).and_return(123456789) source.stub(:id).and_return(123456789)
Draper::Base.new(source).id.should == 123456789 Draper::Decorator.new(source).id.should == 123456789
end end
it "always proxy errors if it is not defined on the decorator itself" do it "always proxy errors if it is not defined on the decorator itself" do
Draper::Base.new(source).errors.should be_an_instance_of ActiveModel::Errors Draper::Decorator.new(source).errors.should be_an_instance_of ActiveModel::Errors
end end
it "never proxy to_param if it is defined on the decorator itself" do it "never proxy to_param if it is defined on the decorator itself" do
@ -359,7 +359,7 @@ describe Draper::Base do
context ".decorate" do context ".decorate" do
context "without any context" do context "without any context" do
subject { Draper::Base.decorate(source) } subject { Draper::Decorator.decorate(source) }
context "when given a collection of source objects" do context "when given a collection of source objects" do
let(:source) { [Product.new, Product.new] } let(:source) { [Product.new, Product.new] }
@ -367,7 +367,7 @@ describe Draper::Base do
its(:size) { should == source.size } its(:size) { should == source.size }
it "returns a collection of wrapped objects" do it "returns a collection of wrapped objects" do
subject.each{ |decorated| decorated.should be_instance_of(Draper::Base) } subject.each{ |decorated| decorated.should be_instance_of(Draper::Decorator) }
end end
it 'should accepted and store a context for a collection' do it 'should accepted and store a context for a collection' do
@ -381,7 +381,7 @@ describe Draper::Base do
let(:source) { Struct.new(:title).new("Godzilla") } let(:source) { Struct.new(:title).new("Godzilla") }
it "returns a wrapped object" do it "returns a wrapped object" do
subject.should be_instance_of(Draper::Base) subject.should be_instance_of(Draper::Decorator)
end end
end end
@ -390,14 +390,14 @@ describe Draper::Base do
let(:source) { [SequelProduct.new, SequelProduct.new] } let(:source) { [SequelProduct.new, SequelProduct.new] }
it "returns a collection of wrapped objects" do it "returns a collection of wrapped objects" do
subject.each{ |decorated| decorated.should be_instance_of(Draper::Base) } subject.each{ |decorated| decorated.should be_instance_of(Draper::Decorator) }
end end
end end
context "when given a single source object" do context "when given a single source object" do
let(:source) { Product.new } let(:source) { Product.new }
it { should be_instance_of(Draper::Base) } it { should be_instance_of(Draper::Decorator) }
context "when the input is already decorated" do context "when the input is already decorated" do
it "does not perform double-decoration" do it "does not perform double-decoration" do
@ -423,7 +423,7 @@ describe Draper::Base do
context "with a context" do context "with a context" do
let(:context) {{ :some => 'data' }} let(:context) {{ :some => 'data' }}
subject { Draper::Base.decorate(source, :context => context) } subject { Draper::Decorator.decorate(source, :context => context) }
context "when given a collection of source objects" do context "when given a collection of source objects" do
let(:source) { [Product.new, Product.new] } let(:source) { [Product.new, Product.new] }
@ -443,34 +443,34 @@ describe Draper::Base do
context "with options" do context "with options" do
let(:options) {{ :more => "settings" }} let(:options) {{ :more => "settings" }}
subject { Draper::Base.decorate(source, options ) } subject { Draper::Decorator.decorate(source, options ) }
its(:options) { should eq(options) } its(:options) { should eq(options) }
end end
context "does not infer collections by default" do context "does not infer collections by default" do
subject { Draper::Base.decorate(source).to_ary } subject { Draper::Decorator.decorate(source).to_ary }
let(:source) { [Product.new, Widget.new] } let(:source) { [Product.new, Widget.new] }
it "returns a collection of wrapped objects all with the same decorator" do it "returns a collection of wrapped objects all with the same decorator" do
subject.first.class.name.should eql 'Draper::Base' subject.first.class.name.should eql 'Draper::Decorator'
subject.last.class.name.should eql 'Draper::Base' subject.last.class.name.should eql 'Draper::Decorator'
end end
end end
context "does not infer single items by default" do context "does not infer single items by default" do
subject { Draper::Base.decorate(source) } subject { Draper::Decorator.decorate(source) }
let(:source) { Product.new } let(:source) { Product.new }
it "returns a decorator of the type explicity used in the call" do it "returns a decorator of the type explicity used in the call" do
subject.class.should eql Draper::Base subject.class.should eql Draper::Decorator
end end
end end
context "returns a collection containing only the explicit decorator used in the call" do context "returns a collection containing only the explicit decorator used in the call" do
subject { Draper::Base.decorate(source, :infer => true).to_ary } subject { Draper::Decorator.decorate(source, :infer => true).to_ary }
let(:source) { [Product.new, Widget.new] } let(:source) { [Product.new, Widget.new] }
@ -481,7 +481,7 @@ describe Draper::Base do
end end
context "when given a single object" do context "when given a single object" do
subject { Draper::Base.decorate(source, :infer => true) } subject { Draper::Decorator.decorate(source, :infer => true) }
let(:source) { Product.new } let(:source) { Product.new }
@ -493,7 +493,7 @@ describe Draper::Base do
context('.==') do context('.==') do
it "compare the decorated models" do it "compare the decorated models" do
other = Draper::Base.new(source) other = Draper::Decorator.new(source)
subject.should == other subject.should == other
end end
end end
@ -502,7 +502,7 @@ describe Draper::Base do
# respond_to? is called by some proxies (id, to_param, errors). # respond_to? is called by some proxies (id, to_param, errors).
# This is, why I stub it this way. # This is, why I stub it this way.
it "delegate respond_to? to the decorated model" do it "delegate respond_to? to the decorated model" do
other = Draper::Base.new(source) other = Draper::Decorator.new(source)
source.stub(:respond_to?).and_return(false) source.stub(:respond_to?).and_return(false)
source.stub(:respond_to?).with(:whatever, true).once.and_return("mocked") source.stub(:respond_to?).with(:whatever, true).once.and_return("mocked")
subject.respond_to?(:whatever, true).should == "mocked" subject.respond_to?(:whatever, true).should == "mocked"
@ -677,26 +677,26 @@ describe Draper::Base do
describe "invalid usages of allows and denies" do describe "invalid usages of allows and denies" do
let(:blank_allows){ let(:blank_allows){
class DecoratorWithInvalidAllows < Draper::Base class DecoratorWithInvalidAllows < Draper::Decorator
allows allows
end end
} }
let(:blank_denies){ let(:blank_denies){
class DecoratorWithInvalidDenies < Draper::Base class DecoratorWithInvalidDenies < Draper::Decorator
denies denies
end end
} }
let(:using_allows_then_denies){ let(:using_allows_then_denies){
class DecoratorWithAllowsAndDenies < Draper::Base class DecoratorWithAllowsAndDenies < Draper::Decorator
allows :hello_world allows :hello_world
denies :goodnight_moon denies :goodnight_moon
end end
} }
let(:using_denies_then_allows){ let(:using_denies_then_allows){
class DecoratorWithDeniesAndAllows < Draper::Base class DecoratorWithDeniesAndAllows < Draper::Decorator
denies :goodnight_moon denies :goodnight_moon
allows :hello_world allows :hello_world
end end
@ -729,7 +729,7 @@ describe Draper::Base do
end end
let(:using_denies_all_then_denies_all) { let(:using_denies_all_then_denies_all) {
class DecoratorWithDeniesAllAndDeniesAll < Draper::Base class DecoratorWithDeniesAllAndDeniesAll < Draper::Decorator
denies_all denies_all
denies_all denies_all
end end
@ -742,25 +742,25 @@ describe Draper::Base do
describe "invalid usages of denies_all" do describe "invalid usages of denies_all" do
let(:using_allows_then_denies_all) { let(:using_allows_then_denies_all) {
class DecoratorWithAllowsAndDeniesAll < Draper::Base class DecoratorWithAllowsAndDeniesAll < Draper::Decorator
allows :hello_world allows :hello_world
denies_all denies_all
end end
} }
let(:using_denies_then_denies_all) { let(:using_denies_then_denies_all) {
class DecoratorWithDeniesAndDeniesAll < Draper::Base class DecoratorWithDeniesAndDeniesAll < Draper::Decorator
denies :goodnight_moon denies :goodnight_moon
denies_all denies_all
end end
} }
let(:using_denies_all_then_allows) { let(:using_denies_all_then_allows) {
class DecoratorWithDeniesAllAndAllows < Draper::Base class DecoratorWithDeniesAllAndAllows < Draper::Decorator
denies_all denies_all
allows :hello_world allows :hello_world
end end
} }
let(:using_denies_all_then_denies) { let(:using_denies_all_then_denies) {
class DecoratorWithDeniesAllAndDenies < Draper::Base class DecoratorWithDeniesAllAndDenies < Draper::Decorator
denies_all denies_all
denies :goodnight_moon denies :goodnight_moon
end end

View File

@ -15,7 +15,7 @@ describe Rails::Generators::DecoratorGenerator do
describe 'app/decorators/your_model_decorator.rb' do describe 'app/decorators/your_model_decorator.rb' do
subject { file('app/decorators/your_model_decorator.rb') } subject { file('app/decorators/your_model_decorator.rb') }
it { should exist } it { should exist }
it { should contain "class YourModelDecorator < Draper::Base" } it { should contain "class YourModelDecorator < Draper::Decorator" }
it { should contain "decorates :your_model" } it { should contain "decorates :your_model" }
end end
end end
@ -31,12 +31,12 @@ describe Rails::Generators::DecoratorGenerator do
end end
context 'parent decorator' do context 'parent decorator' do
describe 'decorator inhereted from Draper::Base' do describe 'decorator inhereted from Draper::Decorator' do
before { run_generator ["YourModel"] } before { run_generator ["YourModel"] }
subject { file('app/decorators/your_model_decorator.rb') } subject { file('app/decorators/your_model_decorator.rb') }
it { should exist } it { should exist }
it { should contain "class YourModelDecorator < Draper::Base" } it { should contain "class YourModelDecorator < Draper::Decorator" }
end end
describe "decorator inhereted from ApplicationDecorator if it's present" do describe "decorator inhereted from ApplicationDecorator if it's present" do

View File

@ -1,4 +1,4 @@
class Decorator < Draper::Base class Decorator < Draper::Decorator
def self.own_class_method def self.own_class_method
"own class method" "own class method"
end end

View File

@ -1,3 +1,3 @@
class DecoratorWithAllows < Draper::Base class DecoratorWithAllows < Draper::Decorator
allows :goodnight_moon allows :goodnight_moon
end end

View File

@ -1,4 +1,4 @@
class DecoratorWithApplicationHelper < Draper::Base class DecoratorWithApplicationHelper < Draper::Decorator
def uses_hello_world def uses_hello_world
h.hello_world h.hello_world
end end

View File

@ -1,3 +1,3 @@
class DecoratorWithDenies < Draper::Base class DecoratorWithDenies < Draper::Decorator
denies :goodnight_moon, :title denies :goodnight_moon, :title
end end

View File

@ -1,3 +1,3 @@
class DecoratorWithDeniesAll < Draper::Base class DecoratorWithDeniesAll < Draper::Decorator
denies_all denies_all
end end

View File

@ -1,4 +1,4 @@
class DecoratorWithMultipleAllows < Draper::Base class DecoratorWithMultipleAllows < Draper::Decorator
allows :goodnight_moon allows :goodnight_moon
allows :hello_world allows :hello_world
end end

View File

@ -1,4 +1,4 @@
class DecoratorWithSpecialMethods < Draper::Base class DecoratorWithSpecialMethods < Draper::Decorator
def to_param def to_param
"foo" "foo"
end end

View File

@ -1,7 +1,7 @@
require './spec/support/samples/namespaced_product' require './spec/support/samples/namespaced_product'
module Namespace module Namespace
class ProductDecorator < Draper::Base class ProductDecorator < Draper::Decorator
decorates :product, :class => Namespace::Product decorates :product, :class => Namespace::Product
end end
end end

View File

@ -1,4 +1,4 @@
class ProductDecorator < Draper::Base class ProductDecorator < Draper::Decorator
decorates :product decorates :product
def awesome_title def awesome_title

View File

@ -1,3 +1,3 @@
class SomeThingDecorator < Draper::Base class SomeThingDecorator < Draper::Decorator
decorates :some_thing decorates :some_thing
end end