-
Notifications
You must be signed in to change notification settings - Fork 526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accept a lambda for context in factory #478
Conversation
Accept a lambda for context in factory
👍 I like the symmetry. |
There's a minor problem here, unfortunately, due to the way ruby destructures hashes with splats:
As a result, if I have an owner with context class Thing < ActiveRecord::Base
belongs_to :user
end
class User < ActiveRecord::Base; end
class ThingDecorator
decorates_association :user
end
class UserDecorator; end
Also, because the identity lambda takes exactly one argument, if I have a more complex context, it raises an ArgumentError:
Rather than defaulting From 507860681dfebb6ee6f2405437d9622412dbec35 Mon Sep 17 00:00:00 2001
From: Simon Coffey
Date: Tue, 19 Feb 2013 14:30:59 +0000
Subject: [PATCH] Pass owner context directly in decorated association
---
lib/draper/decorated_association.rb | 4 ++--
spec/draper/decorated_association_spec.rb | 8 +++-----
2 files changed, 5 insertions(+), 7 deletions(-)
diff --git a/lib/draper/decorated_association.rb b/lib/draper/decorated_association.rb
index b2e8a1b..7aecd41 100644
--- a/lib/draper/decorated_association.rb
+++ b/lib/draper/decorated_association.rb
@@ -11,7 +11,7 @@ module Draper
@scope = options[:scope]
decorator_class = options[:with]
- context = options.fetch(:context, ->(context){ context })
+ context = options.fetch(:context, {})
@factory = Draper::Factory.new(with: decorator_class, context: context)
end
@@ -28,7 +28,7 @@ module Draper
associated = owner.source.send(association)
associated = associated.send(scope) if scope
- @decorated = factory.decorate(associated, context_args: owner.context)
+ @decorated = factory.decorate(associated, context: owner.context)
end
end
diff --git a/spec/draper/decorated_association_spec.rb b/spec/draper/decorated_association_spec.rb
index 62b4fed..f15e10b 100644
--- a/spec/draper/decorated_association_spec.rb
+++ b/spec/draper/decorated_association_spec.rb
@@ -28,10 +28,8 @@ module Draper
end
describe ":context option" do
- it "defaults to the identity function" do
- Factory.should_receive(:new).with do |options|
- options[:context].call(:anything) == :anything
- end
+ it "defaults to the empty hash" do
+ Factory.should_receive(:new).with(with: anything(), context: {})
DecoratedAssociation.new(double, :association, {})
end
end
@@ -48,7 +46,7 @@ module Draper
decorated_association = DecoratedAssociation.new(owner, :association, {})
decorated = double
- factory.should_receive(:decorate).with(associated, context_args: owner_context).and_return(decorated)
+ factory.should_receive(:decorate).with(associated, context: owner_context).and_return(decorated)
expect(decorated_association.call).to be decorated
end
--
1.7.11 |
@urbanautomaton Good catch, thanks. The suggested patch doesn't work because we want the ability to pass a different context - either a static value I think the best fix is just to |
Ah, cool, I wasn't aware of |
Like
decorates_association
, you can now pass a context lambda todecorates_assigned
, which receives the current controller instance and returns a hash:The actual calling of the lambda has been extracted to the Factory·
Closes #467