Guest User

Untitled

a guest
Jan 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. attr_writer :current_step
  2.  
  3. validates_presence_of :shipping_name, :if => lambda { |o| o.current_step == "shipping" }
  4. validates_presence_of :billing_name, :if => lambda { |o| o.current_step == "billing" }
  5.  
  6. def current_step
  7. @current_step || steps.first
  8. end
  9.  
  10. def steps
  11. %w[shipping billing confirmation]
  12. end
  13.  
  14. def next_step
  15. self.current_step = steps[steps.index(current_step)+1]
  16. end
  17.  
  18. def previous_step
  19. self.current_step = steps[steps.index(current_step)-1]
  20. end
  21.  
  22. def first_step?
  23. current_step == steps.first
  24. end
  25.  
  26. def last_step?
  27. current_step == steps.last
  28. end
  29.  
  30. def all_valid?
  31. steps.all? do |step|
  32. self.current_step = step
  33. valid?
  34. end
  35. end
  36.  
  37. def new
  38. session[:order_params] ||= {}
  39. @order = Order.new(session[:order_params])
  40. @order.current_step = session[:order_step]
  41. end
  42.  
  43. def create
  44. session[:order_params].deep_merge!(params[:order]) if params[:order]
  45. @order = Order.new(session[:order_params])
  46. @order.current_step = session[:order_step]
  47. if @order.valid?
  48. if params[:back_button]
  49. @order.previous_step
  50. elsif @order.last_step?
  51. @order.save if @order.all_valid?
  52. else
  53. @order.next_step
  54. end
  55. session[:order_step] = @order.current_step
  56. end
  57. if @order.new_record?
  58. render "new"
  59. else
  60. session[:order_step] = session[:order_params] = nil
  61. flash[:notice] = "Order saved!"
  62. redirect_to @order
  63. end
  64. end
  65.  
  66. <% form_for @order do |f| %>
  67. <%= f.error_messages %>
  68. <%= render "#{@order.current_step}_step", :f => f %>
  69. <p><%= f.submit "Continue" %></p>
  70. <p><%= f.submit "Back", :name => "back_button" unless @order.first_step? %></p>
  71. <% end %>
Add Comment
Please, Sign In to add comment