Skip to content

Commit ab1da1c

Browse files
authored
Centralise detection of previous route (#1222)
* Remove unused previous * Centralise previous object detection. Remove falling back to a number if not found * Apply suggestions from code review
1 parent 38b6335 commit ab1da1c

10 files changed

+18
-11
lines changed

app/controllers/admin/field_groups_controller.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ def new
2222
def edit
2323
@field_group = FieldGroup.find(params[:id])
2424

25-
@previous = FieldGroup.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
26-
2725
respond_with(@field_group)
2826
end
2927

app/controllers/admin/tags_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def new
2828
# GET /admin/tags/1/edit AJAX
2929
#----------------------------------------------------------------------------
3030
def edit
31-
@previous = Tag.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
31+
@previous = Tag.find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
3232
end
3333

3434
# POST /admin/tags

app/controllers/admin/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def new
3535
# GET /admin/users/1/edit AJAX
3636
#----------------------------------------------------------------------------
3737
def edit
38-
@previous = User.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
38+
@previous = User.find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
3939

4040
respond_with(@user)
4141
end

app/controllers/application_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,14 @@ def find_class(asset)
269269
end
270270
end
271271

272+
# In a number of places, we pass ?previous=(id) or ?previous=crm.find_form...
273+
# This method centralises all of the places we can pass in a previous param
274+
# and extracts an int ID, or nil
275+
def detect_previous_id
276+
return unless params[:previous]
277+
return if params[:previous].start_with?("crm")
278+
279+
params[:previous].to_i
280+
end
272281
ActiveSupport.run_load_hooks(:fat_free_crm_application_controller, self)
273282
end

app/controllers/entities/accounts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def new
4545
# GET /accounts/1/edit AJAX
4646
#----------------------------------------------------------------------------
4747
def edit
48-
@previous = Account.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
48+
@previous = Account.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
4949

5050
respond_with(@account)
5151
end

app/controllers/entities/campaigns_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def new
8484
# GET /campaigns/1/edit AJAX
8585
#----------------------------------------------------------------------------
8686
def edit
87-
@previous = Campaign.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
87+
@previous = Campaign.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
8888

8989
respond_with(@campaign)
9090
end

app/controllers/entities/contacts_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def new
5151
#----------------------------------------------------------------------------
5252
def edit
5353
@account = @contact.account || Account.new(user: current_user)
54-
@previous = Contact.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
54+
@previous = Contact.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
5555

5656
respond_with(@contact)
5757
end

app/controllers/entities/leads_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def new
5252
def edit
5353
get_campaigns
5454

55-
@previous = Lead.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
55+
@previous = Lead.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
5656

5757
respond_with(@lead)
5858
end
@@ -108,7 +108,7 @@ def convert
108108
@accounts = Account.my(current_user).order('name')
109109
@opportunity = Opportunity.new(user: current_user, access: "Lead", stage: "prospecting", campaign: @lead.campaign, source: @lead.source)
110110

111-
@previous = Lead.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
111+
@previous = Lead.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
112112

113113
respond_with(@lead)
114114
end

app/controllers/entities/opportunities_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def edit
5757
@account = @opportunity.account || Account.new(user: current_user)
5858
@accounts = Account.my(current_user).order('name')
5959

60-
@previous = Opportunity.my(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
60+
@previous = Opportunity.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
6161

6262
respond_with(@opportunity)
6363
end

app/controllers/tasks_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def edit
5858
@category = Setting.unroll(:task_category)
5959
@asset = @task.asset if @task.asset_id?
6060

61-
@previous = Task.tracked_by(current_user).find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
61+
@previous = Task.tracked_by(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
6262

6363
respond_with(@task)
6464
end

0 commit comments

Comments
 (0)