登录过程中的回调,以便将布尔值更改为 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

因为Braintree::Subscription::Status::Active只是一个指向字符串"Active"的常量,所以它总是"真实的"。这意味着if Braintree::Subscription::Status::Active永远是真的。

您可能希望改为执行以下操作:

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

无需根据块的条件进行条件或显式设置truefalse,并且您已经将User实例传递给块,因此无需从数据库加载一个实例。

最新更新