Skip to content

Commit

Permalink
Merge pull request #222 from NetsoftHoldings/master
Browse files Browse the repository at this point in the history
implement passing parameters to render_wizard and finish_wizard_path
  • Loading branch information
schneems committed Nov 2, 2016
2 parents 46c141f + eac0638 commit c326d2c
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 13 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,39 @@ end

NOTE: The order of the `before_action` matters, when `setup_wizard` is called it will validate the presence of `self.steps`, you must call your custom step setting code before this point.

## Send params to finish_wizard_path method

If you wish to send parameters to the `finish_wizard_path` method that can be done by adding to your controller the method with the params argument `def finish_wizard_path(params) ... end`.

In order to send the parameters to the method here is an example of the show method:

```ruby
steps :first_step, :second_step

def show
# ...
render_wizard(nil, {}, { hello: 'world' })
end

def update
# ...
render_wizard(@user, {}, { hello: 'world' })
end

def finish_wizard_path(params)
# here you can access params and that would be equal to { hello: 'world' }
end
```

The `wizard_path` and `next_wizard_path` methods also take parameters that can then be accessed or visible in the `show` and `update` actions of the controller. You can use the methods like so:

```ruby
next_wizard_path({ hello: 'world' })
wizard_path(nil, { hello: 'world' })
# the wizard_path with the step specified would look like this
wizard_path(:wicked_finish, wizard_id: @user.id, hello: 'world')
```

## Keywords

There are a few "magical" keywords that will take you to the first step,
Expand Down
27 changes: 15 additions & 12 deletions lib/wicked/controller/concerns/render_redirect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ module Wicked::Controller::Concerns::RenderRedirect
extend ActiveSupport::Concern


def render_wizard(resource = nil, options = {})
def render_wizard(resource = nil, options = {}, params = {})
process_resource!(resource)

if @skip_to
redirect_to wizard_path(@skip_to, @wicked_redirect_params || {}), options
url_params = (@wicked_redirect_params || {}).merge(params)
redirect_to wizard_path(@skip_to, url_params), options
else
render_step wizard_value(step), options
render_step(wizard_value(step), options, params)
end
end

Expand All @@ -22,29 +23,31 @@ def process_resource!(resource)
end
end

def render_step(the_step, options = {})
def render_step(the_step, options = {}, params = {})
if the_step.nil? || the_step.to_s == Wicked::FINISH_STEP
redirect_to_finish_wizard options
redirect_to_finish_wizard options, params
else
render the_step, options
end
end

def redirect_to_next(next_step, options = {})
def redirect_to_next(next_step, options = {}, params = {})
if next_step.nil?
redirect_to_finish_wizard(options)
redirect_to_finish_wizard(options, params)
else
redirect_to wizard_path(next_step), options
redirect_to wizard_path(next_step, params), options
end
end

# TODO redirect to resource if one is passed to render_wizard
def finish_wizard_path
'/'
def finish_wizard_path(params = {})
url = '/'
url = "#{url}?#{params.to_query}" unless params.blank?
url
end

def redirect_to_finish_wizard(options = {})
wicked_final_redirect_path = finish_wizard_path
def redirect_to_finish_wizard(options = {}, params = {})
wicked_final_redirect_path = method(:finish_wizard_path).arity == 1 ? finish_wizard_path(params) : finish_wizard_path
Rails.logger.debug("Wizard has finished, redirecting to finish_wizard_path: #{wicked_final_redirect_path.inspect}")
# flash.keep is required for Rails 3 where a flash message is lost on a second redirect.
flash.keep
Expand Down
30 changes: 30 additions & 0 deletions test/dummy/app/controllers/update_params_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class UpdateParamsController < ApplicationController
include Wicked::Wizard

class Thing
def save
true
end
end

steps :first, :second, :last_step

def index

end

def show
render_wizard(nil, {}, { one: 'two' })
end

def update
@thing = Thing.new
render_wizard(@thing, { notice: "Thing was updated from step #{step}." }, { one: 'two' })
end

private

def finish_wizard_path(params)
update_params_path(params)
end
end
16 changes: 16 additions & 0 deletions test/dummy/app/views/update_params/_step_position.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<% wizard_steps.each do |s| %>
<%= s.inspect %>
<%= I18n.t("wicked.#{s}") %>
<p>
<strong><u><%= s.inspect %></u></strong><br />
<%= current_step?(s) ? "#{s} step is the current step" : "nope" %><br />
<%= past_step?(s) ? "#{s} step is a past step" : "nope" %><br />
<%= future_step?(s) ? "#{s} step is a future step" : "nope" %><br />
<%= previous_step?(s) ? "#{s} step was the previous step" : "nope" %><br />
<%= next_step?(s) ? "#{s} step is the next step" : "nope" %><br />
</p>
<% end %>
<%= form_tag update_param_path(step, {one: 'two'}), :method => :put do %>
<%= submit_tag "Next" %>
<% end %>
1 change: 1 addition & 0 deletions test/dummy/app/views/update_params/first.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'step_position' %>
1 change: 1 addition & 0 deletions test/dummy/app/views/update_params/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Index page.
1 change: 1 addition & 0 deletions test/dummy/app/views/update_params/last_step.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'step_position' %>
1 change: 1 addition & 0 deletions test/dummy/app/views/update_params/second.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'step_position' %>
2 changes: 1 addition & 1 deletion test/dummy/app/views/updates/_step_position.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
</p>
<% end %>
<%= form_tag update_path(step), :method => :put do %>
<%= form_tag update_path(step, {one: 'two'}), :method => :put do %>
<%= submit_tag "Next" %>
<% end %>
1 change: 1 addition & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
resources :redirect_to_next
resources :redirect_to_finish_flash
resources :updates
resources :update_params

resources :nested do
resources :builder, :controller => 'nested/builder'
Expand Down
20 changes: 20 additions & 0 deletions test/integration/update_params_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'test_helper'

class UpdateParamsTest < ActiveSupport::IntegrationCase
test 'on first' do
step = :first
visit(update_param_path(step))

click_button("Next")
assert_equal page.current_path, update_param_path(:second)
assert_has_content?("notice:Thing was updated from step first.")

click_button("Next")
assert_equal page.current_path, update_param_path(:last_step)
assert_has_content?("notice:Thing was updated from step second.")

click_button("Next")
assert_equal page.current_url, update_params_url({host: 'www.example.com', one: 'two' })
assert_has_content?("notice:Thing was updated from step last_step.")
end
end

0 comments on commit c326d2c

Please sign in to comment.