Module: ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition::Changeable

Included in:
ActsAsTaggableOnMongoid::Taggable::TagTypeDefinition
Defined in:
lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb

Overview

This module adds methods to a model for the tag_list fields for the Mongoid::Changable attributes of a field including:

* tag_list?
* tag_list_change
* tag_list_changed?
* tag_list_will_change!
* tag_list_changed_from_default?
* tag_list_was
* reset_tag_list!
* reset_tag_list_to_default!

Instance Method Summary collapse

Instance Method Details

#add_changed_from_default?Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 80

def add_changed_from_default?
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("#{tag_list_name}_changed_from_default?") do
      changed_value = tag_definition.default
      current_value = public_send(tag_list_name)

      unless tag_definition.preserve_tag_order?
        changed_value.sort!
        current_value.sort!
      end

      current_value != changed_value
    end
  end
end

#add_get_wasObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 99

def add_get_was
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("#{tag_list_name}_was") do
      return tag_definition.default if new_record?

      if public_send "#{tag_list_name}_changed?"
        changed_attributes[tag_list_name]
      else
        public_send tag_list_name
      end
    end
  end
end

#add_list_changeObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 28

def add_list_change
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("#{tag_list_name}_change") do
      return nil unless public_send("#{tag_list_name}_changed?")

      changed_value = public_send("#{tag_list_name}_was")
      current_value = public_send(tag_list_name)

      [changed_value, current_value] unless current_value == changed_value
    end
  end
end

#add_list_changedObject

rubocop:disable Metrics/AbcSize



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 46

def add_list_changed
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("#{tag_list_name}_changed?") do
      return false unless changed_attributes.key?(tag_list_name)

      changed_value = new_record? ? tag_definition.default : changed_attributes[tag_list_name]
      current_value = public_send(tag_list_name)

      unless tag_definition.preserve_tag_order?
        changed_value.sort!
        current_value.sort!
      end

      current_value != changed_value
    end
  end
end

#add_list_existsObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 17

def add_list_exists
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("#{tag_list_name}?") do
      public_send(tag_list_name).present?
    end
  end
end

#add_reset_listObject



116
117
118
119
120
121
122
123
124
125
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 116

def add_reset_list
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("reset_#{tag_list_name}!") do
      public_send "#{tag_list_name}=", public_send("#{tag_list_name}_was")
    end
  end
end

#add_reset_to_defaultObject



127
128
129
130
131
132
133
134
135
136
# File 'lib/acts_as_taggable_on_mongoid/taggable/tag_type_definition/changeable.rb', line 127

def add_reset_to_default
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("reset_#{tag_list_name}_to_default!") do
      public_send "#{tag_list_name}=", tag_definition.default
    end
  end
end

#add_will_changeObject

rubocop:enable Metrics/AbcSize



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

def add_will_change
  tag_definition = self
  tag_list_name  = tag_definition.tag_list_name

  owner.taggable_mixin.module_eval do
    define_method("#{tag_list_name}_will_change!") do
      attribute_wil_change! tag_list_name
    end
  end
end