Class: ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition

Inherits:
Object
  • Object
show all
Includes:
Attributes, Changeable, 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

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, Names

Instance Attribute Summary collapse

Attributes included from Attributes

#cached_in_model, #default

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Changeable

#add_changed_from_default?, #add_get_was, #add_list_change, #add_list_changed, #add_list_exists, #add_reset_list, #add_reset_to_default, #add_will_change

Methods included from Names

#all_tag_list_name, #all_tag_list_variable_name, #base_tags_method, #single_tag_type, #tag_list_name, #tag_list_variable_name, #taggings_name

Methods included from Attributes

#force_lowercase, #force_parameterize, #parser, #preserve_tag_order, #remove_unused_tags, #taggings_table, #tags_table

Constructor Details

#initialize(owner, tag_type, options = {}) ⇒ TagTypeDefinition

Returns a new instance of TagTypeDefinition.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 20

def initialize(owner, tag_type, options = {})
  options = options.dup

  options.assert_valid_keys(:parser,
                            :preserve_tag_order,
                            :cached_in_model,
                            :force_lowercase,
                            :force_parameterize,
                            :remove_unused_tags,
                            :tags_table,
                            :taggings_table,
                            :default)

  self.default_value = options.delete(:default)

  save_options(options)

  @owner    = owner
  @tag_type = tag_type
end

Instance Attribute Details

#ownerObject (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_typeObject (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



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 41

def self.copy_from(klass, tag_definition)
  dup_hash = %i[parser
                preserve_tag_order
                cached_in_model
                force_lowercase
                force_parameterize
                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, parse: false]

  ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition.new klass,
                                                           tag_definition.tag_type,
                                                           dup_hash
end

Instance Method Details

#add_all_list_getterObject



199
200
201
202
203
204
205
206
207
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 199

def add_all_list_getter
  tag_definition = self

  owner.taggable_mixin.module_eval do
    define_method(tag_definition.all_tag_list_name) do
      all_tags_list_on tag_definition
    end
  end
end

#add_base_tags_methodObject

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.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 107

def add_base_tags_method
  tag_definition   = self
  base_tags_method = tag_definition.base_tags_method

  owner.taggable_mixin.module_eval do
    break if methods.include?(base_tags_method)

    define_method base_tags_method do
      tag_definition.tags_table.where(taggable_type: tag_definition.owner.name)
    end
  end
end

#add_context_taggings_methodObject



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 132

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_methodObject



145
146
147
148
149
150
151
152
153
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 145

def add_context_tags_method
  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_list_getterObject



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 169

def add_list_getter
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method(tag_list_name) do
      tag_list_on tag_definition
    end

    alias_method "#{tag_list_name}_before_type_cast".to_sym, tag_list_name.to_sym
  end
end

#add_list_setterObject



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 182

def add_list_setter
  tag_definition = self

  owner.taggable_mixin.module_eval do
    define_method("#{tag_definition.tag_list_name}=") do |new_tags|
      dup_tags        = Array.wrap(new_tags).dup
      options         = dup_tags.extract_options!.dup
      options[:parse] = options.fetch(:parse) { true }

      new_list = tag_definition.parse(*dup_tags, options)

      mark_tag_list_changed(new_list)
      tag_list_set(new_list)
    end
  end
end

#add_tag_listObject



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 155

def add_tag_list
  add_list_getter
  add_list_setter
  add_all_list_getter
  add_list_exists
  add_list_change
  add_list_changed
  add_changed_from_default?
  add_will_change
  add_get_was
  add_reset_list
  add_reset_to_default
end

#conflicts_with?(tag_definition) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 58

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_relationsObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 86

def define_base_relations
  tag_definition = self

  add_base_tags_method

  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_relationsObject



120
121
122
123
124
125
126
127
128
129
130
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 120

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
  add_context_tags_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.



70
71
72
73
74
75
76
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 70

def parse(*tag_list)
  dup_tag_list     = tag_list.dup
  options          = dup_tag_list.extract_options!.dup
  options[:parser] ||= parser if options[:parse] || options.key?(:parser)

  ActsAsTaggableOnMongoid::TagList.new(self, *dup_tag_list, options)
end

#taggings_orderObject



78
79
80
81
82
83
84
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition.rb', line 78

def taggings_order
  @taggings_order = if preserve_tag_order?
                      [:created_at.asc, :id.asc]
                    else
                      []
                    end
end