跳至內容 跳至搜尋

Active Model Dirty

提供一種方法來追蹤物件中的變更,就像 Active Record 一樣。

實作 ActiveModel::Dirty 的必要條件是

  • 在你的物件中加入 include ActiveModel::Dirty

  • 呼叫 define_attribute_methods 並傳入每個你想要追蹤的方法。

  • 在每次變更追蹤的屬性之前呼叫 *_will_change!

  • 在變更被持久化之後呼叫 changes_applied

  • 當你想要重置變更資訊時,呼叫 clear_changes_information

  • 當你想要恢復先前的資料時,呼叫 restore_attributes

一個最小的實作範例如下

class Person
  include ActiveModel::Dirty

  define_attribute_methods :name

  def initialize
    @name = nil
  end

  def name
    @name
  end

  def name=(val)
    name_will_change! unless val == @name
    @name = val
  end

  def save
    # do persistence work

    changes_applied
  end

  def reload!
    # get the values from the persistence layer

    clear_changes_information
  end

  def rollback!
    restore_attributes
  end
end

一個新建立的 Person 物件是沒有變更的

person = Person.new
person.changed? # => false

變更名稱

person.name = 'Bob'
person.changed?       # => true
person.name_changed?  # => true
person.name_changed?(from: nil, to: "Bob") # => true
person.name_was       # => nil
person.name_change    # => [nil, "Bob"]
person.name = 'Bill'
person.name_change    # => [nil, "Bill"]

儲存變更

person.save
person.changed?      # => false
person.name_changed? # => false

重置變更

person.previous_changes         # => {"name" => [nil, "Bill"]}
person.name_previously_changed? # => true
person.name_previously_changed?(from: nil, to: "Bill") # => true
person.name_previous_change     # => [nil, "Bill"]
person.name_previously_was      # => nil
person.reload!
person.previous_changes         # => {}

復原變更

person.name = "Uncle Bob"
person.rollback!
person.name          # => "Bill"
person.name_changed? # => false

賦予相同的值會使屬性保持不變

person.name = 'Bill'
person.name_changed? # => false
person.name_change   # => nil

哪些屬性已變更?

person.name = 'Bob'
person.changed # => ["name"]
person.changes # => {"name" => ["Bill", "Bob"]}

如果一個屬性被原地修改,則使用 *_will_change! 來標記該屬性正在變更。否則 Active Model 無法追蹤原地修改的屬性變更。請注意,Active Record 可以自動偵測原地修改。你不需要在 Active Record 模型上呼叫 *_will_change!

person.name_will_change!
person.name_change # => ["Bill", "Bill"]
person.name << 'y'
person.name_change # => ["Bill", "Billy"]

方法可以透過 name_changed? 的方式呼叫,或者透過傳入參數到通用方法 attribute_changed?("name") 的方式呼叫。

方法
#
A
C
P
R
已包含的模組

實體公開方法

*_change

此方法會為每個屬性生成。

返回屬性的舊值和新值。

person = Person.new
person.name = 'Nick'
person.name_change # => [nil, 'Nick']
# File activemodel/lib/active_model/dirty.rb, line 155
      

*_changed?

此方法會為每個屬性生成。

如果屬性有未儲存的變更,則返回 true。

person = Person.new
person.name = 'Andrew'
person.name_changed? # => true
# File activemodel/lib/active_model/dirty.rb, line 144
      

*_previous_change

此方法會為每個屬性生成。

返回上次儲存之前屬性的舊值和新值。

person = Person.new
person.name = 'Emmanuel'
person.save
person.name_previous_change # => [nil, 'Emmanuel']
# File activemodel/lib/active_model/dirty.rb, line 193
      

*_previously_changed?(**options)

此方法會為每個屬性生成。

如果屬性先前有未儲存的變更,則返回 true。

person = Person.new
person.name = 'Britanny'
person.save
person.name_previously_changed? # => true
person.name_previously_changed?(from: nil, to: 'Britanny') # => true
# File activemodel/lib/active_model/dirty.rb, line 129
      

*_previously_was

此方法會為每個屬性生成。

返回上次儲存之前屬性的舊值。

person = Person.new
person.name = 'Sage'
person.save
person.name_previously_was  # => nil
# File activemodel/lib/active_model/dirty.rb, line 205
      

*_was

此方法會為每個屬性生成。

返回屬性的舊值。

person = Person.new(name: 'Steph')
person.name = 'Stephanie'
person.name_was # => 'Steph'
# File activemodel/lib/active_model/dirty.rb, line 182
      

*_will_change!

此方法會為每個屬性生成。

如果屬性被原地修改,則使用 *_will_change! 來標記該屬性正在變更。否則 Active Model 無法追蹤原地屬性變更。請注意,Active Record 可以自動偵測原地修改。你不需要在 Active Record 模型上呼叫 *_will_change!

