Module: Diaspora::Taggable

Included in:
Comment, Profile, StatusMessage
Defined in:
lib/diaspora/taggable.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.format_tags(text, opts = {}) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/diaspora/taggable.rb', line 40

def self.format_tags(text, opts={})
  return text if opts[:plain_text]

  text = ERB::Util.h(text) unless opts[:no_escape]
  regex =/(^|\s|>)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u

  text.to_str.gsub(regex) { |matched_string|
    pre, url_bit, clickable = $1, $2, "##{$2}"
    if $2 == '<3'
      # Special case for love, because the world needs more love.
      url_bit = '<3'
    end

    %{#{pre}<a href="/tags/#{url_bit}" class="tag">#{clickable}</a>}
  }.html_safe
end

.included(model) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/diaspora/taggable.rb', line 7

def self.included(model)
  model.class_eval do
    cattr_accessor :field_with_tags
  end
  model.instance_eval do
    before_create :build_tags

    def extract_tags_from sym
      self.field_with_tags = sym
    end
    def field_with_tags_setter
      "#{self.field_with_tags}=".to_sym
    end
  end
end

Instance Method Details

#build_tagsObject


23
24
25
# File 'lib/diaspora/taggable.rb', line 23

def build_tags
  self.tag_list = tag_strings
end

#tag_stringsObject


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/diaspora/taggable.rb', line 27

def tag_strings
  regex = /(?:^|\s)#([#{ActsAsTaggableOn::Tag.tag_text_regexp}]+|<3)/u
  matches = self.
    send( self.class.field_with_tags ).
    scan(regex).
    map { |match| match[0] }
  unique_matches = matches.inject(Hash.new) do |h,element|
    h[element.downcase] = element unless h[element.downcase]
    h
  end
  unique_matches.values
end