Sha256: 85053ca59a64316509fb5b9423cbc85856a0e35d9b5ee02564ceda3479822f29

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

module ActsAsStream
  module StreamActivity

    def package options = {}
      options.assert_valid_keys(:who, :action, :time, :object)
      raise "You need at least a :who and an :action! to create an activity package" if options[:who].nil? or options[:action].nil?
      opts = {:time => Time.now.to_i}.merge options
      # Try to ensure :time is in seconds
      opts[:time] = opts[:time].to_i if opts[:time].is_a?(Time)

      [:who, :object].each do |opt|
        #unless we are ignoring the stream hash for this object, use StreamableObject.stream_hash
#        begin
          begin
            opts[opt] = opts[opt].to_stream_hash
          rescue NoMethodError
            raise "The model #{opts[opt].class.name} does not have a method called #{opts[opt].activity_attr}. Perhaps you should look at your ActsAsStream configuration, or set :activity_attr"
          end
#        rescue NoMethodError
#          raise "The model #{opts[opt].class.name} does not respond to the :to_stream_hash method. Make that happen, or use a custom packager"
#        end
      end
      # then, if everything is fine, bundle it up into a JSON string
      opts.to_json
    end
    
    def parse package
      package = JSON.parse(package)
      package.keys.each{|k| package[k.to_sym] = package[k]; package.delete(k)}

      # Try to cast :who and :object to instances, cowardly return nil if the record is not found, because it might have
      # been deleted from the SQL database and not de-registered from Redis
      begin
        package[:who].keys.each{|k| package[:who] = k.titleize.constantize.find(package[:who][k]["id"].to_i)}
      rescue ActiveRecord::RecordNotFound
        return nil
      end

      begin
        package[:object].keys.each{|k| package[:object] = k.titleize.constantize.find(package[:object][k]["id"].to_i)}
      rescue ActiveRecord::RecordNotFound
        return nil
      end
      package[:time] = Time.at package[:time].to_i
      package
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts_as_stream-0.0.4.rc.3 lib/acts_as_stream/stream_activity.rb
acts_as_stream-0.0.4.rc.2 lib/acts_as_stream/stream_activity.rb