Class: Like

Inherits:
Object
  • Object
show all
Defined in:
app/models/like.rb

Overview

Convenience class for managing like activities

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(activity) ⇒ Like

Initialize a new like activity



29
30
31
# File 'app/models/like.rb', line 29

def initialize(activity)
  @like = activity
end

Instance Attribute Details

#objectObject (readonly)

The object that is liked. It can be an activity



38
39
40
# File 'app/models/like.rb', line 38

def object
  @object
end

Class Method Details

.build(subject, user, object) ⇒ Object



21
22
23
24
25
# File 'app/models/like.rb', line 21

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

.find(subject, object) ⇒ Object

Find the children activity of activity_id liked by subject



7
8
9
10
11
12
13
# File 'app/models/like.rb', line 7

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

.find!(subject, object) ⇒ Object

Like #find but raises error if not found



16
17
18
19
# File 'app/models/like.rb', line 16

def find!(subject, object)
  find(subject, object) ||
    raise(ActiveRecord::RecordNotFound)
end

Instance Method Details

#destroyObject



51
52
53
# File 'app/models/like.rb', line 51

def destroy
  @like.destroy
end

#saveObject



33
34
35
# File 'app/models/like.rb', line 33

def save
  @like.save
end