Module: ActsAsTaggableOn::Taggable::Core
- Defined in:
- lib/acts_as_taggable_on/taggable/core.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #add_custom_context(value) ⇒ Object
- #all_tags_list_on(context) ⇒ Object
-
#all_tags_on(context) ⇒ Object
Returns all tags of a given context.
- #cached_tag_list_on(context) ⇒ Object
- #custom_contexts ⇒ Object
-
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause.
- #is_taggable? ⇒ Boolean
-
#load_tags(tag_list) ⇒ Object
Find existing tags or create non-existing tags.
- #process_dirty_object(context, new_list) ⇒ Object
- #reload(*args) ⇒ Object
- #save_tags ⇒ Object
- #set_tag_list_on(context, new_list) ⇒ Object
- #tag_list_cache_on(context) ⇒ Object
- #tag_list_cache_set_on(context) ⇒ Object
- #tag_list_on(context) ⇒ Object
- #tagging_contexts ⇒ Object
-
#tags_on(context) ⇒ Object
Returns all tags that are not owned of a given context.
Class Method Details
.included(base) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 3 def self.included(base) base.extend ActsAsTaggableOn::Taggable::Core::ClassMethods base.class_eval do attr_writer :custom_contexts after_save :save_tags end base.initialize_acts_as_taggable_on_core end |
Instance Method Details
#add_custom_context(value) ⇒ Object
264 265 266 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 264 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 |
#all_tags_list_on(context) ⇒ Object
293 294 295 296 297 298 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 293 def (context) variable_name = "@all_#{context.to_s.singularize}_list" return instance_variable_get(variable_name) if instance_variable_defined?(variable_name) && instance_variable_get(variable_name) instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new((context).map(&:name)).freeze) end |
#all_tags_on(context) ⇒ Object
Returns all tags of a given context
302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 302 def (context) tagging_table_name = ActsAsTaggableOn::Tagging.table_name opts = ["#{tagging_table_name}.context = ?", context.to_s] scope = .where(opts) if ActsAsTaggableOn::Utils.using_postgresql? group_columns = grouped_column_names_for(ActsAsTaggableOn::Tag) scope.order("max(#{tagging_table_name}.created_at)").group(group_columns) else scope.group("#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}") end.to_a end |
#cached_tag_list_on(context) ⇒ Object
268 269 270 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 268 def cached_tag_list_on(context) self["cached_#{context.to_s.singularize}_list"] end |
#custom_contexts ⇒ Object
256 257 258 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 256 def custom_contexts @custom_contexts ||= [] end |
#grouped_column_names_for(object) ⇒ Object
all column names are necessary for PostgreSQL group clause
252 253 254 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 252 def grouped_column_names_for(object) self.class.grouped_column_names_for(object) end |
#is_taggable? ⇒ Boolean
260 261 262 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 260 def is_taggable? self.class.is_taggable? end |
#load_tags(tag_list) ⇒ Object
Find existing tags or create non-existing tags
368 369 370 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 368 def (tag_list) ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list) end |
#process_dirty_object(context, new_list) ⇒ Object
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 339 def process_dirty_object(context, new_list) value = new_list.is_a?(Array) ? ActsAsTaggableOn::TagList.new(new_list) : new_list attrib = "#{context.to_s.singularize}_list" if changed_attributes.include?(attrib) # The attribute already has an unsaved change. old = changed_attributes[attrib] @changed_attributes.delete(attrib) if old.to_s == value.to_s else old = tag_list_on(context) if self.class.preserve_tag_order @changed_attributes[attrib] = old if old.to_s != value.to_s else @changed_attributes[attrib] = old.to_s if old.sort != ActsAsTaggableOn.default_parser.new(value).parse.sort end end end |
#reload(*args) ⇒ Object
357 358 359 360 361 362 363 364 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 357 def reload(*args) self.class.tag_types.each do |context| instance_variable_set("@#{context.to_s.singularize}_list", nil) instance_variable_set("@all_#{context.to_s.singularize}_list", nil) end super(*args) end |
#save_tags ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 372 def tagging_contexts.each do |context| next unless tag_list_cache_set_on(context) # List of currently assigned tag names tag_list = tag_list_cache_on(context).uniq # Find existing tags or create non-existing tags: = (tag_list, context) # Tag objects for currently assigned tags = (context) # Tag maintenance based on whether preserving the created order of tags if self.class.preserve_tag_order? , = - , - = & if .any? && [0....size] != index = .each_with_index { |_, i| break i unless [i] == [i] } # Update arrays of tag objects |= [index....size] |= [index....size] & # Order the array of tag objects to match the tag list = .map do |t| .find { |n| n.name.downcase == t.name.downcase } end.compact end else # Delete discarded tags and create new tags = - = - end # Destroy old taggings: if .present? taggings.not_owned.by_context(context).destroy_all(tag_id: ) end # Create new taggings: .each do |tag| taggings.create!(tag_id: tag.id, context: context.to_s, taggable: self) end end true end |
#set_tag_list_on(context, new_list) ⇒ Object
326 327 328 329 330 331 332 333 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 326 def set_tag_list_on(context, new_list) add_custom_context(context) variable_name = "@#{context.to_s.singularize}_list" process_dirty_object(context, new_list) unless custom_contexts.include?(context.to_s) instance_variable_set(variable_name, ActsAsTaggableOn.default_parser.new(new_list).parse) end |
#tag_list_cache_on(context) ⇒ Object
277 278 279 280 281 282 283 284 285 286 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 277 def tag_list_cache_on(context) variable_name = "@#{context.to_s.singularize}_list" if instance_variable_get(variable_name) instance_variable_get(variable_name) elsif cached_tag_list_on(context) && self.class.caching_tag_list_on?(context) instance_variable_set(variable_name, ActsAsTaggableOn.default_parser.new(cached_tag_list_on(context)).parse) else instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new((context).map(&:name))) end end |
#tag_list_cache_set_on(context) ⇒ Object
272 273 274 275 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 272 def tag_list_cache_set_on(context) variable_name = "@#{context.to_s.singularize}_list" instance_variable_defined?(variable_name) && instance_variable_get(variable_name) end |
#tag_list_on(context) ⇒ Object
288 289 290 291 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 288 def tag_list_on(context) add_custom_context(context) tag_list_cache_on(context) end |
#tagging_contexts ⇒ Object
335 336 337 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 335 def tagging_contexts custom_contexts + self.class.tag_types.map(&:to_s) end |
#tags_on(context) ⇒ Object
Returns all tags that are not owned of a given context
318 319 320 321 322 323 324 |
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 318 def (context) scope = .where(["#{ActsAsTaggableOn::Tagging.table_name}.context = ? AND #{ActsAsTaggableOn::Tagging.table_name}.tagger_id IS NULL", context.to_s]) # when preserving tag order, return tags in created order # if we added the order to the association this would always apply scope = scope.order("#{ActsAsTaggableOn::Tagging.table_name}.id") if self.class.preserve_tag_order? scope end |