Skip to content

Commit 698a562

Browse files
committed
[#68700] replace score with weight
- add migration for db column name - update AR relations in code - update display texts and locale keys - update test, method, class, variable names - fix formatting mistake in item base controller
1 parent 9d6ba39 commit 698a562

File tree

18 files changed

+137
-106
lines changed

18 files changed

+137
-106
lines changed

app/components/admin/custom_fields/hierarchy/item_component.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ def short_text
5252
def secondary_text
5353
if model.short.present?
5454
"(#{model.short})"
55-
elsif model.score.present?
56-
model.score.to_s
55+
elsif model.weight.present?
56+
model.weight.to_s
5757
end
5858
end
5959

app/components/admin/custom_fields/hierarchy/item_form_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def secondary_input_format
6363
when "hierarchy"
6464
:short
6565
when "weighted_item_list"
66-
:score
66+
:weight
6767
else
6868
raise ArgumentError, "Unsupported field format: #{field_format}"
6969
end

app/contracts/custom_fields/hierarchy/insert_weighted_item_contract.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class InsertWeightedItemContract < DryApplicationContract
3434
params do
3535
required(:parent).filled(type?: CustomField::Hierarchy::Item)
3636
required(:label).filled(:string)
37-
required(:score).filled(:decimal)
37+
required(:weight).filled(:decimal)
3838
end
3939

4040
rule(:parent) do

app/contracts/custom_fields/hierarchy/update_weighted_item_contract.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UpdateWeightedItemContract < DryApplicationContract
3434
params do
3535
required(:item).filled(type?: CustomField::Hierarchy::Item)
3636
required(:label).filled(:string)
37-
required(:score).filled(:decimal)
37+
required(:weight).filled(:decimal)
3838
end
3939

4040
rule(:item) do

app/controllers/admin/custom_fields/hierarchy/items_base_controller.rb

Lines changed: 47 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def update
7878
item: @active_item,
7979
label: input[:label],
8080
short: input[:short],
81-
score: input[:score])
81+
weight: input[:weight])
8282
.either(
8383
->(*) { redirect_to action: :show, id: @active_item.parent, status: :see_other },
8484
lambda do |validation_result|
@@ -135,7 +135,7 @@ def change_parent_dialog
135135
)
136136
end
137137

138-
private
138+
private
139139

140140
def item_service
141141
::CustomFields::Hierarchy::HierarchicalItemService.new
@@ -144,7 +144,7 @@ def item_service
144144
def item_input # rubocop:disable Metrics/AbcSize
145145
input = { parent: @active_item, label: params[:label] }
146146
input[:short] = params[:short] if params[:short].present?
147-
input[:score] = params[:score] if params[:score].present?
147+
input[:weight] = params[:weight] if params[:weight].present?
148148
input[:sort_order] = params[:sort_order].to_i if params[:sort_order].present?
149149

150150
input
@@ -160,73 +160,69 @@ def create_contract
160160
::CustomFields::Hierarchy::InsertListItemContract
161161
when "weighted_item_list"
162162
::CustomFields::Hierarchy::InsertWeightedItemContract
163-
el se
164-
rais e ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
165-
end
163+
else
164+
raise ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
166165
end
166+
end
167167

168168
def update_contract
169169
ca se @custom_field.field_format
170170
wh en "hierarchy"
171-
::CustomFields::Hierarchy::UpdateListItemContract
171+
::CustomFields::Hierarchy::UpdateListItemContract
172172
wh en "weighted_item_list"
173-
::CustomFields::Hierarchy::UpdateWeightedItemContract
173+
::CustomFields::Hierarchy::UpdateWeightedItemContract
174174
el se
175-
rais e ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
176-
end
175+
rais e ArgumentError, "unsupported custom field format '#{@custom_field.field_format}'"
177176
end
177+
end
178178

179-
def add_errors_to_fo(rm(validation_resul)t)
180-
@new_ite m = ::CustomField::Hierarchy::Item.ne w(**item_input)
181-
validation_result.error s(full: true).to_h.each do |attribute, errors|
182-
@new_item.errors.ad d(attribute, errors.joi n(", "
183-
))
184-
end
179+
def add_errors_to_form(validation_result)
180+
@new_item = ::CustomField::Hierarchy::Item.new(**item_input)
181+
validation_result.errors(full: true).to_h.each do |attribute, errors|
182+
@new_item.errors.add(attribute, errors.join(", "))
185183
end
184+
end
186185

