Sha256: 24e794bad880d6634769ee57bd3b36c3bd160c978838e22bcfa8a59b2dae95be

Contents?: true

Size: 1.18 KB

Versions: 9

Compression:

Stored size: 1.18 KB

Contents

# Example from 'Overwriting default accessors' in ActiveRecord::Base.
class Song < ActiveRecord::Base
  has_paper_trail
  attr_accessor :name

  # Uses an integer of seconds to hold the length of the song
  def length=(minutes)
    write_attribute(:length, minutes.to_i * 60)
  end

  def length
    read_attribute(:length) / 60
  end

  # override attributes hashes like some libraries do
  def attributes_with_name
    if name
      attributes_without_name.merge(name: name)
    else
      attributes_without_name
    end
  end

  # `alias_method_chain` is deprecated in rails 5, but we cannot use the
  # suggested replacement, `Module#prepend`, because we still support ruby 1.9.
  alias attributes_without_name attributes
  alias attributes attributes_with_name

  def changed_attributes_with_name
    if name
      changed_attributes_without_name.merge(name: name)
    else
      changed_attributes_without_name
    end
  end

  # `alias_method_chain` is deprecated in rails 5, but we cannot use the
  # suggested replacement, `Module#prepend`, because we still support ruby 1.9.
  alias changed_attributes_without_name changed_attributes
  alias changed_attributes changed_attributes_with_name
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/paper_trail-5.2.3/test/dummy/app/models/song.rb
paper_trail-5.2.3 test/dummy/app/models/song.rb
paper_trail-5.2.2 test/dummy/app/models/song.rb
paper_trail-5.2.1 test/dummy/app/models/song.rb
paper_trail-5.2.0 test/dummy/app/models/song.rb
paper_trail-5.1.1 test/dummy/app/models/song.rb
paper_trail-5.1.0 test/dummy/app/models/song.rb
paper_trail-5.0.1 test/dummy/app/models/song.rb
paper_trail-5.0.0 test/dummy/app/models/song.rb