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

Class Method Details

.included(base) ⇒ Object



7
8
9
10
11
12
13
14
15
16
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 7

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



141
142
143
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 141

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



170
171
172
173
174
175
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 170

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

#all_tags_on(context) ⇒ Object

Returns all tags of a given context



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 179

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::Utils.using_postgresql?
    group_columns = grouped_column_names_for(ActsAsTaggableOn::Tag)
    scope.order(Arel.sql("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



145
146
147
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 145

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

#custom_contextsObject



133
134
135
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 133

def custom_contexts
  @custom_contexts ||= taggings.map(&:context).uniq
end

#grouped_column_names_for(object) ⇒ Object

all column names are necessary for PostgreSQL group clause



129
130
131
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 129

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

#is_taggable?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 137

def is_taggable?
  self.class.is_taggable?
end

#load_tags(tag_list) ⇒ Object

Find existing tags or create non-existing tags



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

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

#reload(*args) ⇒ Object



217
218
219
220
221
222
223
224
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 217

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_tagsObject



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 232

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 = find_or_create_tags_from_list_with_context(tag_list, context)

    # 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

    # Destroy old taggings:
    if old_tags.present?
      taggings.not_owned.by_context(context).where(tag_id: old_tags).destroy_all
    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

#set_tag_list_on(context, new_list) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 203

def set_tag_list_on(context, new_list)
  add_custom_context(context)

  variable_name = "@#{context.to_s.singularize}_list"

  parsed_new_list = ActsAsTaggableOn.default_parser.new(new_list).parse

  instance_variable_set(variable_name, parsed_new_list)
end

#tag_list_cache_on(context) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 154

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) && ensure_included_cache_methods! && 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(tags_on(context).map(&:name)))
  end
end

#tag_list_cache_set_on(context) ⇒ Object



149
150
151
152
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 149

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



165
166
167
168
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 165

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

#tagging_contextsObject



213
214
215
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 213

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

#tags_on(context) ⇒ Object

Returns all tags that are not owned of a given context



195
196
197
198
199
200
201
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 195

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