Module: ActsAsTaggableOn::Taggable::Core

Defined in:
lib/acts_as_taggable_on/acts_as_taggable_on/core.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(base)



3
4
5
6
7
8
9
10
11
12
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/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

- (Object) add_custom_context(value)



245
246
247
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 245

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

- (Object) all_tags_list_on(context)



274
275
276
277
278
279
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 274

def all_tags_list_on(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(all_tags_on(context).map(&:name)).freeze)
end

- (Object) all_tags_on(context)

Returns all tags of a given context



283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 283

def all_tags_on(context)
  tagging_table_name = ActsAsTaggableOn::Tagging.table_name

  opts  =  ["#{tagging_table_name}.context = ?", context.to_s]
  scope = base_tags.where(opts)

  if ActsAsTaggableOn::Tag.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

- (Object) cached_tag_list_on(context)



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

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

- (Object) custom_contexts



237
238
239
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 237

def custom_contexts
  @custom_contexts ||= []
end

- (Object) grouped_column_names_for(object)

all column names are necessary for PostgreSQL group clause



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

def grouped_column_names_for(object)
  self.class.grouped_column_names_for(object)
end

- (Boolean) is_taggable?

Returns:

  • (Boolean)


241
242
243
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 241

def is_taggable?
  self.class.is_taggable?
end

- (Object) load_tags(tag_list)

Find existing tags or create non-existing tags



345
346
347
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 345

def load_tags(tag_list)
  ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list)
end

- (Object) process_dirty_object(context, new_list)



320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 320

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).to_s
    changed_attributes[attrib] = old if (old.to_s != value.to_s)
  end
end

- (Object) reload(*args)



334
335
336
337
338
339
340
341
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 334

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

- (Object) save_tags



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
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
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 349

def save_tags
  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:
    tags = load_tags(tag_list)

    # Tag objects for currently assigned tags
    current_tags = tags_on(context)

    # Tag maintenance based on whether preserving the created order of tags
    if self.class.preserve_tag_order?
      old_tags, new_tags = current_tags - tags, tags - current_tags

      shared_tags = current_tags & tags

      if shared_tags.any? && tags[0...shared_tags.size] != shared_tags
        index = shared_tags.each_with_index { |_, i| break i unless shared_tags[i] == tags[i] }

        # Update arrays of tag objects
        old_tags |= current_tags[index...current_tags.size]
        new_tags |= current_tags[index...current_tags.size] & shared_tags

        # Order the array of tag objects to match the tag list
        new_tags = tags.map do |t|
          new_tags.find { |n| n.name.downcase == t.name.downcase }
        end.compact
      end
    else
      # Delete discarded tags and create new tags
      old_tags = current_tags - tags
      new_tags = tags - current_tags
    end

    # Find taggings to remove:
    if old_tags.present?
      old_taggings = taggings.where(:tagger_type => nil, :tagger_id => nil, :context => context.to_s, :tag_id => old_tags)
    end

    # Destroy old taggings:
    if old_taggings.present?
      ActsAsTaggableOn::Tagging.destroy_all "#{ActsAsTaggableOn::Tagging.primary_key}".to_sym => old_taggings.map(&:id)
    end

    # Create new taggings:
    new_tags.each do |tag|
      taggings.create!(:tag_id => tag.id, :context => context.to_s, :taggable => self)
    end
  end

  true
end

- (Object) set_tag_list_on(context, new_list)



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

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::TagList.from(new_list))
end

- (Object) tag_list_cache_on(context)



258
259
260
261
262
263
264
265
266
267
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 258

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::TagList.from(cached_tag_list_on(context)))
  else
    instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new(tags_on(context).map(&:name)))
  end
end

- (Object) tag_list_cache_set_on(context)



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

def tag_list_cache_set_on(context)
  variable_name = "@#{context.to_s.singularize}_list"
  instance_variable_defined?(variable_name) && !instance_variable_get(variable_name).nil?
end

- (Object) tag_list_on(context)



269
270
271
272
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 269

def tag_list_on(context)
  add_custom_context(context)
  tag_list_cache_on(context)
end

- (Object) tagging_contexts



316
317
318
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 316

def tagging_contexts
  custom_contexts + self.class.tag_types.map(&:to_s)
end

- (Object) tags_on(context)

Returns all tags that are not owned of a given context



299
300
301
302
303
304
305
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 299

def tags_on(context)
  scope = base_tags.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