Sha256: 47caeab7051ac27ef211abc7f09cb92d8567627e42d48af55a973c403e3ed511

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

module Merit
  extend ActiveSupport::Concern

  module ClassMethods
    def has_merit(options = {})
      belongs_to :sash

      if Merit.orm == :mongo_mapper
        plugin Merit
        key :sash_id, String
        key :points, Integer, :default => 0
        key :level, Integer, :default => 0
      elsif Merit.orm == :mongoid
        field :sash_id
        field :points, :type => Integer, :default => 0
        field :level, :type => Integer, :default => 0
      end
    end

    def find_by_id(id)
      Merit.orm == :mongoid ? where(:_id => id).first : super(id)
    end
  end

  def badges
    create_sash_if_none
    sash.badge_ids.collect{|b_id| Badge.find(b_id) }
  end

  # Create sash if doesn't have
  def create_sash_if_none
    if self.sash.blank?
      self.sash = Sash.create
      self.save(:validate => false)
    end
  end
end

if Object.const_defined?('ActiveRecord')
  ActiveRecord::Base.send :include, Merit
end
if Object.const_defined?('MongoMapper')
  MongoMapper::Document.plugin Merit
end
if Object.const_defined?('Mongoid')
  Mongoid::Document.send :include, Merit
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
merit-0.9.5 lib/merit/model_additions.rb
merit-0.9.4 lib/merit/model_additions.rb
merit-0.9.3 lib/merit/model_additions.rb
merit-0.9.2 lib/merit/model_additions.rb
merit-0.9.1 lib/merit/model_additions.rb
merit-0.9.0 lib/merit/model_additions.rb
merit-0.8.1 lib/merit/model_additions.rb