模块:Mongoid::Changeable

扩展方式:
ActiveSupport::Concern
包含在:
可组合
定义于:
lib/mongoid/changeable.rb

Overview

定义脏跟踪的行为。

在命名空间下定义

模块: 类方法 类: 任何内容

实例方法摘要折叠

实例方法详细信息

#attribute_before_last_save (attr) ⇒对象

返回属性在上次保存之前的原始值。

此方法在回调后非常有用,可获取 的原始值

an attribute before the save that triggered the callbacks to run.

参数:

  • attr (符号 | string )

    属性的名称。

返回:

  • ( Object )

    上次保存之前的属性值。



150
151
152
153
# File 'lib/mongoid/changeable.rb', line 150

def attribute_before_last_save(attr)
  attr = database_field_name(attr)
  properties_before_last_save[attr]
end

# ChangedArray<String>

获取文档的已更改属性。

例子:

获取已更改的属性。

model.changed

返回:

  • ( Array<String> )

    已更改的属性。



14
15
16
# File 'lib/mongoid/changeable.rb', line 14

def 已更改
  Changed_attributes.密钥.SELECT { |attr| attribute_change(attr) }
end

#已更改?true | false

文档是否已更改?

例子:

文档是否已更改?

model.changed?

返回:

  • ( true | false )

    如果文档发生更改。



32
33
34
# File 'lib/mongoid/changeable.rb', line 32

def 改变?
  更改.values.任何? { |val| val } || children_changed?
end

# Changed_attributesHash< string , Object>

获取属性更改。

例子:

获取属性更改。

model.changed_attributes

返回:

  • ( Hash< string , Object> )

    属性发生变化。



51
52
53
# File 'lib/mongoid/changeable.rb', line 51

def Changed_attributes
  @changed_attributes ||= {}
end

#changesHash< string , Array<Object, Object> ]更改

获取文档的所有更改。

例子:

获取所有更改。

model.changes

返回:

  • Hash< string , Array<Object, Object> ]更改

    Hash< string , Array<Object, Object> ] 更改。



61
62
63
64
65
66
# File 'lib/mongoid/changeable.rb', line 61

def 更改
  已更改.each_with_object({}) do |attr, 更改|
    更改 = attribute_change(attr)
    更改[attr] = 更改 if 更改
  end.with_indifference_access
end

#children_changed?true | false

注意:

这是有意只考虑子节点而不是后代。

此文档的任何子文档(嵌入式文档)是否已更改?

返回:

  • ( true | false )

    如果有任何子项发生变化。



41
42
43
# File 'lib/mongoid/changeable.rb', line 41

def children_changed?
  @children_may_have_changed || _children.任何?(:changed?)
end

#children_may_have_changed!对象

此方法是私有 API 的一部分。 您应尽可能避免使用此方法,因为它将来可能会被删除或更改。

表示此文档的子文档可能已更改,应在验证文档时进行检查。



22
23
24
# File 'lib/mongoid/changeable.rb', line 22

def children_may_have_changed!
  @children_may_have_changed = true
end

# move_changes对象

保存后调用此方法,以便可以正确切换更改。

这将取消设置已记忆的子数组,将新记录标志设置为 false,将文档设置为已验证,并移动脏更改。

例子:

将更改移至上一个。

person.move_changes


75
76
77
78
79
80
81
82
83
# File 'lib/mongoid/changeable.rb', line 75

def move_changes
  @changes_before_last_save = @previous_changes
  @previous_changes = 更改
  @attributes_before_last_save = @previous_attributes
  @previous_attributes = 属性.dup
  @children_may_have_changed = false
  reset_atomic_updates!
  Changed_attributes.清除
end

# post_persist对象

在持久化文档后需要执行的操作。

例子:

处理帖子持久性。

document.post_persist


89
90
91
92
93
# File 'lib/mongoid/changeable.rb', line 89

def post_persist
  reset_persisted_descendants
  reset_attributes_before_type_cast
  move_changes
end

# previous_changesHash< string , Array<Object, Object> ] 以前的更改

获取文档的先前更改。

例子:

获取以前的更改。

model.previous_changes

返回:

  • ( Hash< string , Array<Object, Object> ] 以前的更改

    Hash< string , Array<Object, Object> ] 以前的更改。



101
102
103
# File 'lib/mongoid/changeable.rb', line 101

def previous_changes
  @previous_changes ||= {}
end

# remove_change (name) ⇒对象

从脏属性哈希中删除更改。 由单字段原子更新器使用。

例子:

删除已标记的更改。

model.remove_change(:field)

参数:

  • 名称 (符号 | string )

    字段的名称。



112
113
114
# File 'lib/mongoid/changeable.rb', line 112

def remove_change(名称)
  Changed_attributes.删除(名称.to_s)
end

#saved_change_to_attribute(attr) ⇒ Array<Object> | nil

返回上次保存期间对属性的更改。

参数:

  • attr (符号 | string )

    属性的名称。

返回:

  • ( Array<Object> | nil )

    如果属性已更改,则返回包含原始值和保存值的大量,否则返回 nil。



161
162
163
164
# File 'lib/mongoid/changeable.rb', line 161

def saved_change_to_attribute(attr)
  attr = database_field_name(attr)
  previous_changes[attr]
end

# Saved_change_to_attribute? (attr, from: Utils::PLACEHOLDER, to: Utils::PLACEHOLDER) ⇒ true | false

返回此属性在上次保存期间是否已更改。

此方法在回调后非常有用,可以查看更改

in an attribute during the save that triggered the callbacks to run.

参数:

  • attr ( string )

    属性的名称。

  • from 对象 (默认为: Utils::PLACEHOLDER

    更改属性的对象(可选)。

  • to 对象 (默认为: Utils::PLACEHOLDER

    属性更改为的对象(可选)。

返回:

  • ( true | false )

    属性在上次保存期间是否已更改。



176
177
178
179
180
181
182
183
184
185
# File 'lib/mongoid/changeable.rb', line 176

def Saved_change_to_attribute?(attr, 来自: Utils::占位符, 至: Utils::占位符)
  更改 = saved_change_to_attribute(attr)
  return false 除非 更改.is_a?(阵列)

  return true if Utils.placeholder?(from) && Utils.placeholder?(to)
  return 更改.first == from if Utils.placeholder?(to)
  return 更改.last == to if Utils.placeholder?(from)

  更改.first == from && 更改.last == to
end

# setter哈希

获取每个已更改字段的所有新值,并将其传递给MongoDB $ 设立修饰符。

例子:

获取原子更新的 setter。

person = Person.new(:title => "Sir")
person.title = "Madam"
person.setters # returns { "title" => "Madam" }

返回:

  • (哈希)

    原子 setter 的Hash



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/mongoid/changeable.rb', line 125

def setter
  mods = {}
  更改.each_pair do |名称, 更改|
    来年 除非 更改

    , new = 更改
    字段 = 字段[名称]
    key = atomic_attribute_name(名称)
    if 字段&。可调整大小?
      字段.add_atomic_changes(self, 名称, key, mods, new, )
    else
      mods[key] = new 除非 atomic_unsets.包括?(key)
    end
  end
  mods
end

#will_save_change_to_attribute?(attr, **kwargs) ⇒ true | false

返回此属性在下次保存时是否会更改。

此方法在验证中和回调之前很有用,以确定

if the next call to save will change a particular attribute.

参数:

  • attr ( string )

    属性的名称。

  • **kwargs

    可选关键字参数。

返回:

  • ( true | false )

    下次保存时该属性是否会更改。



199
200
201
# File 'lib/mongoid/changeable.rb', line 199

def will_save_change_to_attribute?(attr, **kwargs)
  attribute_changed?(attr, **kwargs)
end