Module: MakeTaggable::Taggable::Related

Defined in:
lib/make_taggable/taggable/related.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
# File 'lib/make_taggable/taggable/related.rb', line 3

def self.included(base)
  base.extend MakeTaggable::Taggable::Related::ClassMethods
  base.initialize_make_taggable_related
end

Instance Method Details

#find_matching_contexts(search_context, result_context, options = {}) ⇒ Object



30
31
32
# File 'lib/make_taggable/taggable/related.rb', line 30

def find_matching_contexts(search_context, result_context, options = {})
  matching_contexts_for(search_context.to_s, result_context.to_s, self.class, options)
end

#find_matching_contexts_for(klass, search_context, result_context, options = {}) ⇒ Object



34
35
36
# File 'lib/make_taggable/taggable/related.rb', line 34

def find_matching_contexts_for(klass, search_context, result_context, options = {})
  matching_contexts_for(search_context.to_s, result_context.to_s, klass, options)
end

#matching_contexts_for(search_context, result_context, klass, options = {}) ⇒ Object



38
39
40
41
# File 'lib/make_taggable/taggable/related.rb', line 38

def matching_contexts_for(search_context, result_context, klass, options = {})
  tags_to_find = tags_on(search_context).map { |t| t.name }
  related_where(klass, ["#{exclude_self(klass, id)} #{klass.table_name}.#{klass.primary_key} = #{MakeTaggable::Tagging.table_name}.taggable_id AND #{MakeTaggable::Tagging.table_name}.taggable_type = '#{klass.base_class}' AND #{MakeTaggable::Tagging.table_name}.tag_id = #{MakeTaggable::Tag.table_name}.#{MakeTaggable::Tag.primary_key} AND #{MakeTaggable::Tag.table_name}.name IN (?) AND #{MakeTaggable::Tagging.table_name}.context = ?", tags_to_find, result_context])
end


43
44
45
46
47
# File 'lib/make_taggable/taggable/related.rb', line 43

def related_tags_for(context, klass, options = {})
  tags_to_ignore = Array.wrap(options[:ignore]).map(&:to_s) || []
  tags_to_find = tags_on(context).map { |t| t.name }.reject { |t| tags_to_ignore.include? t }
  related_where(klass, ["#{exclude_self(klass, id)} #{klass.table_name}.#{klass.primary_key} = #{MakeTaggable::Tagging.table_name}.taggable_id AND #{MakeTaggable::Tagging.table_name}.taggable_type = '#{klass.base_class}' AND #{MakeTaggable::Tagging.table_name}.tag_id = #{MakeTaggable::Tag.table_name}.#{MakeTaggable::Tag.primary_key} AND #{MakeTaggable::Tag.table_name}.name IN (?) AND #{MakeTaggable::Tagging.table_name}.context = ?", tags_to_find, context])
end