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`:
``` ruby
class ApplicationDecorator < Draper::Base
class ApplicationDecorator < Draper::Decorator
# your methods go here
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:
```ruby
class ArticleDecorator < Draper::Base
class ArticleDecorator < Draper::Decorator
decorates :article
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:
```ruby
class ArticleDecorator < Draper::Base
class ArticleDecorator < Draper::Decorator
decorates :article
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:
```ruby
class ArticleDecorator < Draper::Base
class ArticleDecorator < Draper::Decorator
decorates :article
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:
```ruby
class ArticleDecorator < Draper::Base
class ArticleDecorator < Draper::Decorator
decorates :article
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:
```ruby
class ArticleDecorator < Draper::Base
class ArticleDecorator < Draper::Decorator
decorates :article
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.
```ruby
class ArticleDecorator < Draper::Base
class ArticleDecorator < Draper::Decorator
decorates :article
decorates_association :author # belongs_to :author association
end
class AuthorDecorator < Draper::Base
class AuthorDecorator < Draper::Decorator
decorates :author
def fancy_name

View File

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

View File

@ -8,7 +8,7 @@ module Draper::ActiveModelSupport
proxies.each do |method_name|
if base.model.respond_to?(method_name)
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|
model.send(method_name, *args, &block)
end

View File

@ -1,5 +1,5 @@
module Draper
class Base
class Decorator
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/array/extract_options'
@ -20,7 +20,7 @@ module Draper
def initialize(input, options = {})
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?
@model = input.kind_of?(Draper::Base) ? input.model : input
@model = input.kind_of?(Draper::Decorator) ? input.model : input
self.options = options
self.extend Draper::ActiveModelSupport::Proxies
end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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