koudoku
koudoku copied to clipboard
How should I override SubscriptionsController?
I'd like to set a different path in after_new_subscription_path
- what is the best way to do that?
I see that I can override this by setting after_new_subscription_path
in my ApplicationController
. How though would I override other things that are not referencing super?
@ericgross I suggest monkey-patching or submitting a PR where you change the koudoku code to allow that
I did that putting this Gem in the Gemfile of my app:
gem "rails_engine_decorators", git: 'https://github.com/atd/rails_engine_decorators.git'
and creating a file called app/decorators/controllers/koudoku/subscriptions_controller_decorator.rb with custom overrides such as:
Koudoku::SubscriptionsController.class_eval do
def index
# don't bother showing the index if they've already got a subscription.
if current_owner and current_owner.subscription.present?
redirect_to koudoku.edit_owner_subscription_path(current_owner, current_owner.subscription)
end
# Don't prep a subscription unless a user is authenticated.
unless no_owner?
# we should also set the owner of the subscription here.
@subscription = ::Subscription.new({Koudoku.owner_id_sym => @owner.id})
@subscription.subscription_owner = @owner
end
render layout: 'landing_page' # THIS IS CUSTOM
end
Hopes this helps
I recently implemented gem "rails_engine_decorators" for the reason above, and besides local development, it is incompatible with Rails 5. It gives the error DEPRECATION WARNING: alias_method_chain is deprecated. Just a heads up to anyone else who finds this.
@gwalshington I'm also on Rails 5. What you can do is, and which is actually much easier, is to
provide a custom after_new_subscription_path
method in your root ApplicationController
.
This works because if you look at Koudoku's Subscription Controller here: https://github.com/andrewculver/koudoku/blob/master/app/controllers/koudoku/subscriptions_controller.rb#L178 it checks for a superclass delegate method. If the superclass does not have an implementation of after_new_subscription_path
then it will use the default path that is built into Koudoku.
To use your own path add the following to your ApplicationController
(in your app):
def after_new_subscription_path(owner, subscription)
# Decide where you want to send the user.
# This is using the root / url as an example
main_app.root_path
end
Now, when a new subscription is created your code will be invoked because https://github.com/andrewculver/koudoku/blob/master/app/controllers/koudoku/subscriptions_controller.rb#L178 see's that you have an implementation in the superclass.
I hope that helps! 😄