Module: Effective::CrudController::Respond

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

Instance Method Summary collapse

Instance Method Details

#respond_with_error(format, resource, action) ⇒ Object



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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/concerns/effective/crud_controller/respond.rb', line 39

def respond_with_error(format, resource, action)
  flash.delete(:success)
  flash.now[:danger] ||= resource_flash(:danger, resource, action)

  redirect_flash if specific_redirect_path?(:error)

  run_callbacks(:resource_render)

  if specific_redirect_path?(:error)
    format.html { redirect_to resource_redirect_path(:error) }
    format.js { redirect_to resource_redirect_path(:error) }
    return
  end

  # HTML responder
  case action.to_sym
  when :create
    format.html { render :new }
  when :update
    format.html { render :edit }
  when :destroy
    format.html do
      redirect_flash
      redirect_to(resource_redirect_path(action))
    end
  else # member action
    format.html do
      if resource_edit_path && referer_redirect_path.to_s.end_with?(resource_edit_path)
        @page_title ||= "Edit #{resource}"
        render :edit
      elsif resource_new_path && referer_redirect_path.to_s.end_with?(resource_new_path)
        @page_title ||= "New #{resource_name.titleize}"
        render :new
      elsif resource_action_path(action) && referer_redirect_path.to_s.end_with?(resource_action_path(action)) && template_present?(action)
        @page_title ||= "#{action.to_s.titleize} #{resource}"
        render(action, locals: { action: action })
      elsif resource_show_path && referer_redirect_path.to_s.end_with?(resource_show_path)
        @page_title ||= resource_name.titleize
        render :show
      else
        @page_title ||= resource.to_s
        redirect_flash
        redirect_to(referer_redirect_path || resource_redirect_path(action))
      end
    end
  end

  format.js do
    view = template_present?(action) ? action : :member_action
    render(view, locals: { action: action }) # action.js.erb
  end
end

#respond_with_success(format, resource, action) ⇒ Object



4
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
# File 'app/controllers/concerns/effective/crud_controller/respond.rb', line 4

def respond_with_success(format, resource, action)
  if specific_redirect_path?(action)
    format.html do
      flash[:success] ||= resource_flash(:success, resource, action)
      redirect_to(resource_redirect_path(action))
    end

    format.js do
      flash[:success] ||= resource_flash(:success, resource, action)
      redirect_to(resource_redirect_path(action))
    end
  elsif template_present?(action)
    format.html do
      flash.now[:success] ||= resource_flash(:success, resource, action)
      render(action) # action.html.haml
    end

    format.js do
      flash.now[:success] ||= resource_flash(:success, resource, action)
      #reload_resource unless action == :destroy # Removed.
      render(action) # action.js.erb
    end
  else # Default
    format.html do
      flash[:success] ||= resource_flash(:success, resource, action)
      redirect_to(resource_redirect_path(action))
    end

    format.js do
      flash.now[:success] ||= resource_flash(:success, resource, action)
      render(:member_action, locals: { action: action })
    end
  end
end