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



222
223
224
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 222

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



248
249
250
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 248

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

#custom_contextsObject



214
215
216
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 214

def custom_contexts
  @custom_contexts ||= []
end

#is_taggable?Boolean

Returns:

  • (Boolean)


218
219
220
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 218

def is_taggable?
  self.class.is_taggable?
end


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

def related_search_options(context, klass, options = {})
  tags_to_find = self.tags_on(context).collect { |t| t.name }

  { :select     => "#{klass.table_name}.*, COUNT(#{Tag.table_name}.id) AS count", 
    :from       => "#{klass.table_name}, #{Tag.table_name}, #{Tagging.table_name}",
    :conditions => ["#{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


261
262
263
264
265
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 261

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



306
307
308
309
310
311
312
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 306

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



278
279
280
281
282
283
284
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 278

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



286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 286

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
  end
  
  true
end

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



252
253
254
255
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 252

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

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



257
258
259
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 257

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



226
227
228
229
230
231
232
233
234
235
236
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 226

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

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

#tag_typesObject



210
211
212
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 210

def tag_types
  self.class.tag_types
end

#tags_on(context, owner = nil) ⇒ Object



238
239
240
241
242
243
244
245
246
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 238

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