module SocMed::Concerns::Likeable

Public Instance Methods

likeable(*attributes) click to toggle source
# File lib/soc_med/concerns/likeable.rb, line 12
def likeable(*attributes)
  attributes = [attributes] unless attributes.is_a?(Array)

  attributes.each do |attribute|
    class_eval "has_many :by_liking_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Like', foreign_key: :owner_id, dependent: :destroy"
    class_eval "has_many :liked_#{attribute}, through: :by_liking_#{attribute}, class_name: '#{attribute.to_s.classify}'"
  end
end
liker(*attributes) click to toggle source
# File lib/soc_med/concerns/likeable.rb, line 21
def liker(*attributes)
  attributes = [attributes] unless attributes.is_a?(Array)

  attributes.each do |attribute|
    class_eval "has_many :by_liked_#{attribute}, -> { order(created_at: :desc) }, class_name: 'SocMed::Like', foreign_key: :target_id, dependent: :destroy"
    class_eval "has_many :liked_by_#{attribute}, through: :by_liked_#{attribute}, class_name: '#{attribute.to_s.classify}'"
  end
end