187-
def add_errors_to_edit_fo(rm(validation_resul)t)
188-
@active_item.assign_attribute s(**validation_result.to_h.slic e(:label, :short, :score))
186+
def add_errors_to_edit_form(validation_result)
187+
@active_item.assign_attributes(**validation_result.to_h.slice(:label, :short, :weight))
189188

190-
validation_result.error s(full: true).to_h.each do |attribute, errors|
191-
@active_item.errors.ad d(attribute, errors.join(", "
192-
))
193-
end
189+
validation_result.errors(full: true).to_h.each do |attribute, errors|
190+
@active_item.errors.add(attribute, errors.join(", "))
194191
end
192+
end
195193

196-
def parse_parent_inp(ut(new_parent_inpu)t)
197-
ca se new_parent_input
198-
in[new_parent]
199-
inpu t = MultiJson.loa d(new_parent, symbolize_keys: true)[:value]
200-
new_paren t = CustomField::Hierarchy::Item.including_children.find_b y(id: input)
194+
def parse_parent_input(new_parent_input)
195+
case new_parent_input
196+
in [new_parent]
197+
input = MultiJson.load(new_parent, symbolize_keys: true)[:value]
198+
new_parent = CustomField::Hierarchy::Item.including_children.find_by(id: input)
201199

202-
if new_parent.present?
203-
Succes s(new_parent)
204-
el se
205-
Failur e(I18n. t(:notice_parent_item_not_found))
206-
end
207-
el se
208-
Failur e("Invalid input: #{new_parent_input}")
200+
if new_parent.present?
201+
Success(new_parent)
202+
else
203+
Failure(I18n.t(:notice_parent_item_not_found))
209204
end
205+
else
206+
Failure("Invalid input: #{new_parent_input}")
210207
end
208+
end
211209

212-
def find_model_object
213-
@objec t = find_custom_field
214-
@custom_fiel d = @object
215-
end
210+
def find_model_object
211+
@object = find_custom_field
212+
@custom_field = @object
213+
end
216214

217-
def find_custom_field
218-
rais e NotImplementedError, "SubclassResponsibility"
219-
end
215+
def find_custom_field
216+
raise NotImplementedError, "SubclassResponsibility"
217+
end
220218

221-
def find_active_item
222-
@active_ite m = if params[:id].present?
223-
CustomField::Hierarchy::Item.including_children.fin d(params[:id])
224-
el se
225-
@object.hierarchy_root
226-
end
227-
end
228-
end
219+
def find_active_item
220+
@active_item = if params[:id].present?
221+
CustomField::Hierarchy::Item.including_children.find(params[:id])
222+
else
223+
@object.hierarchy_root
224+
end
229225
end
230-
en
231-
d
226+
end
227+
end
232228
end

app/forms/custom_fields/hierarchy/item_form.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class ItemForm < ApplicationForm
4949
case @secondary_input_format
5050
when :short
5151
short_input_field(input_group)
52-
when :score
53-
score_input_field(input_group)
52+
when :weight
53+
weight_input_field(input_group)
5454
else
5555
raise ArgumentError, "Unsupported secondary input format: #{secondary_input_format}"
5656
end
@@ -93,18 +93,18 @@ def short_input_field(form_group)
9393
)
9494
end
9595

96-
def score_input_field(form_group)
96+
def weight_input_field(form_group)
9797
form_group.text_field(
98-
name: :score,
99-
label: I18n.t("custom_fields.admin.items.placeholder.score"),
98+
name: :weight,
99+
label: I18n.t("custom_fields.admin.items.placeholder.weight"),
100100
type: :number,
101101
step: :any,
102-
value: @target_item.score,
102+
value: @target_item.weight,
103103
visually_hide_label: true,
104104
full_width: false,
105105
required: true,
106-
placeholder: I18n.t("custom_fields.admin.items.placeholder.score"),
107-
validation_message: validation_message_for(:score)
106+
placeholder: I18n.t("custom_fields.admin.items.placeholder.weight"),
107+
validation_message: validation_message_for(:weight)
108108
)
109109
end
110110

