Class: ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition
- Inherits:
-
Object
- Object
- ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition
- Includes:
- Attributes, Changeable, ListMethods, Names
- Defined in:
- lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb,
lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/names.rb,
lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/attributes.rb,
lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb,
lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/list_methods.rb
Overview
TagTypeDefinition represents the definition for a aingle tag_type in a model.
The options passed into a tag_type defined through acts_as_taggable* method are stored in the tag definition which then drives the creation of the relations and methods that are added to the model.
The TagTypeDefinition mirrors the Configuraiton attributes and defaults any value that isn’t passed in to the value in the Configuration (ActsAsTaggableOnMongoid.configuration)
Defined Under Namespace
Modules: Attributes, Changeable, ListMethods, Names
Instance Attribute Summary collapse
-
#owner ⇒ Object
readonly
Returns the value of attribute owner.
-
#tag_type ⇒ Object
readonly
Returns the value of attribute tag_type.
Attributes included from Attributes
#cached_in_model, #default, #owner_id_field
Class Method Summary collapse
Instance Method Summary collapse
-
#add_base_tags_method ⇒ Object
Mongoid does not allow the ‘through` option for relations, so we de-normalize data and manually add the methods we need for through like functionality.
- #add_context_taggings_method ⇒ Object
- #add_context_tags_method ⇒ Object
- #add_tag_list ⇒ Object
- #add_tag_list_change_methods ⇒ Object
- #conflicts_with?(tag_definition) ⇒ Boolean
- #define_base_relations ⇒ Object
- #define_cache_field ⇒ Object
- #define_relations ⇒ Object
-
#initialize(owner, tag_type, options = {}) ⇒ TagTypeDefinition
constructor
A new instance of TagTypeDefinition.
-
#parse(*tag_list) ⇒ Object
I’ve defined the parser as being required to return an array of strings.
- #taggings_order ⇒ Object
Methods included from Changeable
Methods included from Names
#all_tag_list_name, #all_tag_list_variable_name, #base_tags_method, #from_list_name, #single_tag_type, #tag_list_name, #tag_list_variable_name, #tagger_tag_list_name, #tagger_tag_lists_name, #taggings_name
Methods included from Attributes
#cached_in_model_as_list?, #cached_in_model_field, #default_tagger, #default_tagger_method, #force_lowercase, #force_parameterize, #parser, #preserve_tag_order, #remove_unused_tags, #tag_list_default_tagger, #tag_list_uses_default_tagger?, #taggable_default, #tagger?, #taggings_table, #tags_table
Methods included from ListMethods
#add_all_list_getter, #add_list_getter, #add_list_setter, #add_tag_list_from, #add_tagger_tag_list, #add_tagger_tag_lists
Constructor Details
#initialize(owner, tag_type, options = {}) ⇒ TagTypeDefinition
Returns a new instance of TagTypeDefinition.
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 21 def initialize(owner, tag_type, = {}) = ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition.() default_option = .delete(:default) () self.default_value = default_option @owner = owner @tag_type = tag_type end |
Instance Attribute Details
#owner ⇒ Object (readonly)
Returns the value of attribute owner.
13 14 15 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 13 def owner @owner end |
#tag_type ⇒ Object (readonly)
Returns the value of attribute tag_type.
13 14 15 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 13 def tag_type @tag_type end |
Class Method Details
.copy_from(klass, tag_definition) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 34 def self.copy_from(klass, tag_definition) dup_hash = %i[parser preserve_tag_order cached_in_model force_lowercase force_parameterize owner_id_field remove_unused_tags tags_table taggings_table].each_with_object({}) { |dup_key, opts_hash| opts_hash[dup_key] = tag_definition.public_send(dup_key) } dup_hash[:default] = tag_definition.default.dup ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition.new klass, tag_definition.tag_type, dup_hash end |
.extract_tag_definition_options(options) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 185 def self.() = .dup .assert_valid_keys(:parser, :preserve_tag_order, :cached_in_model, :force_lowercase, :force_parameterize, :owner_id_field, :remove_unused_tags, :tags_table, :taggings_table, :default, :tagger) end |
Instance Method Details
#add_base_tags_method ⇒ Object
Mongoid does not allow the ‘through` option for relations, so we de-normalize data and manually add the methods we need for through like functionality.
113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 113 def tag_definition = self = tag_definition. owner.taggable_mixin.module_eval do break if methods.include?() define_method do tag_definition..where(taggable_type: tag_definition.owner.name) end end end |
#add_context_taggings_method ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 138 def add_context_taggings_method tag_definition = self taggings_name = tag_definition.taggings_name owner.taggable_mixin.module_eval do define_method "#{tag_definition.single_tag_type}_#{taggings_name}".to_sym do public_send(taggings_name). order_by(*tag_definition.taggings_order). for_tag(tag_definition) end end end |
#add_context_tags_method ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 151 def tag_definition = self owner.taggable_mixin.module_eval do define_method tag_definition.tag_type.to_sym do public_send("#{tag_definition.single_tag_type}_#{tag_definition.taggings_name}").map(&:tag) end end end |
#add_tag_list ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 161 def add_tag_list add_list_getter add_list_setter add_tag_list_from add_tagger_tag_list add_tagger_tag_lists add_all_list_getter add_list_exists add_tag_list_change_methods end |
#add_tag_list_change_methods ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 173 def add_tag_list_change_methods add_list_change add_list_changed add_changed_from_default? add_will_change add_get_was add_get_lists_was add_tagger_get_was add_reset_list add_reset_to_default end |
#conflicts_with?(tag_definition) ⇒ Boolean
52 53 54 55 56 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 52 def conflicts_with?(tag_definition) %i[parser preserve_tag_order force_lowercase force_parameterize taggings_table].any? do |setting_name| public_send(setting_name) != tag_definition.public_send(setting_name) end end |
#define_base_relations ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 92 def define_base_relations tag_definition = self owner.class_eval do taggings_name = tag_definition.taggings_name break if relations[taggings_name.to_s] has_many taggings_name, as: :taggable, dependent: :destroy, class_name: tag_definition.taggings_table.name, after_add: :dirtify_tag_list, after_remove: :dirtify_tag_list end end |
#define_cache_field ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 80 def define_cache_field tag_definition = self return unless tag_definition.cached_in_model? owner.class_eval do field_type = tag_definition.cached_in_model_as_list? ? Array : String field tag_definition.cached_in_model_field, type: field_type end end |
#define_relations ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 126 def define_relations # Relations cannot be added for the tags and taggings like they are in ActiveRecord because # Mongoid does not allow for a scope like ActiveRecord does. # # Therefore the relation like actions will have to be defined separately ourselves. If any # relation actions are missed, we'll just have to fix it here when we find them. # (This is far from ideal, but it is the only way to work around the issue at this time.) add_context_taggings_method end |
#parse(*tag_list) ⇒ Object
I’ve defined the parser as being required to return an array of strings. This parse function will take that array and make it a TagList which will then use the tag_definition to apply the rules to that list (like case sensitivity and parameterization, etc.) to get the final list.
64 65 66 67 68 69 70 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 64 def parse(*tag_list) dup_tag_list = tag_list.dup = dup_tag_list..dup [:parser] ||= parser if [:parse] || .key?(:parser) ActsAsTaggableOnMongoid::TagList.new(self, *dup_tag_list, ) end |
#taggings_order ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 72 def taggings_order @taggings_order = if preserve_tag_order? [:created_at.asc, :id.asc] else [] end end |