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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 95

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



151
152
153
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 151

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

#resource_destroy_pathObject



147
148
149
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 147

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

#resource_duplicate_pathObject



135
136
137
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 135

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

#resource_edit_pathObject



139
140
141
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 139

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

#resource_index_pathObject



127
128
129
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 127

def resource_index_path
  effective_resource.action_path(:index)
end

#resource_new_pathObject



131
132
133
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 131

def resource_new_path
  effective_resource.action_path(:new)
end

#resource_redirect_error_path(resource, action) ⇒ Object



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

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

  # 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?

  resource_redirect_path(resource, action)
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



143
144
145
# File 'app/controllers/concerns/effective/crud_controller/paths.rb', line 143

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

#specific_redirect_error_path?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#specific_redirect_path?(action = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

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