Module: Mongoid::TaggableWithContext::ClassMethods
- Defined in:
- lib/mongoid/taggable_with_context.rb
Instance Method Summary collapse
- #clean_up_array(ary = []) ⇒ Object
- #convert_array_to_string(ary = [], separator = TAGGABLE_DEFAULT_SEPARATOR) ⇒ Object
-
#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.
-
#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.
- #get_tag_separator_for(context) ⇒ Object
- #set_tag_separator_for(context, value) ⇒ Object
- #tag_contexts ⇒ Object
- #tag_database_fields ⇒ Object
- #tag_options_for(context) ⇒ Object
-
#taggable(*args) ⇒ Object
Macro to declare a document class as taggable, specify field name for tags, and set options for tagging behavior.
-
#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.
- #tags_for(context, conditions = {}) ⇒ Object
- #tags_with_weight_for(context, conditions = {}) ⇒ Object
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 (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.[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.[context][:separator] = value.nil? ? TAGGABLE_DEFAULT_SEPARATOR : value.to_s end |
#tag_contexts ⇒ Object
104 105 106 |
# File 'lib/mongoid/taggable_with_context.rb', line 104 def tag_contexts self..keys end |
#tag_database_fields ⇒ Object
108 109 110 111 112 |
# File 'lib/mongoid/taggable_with_context.rb', line 108 def tag_database_fields self..keys.map do |context| (context)[:field] end end |
#tag_options_for(context) ⇒ Object
114 115 116 |
# File 'lib/mongoid/taggable_with_context.rb', line 114 def (context) self.[context] end |
#taggable(*args) ⇒ Object
Macro to declare a document class as taggable, specify field name for tags, and set options for tagging 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 = args. = (args.blank? ? :tags : args.shift).to_sym .reverse_merge!( :separator => TAGGABLE_DEFAULT_SEPARATOR, :string_method => "#{}_string".to_sym, :field => ) database_field = [:field] # register / update settings self.[] = self.database_field_to_context_hash[database_field] = # setup fields & indexes field , :type => Array, :default => [:default] index({ database_field => 1 }, { background: true }) # singleton methods class_eval <<-END class << self # retrieve all tags ever created for the model def #{} tags_for(:"#{}") end # retrieve all tags ever created for the model with weights def #{}_with_weight tags_with_weight_for(:"#{}") end def #{}_separator get_tag_separator_for(:"#{}") end def #{}_separator=(value) set_tag_separator_for(:"#{}", value) end def #{}_tagged_with(tags) tagged_with(:"#{}", tags) end end END # instance methods class_eval <<-END def #{[:string_method]} convert_array_to_string(#{database_field}, get_tag_separator_for(:"#{}")) end def #{}=(value) write_attribute(:#{database_field}, format_tags_for_write(value, get_tag_separator_for(:"#{}"))) 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.
145 146 147 148 149 |
# File 'lib/mongoid/taggable_with_context.rb', line 145 def tagged_with(context, ) = convert_string_to_array(, get_tag_separator_for(context)) if .is_a? String database_field = (context)[:field] all_in(database_field => ) end |
#tags_for(context, conditions = {}) ⇒ Object
118 119 120 |
# File 'lib/mongoid/taggable_with_context.rb', line 118 def (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 (context, conditions={}) raise AggregationStrategyMissing end |