Skip to content
This repository was archived by the owner on Mar 9, 2020. It is now read-only.

Commit 4e2d0bf

Browse files
committed
default_responses: extract the if/action/else construct
This could potentially make the default_*_response methods obsolete. Let's see what codeclimate says about it.
1 parent 37139ad commit 4e2d0bf

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

app/controllers/concerns/default_responses.rb

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
1+
# a collection of default responses to reduce clutter in the controllers
12
module DefaultResponses
23
extend ActiveSupport::Concern
34

4-
def default_save_response(model)
5+
def redirect_or_render_on_action(model, success_message, error_view)
56
respond_to do |format|
6-
if model.save
7-
format.html { redirect_to model, notice: '%s was successfully created.' % model.class }
7+
if yield
8+
format.html { redirect_to model, notice: success_message }
89
format.json { render :show, status: :created, location: model }
910
else
10-
format.html { render :new }
11+
format.html { render error_view }
1112
format.json { render json: model.errors, status: :unprocessable_entity }
1213
end
1314
end
1415
end
1516

17+
def default_save_response(model)
18+
msg = format('%s was successfully created.', model.class)
19+
redirect_or_render_on_action(model, msg, :new) { model.save }
20+
end
21+
1622
def default_update_response(model, params)
17-
respond_to do |format|
18-
if model.update(params)
19-
format.html { redirect_to model, notice: '%s was successfully updated.' % model.class }
20-
format.json { render :show, status: :ok, location: model }
21-
else
22-
format.html { render :edit }
23-
format.json { render json: model.errors, status: :unprocessable_entity }
24-
end
25-
end
23+
msg = format('%s was successfully updated.', model.class)
24+
redirect_or_render_on_action(model, msg, :edit) { model.update(params) }
2625
end
2726

2827
def default_destroy_response(destination_url, success_msg)

0 commit comments

Comments
 (0)