Sha256: 263d36b64931a4ecebd61a7d207dccd19157ec43f08a7da7a4212f7702dd9f07

Contents?: true

Size: 1.68 KB

Versions: 12

Compression:

Stored size: 1.68 KB

Contents

module Tagtical
  class Tagging < ::ActiveRecord::Base #:nodoc:
    include Tagtical::ActiveRecord::Backports if ::ActiveRecord::VERSION::MAJOR < 3

    attr_accessible :tag,
      :tag_id,
      :taggable,
      :taggable_type,
      :taggable_id,
      :tagger,
      :tagger_id,
      :relevance

    belongs_to :tag, :class_name => 'Tagtical::Tag'
    belongs_to :taggable, :polymorphic => true

    before_validation  { |record| record.relevance ||= default_relevance }

    validates_presence_of :tag_id, :relevance
    validates_uniqueness_of :tag_id, :scope => [:taggable_type, :taggable_id, :tagger_id]

    validate :validate_relevance

    if Tagtical.config.polymorphic_tagger?
      attr_accessible :tagger_type
      belongs_to :tagger, :polymorphic => true
    else
      belongs_to :tagger, case Tagtical.config.tagger
      when Hash then
        Tagtical.config.tagger
      when true then
        {:class_name => "User"} # default to using User class.
      when String then
        {:class_name => Tagtical.config.tagger}
      end
    end

    class_attribute :default_relevance, :instance_writer => false
    self.default_relevance = 1

    def <=>(tagging)
      relevance <=> tagging.relevance
    end

    def set_tag_target_with_relevance(tag)
      if tag
        tag = tag.dup
        tag.relevance = relevance
        tag[:tagger_id] = tagger_id
      end
      set_tag_target_without_relevance(tag)
    end

    alias_method_chain :set_tag_target, :relevance

    private

    def validate_relevance
      if range = Tagtical::Tag.relevance_range
        errors[:relevance] = "must be between #{range.begin} and #{range.end}" unless range.include?(relevance)
      end
    end

  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
tagtical-1.5.8 lib/tagtical/tagging.rb
tagtical-1.5.7 lib/tagtical/tagging.rb
tagtical-1.5.6 lib/tagtical/tagging.rb
tagtical-1.5.5 lib/tagtical/tagging.rb
tagtical-1.5.4 lib/tagtical/tagging.rb
tagtical-1.5.3 lib/tagtical/tagging.rb
tagtical-1.5.2 lib/tagtical/tagging.rb
tagtical-1.5.1 lib/tagtical/tagging.rb
tagtical-1.5.0 lib/tagtical/tagging.rb
tagtical-1.4.12 lib/tagtical/tagging.rb
tagtical-1.4.11 lib/tagtical/tagging.rb
tagtical-1.4.10 lib/tagtical/tagging.rb