Module: Effective::CrudController::Paths

Included in:
Effective::CrudController
Defined in:
app/controllers/concerns/effective/crud_controller/paths.rb

Instance Method Summary collapse

Instance Method Details

#referer_redirect_pathObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 72

def referer_redirect_path
  url = request.referer.to_s

  # Referer may not always be present
  return if url.blank?

  # Don't redirect back to this resource's show or edit page
  if resource.try(:destroyed?)
    to_param = (resource.to_param || resource.try(:token) || resource.try(:slug) || resource.id) # to_param is nil sometimes
    return if to_param.present? && url.include?("/#{to_param}")
  end

  # Don't redirect back if we're on duplicate action
  return if url.include?('duplicate_id=')

  # Don't redirect unless we recognize the url
  return unless (Rails.application.routes.recognize_path(url) rescue false) || (Rails.application.routes.recognize_path(URI(url).path) rescue false)

  # Redirect to this recognized url
  url
end

#resource_action_path(action) ⇒ Object



123
124
125
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 123

def resource_action_path(action)
  effective_resource.action_path(action.to_sym, resource)
end

#resource_destroy_pathObject



119
120
121
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 119

def resource_destroy_path
  effective_resource.action_path(:destroy, resource)
end

#resource_duplicate_pathObject



107
108
109
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 107

def resource_duplicate_path
  effective_resource.action_path(:new, duplicate_id: resource.id)
end

#resource_edit_pathObject



111
112
113
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 111

def resource_edit_path
  effective_resource.action_path(:edit, resource)
end

#resource_index_pathObject



99
100
101
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 99

def resource_index_path
  effective_resource.action_path(:index)
end

#resource_new_pathObject



103
104
105
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 103

def resource_new_path
  effective_resource.action_path(:new)
end

#resource_redirect_path(resource, action) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 5

def resource_redirect_path(resource, action)
  submit = commit_action(action)
  redirect = submit[:redirect].respond_to?(:call) ? instance_exec(&submit[:redirect]) : submit[:redirect]

  # If we have a specific redirect for it
  commit_action_redirect = case redirect
    when :index     ; resource_index_path
    when :edit      ; resource_edit_path
    when :show      ; resource_show_path
    when :new       ; resource_new_path
    when :duplicate ; resource_duplicate_path
    when :back      ; referer_redirect_path
    when :save      ; [resource_edit_path, resource_show_path].compact.first
    when Symbol     ; resource_action_path(redirect)
    when String     ; redirect
    else            ; nil
  end

  return commit_action_redirect if commit_action_redirect.present?

  # If we have a magic name
  commit_name_redirect = case params[:commit].to_s
    when 'Save and Add New', 'Add New'
      [resource_new_path, resource_index_path]
    when 'Duplicate'
      [resource_duplicate_path, resource_index_path]
    when 'Continue', 'Save and Continue'
      [resource_index_path]
    else
      []
  end.compact.first

  return commit_name_redirect if commit_name_redirect.present?

  # Otherwise consider the action
  commit_default_redirect = case action
  when :create
    [
      (resource_show_path if EffectiveResources.authorized?(self, :show, resource)),
      (resource_edit_path if EffectiveResources.authorized?(self, :edit, resource)),
      (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
    ]
  when :update
    [
      (resource_edit_path if EffectiveResources.authorized?(self, :edit, resource)),
      (resource_show_path if EffectiveResources.authorized?(self, :show, resource)),
      (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
    ]
  when :destroy
    [
      referer_redirect_path,
      (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
    ]
  else
    [
      referer_redirect_path,
      (resource_edit_path if EffectiveResources.authorized?(self, :edit, resource)),
      (resource_show_path if EffectiveResources.authorized?(self, :show, resource)),
      (resource_index_path if EffectiveResources.authorized?(self, :index, resource.class))
    ]
  end.compact.first

  return commit_default_redirect if commit_default_redirect.present?

  root_path
end

#resource_show_pathObject



115
116
117
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 115

def resource_show_path
  effective_resource.action_path(:show, resource)
end

#specific_redirect_path?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 94

def specific_redirect_path?(action = nil)
  submit = commit_action(action)
  (submit[:redirect].respond_to?(:call) ? instance_exec(&submit[:redirect]) : submit[:redirect]).present?
end