2016-10-31 07:00:53 -04:00
|
|
|
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 }
|
|
|
|
|
2017-01-05 07:43:50 -05:00
|
|
|
after_update :rename_descendants, if: :path_changed?
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2017-01-05 07:43:50 -05:00
|
|
|
def rename_descendants
|
2016-10-31 07:00:53 -04:00
|
|
|
# We update each row separately because MySQL does not have regexp_replace.
|
|
|
|
# rubocop:disable Rails/FindEach
|
2016-12-19 15:36:47 -05:00
|
|
|
Route.where('path LIKE ?', "#{path_was}/%").each do |route|
|
2016-10-31 07:00:53 -04:00
|
|
|
# Note that update column skips validation and callbacks.
|
2017-01-05 12:20:12 -05:00
|
|
|
# We need this to avoid recursive call of rename_descendants method
|
2016-10-31 07:00:53 -04:00
|
|
|
route.update_column(:path, route.path.sub(path_was, path))
|
|
|
|
end
|
2017-01-05 12:20:12 -05:00
|
|
|
# rubocop:enable Rails/FindEach
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|