Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How should I override SubscriptionsController? #165

Open
ericgross opened this issue Jun 17, 2016 · 5 comments
Open

How should I override SubscriptionsController? #165

ericgross opened this issue Jun 17, 2016 · 5 comments

Comments

@ericgross
Copy link

I'd like to set a different path in after_new_subscription_path - what is the best way to do that?

@ericgross
Copy link
Author

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?

@yas4891
Copy link
Collaborator

yas4891 commented Aug 7, 2016

@ericgross I suggest monkey-patching or submitting a PR where you change the koudoku code to allow that

@rvalyi
Copy link

rvalyi commented Aug 21, 2016

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

@gwalshington
Copy link

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.

@donnfelker
Copy link

@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! 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants