2017-05-01 16:46:30 -04:00
|
|
|
module RoutableActions
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
2017-05-04 17:20:13 -04:00
|
|
|
def find_routable!(routable_klass, requested_full_path, extra_authorization_method: nil)
|
|
|
|
routable = routable_klass.find_by_full_path(requested_full_path, follow_redirects: request.get?)
|
|
|
|
|
|
|
|
if authorized?(routable_klass, routable, extra_authorization_method)
|
|
|
|
ensure_canonical_path(routable, requested_full_path)
|
|
|
|
routable
|
|
|
|
else
|
|
|
|
route_not_found
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def authorized?(routable_klass, routable, extra_authorization_method)
|
|
|
|
action = :"read_#{routable_klass.to_s.underscore}"
|
|
|
|
return false unless can?(current_user, action, routable)
|
|
|
|
|
|
|
|
if extra_authorization_method
|
|
|
|
send(extra_authorization_method, routable)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-01 16:46:30 -04:00
|
|
|
def ensure_canonical_path(routable, requested_path)
|
|
|
|
return unless request.get?
|
|
|
|
|
2017-05-04 17:20:13 -04:00
|
|
|
canonical_path = routable.try(:full_path) || routable.namespace.full_path
|
|
|
|
if canonical_path != requested_path
|
2017-05-04 14:12:19 -04:00
|
|
|
flash[:notice] = 'This project has moved to this location. Please update your links and bookmarks.'
|
2017-05-04 17:20:13 -04:00
|
|
|
redirect_to request.original_url.sub(requested_path, canonical_path)
|
2017-05-01 16:46:30 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|