mirror of
https://github.com/drapergem/draper
synced 2023-03-27 23:21:17 -04:00
Add features around url helpers
This commit is contained in:
parent
f197ced4ca
commit
bfc33537b9
3 changed files with 50 additions and 26 deletions
17
features/route_helpers.feature
Normal file
17
features/route_helpers.feature
Normal file
|
@ -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
|
||||||
|
|
20
features/step_definitions/route_steps.rb
Normal file
20
features/step_definitions/route_steps.rb
Normal file
|
@ -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
|
|
@ -9,32 +9,19 @@ class PostDecorator < Draper::Decorator
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Accessing Helpers
|
def path_helper_with_model
|
||||||
# You can access any helper via a proxy
|
{:post_path => h.post_path(self.model)}
|
||||||
#
|
end
|
||||||
# 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)
|
|
||||||
|
|
||||||
# Defining an Interface
|
def path_helper_with_model_id
|
||||||
# Control access to the wrapped subject's methods using one of the following:
|
{:post_path => h.post_path(:id => self.model.id)}
|
||||||
#
|
end
|
||||||
# To allow only the listed methods (whitelist):
|
|
||||||
# allows :method1, :method2
|
|
||||||
#
|
|
||||||
# To allow everything except the listed methods (blacklist):
|
|
||||||
# denies :method1, :method2
|
|
||||||
|
|
||||||
# Presentation Methods
|
def url_helper_with_model
|
||||||
# Define your own instance methods, even overriding accessors
|
{:post_url => h.post_url(self.model)}
|
||||||
# generated by ActiveRecord:
|
end
|
||||||
#
|
|
||||||
# def created_at
|
def url_helper_with_model_id
|
||||||
# h.content_tag :span, attributes["created_at"].strftime("%a %m/%d/%y"),
|
{:post_url => h.post_url(:id => self.model.id)}
|
||||||
# :class => 'timestamp'
|
end
|
||||||
# end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue