模块:Mongoid::Touchable::InstanceMethods

包含在:
文档
定义于:
lib/mongoid/touchable.rb

Overview

TODO:

使用 ActiveSupport::Concern 进行重构

用于提供混入功能。

实例方法摘要折叠

实例方法详细信息

# _clear_touch_updates (字段 = nil) ⇒对象

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

清除由触摸操作引起的模型更改。

参数:

  • 字段 符号 (默认为: nil

    要更新的附加字段的名称。



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

def _clear_touch_updates(字段 = nil)
  remove_change(:updated_at)
  remove_change(字段) if 字段
  _parent._clear_touch_updates if _touchable_parent?
end

# _gather_touch_updates (now, 字段 = nil) ⇒ Hash< string , Time>

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

在当前文档及其每个父文档(包括节点)上递归设置可触摸字段。 返回要对根文档执行的组合原子 $ 设立操作。

参数:

  • now 时间

    用于同步触摸时间的时间戳。

  • 字段 符号 (默认为: nil

    要更新的附加字段的名称。

返回:

  • ( Hash< string , Time> )

    作为原子 $设立执行的触摸操作。



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/mongoid/touchable.rb', line 71

def _gather_touch_updates(now, 字段 = nil)
  return if touch_callbacks_suppressed?

  字段 = database_field_name(字段)

  write_attribute(:updated_at, now) if respond_to?(:updated_at=)
  write_attribute(字段, now) if 字段.现在?

  接触 = _extract_touches_from_atomic_sets(字段) || {}
  接触.合并!(_parent._gather_touch_updates(now) || {}) if _touchable_parent?
  接触
end

# _run_touch_callbacks_from_root对象

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

为文档及其父文档递归运行 :touch 回调,从根文档开始,级联到每个连续的子文档。



100
101
102
103
104
105
# File 'lib/mongoid/touchable.rb', line 100

def _run_touch_callbacks_from_root
  return if touch_callbacks_suppressed?

  _parent._run_touch_callbacks_from_root if _touchable_parent?
  run_callbacks(:touch)
end

# _touchable_parent?布尔

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

指示父项是否存在且可触摸。

返回:



110
111
112
# File 'lib/mongoid/touchable.rb', line 110

def _touchable_parent?
  _parent && _association&。inverse_association&。touchable?
end

#subsup_touch_callbacks(&block) ⇒ 对象

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

在区块的持续时间内,为包含此模块的类抑制调用触摸回调。

例子:

抑制 Person 文档上的触摸回调:

person.suppress_touch_callbacks { ... }


18
19
20
# File 'lib/mongoid/touchable.rb', line 18

def subsup_touch_callbacks()
  Touchable.subsup_touch_callbacks(self.class.名称, )
end

# touch (字段 = nil) ⇒ true/false

注意:

如果设立了这些选项,则不会自动构建关联。

触摸文档,实际上是将其 updated_at 时间戳和(可选)提供的字段更新为当前时间。 如果存在任何带有 touch 选项的 belongs_to 关联,它们也将被更新。

例子:

更新 updated_at 时间戳。

document.touch

更新 updated_at 和提供的时间戳。

document.touch(:audited)

参数:

  • 字段 符号 (默认为: nil

    要更新的附加字段的名称。

返回:

  • ( true/false )

    如果文档为 new_record,则为 false,否则为 true。



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mongoid/touchable.rb', line 47

def touch(字段 = nil)
  return false if _root.new_record?

  开始
    接触 = _gather_touch_updates(时间.Current, 字段)
    _root.发送(:persist_atomic_operations, ' $set ' => 接触) if 接触.现在?
    _run_touch_callbacks_from_root
  确保
    _clear_touch_updates(字段)
  end

  true
end

# touch_callbacks_suppressed?true | false

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

查询包含此模块的类的触摸回调是否被抑制。

返回:

  • ( true | false )

    是否抑制触摸回调。



28
29
30
# File 'lib/mongoid/touchable.rb', line 28

def touch_callbacks_suppressed?
  Touchable.touch_callbacks_suppressed?(self.class.名称)
end