2017-05-01 20:46:30 +00:00
module RoutableActions
extend ActiveSupport :: Concern
2017-05-05 00:06:01 +00:00
def find_routable! ( routable_klass , requested_full_path , extra_authorization_proc : nil )
2017-05-04 21:20:13 +00:00
routable = routable_klass . find_by_full_path ( requested_full_path , follow_redirects : request . get? )
2017-05-11 20:57:03 +00:00
if routable_authorized? ( routable , extra_authorization_proc )
2017-05-04 21:20:13 +00:00
ensure_canonical_path ( routable , requested_full_path )
routable
else
route_not_found
nil
end
end
2017-05-11 20:57:03 +00:00
def routable_authorized? ( routable , extra_authorization_proc )
action = :" read_ #{ routable . class . to_s . underscore } "
2017-05-04 21:20:13 +00:00
return false unless can? ( current_user , action , routable )
2017-05-05 00:06:01 +00:00
if extra_authorization_proc
extra_authorization_proc . call ( routable )
2017-05-04 21:20:13 +00:00
else
true
end
end
2017-05-18 19:56:39 +00:00
def ensure_canonical_path ( routable , requested_full_path )
2017-05-01 20:46:30 +00:00
return unless request . get?
2017-05-05 00:06:01 +00:00
canonical_path = routable . full_path
2017-05-18 19:56:39 +00:00
if canonical_path != requested_full_path
if canonical_path . casecmp ( requested_full_path ) != 0
flash [ :notice ] = " #{ routable . class . to_s . titleize } ' #{ requested_full_path } ' was moved to ' #{ canonical_path } '. Please update any links and bookmarks that may still have the old path. "
2017-05-05 00:06:01 +00:00
end
2018-01-11 16:34:01 +00:00
2017-05-18 23:23:05 +00:00
redirect_to build_canonical_path ( routable )
2017-05-01 20:46:30 +00:00
end
end
end