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

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

Instance Method Summary collapse

Instance Method Details

#add_custom_context(value) ⇒ Object



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

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



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

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, TagList.new(all_tags_on(context).map(&:name)).freeze)
end

#all_tags_on(context) ⇒ Object

Returns all tags of a given context



174
175
176
177
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 174

def all_tags_on(context)
  opts =  ["#{Tagging.table_name}.context = ?", context.to_s]
  base_tags.where(opts).order("#{Tagging.table_name}.created_at").group("#{Tagging.table_name}.tag_id").all
end

#cached_tag_list_on(context) ⇒ Object



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

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

#custom_contextsObject



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

def custom_contexts
  @custom_contexts ||= []
end

#grouped_column_names_for(object) ⇒ Object

all column names are necessary for PostgreSQL group clause



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

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

#is_taggable?Boolean

Returns:

  • (Boolean)


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

def is_taggable?
  self.class.is_taggable?
end

#reloadObject



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

def reload
  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
end

#save_tagsObject



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 205

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 = 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:
      Tagging.destroy_all :id => 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



185
186
187
188
189
190
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 185

def set_tag_list_on(context, new_list)
  add_custom_context(context)

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

#tag_list_cache_on(context) ⇒ Object



155
156
157
158
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 155

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

#tag_list_cache_set_on(context) ⇒ Object



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

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



160
161
162
163
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 160

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

#tagging_contextsObject



192
193
194
# File 'lib/acts_as_taggable_on/acts_as_taggable_on/core.rb', line 192

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



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

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