1
0
Fork 0
mirror of https://github.com/drapergem/draper synced 2023-03-27 23:21:17 -04:00

Remove context

`context` and `options` were performing essentially the same
function.

We can just use `decorate(source, role: :admin)`
instead of `decorate(source, context: {role: :admin})`.
This commit is contained in:
Andrew Haines 2012-11-07 23:58:36 +00:00
parent 17cf08a0c1
commit aeac106de5
6 changed files with 107 additions and 108 deletions

View file

@ -56,8 +56,9 @@ module Draper
"#<CollectionDecorator of #{decorator_class} for #{source.inspect}>"
end
def context=(input)
map {|item| item.context = input }
def options=(options)
each {|item| item.options = options }
@options = options
end
protected

View file

@ -199,14 +199,6 @@ module Draper
raise no_method_error
end
def context
options.fetch(:context, {})
end
def context=(input)
options[:context] = input
end
# For ActiveModel compatibilty
def to_model
self

View file

@ -24,7 +24,7 @@ module Draper
def method_missing(method, *args, &block)
if method.to_s.match(/^find_((all_|last_)?by_|or_(initialize|create)_by_).*/)
decorate(finder_class.send(method, *args, &block), context: args.dup.extract_options!)
decorate(finder_class.send(method, *args, &block), args.dup.extract_options!)
else
finder_class.send(method, *args, &block)
end

View file

@ -16,6 +16,32 @@ describe Draper::CollectionDecorator do
subject.map{|item| item.source}.should == source
end
context "with options" do
subject { Draper::CollectionDecorator.new(source, with: ProductDecorator, some: "options") }
its(:options) { should == {some: "options"} }
it "passes options to the individual decorators" do
subject.each do |item|
item.options.should == {some: "options"}
end
end
describe "#options=" do
it "updates the options on the collection decorator" do
subject.options = {other: "options"}
subject.options.should == {other: "options"}
end
it "updates the options on the individual decorators" do
subject.options = {other: "options"}
subject.each do |item|
item.options.should == {other: "options"}
end
end
end
end
describe "#initialize" do
context "when the :with option is given" do
context "and the decorator can't be inferred from the class" do

View file

@ -2,11 +2,20 @@ require 'spec_helper'
describe Draper::Decorator do
before { ApplicationController.new.view_context }
subject{ Decorator.new(source) }
subject { Decorator.new(source) }
let(:source){ Product.new }
let(:non_active_model_source){ NonActiveModelProduct.new }
describe "#initialize" do
it "sets the source" do
subject.source.should be source
end
it "stores options" do
decorator = Decorator.new(source, some: "options")
decorator.options.should == {some: "options"}
end
context "when decorating an instance of itself" do
it "does not redecorate" do
decorator = ProductDecorator.new(source)
@ -47,109 +56,85 @@ describe Draper::Decorator do
end
describe ".decorate" do
context "without any context" do
subject { Draper::Decorator.decorate(source) }
subject { ProductDecorator.decorate(source) }
context "when given a collection of source objects" do
let(:source) { [Product.new, Product.new] }
context "when given a single source object" do
let(:source) { Widget.new }
its(:size) { should == source.size }
it "returns a decorator" do
subject.should be_a ProductDecorator
subject.source.should be source
end
end
it "returns a collection of wrapped objects" do
subject.each{ |decorated| decorated.should be_instance_of(Draper::Decorator) }
end
context "when given a collection of source objects" do
let(:source) { [Product.new, Widget.new] }
it 'should accepted and store a context for a collection' do
subject.context = :admin
subject.each { |decorated| decorated.context.should == :admin }
end
it "returns a collection decorator" do
subject.should be_a Draper::CollectionDecorator
subject.source.should be source
end
context "when given a struct" do
# Struct objects implement #each
let(:source) { Struct.new(:title).new("Godzilla") }
it "returns a wrapped object" do
subject.should be_instance_of(Draper::Decorator)
end
it "uses itself as the item decorator by default" do
subject.each {|item| item.should be_a ProductDecorator}
end
context "when given a collection of sequel models" do
# Sequel models implement #each
let(:source) { [SequelProduct.new, SequelProduct.new] }
context "when given :with => :infer" do
subject { ProductDecorator.decorate(source, with: :infer) }
it "returns a collection of wrapped objects" do
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 "aliases .new" do
ProductDecorator.should_receive(:new).with(source, {some: "options"}).and_return(:a_new_decorator)
ProductDecorator.decorate(source, some: "options").should be :a_new_decorator
it "infers the item decorators" do
subject.first.should be_a ProductDecorator
subject.last.should be_a WidgetDecorator
end
end
end
context "with a context" do
let(:context) { {some: 'data'} }
context "when given a struct" do
# Struct objects implement #each
let(:source) { Struct.new(:title).new("Godzilla") }
subject { Draper::Decorator.decorate(source, context: context) }
context "when given a collection of source objects" do
let(:source) { [Product.new, Product.new] }
it "returns a collection of wrapped objects with the context" do
subject.each {|decorated| decorated.context.should == context }
end
it "returns a decorator" do
subject.should be_a ProductDecorator
end
end
context "when given a single source object" do
let(:source) { Product.new }
context "when given a Sequel model" do
# Sequel models implement #each
let(:source) { SequelProduct.new }
its(:context) { should == context }
it "returns a decorator" do
subject.should be_a ProductDecorator
end
end
context "when given a collection of Sequel models" do
# Sequel models implement #each
let(:source) { [SequelProduct.new, SequelProduct.new] }
it "returns a collection decorator" do
subject.should be_a Draper::CollectionDecorator
end
end
context "with options" do
let(:options) { {more: "settings"} }
let(:options) { {some: "options"} }
subject { Draper::Decorator.decorate(source, options ) }
its(:options) { should == options }
end
context "when given a single source object" do
let(:source) { Product.new }
context "does not infer collections by default" do
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.should be_an_instance_of Draper::Decorator
subject.last.should be_an_instance_of Draper::Decorator
it "passes the options to the decorator" do
subject.options.should == options
end
end
end
context "does not infer single items by default" do
subject { Draper::Decorator.decorate(source) }
context "when given a collection of source objects" do
let(:source) { [Product.new, Product.new] }
let(:source) { Product.new }
it "returns a decorator of the type explicity used in the call" do
subject.should be_an_instance_of Draper::Decorator
end
end
context "returns a collection containing only the explicit decorator used in the call" do
subject { Draper::Decorator.decorate(source, with: :infer) }
let(:source) { [Product.new, Widget.new] }
it "returns a mixed collection of wrapped objects" do
subject.first.should be_an_instance_of ProductDecorator
subject.last.should be_an_instance_of WidgetDecorator
it "passes the options to the collection decorator" do
subject.options.should == options
end
end
end
end

