Module: RoutableActions

Instance Method Summary collapse

Instance Method Details

#ensure_canonical_path(routable, routable_full_path) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/concerns/routable_actions.rb', line 45

def ensure_canonical_path(routable, routable_full_path)
  return unless request.get?

  canonical_path = routable.full_path
  return unless canonical_path != routable_full_path

  if !request.xhr? && request.format.html? && canonical_path.casecmp(routable_full_path) != 0
    flash[:notice] = "#{routable.class.to_s.titleize} '#{routable_full_path}' was moved to '#{canonical_path}'. Please update any links and bookmarks that may still have the old path."
  end

  redirect_to build_canonical_path(routable), status: :moved_permanently
end

#find_routable!(routable_klass, routable_full_path, full_path, extra_authorization_proc: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'app/controllers/concerns/routable_actions.rb', line 6

def find_routable!(routable_klass, routable_full_path, full_path, extra_authorization_proc: nil)
  routable = routable_klass.find_by_full_path(routable_full_path, follow_redirects: request.get?)
  if routable_authorized?(routable, extra_authorization_proc)
    ensure_canonical_path(routable, routable_full_path)
    routable
  else
    perform_not_found_actions(routable, not_found_actions, full_path)

    route_not_found unless performed?

    nil
  end
end

#not_found_actionsObject



20
21
22
# File 'app/controllers/concerns/routable_actions.rb', line 20

def not_found_actions
  [ProjectUnauthorized::ControllerActions.on_routable_not_found]
end

#perform_not_found_actions(routable, actions, full_path) ⇒ Object



24
25
26
27
28
29
30
# File 'app/controllers/concerns/routable_actions.rb', line 24

def perform_not_found_actions(routable, actions, full_path)
  actions.each do |action|
    break if performed?

    instance_exec(routable, full_path, &action)
  end
end

#routable_authorized?(routable, extra_authorization_proc) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/concerns/routable_actions.rb', line 32

def routable_authorized?(routable, extra_authorization_proc)
  return false unless routable

  action = :"read_#{routable.class.to_s.underscore}"
  return false unless can?(current_user, action, routable)

  if extra_authorization_proc
    extra_authorization_proc.call(routable)
  else
    true
  end
end