Reputation: 7956
My question is similar to this one, which doesn't have an answer.
I want to add after_filter :identify_with_segment, only: [:create]
to both my RegistrationsController < Devise::RegistrationsController
and SessionsController < Devise::SessionsController
. Since both of these controllers themselves inherit from DeviseController
, I thought that a DRY way to do this would be to extend DeviseController
and define the method there. However, I keep getting an unitialized constant
error.
My code:
class DeviseController < DeviseController
def identify_with_segment
..
end
end
I realize the way this class is defined just looks wrong. I've also tried class DeviseController < Devise::DeviseController
but that doesn't work either.
Can anyone explain the proper way to extend the DeviseController
that these other controllers depend on?
Upvotes: 0
Views: 199
Reputation: 23949
If you want to open DeviseController
, you can just try this:
class DeviseController
def identify_with_segment
# ...
end
end
Lots of confusing things are done in the name of strict DRY-ness, this may be one of those things. Sometimes repeating yourself makes things clear and understandable for the human that comes after you.
Another option would be to put this functionality into a concern, and include
that module in your RegistrationsController
and SessionsController
. Then it's explicit what you're doing and you aren't modifying classes that you don't own. Something like:
# app/controllers/concerns/whatever_it_is_this_is_doing.rb
module WhateverItIsThisIsDoing
extend ActiveSupport::Concern
def identify_with_segment
# ...
end
included do
after_filter :identify_with_segment, only: [:create]
end
end
# app/controllers/registrations_controller.rb
class RegistrationsController
include WhateverItIsThisIsDoing
end
# app/controllers/sessions_controller.rb
class SessionsController
include WhateverItIsThisIsDoing
end
Upvotes: 1