module SemanticallyTaggable::Taggable::Core::InstanceMethods

Public Instance Methods

add_custom_scheme_name(value) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 176
def add_custom_scheme_name(value)
  custom_scheme_names << value.to_s unless custom_scheme_names.include?(value.to_s) or self.class.scheme_names.map(&:to_s).include?(value.to_s)
end
all_tags_list_on(scheme_name) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 199
def all_tags_list_on(scheme_name)
  variable_name = "@all_#{scheme_name.to_s.singularize}_list"
  return instance_variable_get(variable_name) if instance_variable_get(variable_name)

  instance_variable_set(variable_name, SemanticallyTaggable::TagList.new(all_tags_on(scheme_name).map(&:name)).freeze)
end
all_tags_on(scheme_name) click to toggle source

Returns all tags of a given context

# File lib/semantically_taggable/semantically_taggable/core.rb, line 208
def all_tags_on(scheme_name)
  tagging_table_name = SemanticallyTaggable::Tagging.table_name

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

  if SemanticallyTaggable::Tag.using_postgresql?
    group_columns = grouped_column_names_for(SemanticallyTaggable::Tag)
    scope = scope.order("max(#{tagging_table_name}.created_at)").group(group_columns)
  else
    scope = scope.group("#{SemanticallyTaggable::Tag.table_name}.#{SemanticallyTaggable::Tag.primary_key}")
  end

  scope.all
end
cached_tag_list_on(scheme_name) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 180
def cached_tag_list_on(scheme_name)
  self["cached_#{scheme_name.to_s.singularize}_list"]
end
custom_scheme_names() click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 168
def custom_scheme_names
  @custom_scheme_names ||= []
end
grouped_column_names_for(object) click to toggle source

all column names are necessary for PostgreSQL group clause

# File lib/semantically_taggable/semantically_taggable/core.rb, line 164
def grouped_column_names_for(object)
  self.class.grouped_column_names_for(object)
end
is_taggable?() click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 172
def is_taggable?
  self.class.is_taggable?
end
reload(*args) click to toggle source
Calls superclass method
# File lib/semantically_taggable/semantically_taggable/core.rb, line 242
def reload(*args)
  self.class.scheme_names.each do |scheme_name|
    instance_variable_set("@#{scheme_name.to_s.singularize}_list", nil)
    instance_variable_set("@all_#{scheme_name.to_s.singularize}_list", nil)
  end

  super(*args)
end
save_tags() click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 251
def save_tags
  tagging_scheme_names.each do |scheme_name|
    next unless tag_list_cache_set_on(scheme_name)

    tag_list = tag_list_cache_on(scheme_name).uniq

    # Find existing tags or create non-existing tags:
    tag_list = SemanticallyTaggable::Tag.find_or_create_all_with_like_by_name(tag_list, scheme_name.to_sym)

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

    # Find taggings to remove:
    old_taggings = taggings.joins(:tag => :scheme) \
      .where(:schemes => { :name => scheme_name.to_s}, :tag_id => old_tags).all

    if old_taggings.present?
      # Destroy old taggings:
      SemanticallyTaggable::Tagging.destroy_all :id => old_taggings.map(&:id)
    end

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

  true
end
set_tag_list_on(scheme_name, new_list) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 230
def set_tag_list_on(scheme_name, new_list)
  add_custom_scheme_name(scheme_name)

  scheme = SemanticallyTaggable::Scheme.by_name(scheme_name)
  variable_name = "@#{scheme_name.to_s.singularize}_list"
  instance_variable_set(variable_name, SemanticallyTaggable::TagList.from(new_list, scheme.delimiter))
end
tag_list_cache_on(scheme_name) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 189
def tag_list_cache_on(scheme_name)
  variable_name = "@#{scheme_name.to_s.singularize}_list"
  instance_variable_get(variable_name) || instance_variable_set(variable_name, SemanticallyTaggable::TagList.new(tags_on(scheme_name).map(&:name)))
end
tag_list_cache_set_on(scheme_name) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 184
def tag_list_cache_set_on(scheme_name)
  variable_name = "@#{scheme_name.to_s.singularize}_list"
  !instance_variable_get(variable_name).nil?
end
tag_list_on(scheme_name) click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 194
def tag_list_on(scheme_name)
  add_custom_scheme_name(scheme_name)
  tag_list_cache_on(scheme_name)
end
tagging_scheme_names() click to toggle source
# File lib/semantically_taggable/semantically_taggable/core.rb, line 238
def tagging_scheme_names
  custom_scheme_names + self.class.scheme_names.map(&:to_s)
end
tags_on(scheme_name) click to toggle source

Returns all tags in a given scheme

# File lib/semantically_taggable/semantically_taggable/core.rb, line 226
def tags_on(scheme_name)
  base_tags.joins(:scheme).where(["schemes.name = ?", scheme_name.to_s]).all
end