継続的ブログ

主にweb系の技術について書いています

mark_for_destruction を before_validation で

gitlabのコード読んでいていいなと思ったやつ。

before_validation で値が空のものに mark_for_destruction でフラグ立ててやる。

これなら空のやつでもとりあえず save しとけばいいので、before_validation でやるのもいいなと思った。

  • project.rb
before_validation :mark_remote_mirrors_for_removal, if: -> { RemoteMirror.table_exists? }

...

def mark_remote_mirrors_for_removal
  remote_mirrors.each(&:mark_for_delete_if_blank_url)
end

https://github.com/gitlabhq/gitlabhq/blob/d5994552b2088c0c27e419b3ae2c4432ea329a82/app/models/project.rb

  • remote_mirror.rb
def mark_for_delete_if_blank_url
  mark_for_destruction if url.blank?
end

https://github.com/gitlabhq/gitlabhq/blob/299011313cef8fbeb8d5eeafcc60374211c88ec8/app/models/remote_mirror.rb

gitlabのコード読むのはいい勉強になる。

github.com