Skip to content

Commit 0c91218

Browse files
committed
refactored project loading
1 parent 3809866 commit 0c91218

6 files changed

Lines changed: 55 additions & 35 deletions

File tree

.rubocop_todo.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2020-11-28 07:55:00 UTC using RuboCop version 1.3.1.
3+
# on 2020-11-29 15:24:20 UTC using RuboCop version 1.3.1.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -11,7 +11,7 @@ Lint/MissingSuper:
1111
Exclude:
1212
- 'lib/bitcoin_rpc.rb'
1313

14-
# Offense count: 17
14+
# Offense count: 18
1515
# Configuration parameters: IgnoredMethods.
1616
Metrics/AbcSize:
1717
Max: 51
@@ -25,14 +25,14 @@ Metrics/BlockLength:
2525
# Offense count: 2
2626
# Configuration parameters: CountComments, CountAsOne.
2727
Metrics/ClassLength:
28-
Max: 184
28+
Max: 187
2929

30-
# Offense count: 6
30+
# Offense count: 5
3131
# Configuration parameters: IgnoredMethods.
3232
Metrics/CyclomaticComplexity:
3333
Max: 29
3434

35-
# Offense count: 21
35+
# Offense count: 20
3636
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods.
3737
Metrics/MethodLength:
3838
Max: 57
@@ -42,10 +42,10 @@ Metrics/MethodLength:
4242
Metrics/ModuleLength:
4343
Max: 163
4444

45-
# Offense count: 6
45+
# Offense count: 5
4646
# Configuration parameters: IgnoredMethods.
4747
Metrics/PerceivedComplexity:
48-
Max: 20
48+
Max: 18
4949

5050
# Offense count: 1
5151
# Configuration parameters: NamePrefix, ForbiddenPrefixes, AllowedMethods, MethodDefinitionMacros.

app/controllers/application_controller.rb

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -31,33 +31,13 @@ def locale_from_http_accept_language
3131
http_accept_language.compatible_language_from(::Rails.application.config.available_locales)&.to_sym
3232
end
3333

34-
def load_project(params)
35-
return unless (is_via_project = is_a? ProjectsController) ||
36-
(is_via_tips = is_a? TipsController) ||
37-
(is_via_deposits = is_a? DepositsController)
38-
39-
is_standard_path = params[:id].present? && is_via_project
40-
is_association_path = params[:project_id].present?
41-
is_pretty_path = params[:service].present? && params[:repo].present?
42-
return unless is_standard_path || is_association_path || is_pretty_path
43-
44-
if (is_standard_path || is_association_path) &&
45-
(project_id = is_via_project ? params[:id] : params[:project_id]) &&
46-
(@project = (Project.where id: project_id).first)
47-
if is_via_tips
48-
redirect_to project_tips_pretty_path @project.host, @project.full_name
49-
elsif is_via_deposits
50-
redirect_to project_deposits_pretty_path @project.host, @project.full_name
51-
end
52-
elsif is_pretty_path
53-
@project = Project.where(host: params[:service])
54-
.where('lower(`full_name`) = ?', params[:repo].downcase).first
55-
end
56-
57-
return if @project.present?
34+
def pretty_project_path?
35+
params[:service].present? && params[:repo].present?
36+
end
5837

38+
def project_not_found
5939
flash[:error] = I18n.t('errors.project_not_found')
60-
redirect_to projects_path
40+
redirect_to(projects_path)
6141
end
6242

6343
def load_user(params)

app/controllers/deposits_controller.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class DepositsController < ApplicationController
4-
before_action { load_project params }
4+
before_action { load_project }
55

66
def index
77
@deposits = if @project.present?
@@ -17,4 +17,19 @@ def index
1717
format.csv { render csv: @deposits, except: %i[updated_at confirmations fee_size], add_methods: %i[project_name fee confirmed?] }
1818
end
1919
end
20+
21+
private
22+
23+
def load_project
24+
return unless pretty_project_path? || params[:project_id].present?
25+
26+
if pretty_project_path?
27+
@project = Project.first_by_service_and_repo(params[:service], params[:repo])
28+
elsif params[:project_id].present?
29+
@project = Project.where(id: params[:project_id]).first
30+
redirect_to project_deposits_pretty_path(@project.host, @project.full_name) if @project
31+
end
32+
33+
project_not_found unless @project
34+
end
2035
end

app/controllers/projects_controller.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ def decide_tip_amounts
7272
private
7373

7474
def load_project
75-
super params
75+
@project = if pretty_project_path?
76+
Project.first_by_service_and_repo(params[:service], params[:repo])
77+
else
78+
Project.where(id: params[:id]).first
79+
end
80+
81+
project_not_found unless @project
7682
end
7783

7884
def project_params

app/controllers/tips_controller.rb

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
class TipsController < ApplicationController
4-
before_action { load_project params }
4+
before_action { load_project }
55
before_action { load_user params }
66

77
def index
@@ -25,4 +25,19 @@ def index
2525
format.csv { render csv: @tips, except: %i[updated_at commit commit_message refunded_at decided_at], add_methods: %i[user_name project_name decided? claimed? paid? refunded? txid] }
2626
end
2727
end
28+
29+
private
30+
31+
def load_project
32+
return unless pretty_project_path? || params[:project_id].present?
33+
34+
if pretty_project_path?
35+
@project = Project.first_by_service_and_repo(params[:service], params[:repo])
36+
elsif params[:project_id].present?
37+
@project = Project.where(id: params[:project_id]).first
38+
redirect_to project_tips_pretty_path(@project.host, @project.full_name) if @project
39+
end
40+
41+
project_not_found unless @project
42+
end
2843
end

app/models/project.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,5 +238,9 @@ class << self
238238
def export_labels
239239
Hash[pluck(:bitcoin_address, :full_name)].to_json
240240
end
241+
242+
def first_by_service_and_repo(service, repo)
243+
where(host: service).where('lower(`full_name`) = ?', repo.downcase).first
244+
end
241245
end
242246
end

0 commit comments

Comments
 (0)