Sha256: fcbcd826c02d4dc58c4daff4bab0467844978145164bf2c3f603547283ccc4c3

Contents?: true

Size: 1.17 KB

Versions: 6

Compression:

Stored size: 1.17 KB

Contents

class Badge < ActiveRecord::Base
  has_many :badges_sashes
  has_many :sashes, :through => :badges_sashes

  # Grant badge to sash
  def grant_to(object_or_sash)
    object_or_sash.create_sash_if_none unless object_or_sash.kind_of?(Sash)
    sash = object_or_sash.respond_to?(:sash) ? object_or_sash.sash : object_or_sash
    unless sash.badges.include? self
      sash.badges << self
      sash.save
    end
  end

  # Take out badge from sash
  def delete_from(object_or_sash)
    object_or_sash.create_sash_if_none unless object_or_sash.kind_of?(Sash)
    sash = object_or_sash.respond_to?(:sash) ? object_or_sash.sash : object_or_sash
    if sash.badges.include? self
      sash.badges -= [self]
      sash.save
    end
  end

  # Give rank to sash if it's greater. Delete lower ranks it may have.
  def grant_rank_to(sash)
    # Grant to sash if had lower rank. Do nothing if has same or greater rank.
    if sash.has_lower_rank_than(self)
      sash.badges -= Badge.where(:name => name) # Clean up old ranks
      sash.badges << self
      sash.save
    end
  end

  def self.latest(limit = nil)
    scope = order('created_at DESC')
    limit.present? ? scope.limit(limit) : scope
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
merit-0.2.5 app/models/badge.rb
merit-0.2.4 app/models/badge.rb
merit-0.2.2 app/models/badge.rb
merit-0.2.1 app/models/badge.rb
merit-0.2.0 app/models/badge.rb
merit-0.1.3 app/models/badge.rb