christospappas / streama

Streama is a simple activity stream gem for use with the Mongoid ODM framework.
MIT License
132 stars 30 forks source link

0.3.6 - NoMethodError: undefined method `find' for Object:Class #19

Closed antulik closed 11 years ago

antulik commented 12 years ago

Using mongoid 2.0. Works in steama 0.3.5, but not in 0.3.6.

NoMethodError: undefined method `find' for Object:Class
    from /Users/anton/.rvm/gems/ruby-1.9.3-p194@oddsense/gems/streama-0.3.6/lib/streama/activity.rb:90:in `load_instance'
    from /Users/anton/.rvm/gems/ruby-1.9.3-p194@oddsense/gems/streama-0.3.6/lib/streama/activity.rb:78:in `publish'
    from /Users/anton/.rvm/gems/ruby-1.9.3-p194@oddsense/gems/streama-0.3.6/lib/streama/activity.rb:55:in `publish'
    from /Users/anton/.rvm/gems/ruby-1.9.3-p194@oddsense/gems/streama-0.3.6/lib/streama/actor.rb:29:in `publish_activity'
    from (irb):3

did some debugging:

error happens here: https://github.com/christospappas/streama/blob/master/lib/streama/activity.rb#L90

    def load_instance(type)
      (data = self.read_attribute(type)).is_a?(Hash) ? data['type'].to_s.camelcase.constantize.find(data['id']) : data
    end

type == :actor self.read_attribute(type) returns hash which has '_type' but no 'type' key.

After plain a bit more and reverting that line https://github.com/christospappas/streama/commit/9010eebb8f509567adfb717fbd4e0c0a630ce835#L2R11

to this:

    field :actor,         :type => Hash

I have only actor but probably other fields will have the same issue.

Everything works after that reverted line. self.read_attribute(type) returns Object but not hash. Can't really explain that behaviour, but hopefully it will give you some ideas where to look.

khoan commented 12 years ago

This is how mongoid 2.0 set attributes

bam = Object.new
def bam.attributes
  {bam: "bam", _id: "123", _type: "Bam"}
end

class Bam
  include Mongoid::Document

  field :bam
  field :fam, type: Hash
end

result = Bam.new(bam: bam, fam: bam)
result.bam
# => {"bam" => "bam", "_id" => "123", "_type" => "Bam"}
result.fam
# => #<Object...>

I fathom that this is what is tripping streama load_instance. So if we patch up lib/streama/actor.rb , then should be better.

# Caveat: this is untested
#
# open up Streama::Actor and patch
#
# pristine code is located here lib/streama/actor.rb
module Streama
  module Actor
    def publish_activity(name, options={})
      options[:receivers] = self.send(options[:receivers]) if options[:receivers].is_a?(Symbol)
      data = {:actor => {"id" => self.id, "type" => self.class.name}}.merge(options) # explicitly set id and type key
      activity = activity_class.publish(name, data)
    end
  end
end
christospappas commented 11 years ago

Streama >= 3.6 requires Mongoid 3. The Gemfile has been updated to require this.