Module: ActiveRecord::Acts::TaggableOn::InstanceMethods

Defined in:
lib/acts_as_taggable_on/acts_as_taggable_on.rb

Instance Method Summary collapse

Instance Method Details

#add_custom_context(value) ⇒ Object



232
233
234
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 232

def add_custom_context(value)
  custom_contexts << value.to_s unless custom_contexts.include?(value.to_s) or self.class.tag_types.map(&:to_s).include?(value.to_s)
end

#cached_tag_list_on(context) ⇒ Object



264
265
266
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 264

def cached_tag_list_on(context)
  self["cached_#{context.to_s.singularize}_list"]
end

#custom_contextsObject



224
225
226
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 224

def custom_contexts
  @custom_contexts ||= []
end

#is_taggable?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 228

def is_taggable?
  self.class.is_taggable?
end


288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 288

def related_search_options(context, klass, options = {})
  tags_to_find = self.tags_on(context).collect { |t| t.name }
  
  exclude_self = "#{klass.table_name}.id != #{self.id} AND" if self.class == klass

  { :select     => "#{klass.table_name}.*, COUNT(#{Tag.table_name}.id) AS count", 
    :from       => "#{klass.table_name}, #{Tag.table_name}, #{Tagging.table_name}",
    :conditions => ["#{exclude_self} #{klass.table_name}.id = #{Tagging.table_name}.taggable_id AND #{Tagging.table_name}.taggable_type = '#{klass.to_s}' AND #{Tagging.table_name}.tag_id = #{Tag.table_name}.id AND #{Tag.table_name}.name IN (?)", tags_to_find],
    :group      => "#{klass.table_name}.id",
    :order      => "count DESC"
  }.update(options)
end


282
283
284
285
286
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 282

def related_tags_for(context, klass, options = {})
  search_conditions = related_search_options(context, klass, options)

  klass.find(:all, search_conditions)
end

#reload_with_tag_list(*args) ⇒ Object



331
332
333
334
335
336
337
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 331

def reload_with_tag_list(*args)
  self.class.tag_types.each do |tag_type|
    self.instance_variable_set("@#{tag_type.to_s.singularize}_list", nil)
  end
  
  reload_without_tag_list(*args)
end

#save_cached_tag_listObject



301
302
303
304
305
306
307
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 301

def save_cached_tag_list
  self.class.tag_types.map(&:to_s).each do |tag_type|
    if self.class.send("caching_#{tag_type.singularize}_list?")
      self["cached_#{tag_type.singularize}_list"] = send("#{tag_type.singularize}_list").to_s
    end
  end
end

#save_tagsObject



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 309

def save_tags          
  (custom_contexts + self.class.tag_types.map(&:to_s)).each do |tag_type|
    next unless instance_variable_get("@#{tag_type.singularize}_list")
    owner = instance_variable_get("@#{tag_type.singularize}_list").owner
    new_tag_names = instance_variable_get("@#{tag_type.singularize}_list") - tags_on(tag_type).map(&:name)
    old_tags = tags_on(tag_type).reject { |tag| instance_variable_get("@#{tag_type.singularize}_list").include?(tag.name) }
  
    self.class.transaction do
      base_tags.delete(*old_tags) if old_tags.any?
      new_tag_names.each do |new_tag_name|
        new_tag = Tag.find_or_create_with_like_by_name(new_tag_name)
        Tagging.create(:tag_id => new_tag.id, :context => tag_type, 
                       :taggable => self, :tagger => owner)
      end
    end
    
    instance_variable_set("@#{tag_type.singularize}_list_changed", false)
  end
  
  true
end

#set_tag_list_on(context, new_list, tagger = nil) ⇒ Object



268
269
270
271
272
273
274
275
276
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 268

def set_tag_list_on(context,new_list, tagger=nil)
  old_list = instance_variable_get("@#{context.to_s.singularize}_list")
  instance_variable_set("@#{context.to_s.singularize}_list", TagList.from_owner(tagger, new_list))
  add_custom_context(context)
  
  if old_list != instance_variable_get("@#{context.to_s.singularize}_list")
    instance_variable_set("@#{context.to_s.singularize}_list_changed", true)
  end
end

#tag_counts_on(context, options = {}) ⇒ Object



278
279
280
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 278

def tag_counts_on(context,options={})
  self.class.tag_counts_on(context,{:conditions => ["#{Tag.table_name}.name IN (?)", tag_list_on(context)]}.reverse_merge!(options))
end

#tag_list_on(context, owner = nil) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 236

def tag_list_on(context, owner=nil)
  var_name = context.to_s.singularize + "_list"
  add_custom_context(context)
  list = instance_variable_get("@#{var_name}")

  if list.nil?
    if !owner && self.class.caching_tag_list_on?(context) and !(cached_value = cached_tag_list_on(context)).nil?
      list = instance_variable_set("@#{var_name}", TagList.from(self["cached_#{var_name}"]))
    else
      list = instance_variable_set("@#{var_name}", TagList.new(*tags_on(context, owner).map(&:name)))
    end
  end

  list.context = context.to_s
  list.taggable = self
  list
end

#tag_typesObject



220
221
222
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 220

def tag_types
  self.class.tag_types
end

#tags_on(context, owner = nil) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 254

def tags_on(context, owner=nil)
  if owner
    opts = {:conditions => ["context = ? AND tagger_id = ? AND tagger_type = ?",
                            context.to_s, owner.id, owner.class.to_s]}
  else
    opts = {:conditions => ["context = ?", context.to_s]}
  end
  base_tags.find(:all, opts)
end