Skip to content

Commit cea65e2

Browse files
committed
Use delegation for to_s in models
Also use a proper localization for more models.
1 parent 545ee31 commit cea65e2

13 files changed

+21
-41
lines changed

app/models/codeharbor_link.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ class CodeharborLink < ApplicationRecord
77

88
belongs_to :user, class_name: 'InternalUser'
99

10-
delegate :to_s, to: :id
10+
def to_s
11+
"#{model_name.human} #{id}"
12+
end
1113
end

app/models/consumer.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@ class Consumer < ApplicationRecord
1616

1717
after_create :generate_internal_study_group
1818

19+
delegate :to_s, to: :name
20+
1921
def generate_internal_study_group
2022
StudyGroup.create!(consumer: self, name: "Default Study Group for #{name}", external_id: nil)
2123
end
2224
private :generate_internal_study_group
2325

24-
def to_s
25-
name
26-
end
27-
2826
def self.ransackable_attributes(_auth_object = nil)
2927
%w[id]
3028
end

app/models/contributor.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Contributor < ApplicationRecord
99

1010
has_many :submissions, as: :contributor
1111

12+
delegate :to_s, to: :displayname
13+
1214
def learner?
1315
raise NotImplementedError
1416
end
@@ -33,10 +35,6 @@ def programming_group?
3335
is_a?(ProgrammingGroup)
3436
end
3537

36-
def to_s
37-
displayname
38-
end
39-
4038
def to_page_context
4139
{
4240
id:,

app/models/error_template.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@ class ErrorTemplate < ApplicationRecord
44
belongs_to :execution_environment
55
has_and_belongs_to_many :error_template_attributes
66

7-
def to_s
8-
name
9-
end
7+
delegate :to_s, to: :name
108
end

app/models/error_template_attribute.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
class ErrorTemplateAttribute < ApplicationRecord
44
has_and_belongs_to_many :error_template
55

6-
def to_s
7-
key
8-
end
6+
delegate :to_s, to: :key
97
end

app/models/execution_environment.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,7 @@ class ExecutionEnvironment < ApplicationRecord
4040
after_rollback :delete_runner_environment, on: :create
4141
after_rollback :sync_runner_environment, on: %i[update destroy]
4242

43-
def to_s
44-
name
45-
end
43+
delegate :to_s, to: :name
4644

4745
def to_json(*_args)
4846
{

app/models/exercise.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ class Exercise < ApplicationRecord
4646
validates :token, presence: true, uniqueness: true
4747
validates :uuid, uniqueness: {if: -> { uuid.present? }}
4848

49+
delegate :to_s, to: :title
50+
4951
@working_time_statistics = nil
5052
attr_reader :working_time_statistics
5153

@@ -558,10 +560,6 @@ def set_default_values
558560
end
559561
private :set_default_values
560562

561-
def to_s
562-
title
563-
end
564-
565563
def valid_main_file?
566564
if files.count(&:main_file?) > 1
567565
errors.add(:files, :at_most_one_main_file)

app/models/file_template.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
class FileTemplate < ApplicationRecord
44
belongs_to :file_type
55

6-
def to_s
7-
name
8-
end
6+
delegate :to_s, to: :name
97
end

app/models/file_type.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class FileType < ApplicationRecord
2828
validates :renderable, inclusion: [true, false]
2929
validates :file_extension, length: {minimum: 0, allow_nil: false}
3030

31+
delegate :to_s, to: :name
32+
3133
%i[audio compressed csv excel image pdf powerpoint video word].each do |type|
3234
define_method(:"#{type}?") do
3335
self.class.const_get(:"#{type.upcase}_FILE_EXTENSIONS").include?(file_extension)
@@ -42,8 +44,4 @@ def set_default_values
4244
def programming_language
4345
editor_mode&.gsub('ace/mode/', '')
4446
end
45-
46-
def to_s
47-
name
48-
end
4947
end

app/models/intervention.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ class Intervention < ApplicationRecord
66
has_many :internal_users, through: :user_exercise_interventions, source: :contributor, source_type: 'InternalUser'
77
has_many :programming_groups, through: :user_exercise_interventions, source: :contributor, source_type: 'ProgrammingGroup'
88

9-
def to_s
10-
name
11-
end
9+
delegate :to_s, to: :name
1210

1311
def contributors
1412
@contributors ||= internal_users.distinct + external_users.distinct + programming_groups.distinct

0 commit comments

Comments
 (0)