View file

@ -15,9 +15,9 @@ describe Draper::Finders do
decorator.source.should be found
end
it "accepts a context" do
decorator = ProductDecorator.find(1, context: :admin)
decorator.context.should == :admin
it "passes options to the decorator" do
decorator = ProductDecorator.find(1, some: "options")
decorator.options.should == {some: "options"}
end
end
@ -60,15 +60,10 @@ describe Draper::Finders do
ProductDecorator.find_or_create_by_name_and_size("apples", "large")
end
it "accepts options" do
Product.should_receive(:find_by_name_and_size).with("apples", "large", {role: :admin})
ProductDecorator.find_by_name_and_size("apples", "large", role: :admin)
end
it "sets the context to the options" do
Product.should_receive(:find_by_name_and_size).with("apples", "large", {role: :admin})
decorator = ProductDecorator.find_by_name_and_size("apples", "large", role: :admin)
decorator.context.should == {role: :admin}
it "passes options to the decorator" do
Product.should_receive(:find_by_name_and_size).with("apples", "large", {some: "options"})
decorator = ProductDecorator.find_by_name_and_size("apples", "large", some: "options")
decorator.options.should == {some: "options"}
end
end
@ -79,9 +74,9 @@ describe Draper::Finders do
collection.first.should be_a ProductDecorator
end
it "accepts a context" do
collection = ProductDecorator.all(context: :admin)
collection.first.context.should == :admin
it "passes options to the collection decorator" do
collection = ProductDecorator.all(some: "options")
collection.options.should == {some: "options"}
end
end
@ -99,9 +94,9 @@ describe Draper::Finders do
decorator.source.should be first
end
it "accepts a context" do
decorator = ProductDecorator.first(context: :admin)
decorator.context.should == :admin
it "passes options to the decorator" do
decorator = ProductDecorator.first(some: "options")
decorator.options.should == {some: "options"}
end
end
@ -119,9 +114,9 @@ describe Draper::Finders do
decorator.source.should be last
end
it "accepts a context" do
decorator = ProductDecorator.last(context: :admin)
decorator.context.should == :admin
it "passes options to the decorator" do
decorator = ProductDecorator.last(some: "options")
decorator.options.should == {some: "options"}
end
end