i automate task in rails app, create rake task, code need in controller action, call controller action instead of write "same" code in task , save lines of code. possible?
well, example. have usecase auto renew stripe customers subscriptions both rake tasks , using client side . now, write poro class below:
class autorenewsubscription attr_reader :coupon, :email def initialize args = {} @email = args[:email] @coupon = args[:coupon] #.... end def run! user_current_plan.toggle!(:auto_renew) case action when :resume resume_subscription when :cancel cancel_subscription end end #... more code need. end
you can put class inside app/services/auto_renew_subscription.rb
folder. class available globally. so, call inside controller :
class subscriptioncontroller < applicationcontroller def create #.. logic autorenewsubscription.new( coupon: "vxtyre", email: 'some@email.com' ).run! end end
and call rake task :
desc "this task auto renew user subscriptions" task :auto_renew => :environment puts "auto renew." autorenewsubscription.new( coupon: "vxtyre", email: 'some@email.com' ).run! end
this think way solve issue. hope idea. :)
Comments
Post a Comment