Module: ActsAsTaggableOn::Taggable::Core::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#add_custom_context(value) ⇒ Object



185
186
187
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 185

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



208
209
210
211
212
213
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 208

def all_tags_list_on(context)
  variable_name = "@all_#{context.to_s.singularize}_list"
  return instance_variable_get(variable_name) if 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



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 217

def all_tags_on(context)
  tag_table_name = ActsAsTaggableOn::Tag.table_name
  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 = scope.order("max(#{tagging_table_name}.created_at)").group(group_columns)
  else
    scope = scope.group("#{ActsAsTaggableOn::Tag.table_name}.#{ActsAsTaggableOn::Tag.primary_key}")
  end

  scope.all
end

#cached_tag_list_on(context) ⇒ Object



189
190
191
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 189

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

#custom_contextsObject



177
178
179
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 177

def custom_contexts
  @custom_contexts ||= []
end

#grouped_column_names_for(object) ⇒ Object

all column names are necessary for PostgreSQL group clause



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

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

#is_taggable?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 181

def is_taggable?
  self.class.is_taggable?
end

#reload(*args) ⇒ Object



251
252
253
254
255
256
257
258
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 251

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



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 260

def save_tags
  tagging_contexts.each do |context|
    next unless tag_list_cache_set_on(context)

    tag_list = tag_list_cache_on(context).uniq

    # Find existing tags or create non-existing tags:
    tag_list = ActsAsTaggableOn::Tag.find_or_create_all_with_like_by_name(tag_list)

    current_tags = tags_on(context)
    old_tags     = current_tags - tag_list
    new_tags     = tag_list     - current_tags

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

    if old_taggings.present?
      # Destroy old taggings:
      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

#set_tag_list_on(context, new_list) ⇒ Object



240
241
242
243
244
245
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 240

def set_tag_list_on(context, new_list)
  add_custom_context(context)

  variable_name = "@#{context.to_s.singularize}_list"
  instance_variable_set(variable_name, ActsAsTaggableOn::TagList.from(new_list))
end

#tag_list_cache_on(context) ⇒ Object



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

def tag_list_cache_on(context)
  variable_name = "@#{context.to_s.singularize}_list"
  instance_variable_get(variable_name) || instance_variable_set(variable_name, ActsAsTaggableOn::TagList.new(tags_on(context).map(&:name)))
end

#tag_list_cache_set_on(context) ⇒ Object



193
194
195
196
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 193

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

#tag_list_on(context) ⇒ Object



203
204
205
206
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 203

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

#tagging_contextsObject



247
248
249
# File 'lib/acts_as_taggable_on/taggable/core.rb', line 247

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



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

def tags_on(context)
  base_tags.where(["#{ActsAsTaggableOn::Tagging.table_name}.context = ? AND #{ActsAsTaggableOn::Tagging.table_name}.tagger_id IS NULL", context.to_s]).all
end