kristianmandrup/mongoid-geo

mongoid-geo + omniauth

TeuF opened this issue · 6 comments

TeuF commented

Hi,

I'm trying to use mongoid-geo with an existant project using omniauth.
But with mongoid-geo, I can't login any more.

Any idea ?

Thanks,

/Users/home/.bundler/ruby/1.9.1/mongoid-geo-b7e3f9fe78ae/lib/mongoid/geo/fields.rb:19:in `block (2 levels) in create_accessors'
devise (1.2.rc) lib/devise/models/trackable.rb:16:in `update_tracked_fields!'
mongoid (2.0.0.rc.7) lib/mongoid/relations/proxy.rb:124:in `method_missing'
devise (1.2.rc) lib/devise/hooks/trackable.rb:7:in `block in '
warden (1.0.3) lib/warden/hooks.rb:14:in `call'
warden (1.0.3) lib/warden/hooks.rb:14:in `block in _run_callbacks'
warden (1.0.3) lib/warden/hooks.rb:9:in `each'
warden (1.0.3) lib/warden/hooks.rb:9:in `_run_callbacks'
warden (1.0.3) lib/warden/manager.rb:53:in `_run_callbacks'
warden (1.0.3) lib/warden/proxy.rb:164:in `set_user'
devise (1.2.rc) lib/devise/controllers/helpers.rb:112:in `sign_in'
devise (1.2.rc) lib/devise/controllers/helpers.rb:204:in `sign_in_and_redirect'
app/controllers/authentications_controller.rb:13:in `create'
...

I guess my monkey-patching of the fields setter is not stable in all cases. Feel free to have a look in the code and see if you can come up with a patch that works for you. Otherwise I recommend leaving out fields.rb from the require and then explicitly require this file if/when you need it.

My latest commit to master should "release" fields, I hope you can patch it so it can be included back in ;) Just clone the repo and reference it from your rails app using the :path option and I'm sure you can debug it and find an elegant little solution.

TeuF commented

I will try this week end. Not sure I find it, will do my best ;)

I think this should fix it ;)

      def create_accessors(name, meth, options = {})
        generated_field_methods.module_eval do
          define_method(meth) { read_attribute(name) }
          define_method("#{meth}=") do |value| 
            if options[:type] == Array && options[:geo]
              value = case value
              when String 
                value.split(",").map(&:to_f)
              when Array 
                value.compact.extend(Mongoid::Geo::Point).to_points
              else
                !value.nil? ? value.extend(Mongoid::Geo::Point).to_point : value
              end
              value = value[0..1] if !value.nil?
            end            
            write_attribute(name, value) 
          end
          define_method("#{meth}?") do
            attr = read_attribute(name)
            (options[:type] == Boolean) ? attr == true : attr.present?
          end
        end

I just moved value = value[0..1] if !value.nil? into the if block.

So you can just use the latest mongoid_geo via the :github option, sth like this:

gem 'mongoid_geo', '>= 0.1.2', :git => 'https://github.com/kristianmandrup/mongoid_geo.git'

Then for now in your code somewhere, fx in a Rails initializer

require 'mongoid/geo/fields'

If you want/need to use the special :geo option on your location field attribute

TeuF commented

Oooh yeah! That's great. Thanks a lot.