person = Person.new('Sandy')
person.name_will_change!
person.name_change # => ['Sandy', 'Sandy']
# File activemodel/lib/active_model/dirty.rb, line 166
      

attribute_changed?(attr_name, **options)

*_changed? 屬性方法的派發目標。

# File activemodel/lib/active_model/dirty.rb, line 300
def attribute_changed?(attr_name, **options)
  mutations_from_database.changed?(attr_name.to_s, **options)
end

attribute_previously_changed?(attr_name, **options)

*_previously_changed? 屬性方法的派發目標。

# File activemodel/lib/active_model/dirty.rb, line 310
def attribute_previously_changed?(attr_name, **options)
  mutations_before_last_save.changed?(attr_name.to_s, **options)
end

attribute_previously_was(attr_name)

*_previously_was 屬性方法的派發目標。

# File activemodel/lib/active_model/dirty.rb, line 315
def attribute_previously_was(attr_name)
  mutations_before_last_save.original_value(attr_name.to_s)
end

attribute_was(attr_name)

*_was 屬性方法的派發目標。

# File activemodel/lib/active_model/dirty.rb, line 305
def attribute_was(attr_name)
  mutations_from_database.original_value(attr_name.to_s)
end

changed()

返回一個陣列,其中包含具有未儲存變更的屬性名稱。

person.changed # => []
person.name = 'bob'
person.changed # => ["name"]
# File activemodel/lib/active_model/dirty.rb, line 295
def changed
  mutations_from_database.changed_attribute_names
end

changed?()

如果任何屬性具有未儲存的變更,則返回 true,否則返回 false

person.changed? # => false
person.name = 'bob'
person.changed? # => true
# File activemodel/lib/active_model/dirty.rb, line 286
def changed?
  mutations_from_database.any_changes?
end

changed_attributes()

返回一個雜湊,其中包含具有未儲存變更的屬性,指示其原始值,例如 attr => 原始值

person.name # => "bob"
person.name = 'robert'
person.changed_attributes # => {"name" => "bob"}
# File activemodel/lib/active_model/dirty.rb, line 343
def changed_attributes
  mutations_from_database.changed_values
end

changes()

返回一個雜湊,其中包含已變更的屬性,指示其原始值和新值,例如 attr => [原始值,新值]

person.changes # => {}
person.name = 'bob'
person.changes # => { "name" => ["bill", "bob"] }
# File activemodel/lib/active_model/dirty.rb, line 353
def changes
  mutations_from_database.changes
end

changes_applied()

清除變更資料,並將 changes 移至 previous_changes,將 mutations_from_database 移至 mutations_before_last_save

# File activemodel/lib/active_model/dirty.rb, line 272
def changes_applied
  unless defined?(@attributes)
    mutations_from_database.finalize_changes
  end
  @mutations_before_last_save = mutations_from_database
  forget_attribute_assignments
  @mutations_from_database = nil
end

clear_*_change

此方法會為每個屬性生成。

清除屬性的所有變更資料:目前變更和先前變更。

person = Person.new(name: 'Chris')
person.name = 'Jason'
person.name_change # => ['Chris', 'Jason']
person.clear_name_change
person.name_change # => nil
# File activemodel/lib/active_model/dirty.rb, line 241
attribute_method_suffix "_previously_changed?", "_changed?", parameters: "**options"

clear_attribute_changes(attr_names)

# File activemodel/lib/active_model/dirty.rb, line 331
def clear_attribute_changes(attr_names)
  attr_names.each do |attr_name|
    clear_attribute_change(attr_name)
  end
end

clear_changes_information()

清除所有變更資料:目前變更和先前變更。

# File activemodel/lib/active_model/dirty.rb, line 325
def clear_changes_information
  @mutations_before_last_save = nil
  forget_attribute_assignments
  @mutations_from_database = nil
end

previous_changes()

返回在模型儲存之前已變更的屬性雜湊。

person.name # => "bob"
person.name = 'robert'
person.save
person.previous_changes # => {"name" => ["bob", "robert"]}
# File activemodel/lib/active_model/dirty.rb, line 363
def previous_changes
  mutations_before_last_save.changes
end

restore_*!

此方法會為每個屬性生成。

將屬性恢復為舊值。

person = Person.new
person.name = 'Amanda'
person.restore_name!
person.name # => nil
# File activemodel/lib/active_model/dirty.rb, line 217
      

restore_attributes(attr_names = changed)

恢復所提供屬性的所有先前資料。

# File activemodel/lib/active_model/dirty.rb, line 320
def restore_attributes(attr_names = changed)
  attr_names.each { |attr_name| restore_attribute!(attr_name) }
end