Sha256: ab083447d2cc255734b69f0528a6d7741ecfc32253f8e223943298ba1578fd68

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

class Tag < ActiveRecord::Base
  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
    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
#  name_transcription :string
#  created_at         :datetime
#  updated_at         :datetime
#  taggings_count     :integer          default(0)
#

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
enju_bookmark-0.2.2 app/models/tag.rb
enju_bookmark-0.2.1 app/models/tag.rb
enju_bookmark-0.2.0 app/models/tag.rb
enju_bookmark-0.2.0.beta.5 app/models/tag.rb
enju_bookmark-0.2.0.beta.3 app/models/tag.rb