app/models/custom_field.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def cast_value(value)
232232
value.to_f
233233
when "user", "version"
234234
field_format.classify.constantize.find_by(id: value.to_i)
235-
when "hierarchy", "scored_list"
235+
when "hierarchy", "weighted_item_list"
236236
CustomField::Hierarchy::Item.find_by(id: value.to_i)
237237
end
238238
end

app/models/custom_value/scored_list_strategy.rb renamed to app/models/custom_value/weighted_item_list_strategy.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
# See COPYRIGHT and LICENSE files for more details.
2929
#++
3030

31-
class CustomValue::ScoredListStrategy < CustomValue::HierarchyStrategy
31+
class CustomValue::WeightedItemListStrategy < CustomValue::HierarchyStrategy
3232
def typed_value
33-
cached_ar_object&.score
33+
cached_ar_object&.weight
3434
end
3535
end

app/services/custom_fields/hierarchy/hierarchical_item_service.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ def generate_root(custom_field)
5050
# @param parent [CustomField::Hierarchy::Item] the parent of the node
5151
# @param label [String] the node label/name that must be unique at the same tree level
5252
# @param short [String] an alias for the node
53-
# @param score [Decimal] a numeric value for the node
53+
# @param weight [Decimal] a numeric value for the node
5454
# @param sort_order [Integer] the position into which insert the item.
5555
# @return [Success(CustomField::Hierarchy::Item), Failure(Dry::Validation::Result), Failure(ActiveModel::Errors)]
56-
def insert_item(contract_class:, parent:, label:, short: nil, score: nil, sort_order: nil)
56+
def insert_item(contract_class:, parent:, label:, short: nil, weight: nil, sort_order: nil)
5757
contract_class
5858
.new
59-
.call({ parent:, label:, short:, score: })
59+
.call({ parent:, label:, short:, weight: })
6060
.to_monad
6161
.bind { |validation| create_child_item(validation:, sort_order:) }
6262
end
@@ -67,12 +67,12 @@ def insert_item(contract_class:, parent:, label:, short: nil, score: nil, sort_o
6767
# @param item [CustomField::Hierarchy::Item] the item to be updated
6868
# @param label [String] the node label/name that must be unique at the same tree level
6969
# @param short [String] an alias for the node
70-
# @param score [Decimal] a numeric value for the node
70+
# @param weight [Decimal] a numeric value for the node
7171
# @return [Success(CustomField::Hierarchy::Item), Failure(Dry::Validation::Result), Failure(ActiveModel::Errors)]
72-
def update_item(contract_class:, item:, label: nil, short: nil, score: nil)
72+
def update_item(contract_class:, item:, label: nil, short: nil, weight: nil)
7373
contract_class
7474
.new
75-
.call({ item:, label:, short:, score: })
75+
.call({ item:, label:, short:, weight: })
7676
.to_monad
7777
.bind { |attributes| update_item_attributes(item:, attributes:) }
7878
end
@@ -188,8 +188,8 @@ def create_child_item(validation:, sort_order: nil)
188188
end
189189

190190
def update_item_attributes(item:, attributes:)
191-
if item.update(label: attributes[:label], short: attributes[:short], score: attributes[:score])
192-
if item.score_previously_changed?
191+
if item.update(label: attributes[:label], short: attributes[:short], weight: attributes[:weight])
192+
if item.weight_previously_changed?
193193
# Only changes to item are of interest, so no need to pass descendant ids
194194
update_calculated_values_for_hierarchy(item_ids: item.id, custom_field: item.root&.custom_field)
195195
end

config/initializers/custom_field_format.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
label: :label_weighted_item_list,
9696
only: %w(Project WorkPackage),
9797
order: 13,
98-
formatter: "CustomValue::ScoredListStrategy")
98+
formatter: "CustomValue::WeightedItemListStrategy")
9999

100100
fields.register OpenProject::CustomFieldFormat.new("calculated_value",
101101
label: :label_calculated_value,

0 commit comments

Comments
 (0)