Skip to content

Commit

Permalink
Added presenter#accepts?(sym) so you can query a presenter instance t…
Browse files Browse the repository at this point in the history
…o see if presents on a particular symbol. Useful in Rails applications so controllers can always set the current_user on any presenter that accepts :current_user
  • Loading branch information
zdennis committed Feb 16, 2009
1 parent d61eaba commit 2ed3432
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion History.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
== 0.9.0, February 14th, 2009
== February 16th, 2009

* Added ability to query a presenter instance if it accepts a particular symbol using #accepts?

== 0.8.0, February 14th, 2009

* Removed/deprecated :requiring option in favor of :accepts
* CachingPresenter no longer tries to cache writer methods, ie; #foo=
Expand Down
5 changes: 5 additions & 0 deletions lib/caching_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def respond_to?(*args)
super || presents.respond_to?(*args)
end

def accepts?(name)
@accepts.include?(name.to_sym)
end

def self.inherited(subclass)
write_presents_for_subclass subclass
subclass.extend Memoizable
Expand Down Expand Up @@ -61,6 +65,7 @@ def write_constructor(options)
constructor_options[:accepts] ||= []
klass = self
define_method(:initialize) do |args|
@accepts = constructor_options[:accepts].dup
@presents_id = options[:presents]
args = args.dup
raise ArgumentError, "missing object to present on: #{@presents_id}" unless args[@presents_id]
Expand Down
6 changes: 6 additions & 0 deletions spec/caching_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class NamedSpacedObjectPresenter < CachingPresenter
SingleObjectPresenter.presents.should == :foo
Class.new(SingleObjectPresenter).presents.should == :foo
end

it "should be able to be queried for what it accepts" do
presenter = SingleObjectPresenter.new :foo => mock("foo")
presenter.accepts?(:bar).should be_true
presenter.accepts?(:unknown).should be_false
end

%w(class id to_param).each do |method|
it "should always delegate #{method} to the source of the presenter" do
Expand Down

0 comments on commit 2ed3432

Please sign in to comment.