Class: Likeable::Like

Inherits:
Object
  • Object
show all
Defined in:
lib/likeable/like.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Like

Returns a new instance of Like.



6
7
8
9
10
11
# File 'lib/likeable/like.rb', line 6

def initialize(options = {})
  self.created_at = Time.at(options[:time].try(:to_f)||Time.now)
  self.target     = options[:target]
  self.user_id    = options[:user].try(:id) || options[:user_id]
  self.like_user  = options[:user]
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



4
5
6
# File 'lib/likeable/like.rb', line 4

def created_at
  @created_at
end

#like_userObject

Returns the value of attribute like_user.



4
5
6
# File 'lib/likeable/like.rb', line 4

def like_user
  @like_user
end

#targetObject

Returns the value of attribute target.



4
5
6
# File 'lib/likeable/like.rb', line 4

def target
  @target
end

#user_idObject

Returns the value of attribute user_id.



4
5
6
# File 'lib/likeable/like.rb', line 4

def user_id
  @user_id
end

Instance Method Details

#idObject



13
14
15
# File 'lib/likeable/like.rb', line 13

def id
  Digest::SHA1.hexdigest("#{user_id}#{target.class}#{target.id}#{created_at}")
end

#to_hash(type = :full) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/likeable/like.rb', line 23

def to_hash(type=:full)
  {
    :created_at => created_at.iso8601,
    :type       => target.class.name.gsub(/^[A-Za-z]+::/, '').underscore.downcase.to_sym,
    :target     => target.to_hash(type),
    :user       => user.to_hash(type)
  }
end

#userObject



17
18
19
20
21
# File 'lib/likeable/like.rb', line 17

def user
  @user ||= like_user
  @user ||= Likeable.find_one(Likeable.user_class, user_id)
  @user
end