Skip to content

Commit

Permalink
Centralise detection of previous route (#1222)
Browse files Browse the repository at this point in the history
* Remove unused previous

* Centralise previous object detection. Remove falling back to a number if not found

* Apply suggestions from code review
  • Loading branch information
CloCkWeRX authored Jan 9, 2024
1 parent 38b6335 commit ab1da1c
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 11 deletions.
2 changes: 0 additions & 2 deletions app/controllers/admin/field_groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ def new
def edit
@field_group = FieldGroup.find(params[:id])

@previous = FieldGroup.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/

respond_with(@field_group)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def new
# GET /admin/tags/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@previous = Tag.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
@previous = Tag.find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id
end

# POST /admin/tags
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def new
# GET /admin/users/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@previous = User.find_by_id(Regexp.last_match[1]) || Regexp.last_match[1].to_i if params[:previous].to_s =~ /(\d+)\z/
@previous = User.find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@user)
end
Expand Down
9 changes: 9 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,5 +269,14 @@ def find_class(asset)
end
end

# In a number of places, we pass ?previous=(id) or ?previous=crm.find_form...
# This method centralises all of the places we can pass in a previous param
# and extracts an int ID, or nil
def detect_previous_id
return unless params[:previous]
return if params[:previous].start_with?("crm")

params[:previous].to_i
end
ActiveSupport.run_load_hooks(:fat_free_crm_application_controller, self)
end
2 changes: 1 addition & 1 deletion app/controllers/entities/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def new
# GET /accounts/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@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/
@previous = Account.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@account)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/entities/campaigns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def new
# GET /campaigns/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@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/
@previous = Campaign.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@campaign)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/entities/contacts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def new
#----------------------------------------------------------------------------
def edit
@account = @contact.account || Account.new(user: current_user)
@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/
@previous = Contact.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@contact)
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/entities/leads_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def new
def edit
get_campaigns

@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/
@previous = Lead.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

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

@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/
@previous = Lead.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@lead)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/entities/opportunities_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def edit
@account = @opportunity.account || Account.new(user: current_user)
@accounts = Account.my(current_user).order('name')

@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/
@previous = Opportunity.my(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@opportunity)
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def edit
@category = Setting.unroll(:task_category)
@asset = @task.asset if @task.asset_id?

@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/
@previous = Task.tracked_by(current_user).find_by_id(detect_previous_id) || detect_previous_id if detect_previous_id

respond_with(@task)
end
Expand Down

0 comments on commit ab1da1c

Please sign in to comment.