summaryrefslogtreecommitdiff
path: root/app/models/route.rb
blob: d40214b9da6ac93904ce7ebbb4b0352d353ae511 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Route < ActiveRecord::Base
  belongs_to :source, polymorphic: true

  validates :source, presence: true

  validates :path,
    length: { within: 1..255 },
    presence: true,
    uniqueness: { case_sensitive: false }

  after_update :rename_children, if: :path_changed?

  def rename_children
    # We update each row separately because MySQL does not have regexp_replace.
    # rubocop:disable Rails/FindEach
    Route.where('path LIKE ?', "#{path_was}%").each do |route|
      # Note that update column skips validation and callbacks.
      # We need this to avoid recursive call of rename_children method
      route.update_column(:path, route.path.sub(path_was, path))
    end
  end
end