Module: ActsAsLikeable::Liker

Defined in:
lib/acts_as_likeable/liker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
# File 'lib/acts_as_likeable/liker.rb', line 4

def self.included(base)
  base.class_eval do
    belongs_to :liker, :polymorphic => true
    has_many :likes, :as => :liker, :dependent => :delete_all do
      def likeables
        includes(:likeable).map(&:likeable)
      end
    end
  end
end

Instance Method Details

#find_likes(extra_conditions = {}) ⇒ Object



41
42
43
# File 'lib/acts_as_likeable/liker.rb', line 41

def find_likes(extra_conditions = {})
  self.likes.where(extra_conditions)
end

#like(args = {}) ⇒ Object

saving



31
32
33
34
# File 'lib/acts_as_likeable/liker.rb', line 31

def like args = {}
  return nil if args[:likeable].nil?
  args[:likeable].like args.merge({:liker => self})
end

#liked(model = nil) ⇒ Object



16
17
18
# File 'lib/acts_as_likeable/liker.rb', line 16

def liked model=nil
  like :likeable => model
end

#liked?(likeable) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
# File 'lib/acts_as_likeable/liker.rb', line 24

def liked? likeable
   = find_likes(:likeable_id => likeable.id, :likeable_type => likeable.class.name)
  .size > 0
end

#unlike(args = {}) ⇒ Object



36
37
38
39
# File 'lib/acts_as_likeable/liker.rb', line 36

def unlike args = {}
  return false if args[:likeable].nil?
  args[:likeable].unlike args.merge({:liker => self})
end

#unliked(model = nil) ⇒ Object



20
21
22
# File 'lib/acts_as_likeable/liker.rb', line 20

def unliked model=nil
  unlike :likeable => model
end