Module: Mongoid::TaggableWithContext::ClassMethods

Defined in:
lib/mongoid/taggable_with_context.rb

Instance Method Summary collapse

Instance Method Details

#clean_up_array(ary = []) ⇒ Object



172
173
174
175
176
177
178
# File 'lib/mongoid/taggable_with_context.rb', line 172

def clean_up_array(ary = [])
  # 0). remove all nil values
  # 1). strip all white spaces. Could leave blank strings (e.g. foo, , bar, baz)
  # 2). remove all blank strings
  # 3). remove duplicate
  ary.compact.map(&:strip).reject(&:blank?).uniq
end

#convert_array_to_string(ary = [], separator = TAGGABLE_DEFAULT_SEPARATOR) ⇒ Object



167
168
169
170
# File 'lib/mongoid/taggable_with_context.rb', line 167

def convert_array_to_string(ary = [], separator = TAGGABLE_DEFAULT_SEPARATOR)
  #ary.join(separator)
  (ary || []).join(separator)
end

#convert_string_to_array(str = "", separator = TAGGABLE_DEFAULT_SEPARATOR) ⇒ Object

Helper method to convert a String to an Array based on the configured tag separator.



163
164
165
# File 'lib/mongoid/taggable_with_context.rb', line 163

def convert_string_to_array(str = "", separator = TAGGABLE_DEFAULT_SEPARATOR)
  clean_up_array(str.split(separator))
end

#format_tags_for_write(value, separator = TAGGABLE_DEFAULT_SEPARATOR) ⇒ Object

Helper method to convert a a tag input value of unknown type to a formatted array.



153
154
155
156
157
158
159
# File 'lib/mongoid/taggable_with_context.rb', line 153

def format_tags_for_write(value, separator = TAGGABLE_DEFAULT_SEPARATOR)
  if value.is_a? Array
    clean_up_array(value)
  else
    convert_string_to_array(value, separator)
  end
end

#get_tag_separator_for(context) ⇒ Object



126
127
128
# File 'lib/mongoid/taggable_with_context.rb', line 126

def get_tag_separator_for(context)
  self.taggable_with_context_options[context][:separator]
end

#set_tag_separator_for(context, value) ⇒ Object



130
131
132
# File 'lib/mongoid/taggable_with_context.rb', line 130

def set_tag_separator_for(context, value)
  self.taggable_with_context_options[context][:separator] = value.nil? ? TAGGABLE_DEFAULT_SEPARATOR : value.to_s
end

#tag_contextsObject



104
105
106
# File 'lib/mongoid/taggable_with_context.rb', line 104

def tag_contexts
  self.taggable_with_context_options.keys
end

#tag_database_fieldsObject



108
109
110
111
112
# File 'lib/mongoid/taggable_with_context.rb', line 108

def tag_database_fields
  self.taggable_with_context_options.keys.map do |context|
    tag_options_for(context)[:field]
  end
end

#tag_options_for(context) ⇒ Object



114
115
116
# File 'lib/mongoid/taggable_with_context.rb', line 114

def tag_options_for(context)
  self.taggable_with_context_options[context]
end

#taggable(*args) ⇒ Object

Macro to declare a document class as taggable, specify field name for tags, and set options for tagging behavior.

Examples:

Define a taggable document.


class Article
  include Mongoid::Document
  include Mongoid::Taggable
  taggable :keywords, :separator => ' ', :aggregation => true, :default_type => "seo"
end

Parameters:

  • field (Symbol)

    The name of the field for tags.

  • options (Hash)

    Options for taggable behavior.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/mongoid/taggable_with_context.rb', line 46

def taggable(*args)
  # init variables
  options = args.extract_options!
  tags_name = (args.blank? ? :tags : args.shift).to_sym
  options.reverse_merge!(
    :separator => TAGGABLE_DEFAULT_SEPARATOR,
    :string_method => "#{tags_name}_string".to_sym,
    :field => tags_name
  )
  database_field = options[:field]

  # register / update settings
  self.taggable_with_context_options[tags_name] = options
  self.database_field_to_context_hash[database_field] = tags_name

  # setup fields & indexes
  field tags_name, :type => Array, :default => options[:default]

  index({ database_field => 1 }, { background: true })

  # singleton methods
  class_eval <<-END
    class << self
      # retrieve all tags ever created for the model
      def #{tags_name}
        tags_for(:"#{tags_name}")
      end

      # retrieve all tags ever created for the model with weights
      def #{tags_name}_with_weight
        tags_with_weight_for(:"#{tags_name}")
      end

      def #{tags_name}_separator
        get_tag_separator_for(:"#{tags_name}")
      end

      def #{tags_name}_separator=(value)
        set_tag_separator_for(:"#{tags_name}", value)
      end

      def #{tags_name}_tagged_with(tags)
        tagged_with(:"#{tags_name}", tags)
      end
    end
  END

  # instance methods
  class_eval <<-END
    def #{options[:string_method]}
      convert_array_to_string(#{database_field}, get_tag_separator_for(:"#{tags_name}"))
    end
    def #{tags_name}=(value)
      write_attribute(:#{database_field}, format_tags_for_write(value, get_tag_separator_for(:"#{tags_name}")))
    end
  END
end

#tagged_with(context, tags) ⇒ Criteria

Find documents tagged with all tags passed as a parameter, given as an Array or a String using the configured separator.

Examples:

Find matching all tags in an Array.

Article.tagged_with(['ruby', 'mongodb'])

Find matching all tags in a String.

Article.tagged_with('ruby, mongodb')

Parameters:

  • :field (String)

    The field name of the tag.

  • :tags (Array<String, Symbol>, String)

    Tags to match.

Returns:

  • (Criteria)

    A new criteria.



145
146
147
148
149
# File 'lib/mongoid/taggable_with_context.rb', line 145

def tagged_with(context, tags)
  tags = convert_string_to_array(tags, get_tag_separator_for(context)) if tags.is_a? String
  database_field = tag_options_for(context)[:field]
  all_in(database_field => tags)
end

#tags_for(context, conditions = {}) ⇒ Object



118
119
120
# File 'lib/mongoid/taggable_with_context.rb', line 118

def tags_for(context, conditions={})
  raise AggregationStrategyMissing
end

#tags_with_weight_for(context, conditions = {}) ⇒ Object



122
123
124
# File 'lib/mongoid/taggable_with_context.rb', line 122

def tags_with_weight_for(context, conditions={})
  raise AggregationStrategyMissing
end