2012-11-04 55 views
12

我正在嘗試向我的Spree 1.2商店添加額外步驟,這將允許客戶創建訂閱。我插入了該步驟,並呈現了正確的視圖,但是當用戶單擊「保存並繼續」時,將呈現下一步,但實際上並未保存任何內容。將步驟添加到Spree結帳過程

我明白,我需要添加一個state_callback,但我不知道如何做到這一點,施普雷文檔解決這個非常欠缺(大概是因爲它是相當新)

目前我有在我的分機以下幾點:

型號/大禮包/ order_decorator.rb

Spree::Order.class_eval do 
    belongs_to :subscription 

    accepts_nested_attributes_for :subscription 

    # This doesn't appear to be called 
    Spree::Order.state_machine.after_transition :from => :subscription, 
               :do => :valid_subs? 

    checkout_flow do 
    go_to_state :address 
    go_to_state :subscription 
    go_to_state :payment, :if => lambda { |order| order.payment_required? } 
    go_to_state :confirm, :if => lambda { |order| order.confirmation_required? } 
    go_to_state :complete 
    remove_transition :from => :delivery, :to => :confirm 
    end 
end 

不完全肯定accepts_nested_attributes是必要的,但我對這種開發方法是試錯到目前爲止,它結束了呆在那裏。

在模型/ subscription.rb

class Subscription < ActiveRecord::Base 

    attr_accessible :start_date, :frequency 

    belongs_to :user 
    has_many :orders 
    has_many :products 

    validates :start_date, :frequency, :presence => true 

    def schedule 
    ...code that returns a list of dates rendered on FE... 
    end 

    private #---- 

    ... some methods used in schedule ... 

    def valid_subs? 
    binding.pry # never called 
    end 

    def after_subscription 
    binding.pry # never called either... 
    end 
end 

的意見/大禮包/結算/ _subscription.html.erb

<h1><%= t(:"subscription.title") %></h1> 

<div class="columns alpha six" data-hook="subscription_calendar_fieldset_wrapper"> 
    <fieldset id="subscription_calendar" data-hook> 
    <%= form.fields_for :subscription_picker do |subscription_picker| %> 
     <legend><%= t(:"subscription.first_delivery") %></legend> 
     <%= render :partial => 'subscription/picker' %> 
    <% end %> 
    </fieldset> 
</div> 

<div class="columns omega six" data-hook="subscription_dates_fieldset_wrapper"> 
    <fieldset id="subscription_dates" data-hook> 
    <legend align="center"><%= t(:"subscription.next_deliveries") %></legend> 
    <div class='dates'></div> 
    </fieldset> 
</div> 

<div class="form-buttons" data-hook="buttons" style='clear:both;'> 
    <%= submit_tag t(:save_and_continue), :class => 'continue button primary' %> 
</div> 

的意見/訂閱/ _picker.html.erb

<div class='row'> 
    <label for="subscription_frequency">Occurs every:</label> 
    <% frequency_options = [["2 Weeks", 14], ["3 Weeks", 21], ["Month", 30], ["2 Months", 60], ["3 Months", 90]] %> 
    <%= select(:subscription, :frequency, options_for_select(frequency_options, 30), {}) %> 
</div> 
<div id="start-date-picker" class="calendar"></div> 
<%= hidden_field(:subscription, :start_date, {value: (DateTime.now + 14).to_date.iso8601}) %> 

... JS that creates the calendar ... 

點擊'保存並繼續'我看到下面的參數發送:

{ 
        "utf8" => "✓", 
       "_method" => "put", 
    "authenticity_token" => "...BLAH...", 
      "subscription" => { 
     "frequency" => "30", 
     "start_date" => "2012-11-17" 
    }, 
       "commit" => "Save and Continue", 
      "controller" => "spree/checkout", 
       "action" => "update", 
       "state" => "subscription" 
} 
+0

你有裝修自動化測試嗎?你在測試中看到了什麼?記錄是否在單元測試級別保存? – WattsInABox

回答

3

你有兩個問題。您的回調不會觸發,因爲對checkout_flow的調用會擦除當前狀態機並將其替換。移動此代碼後您的來電checkout_flow:

Spree::Order.state_machine.after_transition :from => :subscription, 
              :do => :valid_subs? 

其次,你的訂閱信息不保存,因爲需要的參數作爲命令的一部分傳遞。該PARAMS應該出現嵌套這樣的:

"order" => {  
    "subscription" => { 
     "frequency" => "30", 
     "start_date" => "2012-11-17" 
    } 
} 

你可以做到這一點通過連接你的電話來選擇和hidden_​​field到適當的形式 'subscription_picker':

<%= subscription_picker.select(:frequency, options_for_select(frequency_options, 30), {}) %> 

<%= subscription_picker.hidden_field(:start_date, {value: (DateTime.now + 14).to_date.iso8601}) %> 

您可能應該將表單對象傳遞給您的部分作爲樣式和可理解性的顯式參數。

乾杯。