Skip to content
turadg edited this page Nov 18, 2012 · 3 revisions

Welcome to the thumbs_up wiki!

Examples

Permission to vote more than once

  # One vote per user, unless editor role or higher
  validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id],
    message: "only one vote allowed",
    unless: Proc.new { |vote| vote.voter.role? :editor }

Permission to vote once daily

(contributed in #54)

module ThumbsUp
  module ActsAsVoter
    module InstanceMethods
      def voted_for_today?(voteable)
        0 < Vote.where(
          :voter_id => self.id,
          :voter_type => self.class.base_class.name,
          :voteable_id => voteable.id,
          :voteable_type => voteable.class.base_class.name,
          :day_created_at => DateTime.now.utc.midnight
        ).count
      end
    end
  end
end