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-02-04 13:26:11 -05:00
|
|
|
after_update :rename_descendants
|
2016-10-31 07:00:53 -04:00
|
|
|
|
2017-03-21 12:04:12 -04:00
|
|
|
scope :inside_path, -> (path) { where('routes.path LIKE ?', "#{sanitize_sql_like(path)}/%") }
|
|
|
|
|
2017-01-05 07:43:50 -05:00
|
|
|
def rename_descendants
|
2017-02-04 13:26:11 -05:00
|
|
|
if path_changed? || name_changed?
|
2017-03-21 12:04:12 -04:00
|
|
|
descendants = self.class.inside_path(path_was)
|
2017-02-04 13:26:11 -05:00
|
|
|
|
|
|
|
descendants.each do |route|
|
2017-02-08 15:54:33 -05:00
|
|
|
attributes = {}
|
|
|
|
|
|
|
|
if path_changed? && route.path.present?
|
|
|
|
attributes[:path] = route.path.sub(path_was, path)
|
|
|
|
end
|
|
|
|
|
2017-03-20 19:17:14 -04:00
|
|
|
if name_changed? && name_was.present? && route.name.present?
|
2017-02-08 15:54:33 -05:00
|
|
|
attributes[:name] = route.name.sub(name_was, name)
|
|
|
|
end
|
2017-02-04 13:26:11 -05:00
|
|
|
|
|
|
|
# Note that update_columns skips validation and callbacks.
|
|
|
|
# We need this to avoid recursive call of rename_descendants method
|
2017-02-08 15:54:33 -05:00
|
|
|
route.update_columns(attributes) unless attributes.empty?
|
2017-02-04 13:26:11 -05:00
|
|
|
end
|
2016-10-31 07:00:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|