Module: RocketTag::Taggable::InstanceMethods

Defined in:
lib/rocket_tag/taggable.rb

Instance Method Summary collapse

Instance Method Details

#cache_tagsObject



83
84
85
86
87
88
89
90
91
# File 'lib/rocket_tag/taggable.rb', line 83

def cache_tags
  unless @tags_cached
    tags_by_context ||= send("taggings").group_by{|f| f.context }
    tags_by_context.each do |context,v|
      write_context context, v.map{|t| t.tag.name}
    end
    @tags_cached = true
  end
end

#reload_with_tags(options = nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/rocket_tag/taggable.rb', line 74

def reload_with_tags(options = nil)
  self.class.rocket_tag.contexts.each do |context|
    write_context context, []
  end
  @tags_cached = false
  cache_tags
  reload_without_tags(options)
end

#tagged_similar(options = {}) ⇒ Object

Find models with similar tags to the current model ordered is decending order of the number of matches



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rocket_tag/taggable.rb', line 106

def tagged_similar options = {}
  context = options.delete :on

  contexts = self.class.normalize_contexts context, 
    self.class.rocket_tag.contexts

  q = self.class.tagged_with Hash[*contexts.map{|c| 
    [c, tags_for_context(c)]
  }.flatten(1)]
 
  # Exclude self from the results
  q.where{id!=my{id}}

end

#tags_for_context(context) ⇒ Object



98
99
100
101
# File 'lib/rocket_tag/taggable.rb', line 98

def tags_for_context context
  @contexts ||= {}
  @contexts[context.to_sym] || []
end

#write_context(context, list) ⇒ Object



93
94
95
96
# File 'lib/rocket_tag/taggable.rb', line 93

def write_context context, list
  @contexts ||= {}
  @contexts[context.to_sym] = list
end