* Create migrations
* Create PageRedirect model
* Refactor migration and add timestamps
* Add routes for internal/page_redirects
* Add index controller action and view
* Change background color of version in /internal
* Add page_redirect factory and index specs
* Fix specs
* Use ransack for search
* Alphabetize internal sidenav
* Rename spec
* Add edit view with destroy button
* Refactor page_redirect form partial
* Update error messages and fix redirect
* Small fixes and hookup new and create
* Specs FTW
* Fix migration, overridden --> source
* Add PageRedirect model specs
- Validate presence of status
* Update routes
* Code climate fixes
* Add old_slug_url and new_slug_url helper methods
* Prevent updating old_slug, refactor _form
* Add URLs to index view for slugs
* Better spec wording
* Change version to badges and add to edit view
* Update destroy response
* slug --> path 🙈
* Add PageRedirects to seed file
* Fix seed file
* ACTUALLY fix the seed file
* slug --> path in PageRedirect factory
* Remove bug fix from seeds file
* Move menu items to controller constant
* Update source type validation in model spec
* Rename page_redirect --> path_redirect
* Add AuditLog for admin create, destroy, and update
* Remove redundant index
* Cleanup old name of page_redirect
* Remove old model
* Update AuditLog to :internal
* Titleize search placeholder task in internal
* Add comment to explain MENU_ITEMS constant
* Add warning text on edit page about many updates
* Remove default and allow null on source
* Fix comment
* Add path_redirect validations and model specs
- Validate old_path != new_path
- Validate new_path isn't an existing redirect
40 lines
1.1 KiB
Ruby
40 lines
1.1 KiB
Ruby
class PathRedirect < ApplicationRecord
|
|
SOURCES = %w[admin service].freeze
|
|
|
|
validates :old_path, presence: true, uniqueness: true
|
|
validates :new_path, presence: true
|
|
|
|
# Validate old_path != new_path
|
|
validates :old_path, exclusion: { in: ->(path_redirect) { [path_redirect.new_path] }, message: "the old_path cannot be the same as the new_path" }
|
|
|
|
validates :source, inclusion: { in: SOURCES }, allow_blank: true
|
|
|
|
# This issues a DB query so best to keep this validation as far down as possible
|
|
validate :new_path_wont_redirect
|
|
|
|
before_save :increment_version, if: :will_save_change_to_new_path?
|
|
|
|
resourcify
|
|
|
|
def old_path_url
|
|
URL.url(old_path)
|
|
end
|
|
|
|
def new_path_url
|
|
URL.url(new_path)
|
|
end
|
|
|
|
private
|
|
|
|
def increment_version
|
|
self.version += 1
|
|
end
|
|
|
|
# This ensures we don't end up in an infinite redirect loop where
|
|
# /old_path --> /new_path and then another record has /new_path --> /old_path
|
|
def new_path_wont_redirect
|
|
return unless PathRedirect.find_by(old_path: new_path)
|
|
|
|
errors.add(:new_path, "this new_path is already being redirected")
|
|
end
|
|
end
|