Module: Mongoid::TaggableWithContext::ClassMethods

Defined in:
lib/mongoid_taggable_with_context/meta.rb

Instance Method Summary collapse

Instance Method Details

#taggable(*args) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongoid_taggable_with_context/meta.rb', line 24

def taggable(*args)
  taggable_original(*args)

  options = args.extract_options!
  tags_field = (args.blank? ? :tags : args.shift).to_sym
  options = self.taggable_with_context_options[tags_field]

  # META Handling
  if options[:enable_meta]

    # instance methods
    class_eval <<-END

    # retrieve meta tags in various formats
    def #{tags_field}_having_meta
      self.#{tags_field}_having_meta_array.join(get_tag_separator_for(:"#{tags_field}"))
    end

    def #{tags_field}_having_meta_array
      self.#{tags_field}_having_and_including_meta.collect{|i| i[0]}
    end

    def #{tags_field}_having_and_including_meta
      self.taggable_meta_tags.where(context: "#{tags_field}").collect{|i| [i.name, i.meta] }
    end

    def #{tags_field}_including_meta
      normal_tags = self.#{tags_field}_array - self.#{tags_field}_having_meta_array
      self.#{tags_field}_having_and_including_meta + normal_tags.collect{|i| [i, {}]}
    end

    # adds single meta enhanced tag
    def add_#{tags_field.to_s.singularize}_with_meta(tag_name, meta)
      self.taggable_meta_tags.create(:context => "#{tags_field}", :name => tag_name.strip, :meta => meta)
      self.#{tags_field}_array << tag_name

      # TODO use this ti utilize taggable setters; BUT breaks update_on_save
      # sep = get_tag_separator_for(:"#{tags_field}")
      # self.#{tags_field} = convert_array_to_string( (convert_string_to_array(self.#{tags_field}, sep) | [tag_name]), sep)

      self.save!
    end
    END
  end

end

#taggable_originalObject



22
# File 'lib/mongoid_taggable_with_context/meta.rb', line 22

alias_method :taggable_original, :taggable