Sha256: a392af01f1c9f2189717903f7f13ef1ee4e94b8294f16a3014be1fc9c85cfdc4

Contents?: true

Size: 1.14 KB

Versions: 58

Compression:

Stored size: 1.14 KB

Contents

# Convenience class for managing like activities
class Like
  attr_reader :object

  class << self
    # Find the children activity of activity_id liked by subject
    def find(subject, object)
      like = new(object.liked_by(subject).readonly(false).first)
      return nil if like.object.nil?
      # Cache object to make it available before it is destroyed
      like.object
      like
    end

    # Like #find but raises error if not found
    def find!(subject, object)
      find(subject, object) ||
        raise(ActiveRecord::RecordNotFound)
    end

    def build(subject, user, object)
       l = self.find(subject,object)
       l = new object.new_like(subject, user) if l.nil?
       l
    end
  end

  # Initialize a new like activity
  def initialize(activity)
    @like = activity
  end

  def save
    @like.save
  end
  
  # The object that is liked. It can be an activity
  def object
    @object ||=
      if @like.nil?
        nil
      elsif @like.is_root?
        obj = @like.direct_object
        obj = obj.subject if obj.is_a?(Actor)
        obj
      else
        @like.parent
      end
  end
  
  def destroy
    @like.destroy
  end
end

Version data entries

58 entries across 58 versions & 2 rubygems

Version Path
social_stream-2.2.2 base/app/models/like.rb
social_stream-base-2.2.2 app/models/like.rb
social_stream-2.2.1 base/app/models/like.rb
social_stream-base-2.2.1 app/models/like.rb
social_stream-2.2.0 base/app/models/like.rb
social_stream-base-2.2.0 app/models/like.rb
social_stream-2.1.1 base/app/models/like.rb
social_stream-1.1.12 base/app/models/like.rb
social_stream-base-1.1.11 app/models/like.rb
social_stream-2.1.0 base/app/models/like.rb
social_stream-base-2.1.0 app/models/like.rb
social_stream-2.0.4 base/app/models/like.rb
social_stream-base-2.0.4 app/models/like.rb
social_stream-2.0.3 base/app/models/like.rb
social_stream-base-2.0.3 app/models/like.rb
social_stream-1.1.11 base/app/models/like.rb
social_stream-base-1.1.10 app/models/like.rb
social_stream-1.1.10 base/app/models/like.rb
social_stream-base-1.1.9 app/models/like.rb
social_stream-1.1.9 base/app/models/like.rb