跳至內容 跳至搜尋

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
包含模組

執行個體公開方法

*_變更

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

傳回屬性的舊值和新值。

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

*_已變更?

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

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

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

*_先前變更

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

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

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

*_先前已變更(**選項)**

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

如果屬性先前有未儲存的變更,則傳回 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
      

*_先前為

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

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

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

*_為

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

傳回屬性的舊值。

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

*_將變更!

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

如果屬性已就地修改,請使用 *_將變更! 標記屬性正在變更。否則,Active Model 無法追蹤就地屬性的變更。請注意,Active Record 可以自動偵測就地修改。您不需要對 Active Record 模型呼叫 *_將變更!

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

屬性已變更?(attr_name, **選項)

屬性方法 *_changed? 的調度目標。

# File activemodel/lib/active_model/dirty.rb, line 293
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 303
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 308
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 298
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 288
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 279
def changed?
  mutations_from_database.any_changes?
end

changed_attributes()

傳回一個雜湊,其中包含未儲存變更的屬性,並指出其原始值,例如 attr => original value

person.name # => "bob"
person.name = 'robert'
person.changed_attributes # => {"name" => "bob"}
# File activemodel/lib/active_model/dirty.rb, line 336
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 346
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 265
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 324
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 318
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 356
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 313
def restore_attributes(attr_names = changed)
  attr_names.each { |attr_name| restore_attribute!(attr_name) }
end