Sha256: be1c130586d67dcfd095b5b8130c0e941f81a57e86a8e32169e94363fd855816

Contents?: true

Size: 1.4 KB

Versions: 10

Compression:

Stored size: 1.4 KB

Contents

class Tag < ActiveRecord::Base
  attr_accessible :name, :name_transcription
  has_many :taggings, :dependent => :destroy, :class_name => 'ActsAsTaggableOn::Tagging'
  validates :name, :presence => true
  after_save :save_taggings
  after_destroy :save_taggings

  extend FriendlyId
  friendly_id :name

  searchable do
    text :name
    string :name
    time :created_at
    time :updated_at
    integer :bookmark_ids, :multiple => true do
      tagged(Bookmark).compact.collect(&:id)
    end
    integer :taggings_count do
      taggings.size
    end
  end

  paginates_per 10

  def self.bookmarked(bookmark_ids, options = {})
    count = Tag.count
    count = Tag.default_per_page if count == 0
    unless bookmark_ids.empty?
      tags = Tag.search do
        with(:bookmark_ids).any_of bookmark_ids
        order_by :taggings_count, :desc
        paginate(:page => 1, :per_page => count)
      end.results
    end
  end

  def save_taggings
    self.taggings.collect(&:taggable).each do |t| t.save end
  end

  def tagged(taggable_type)
    self.taggings.where(:taggable_type => taggable_type.to_s).includes(:taggable).collect(&:taggable)
  end
end

# == Schema Information
#
# Table name: tags
#
#  id                 :integer          not null, primary key
#  name               :string(255)
#  name_transcription :string(255)
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
enju_bookmark-0.1.2.pre14 app/models/tag.rb
enju_bookmark-0.1.2.pre13 app/models/tag.rb
enju_bookmark-0.1.2.pre12 app/models/tag.rb
enju_bookmark-0.1.2.pre11 app/models/tag.rb
enju_bookmark-0.1.2.pre10 app/models/tag.rb
enju_bookmark-0.1.2.pre9 app/models/tag.rb
enju_bookmark-0.1.2.pre8 app/models/tag.rb
enju_bookmark-0.1.2.pre7 app/models/tag.rb
enju_bookmark-0.1.2.pre6 app/models/tag.rb
enju_bookmark-0.1.2.pre5 app/models/tag.rb