2017-08-02 3 views
0

私のアプリでdevise gemを使用しています。サインイン中にユーザーモデルからメソッドを渡すために使用するコールバックは何ですか?ブール値をtrueまたはfalseに変更するためのログイン処理中のコールバック

Warden::Manager.after_set_user ? before_validation? before_save? before_create?

方法の詳細:ユーザーテーブルのis_active: 私はブール値の列を持っています。 ユーザーがサインインしようとするたびに、braintreeコードは、ユーザーがbraintree APIを介してアクティブな購読をしているかどうかを確認します。また、ユーザーがアクティブであるかどうかに関わらず、is_active列をtrueまたはfalseに更新しようとしています。

私は現在、これを使用していますが、それは働いていない:

Warden::Manager.after_set_user :scope => :user do |user, auth, opts| 

    customer = Braintree::Customer.find('customer_id') 
    customer_card = customer.payment_methods[0].token 
    payment_method = Braintree::PaymentMethod.find(customer_card) 
    sub = payment_method.subscriptions[0] 
    sub.status 

if Braintree::Subscription::Status::Active 
    obj = User.find_by_id(params[:id]) 
    obj.is_active = true 
    obj.save! 

else 
    obj = User.find_by_id(params[:id]) 
    obj.is_active = false 
    obj.save! 
end 
end 

def active_for_authentication? 
    super && is_active? 
end 

def inactive_message 
    "User not active, please subscribe!" 
end 

答えて

0

Braintree::Subscription::Status::Activeは単にconstant pointing to a string"Active"あるので、それは常に "truthy" です。これは、if Braintree::Subscription::Status::Activeが常に真であることを意味します。

はおそらく、代わりにこれをしたい:

user.update(is_active: sub.status == Braintree::Subscription::Status::Active) 

は必要ないという条件付きまたは明示的にブロックの状態に基づいてtrueまたはfalseを設定し、すでにブロックに渡されたUserインスタンスを持っているので、データベースからロードする必要はありません。

+0

返信いただきありがとうございます@coreyward!私が持っているもう一つの質問は 'Warden :: Manager.after_set_user'以外のコールバックを使うべきでしょうか? – Theopap

関連する問題