Module: ActiveRecord::Acts::TaggableOn::InstanceMethods
- Defined in:
- lib/acts_as_taggable_on/acts_as_taggable_on.rb
Instance Method Summary collapse
- #add_custom_context(value) ⇒ Object
- #cached_tag_list_on(context) ⇒ Object
- #custom_contexts ⇒ Object
- #is_taggable? ⇒ Boolean
- #related_search_options(context, klass, options = {}) ⇒ Object
- #related_tags_for(context, klass, options = {}) ⇒ Object
- #reload_with_tag_list(*args) ⇒ Object
- #save_cached_tag_list ⇒ Object
- #save_tags ⇒ Object
- #set_tag_list_on(context, new_list, tagger = nil) ⇒ Object
- #tag_counts_on(context, options = {}) ⇒ Object
- #tag_list_on(context, owner = nil) ⇒ Object
- #tag_types ⇒ Object
- #tags_on(context, owner = nil) ⇒ Object
Instance Method Details
#add_custom_context(value) ⇒ Object
266 267 268 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 266 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
292 293 294 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 292 def cached_tag_list_on(context) self["cached_#{context.to_s.singularize}_list"] end |
#custom_contexts ⇒ Object
258 259 260 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 258 def custom_contexts @custom_contexts ||= [] end |
#is_taggable? ⇒ Boolean
262 263 264 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 262 def is_taggable? self.class.is_taggable? end |
#related_search_options(context, klass, options = {}) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 311 def (context, klass, = {}) = self.(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 (?)", ], :group => "#{klass.table_name}.id", :order => "count DESC" }.update() end |
#related_tags_for(context, klass, options = {}) ⇒ Object
305 306 307 308 309 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 305 def (context, klass, = {}) search_conditions = (context, klass, ) klass.find(:all, search_conditions) end |
#reload_with_tag_list(*args) ⇒ Object
352 353 354 355 356 357 358 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 352 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_list ⇒ Object
324 325 326 327 328 329 330 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 324 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_tags ⇒ Object
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 332 def (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") - (tag_type).map(&:name) = (tag_type, owner).reject { |tag| instance_variable_get("@#{tag_type.singularize}_list").include?(tag.name) } self.class.transaction do .delete(*) if .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
296 297 298 299 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 296 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
301 302 303 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 301 def tag_counts_on(context, ={}) self.class.tag_counts_on(context, .merge(:id => self.id)) end |
#tag_list_on(context, owner = nil) ⇒ Object
270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 270 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(*(context, owner).map(&:name))) end end |
#tag_types ⇒ Object
254 255 256 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 254 def tag_types self.class.tag_types end |
#tags_on(context, owner = nil) ⇒ Object
282 283 284 285 286 287 288 289 290 |
# File 'lib/acts_as_taggable_on/acts_as_taggable_on.rb', line 282 def (context, owner=nil) if owner opts = {:conditions => ["#{Tagging.table_name}.context = ? AND #{Tagging.table_name}.tagger_id = ? AND #{Tagging.table_name}.tagger_type = ?", context.to_s, owner.id, owner.class.to_s]} else opts = {:conditions => ["#{Tagging.table_name}.context = ?", context.to_s]} end .find(:all, opts) end |