Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

RailsのDBに紐付かないModelでもActiveModel::Dirtyを使って要素の変更を検知したい

More than 1 year has passed since last update.
class Hoge
  include ActiveModel::Model
  include ActiveModel::Dirty

  def self.attributes
    [ :name, :address, :tel, :note ]
  end
  attr_accessor *self.attributes
  define_attribute_methods *self.attributes

  def initialize(args)
    args.each do |k,v|
      raise "unpermitted args:#{k}" unless self.class.attributes.index(k)
      self.send("#{k}=", v)
    end

    # 要素の変更検知
    self.class.attributes.each do |attribute_name|
      define_singleton_method("#{attribute_name}=") do |value|
        # self.class.send(:define_method, "#{attribute_name}=")でもOK
        self.send("#{attribute_name}_will_change!") unless value==self.send(attribute_name)
        super(value)
      end
    end
  end
end

使用例

# 初期化
> hoge = Hoge.new(name: 'yamada', tel: '090512221234')
=> #<Hoge:0x007f8f3b093b58 @name="yamada", @tel="090512221234">

# 変更なしとなることを確認
> hoge.changed?
=> false
> hoge.changes
=> {}

# 変更してみる
> hoge.tel = "08013456789"
=> "08013456789"

# 変更が検知される
> hoge.changed?
=> true
# 差分もバッチリ
> hoge.changes
=> {"tel"=>["090512221234", "08013456789"]}
yaboojp
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away