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:
parent
17cf08a0c1
commit
aeac106de5
6 changed files with 107 additions and 108 deletions
|
@ -56,8 +56,9 @@ module Draper
|
||||||
"#<CollectionDecorator of #{decorator_class} for #{source.inspect}>"
|
"#<CollectionDecorator of #{decorator_class} for #{source.inspect}>"
|
||||||
end
|
end
|
||||||
|
|
||||||
def context=(input)
|
def options=(options)
|
||||||
map {|item| item.context = input }
|
each {|item| item.options = options }
|
||||||
|
@options = options
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
|
@ -199,14 +199,6 @@ module Draper
|
||||||
raise no_method_error
|
raise no_method_error
|
||||||
end
|
end
|
||||||
|
|
||||||
def context
|
|
||||||
options.fetch(:context, {})
|
|
||||||
end
|
|
||||||
|
|
||||||
def context=(input)
|
|
||||||
options[:context] = input
|
|
||||||
end
|
|
||||||
|
|
||||||
# For ActiveModel compatibilty
|
# For ActiveModel compatibilty
|
||||||
def to_model
|
def to_model
|
||||||
self
|
self
|
||||||
|
|
|
@ -24,7 +24,7 @@ module Draper
|
||||||
|
|
||||||
def method_missing(method, *args, &block)
|
def method_missing(method, *args, &block)
|
||||||
if method.to_s.match(/^find_((all_|last_)?by_|or_(initialize|create)_by_).*/)
|
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
|
else
|
||||||
finder_class.send(method, *args, &block)
|
finder_class.send(method, *args, &block)
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,6 +16,32 @@ describe Draper::CollectionDecorator do
|
||||||
subject.map{|item| item.source}.should == source
|
subject.map{|item| item.source}.should == source
|
||||||
end
|
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
|
describe "#initialize" do
|
||||||
context "when the :with option is given" do
|
context "when the :with option is given" do
|
||||||
context "and the decorator can't be inferred from the class" do
|
context "and the decorator can't be inferred from the class" do
|
||||||
|
|
|
@ -2,11 +2,20 @@ require 'spec_helper'
|
||||||
|
|
||||||
describe Draper::Decorator do
|
describe Draper::Decorator do
|
||||||
before { ApplicationController.new.view_context }
|
before { ApplicationController.new.view_context }
|
||||||
subject{ Decorator.new(source) }
|
subject { Decorator.new(source) }
|
||||||
let(:source){ Product.new }
|
let(:source){ Product.new }
|
||||||
let(:non_active_model_source){ NonActiveModelProduct.new }
|
let(:non_active_model_source){ NonActiveModelProduct.new }
|
||||||
|
|
||||||
describe "#initialize" do
|
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
|
context "when decorating an instance of itself" do
|
||||||
it "does not redecorate" do
|
it "does not redecorate" do
|
||||||
decorator = ProductDecorator.new(source)
|
decorator = ProductDecorator.new(source)
|
||||||
|
@ -47,21 +56,36 @@ describe Draper::Decorator do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".decorate" do
|
describe ".decorate" do
|
||||||
context "without any context" do
|
subject { ProductDecorator.decorate(source) }
|
||||||
subject { Draper::Decorator.decorate(source) }
|
|
||||||
|
|
||||||
context "when given a collection of source objects" do
|
context "when given a single source object" do
|
||||||
let(:source) { [Product.new, Product.new] }
|
let(:source) { Widget.new }
|
||||||
|
|
||||||
its(:size) { should == source.size }
|
it "returns a decorator" do
|
||||||
|
subject.should be_a ProductDecorator
|
||||||
it "returns a collection of wrapped objects" do
|
subject.source.should be source
|
||||||
subject.each{ |decorated| decorated.should be_instance_of(Draper::Decorator) }
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should accepted and store a context for a collection' do
|
context "when given a collection of source objects" do
|
||||||
subject.context = :admin
|
let(:source) { [Product.new, Widget.new] }
|
||||||
subject.each { |decorated| decorated.context.should == :admin }
|
|
||||||
|
it "returns a collection decorator" do
|
||||||
|
subject.should be_a Draper::CollectionDecorator
|
||||||
|
subject.source.should be source
|
||||||
|
end
|
||||||
|
|
||||||
|
it "uses itself as the item decorator by default" do
|
||||||
|
subject.each {|item| item.should be_a ProductDecorator}
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when given :with => :infer" do
|
||||||
|
subject { ProductDecorator.decorate(source, with: :infer) }
|
||||||
|
|
||||||
|
it "infers the item decorators" do
|
||||||
|
subject.first.should be_a ProductDecorator
|
||||||
|
subject.last.should be_a WidgetDecorator
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -69,87 +93,48 @@ describe Draper::Decorator do
|
||||||
# Struct objects implement #each
|
# Struct objects implement #each
|
||||||
let(:source) { Struct.new(:title).new("Godzilla") }
|
let(:source) { Struct.new(:title).new("Godzilla") }
|
||||||
|
|
||||||
it "returns a wrapped object" do
|
it "returns a decorator" do
|
||||||
subject.should be_instance_of(Draper::Decorator)
|
subject.should be_a ProductDecorator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when given a collection of sequel models" do
|
context "when given a Sequel model" do
|
||||||
|
# Sequel models implement #each
|
||||||
|
let(:source) { SequelProduct.new }
|
||||||
|
|
||||||
|
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
|
# Sequel models implement #each
|
||||||
let(:source) { [SequelProduct.new, SequelProduct.new] }
|
let(:source) { [SequelProduct.new, SequelProduct.new] }
|
||||||
|
|
||||||
it "returns a collection of wrapped objects" do
|
it "returns a collection decorator" do
|
||||||
subject.each{ |decorated| decorated.should be_instance_of(Draper::Decorator) }
|
subject.should be_a Draper::CollectionDecorator
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "with a context" do
|
|
||||||
let(:context) { {some: 'data'} }
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
|
|
||||||
context "when given a single source object" do
|
|
||||||
let(:source) { Product.new }
|
|
||||||
|
|
||||||
its(:context) { should == context }
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "with options" do
|
context "with options" do
|
||||||
let(:options) { {more: "settings"} }
|
let(:options) { {some: "options"} }
|
||||||
|
|
||||||
subject { Draper::Decorator.decorate(source, options ) }
|
subject { Draper::Decorator.decorate(source, options ) }
|
||||||
|
|
||||||
its(:options) { should == options }
|
context "when given a single source object" do
|
||||||
end
|
|
||||||
|
|
||||||
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
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
context "does not infer single items by default" do
|
|
||||||
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 "passes the options to the decorator" do
|
||||||
subject.should be_an_instance_of Draper::Decorator
|
subject.options.should == options
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "returns a collection containing only the explicit decorator used in the call" do
|
context "when given a collection of source objects" do
|
||||||
subject { Draper::Decorator.decorate(source, with: :infer) }
|
let(:source) { [Product.new, Product.new] }
|
||||||
|
|
||||||
let(:source) { [Product.new, Widget.new] }
|
it "passes the options to the collection decorator" do
|
||||||
|
subject.options.should == options
|
||||||
it "returns a mixed collection of wrapped objects" do
|
end
|
||||||
subject.first.should be_an_instance_of ProductDecorator
|
|
||||||
subject.last.should be_an_instance_of WidgetDecorator
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,9 +15,9 @@ describe Draper::Finders do
|
||||||
decorator.source.should be found
|
decorator.source.should be found
|
||||||
end
|
end
|
||||||
|
|
||||||
it "accepts a context" do
|
it "passes options to the decorator" do
|
||||||
decorator = ProductDecorator.find(1, context: :admin)
|
decorator = ProductDecorator.find(1, some: "options")
|
||||||
decorator.context.should == :admin
|
decorator.options.should == {some: "options"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -60,15 +60,10 @@ describe Draper::Finders do
|
||||||
ProductDecorator.find_or_create_by_name_and_size("apples", "large")
|
ProductDecorator.find_or_create_by_name_and_size("apples", "large")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "accepts options" do
|
it "passes options to the decorator" do
|
||||||
Product.should_receive(:find_by_name_and_size).with("apples", "large", {role: :admin})
|
Product.should_receive(:find_by_name_and_size).with("apples", "large", {some: "options"})
|
||||||
ProductDecorator.find_by_name_and_size("apples", "large", role: :admin)
|
decorator = ProductDecorator.find_by_name_and_size("apples", "large", some: "options")
|
||||||
end
|
decorator.options.should == {some: "options"}
|
||||||
|
|
||||||
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}
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -79,9 +74,9 @@ describe Draper::Finders do
|
||||||
collection.first.should be_a ProductDecorator
|
collection.first.should be_a ProductDecorator
|
||||||
end
|
end
|
||||||
|
|
||||||
it "accepts a context" do
|
it "passes options to the collection decorator" do
|
||||||
collection = ProductDecorator.all(context: :admin)
|
collection = ProductDecorator.all(some: "options")
|
||||||
collection.first.context.should == :admin
|
collection.options.should == {some: "options"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -99,9 +94,9 @@ describe Draper::Finders do
|
||||||
decorator.source.should be first
|
decorator.source.should be first
|
||||||
end
|
end
|
||||||
|
|
||||||
it "accepts a context" do
|
it "passes options to the decorator" do
|
||||||
decorator = ProductDecorator.first(context: :admin)
|
decorator = ProductDecorator.first(some: "options")
|
||||||
decorator.context.should == :admin
|
decorator.options.should == {some: "options"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -119,9 +114,9 @@ describe Draper::Finders do
|
||||||
decorator.source.should be last
|
decorator.source.should be last
|
||||||
end
|
end
|
||||||
|
|
||||||
it "accepts a context" do
|
it "passes options to the decorator" do
|
||||||
decorator = ProductDecorator.last(context: :admin)
|
decorator = ProductDecorator.last(some: "options")
|
||||||
decorator.context.should == :admin
|
decorator.options.should == {some: "options"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue