-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using the cucumber World API to insert pickle. find_path_for is renam…
…ed as pickle_path_for
- Loading branch information
Showing
5 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'pickle' | ||
require 'pickle/email' | ||
require 'pickle/email/parser' | ||
|
||
# add email parser expressions | ||
Pickle::Parser.send :include, Pickle::Email::Parser | ||
|
||
# make world pickle/email aware | ||
World do |world| | ||
class << world | ||
include Pickle::Email | ||
end | ||
world | ||
end | ||
|
||
# shortcuts for use in step regexps | ||
class << self | ||
delegate :capture_email, :to => 'Pickle.parser' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Pickle | ||
module Page | ||
# given args of pickle model name, and an optional extra action, or segment, will attempt to find | ||
# a matching named route | ||
# | ||
# pickle_path_for 'the user', :action => 'edit' # => /users/3/edit | ||
# pickle_path_for 'the user', 'the comment' # => /users/3/comments/1 | ||
# pickle_path_for 'the user', :segment => 'comments' # => /users/3/comments | ||
# | ||
# If you don;t know if the 'extra' part of the path is an action or a segment, then just | ||
# pass it as 'extra' and this method will run through the possibilities | ||
# | ||
# pickle_path_for 'the user', :extra => 'new comment' # => /users/3/comments/new | ||
def pickle_path_for(*pickle_names) | ||
options = pickle_names.extract_options! | ||
models = pickle_names.map{|m| model(m)} | ||
if options[:extra] | ||
parts = options[:extra].underscore.gsub(' ','_').split("_") | ||
find_path_using_action_segment_combinations(models, parts) | ||
else | ||
path_for_models_action_segment(models, options[:action], options[:segment]) | ||
end or raise "Could not figure out a path for #{pickle_names.inspect} #{options.inspect}" | ||
end | ||
|
||
protected | ||
def find_path_using_action_segment_combinations(models, parts) | ||
path = nil | ||
(0..parts.length).each do |idx| | ||
action = parts.slice(0, idx).join('_') | ||
segment = parts.slice(idx, parts.length).join('_') | ||
path = path_for_models_action_segment(models, action, segment) and break | ||
end | ||
path | ||
end | ||
|
||
def path_for_models_action_segment(models, action, segment) | ||
action.blank? or action = action.downcase.gsub(' ','_') | ||
segment.blank? or segment = segment.downcase.gsub(' ','_') | ||
model_names = models.map{|m| m.class.name.underscore}.join("_") | ||
parts = [action, model_names, segment].reject(&:blank?) | ||
send("#{parts.join('_')}_path", *models) rescue nil | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'pickle' | ||
require 'pickle/page' | ||
|
||
# make world pickle/page aware | ||
World do |world| | ||
class << world | ||
include Pickle::Page | ||
end | ||
world | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'pickle' | ||
|
||
# make cucumber world pickle aware | ||
World do |world| | ||
class << world | ||
include Pickle::Session | ||
end | ||
world | ||
end | ||
|
||
# shortcuts to regexps for use in step definition regexps | ||
class << self | ||
delegate :capture_model, :capture_fields, :capture_factory, :capture_plural_factory, :capture_predicate, :to => 'Pickle.parser' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper')) | ||
|
||
describe Pickle::Page do | ||
include Pickle::Page | ||
|
||
before do | ||
stub!(:model).and_return(@user = mock_model(User)) | ||
end | ||
|
||
describe "#pickle_path_for" do | ||
it "('a user', 'the user: \"fred\"') should retrieve 'a user', and 'the user: \"fred\"' models" do | ||
should_receive(:model).with('a user') | ||
should_receive(:model).with('the user: "fred"') | ||
stub!(:user_user_path).and_return('the path') | ||
pickle_path_for 'a user', 'the user: "fred"' | ||
end | ||
|
||
it "('a user', :action => 'foo') should return foo_user_path(<user>)" do | ||
should_receive(:foo_user_path).with(@user).and_return('the path') | ||
pickle_path_for('a user', :action => 'foo').should == 'the path' | ||
end | ||
|
||
it "('a user', :action => 'foo') should raise informative error if foo_user_path not defined" do | ||
should_receive(:foo_user_path).with(@user).and_raise(NoMethodError) | ||
lambda { pickle_path_for('a user', :action => 'foo') }.should raise_error(Exception, /Could not figure out a path for/) | ||
end | ||
|
||
it "('a user', :segment => 'foo') should return user_foo_path(<user>)" do | ||
should_receive(:user_foo_path).with(@user).and_return('the path') | ||
pickle_path_for('a user', :segment => 'foo').should == 'the path' | ||
end | ||
|
||
it "('a user', :segment => 'foo') should raise informative error if foo_user_path not defined" do | ||
should_receive(:user_foo_path).with(@user).and_raise(NoMethodError) | ||
lambda { pickle_path_for('a user', :segment => 'foo') }.should raise_error(Exception, /Could not figure out a path for/) | ||
end | ||
|
||
it "('a user', :action => 'new', :segment => 'comment') should return new_user_comment_path(<user>)" do | ||
should_receive(:new_user_comment_path).with(@user).and_return('the path') | ||
pickle_path_for('a user', :segment => 'comment', :action => 'new').should == 'the path' | ||
end | ||
|
||
it "('a user', :action => 'new', :segment => 'comment') should raise informative error if new_user_comment_path not defined" do | ||
should_receive(:new_user_comment_path).with(@user).and_raise(NoMethodError) | ||
lambda { pickle_path_for('a user', :action => 'new', :segment => 'comment') }.should raise_error(Exception, /Could not figure out a path for/) | ||
end | ||
|
||
it "('a user', :extra => 'new comment') should return new_user_comment_path(<user>)" do | ||
should_receive(:new_user_comment_path).with(@user).and_return('the path') | ||
pickle_path_for('a user', :extra => 'new comment').should == 'the path' | ||
end | ||
|
||
it "('a user', :extra => 'new comment') should raise informative error if new_user_comment_path not defined" do | ||
should_receive(:new_user_comment_path).with(@user).and_raise(NoMethodError) | ||
lambda { pickle_path_for('a user', :extra => 'new comment') }.should raise_error(Exception, /Could not figure out a path for/) | ||
end | ||
|
||
describe "(private API)" do | ||
it "('a user', :extra => 'new ish comment') should try combinations of 'new', 'ish', 'comment'" do | ||
should_receive(:path_for_models_action_segment).with([@user], '', 'new_ish_comment').once | ||
should_receive(:path_for_models_action_segment).with([@user], 'new', 'ish_comment').once | ||
should_receive(:path_for_models_action_segment).with([@user], 'new_ish', 'comment').once | ||
should_receive(:path_for_models_action_segment).with([@user], 'new_ish_comment', '').once | ||
lambda { pickle_path_for('a user', :extra => 'new ish comment') }.should raise_error(Exception, /Could not figure out a path for/) | ||
end | ||
end | ||
end | ||
end |