2012-11-04 11 views
12

私はSpree 1.2ストアに余分なステップを追加しようとしています。これにより、顧客はサブスクリプションを作成できます。私はステップを挿入し、正しいビューをレンダリングしましたが、ユーザーが「保存して続行」をクリックすると次のステップがレンダリングされますが、実際には何も保存されません。Spreeチェックアウトプロセスへのステップの追加

私が持っている現在では、私は私がstate_callbackを追加する必要があることを理解しますが、(それはかなり新しいですので、おそらく)私はこれを行う方法がわからないんだけどとシュプレードキュメントは非常にこの問題を回避欠けている

私の拡張機能で、以下:

accepts_nested_attributesが必要ですが、それが終わったので、このための私のdevのアプローチは、これまで試行錯誤してきました/ 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 

全くわからないこと

モデル/まくりますそこに泊まる。

モデルで

/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

あなたには2つの問題があります。 checkout_flowの呼び出しが現在の状態マシンを消去して置き換えるため、コールバックは起動しません。 checkout_flowためにあなたのコールの後にこのコードを移動:パラメータは注文の一部として渡す必要があるため

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

第二に、あなたのサブスクリプション情報が保存されていません。

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

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

:あなたは適切なフォームのsubscription_picker 'にあなたの選択の呼び出しとhidden_​​fieldを取り付けていることを行うことができます

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

:paramsがこのように入れ子に表示されますおそらくスタイルとわかりやすさのための明示的なパラメータとしてフォームオブジェクトを部分的に渡すべきです。

乾杯。

関連する問題