diff --git a/features/route_helpers.feature b/features/route_helpers.feature new file mode 100644 index 0000000..4b15c2e --- /dev/null +++ b/features/route_helpers.feature @@ -0,0 +1,17 @@ +Feature: Route helpers should work in decorators + + Background: + Given a post exists + + Scenario: + Then a _path helper with the underlying model works + + Scenario: + Then a _path helper with the underlying model's id works + + Scenario: + Then a _url helper with the underlying model works + + Scenario: + Then a _url helper with the underlying model's id works + diff --git a/features/step_definitions/route_steps.rb b/features/step_definitions/route_steps.rb new file mode 100644 index 0000000..90dad4e --- /dev/null +++ b/features/step_definitions/route_steps.rb @@ -0,0 +1,20 @@ +Given /^a post exists$/ do + @post = Post.create + @decorator = PostDecorator.decorate(@post) +end + +Then /^a _path helper with the underlying model works$/ do + @decorator.path_helper_with_model.should == {:post_path => "/posts/#{@post.id}"} +end + +Then /^a _path helper with the underlying model's id works$/ do + @decorator.path_helper_with_model_id.should == {:post_path => "/posts/#{@post.id}"} +end + +Then /^a _url helper with the underlying model works$/ do + @decorator.url_helper_with_model.should == {:post_url => "http://www.example.com/posts/#{@post.id}"} +end + +Then /^a _url helper with the underlying model's id works$/ do + @decorator.url_helper_with_model_id.should == {:post_url => "http://www.example.com/posts/#{@post.id}"} +end diff --git a/spec/dummy/app/decorators/post_decorator.rb b/spec/dummy/app/decorators/post_decorator.rb index 6a4e4f4..f172bae 100644 --- a/spec/dummy/app/decorators/post_decorator.rb +++ b/spec/dummy/app/decorators/post_decorator.rb @@ -9,32 +9,19 @@ class PostDecorator < Draper::Decorator end end - # Accessing Helpers - # You can access any helper via a proxy - # - # Normal Usage: helpers.number_to_currency(2) - # Abbreviated : h.number_to_currency(2) - # - # Or, optionally enable "lazy helpers" by including this module: - # include Draper::LazyHelpers - # Then use the helpers with no proxy: - # number_to_currency(2) + def path_helper_with_model + {:post_path => h.post_path(self.model)} + end - # Defining an Interface - # Control access to the wrapped subject's methods using one of the following: - # - # To allow only the listed methods (whitelist): - # allows :method1, :method2 - # - # To allow everything except the listed methods (blacklist): - # denies :method1, :method2 + def path_helper_with_model_id + {:post_path => h.post_path(:id => self.model.id)} + end - # Presentation Methods - # Define your own instance methods, even overriding accessors - # generated by ActiveRecord: - # - # def created_at - # h.content_tag :span, attributes["created_at"].strftime("%a %m/%d/%y"), - # :class => 'timestamp' - # end + def url_helper_with_model + {:post_url => h.post_url(self.model)} + end + + def url_helper_with_model_id + {:post_url => h.post_url(:id => self.model.id)} + end end