Overriding Devise::Models::Trackable

1,507 views
Skip to first unread message

Arthur Chui

unread,
Dec 5, 2012, 7:43:38 PM12/5/12
to plataforma...@googlegroups.com
I need to track a few more things about a user after sign up. What is the best way to override the module? If I create a new module in the app, how is it being included into the User model? 

Please advise. Many thanks!

Arthur

Carlos Antonio da Silva

unread,
Dec 6, 2012, 8:30:04 AM12/6/12
to Devise
Just override this method in your model and do whatever you want to do before calling super: https://github.com/plataformatec/devise/blob/master/lib/devise/models/trackable.rb#L18

    def update_tracked_fields!(*)
      self.foo = 'bar'
      super
    end



Arthur

--
 
 
 



--
At.
Carlos Antonio

Arthur Chui

unread,
Dec 6, 2012, 7:07:38 PM12/6/12
to plataforma...@googlegroups.com
Thanks Carlos. That's very easy to inherit the method in models.

Does Devise provide a way to add custom hook like Trackable? Say I want to build more own "Whatever-able". Is there a way to add the hook and module from my app?

Carlos Antonio da Silva

unread,
Dec 7, 2012, 6:09:47 AM12/7/12
to Devise
Sure, there is a way. But it's not provided by devise itself, but by warden, the internal lib devise uses. See for instance our trackable module: https://github.com/plataformatec/devise/blob/master/lib/devise/hooks/trackable.rb

Check out this wiki page on warden to see more about these hooks/callbacks: https://github.com/hassox/warden/wiki/Callbacks

If you want to add a full module, say with a model module to be included, routes and so on, you'll have to rely on Devise.add_module: https://github.com/plataformatec/devise/blob/master/lib/devise.rb#L302. Check the docs and other modules to see examples on how to use it.

On Thu, Dec 6, 2012 at 10:07 PM, Arthur Chui <arthu...@reverbhq.com> wrote:
to



--
At.
Carlos Antonio

Bruno Machado

unread,
Sep 18, 2013, 2:10:26 PM9/18/13
to plataforma...@googlegroups.com
Hi Carlos,
How can I change the way devise saves the user current_ip?
I am using cloudflare and request.remote_ip holds one of cloudflares CDN and not the real user IP, which is in anohter header var "HTTP_CF_CONNECTING_IP".

thanks in advance,

Vasiliy Ermolovich

unread,
Sep 18, 2013, 3:04:58 PM9/18/13
to plataforma...@googlegroups.com
I think you can redefine `update_tracked_fields!` method in User model and change https://github.com/plataformatec/devise/blob/master/lib/devise/models/trackable.rb#L23 this line to getting IP from `request.headers` or something like this.

Bruno Machado wrote:

Hi Carlos,
How can I change the way devise saves the user current_ip?
I am using cloudflare and request.remote_ip holds one of cloudflares
CDN and not the real user IP, which is in anohter header var
"HTTP_CF_CONNECTING_IP".

thanks in advance,


On Friday, December 7, 2012 11:09:47 AM UTC, Carlos Antonio da Silva
wrote:

    Sure, there is a way. But it's not provided by devise itself, but
    by warden, the internal lib devise uses. See for instance our
    trackable module:
    https://github.com/plataformatec/devise/blob/master/lib/devise/hooks/trackable.rb
    <https://github.com/plataformatec/devise/blob/master/lib/devise/hooks/trackable.rb>

    Check out this wiki page on warden to see more about these
    hooks/callbacks: https://github.com/hassox/warden/wiki/Callbacks
    <https://github.com/hassox/warden/wiki/Callbacks>

    If you want to add a full module, say with a model module to be
    included, routes and so on, you'll have to rely on
    Devise.add_module:
    https://github.com/plataformatec/devise/blob/master/lib/devise.rb#L302

    Check the docs and other modules to see examples on how to use it.

    On Thu, Dec 6, 2012 at 10:07 PM, Arthur Chui
    <arthu...@reverbhq.com <javascript:>> wrote:

        to




    --
    At.
    Carlos Antonio

--

---
You received this message because you are subscribed to the Google
Groups "Devise" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to plataformatec-de...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Bruno Machado

unread,
Sep 18, 2013, 6:19:01 PM9/18/13
to plataforma...@googlegroups.com
Thanks a lot, I'll try and give feedback.


Bruno Machado



--
 
---
You received this message because you are subscribed to a topic in the Google Groups "Devise" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/plataformatec-devise/aCuFLef16Sk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to plataformatec-de...@googlegroups.com.

Bruno Machado

unread,
Sep 18, 2013, 6:38:03 PM9/18/13
to plataforma...@googlegroups.com
Thanks, that worked perfectly!

Bruno Machado

Reply all
Reply to author
Forward
0 new messages