Module: Mongoid::TaggableWithContext::ClassMethods
- Defined in:
- lib/mongoid/taggable_with_context.rb
Instance Method Summary collapse
-
#format_tags_for(context, value) ⇒ Object
Helper method to convert a a tag input value of unknown type to a formatted array.
- #get_tag_group_by_field_for(context) ⇒ Object
- #get_tag_separator_for(context) ⇒ 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, group_by = nil, conditions = {}) ⇒ Object
- #tags_with_weight_for(context, group_by = nil, conditions = {}) ⇒ Object
Instance Method Details
#format_tags_for(context, value) ⇒ Object
Helper method to convert a a tag input value of unknown type to a formatted array.
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/mongoid/taggable_with_context.rb', line 102 def (context, value) # 0) Tags must be an array or a string raise InvalidTagsFormat unless value.is_a?(Array) || value.is_a?(String) # 1) convert String to Array value = value.split(get_tag_separator_for(context)) if value.is_a? String # 2) remove all nil values # 3) strip all white spaces. Could leave blank strings (e.g. foo, , bar, baz) # 4) remove all blank strings # 5) remove duplicate value.compact.map(&:strip).reject(&:blank?).uniq end |
#get_tag_group_by_field_for(context) ⇒ Object
79 80 81 |
# File 'lib/mongoid/taggable_with_context.rb', line 79 def get_tag_group_by_field_for(context) self.[context][:group_by_field] end |
#get_tag_separator_for(context) ⇒ Object
75 76 77 |
# File 'lib/mongoid/taggable_with_context.rb', line 75 def get_tag_separator_for(context) self.[context][:separator] end |
#tag_contexts ⇒ Object
53 54 55 |
# File 'lib/mongoid/taggable_with_context.rb', line 53 def tag_contexts self..keys end |
#tag_database_fields ⇒ Object
57 58 59 60 61 |
# File 'lib/mongoid/taggable_with_context.rb', line 57 def tag_database_fields self..keys.map do |context| (context)[:db_field] end end |
#tag_options_for(context) ⇒ Object
63 64 65 |
# File 'lib/mongoid/taggable_with_context.rb', line 63 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.
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mongoid/taggable_with_context.rb', line 41 def taggable(*args) # init variables = args. field = args.present? ? args.shift.to_sym : DEFAULT_FIELD added = add_taggable(field, ) # TODO: test if this is needed # descendants.each do |subclass| # subclass.add_taggable(field, options) # end added 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.
94 95 96 97 98 |
# File 'lib/mongoid/taggable_with_context.rb', line 94 def tagged_with(context, ) = (context, ) field = (context)[:field] all_in(field => ) end |
#tags_for(context, group_by = nil, conditions = {}) ⇒ Object
67 68 69 |
# File 'lib/mongoid/taggable_with_context.rb', line 67 def (context, group_by=nil, conditions={}) raise AggregationStrategyMissing end |
#tags_with_weight_for(context, group_by = nil, conditions = {}) ⇒ Object
71 72 73 |
# File 'lib/mongoid/taggable_with_context.rb', line 71 def (context, group_by=nil, conditions={}) raise AggregationStrategyMissing end |