Skip to content

Commit

Permalink
Code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
nanego committed Apr 17, 2024
1 parent a0e5ea4 commit b67ec26
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 72 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
ruby: ['3.1']
ruby: ['3.2']
db: ['postgres']
fail-fast: false

Expand Down
11 changes: 4 additions & 7 deletions app/helpers/issue_templates_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,11 @@ def link_to_issue_template(template)
end

def numeric_edit_tag(tag_id, tag_name, value, min_value, max_value, options = {})
# Generate the range field tag with specified options
edit_tag = range_field_tag(tag_name,
value,
options.merge(id:tag_id,
min: min_value,
max: max_value
))

value,
options.merge(id: tag_id,
min: min_value,
max: max_value))
edit_tag << content_tag(:span, value, class: "range_selected_value")
# Add JavaScript to update the displayed value
edit_tag << javascript_tag(
Expand Down
31 changes: 10 additions & 21 deletions app/models/issue_template_section.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,11 @@ def section_basic_entry(value, textile: true)
end
end

# validate_numeric_value method
#
# Validates whether a given value is numeric.
#
# Parameters:
# - field: The name of the field being validated.
# - value: The value to be validated.
# - errors: An optional object representing validation errors. If provided, errors will be added to it.
def validate_numerci_value(field, value, errors)
unless /^[+-]?\d+$/.match?(value.to_s.strip)
# Add an error if the value is not numeric
errors.add(field, ::I18n.t('activerecord.errors.messages.not_a_number')) unless errors.nil? || field.nil?
# Return false as validation failed
return false
def validate_numeric_value(field, value, errors)
if value.to_s != value.to_i.to_s
errors.add(field, ::I18n.t('activerecord.errors.messages.not_a_number')) if errors.present? && field.present?
return falseq
end
# Return true as validation succeeded
return true
end
end

Expand Down Expand Up @@ -105,19 +93,20 @@ def rendered_value(section_attributes, textile: true, value_only: false)
class IssueTemplateSectionNumeric < IssueTemplateSection
validates_presence_of :title
before_validation :validate_single_value
# Accessor for the integer_field attribute

# Used for custom error message in case of validation failure
attr_accessor :integer_field, :default_value_integer

def validate_single_value
# Call the method to validate if the value is numeric
if !empty_value.empty? && validate_numerci_value(:default_value_integer, empty_value, errors)
if empty_value.present? && validate_numeric_value(:default_value_integer, empty_value, errors)
value = empty_value.to_i
if value < min_value
errors.add(:integer_field, ::I18n.t('activerecord.errors.messages.greater_than_or_equal_to', :count => min_value))
errors.add(:integer_field,
::I18n.t('activerecord.errors.messages.greater_than_or_equal_to', count: min_value))
end
if value > max_value
errors.add(:integer_field, ::I18n.t('activerecord.errors.messages.less_than_or_equal_to', :count => max_value))
errors.add(:integer_field,
::I18n.t('activerecord.errors.messages.less_than_or_equal_to', count: max_value))
end
end
errors
Expand Down
2 changes: 1 addition & 1 deletion app/views/issue_templates/sections/_date_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<%= form.text_field :description, :placeholder => "Description", label: "Description", :size => 80 %>
</p>
<p>
<%= form.check_box :required, label: "Champ obligatoire" %>
<%= form.check_box :required, label: :label_field_required %>
</p>
<p>
<%= form.select :select_type,
Expand Down
2 changes: 1 addition & 1 deletion app/views/issue_templates/sections/_field_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<%= form.text_field :empty_value, label: "Valeur par défaut", :size => 80 %>
</p>
<p>
<%= form.check_box :required, label: "Champ obligatoire" %>
<%= form.check_box :required, label: :label_field_required %>
</p>
</div>
<% end %>
2 changes: 1 addition & 1 deletion app/views/issue_templates/sections/_section_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<%= form.text_field :empty_value, :size => 80, label: "Valeur par défaut" %>
</p>
<p>
<%= form.check_box :required, label: "Champ obligatoire" %>
<%= form.check_box :required, label: :label_field_required %>
</p>
<p>
<%= form.check_box :show_toolbar, label: "Afficher la barre d'outils" %>
Expand Down
16 changes: 8 additions & 8 deletions app/views/issues/sections/_numeric_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<% end %>
<% end %>
<% value = reload_current_value(@sections_attributes, section, group_index) || section.empty_value %>
<% if section.select_type =="1" %>
<% if section.select_type == "1" # Range %>
<% section_group = section.issue_template_section_group %>
<% field_id = "issue_issue_template_section_groups_attributes_#{section_group.id}_#{group_index}_sections_attributes_#{section.id}_empty_value" %>
<% field_name = "issue[issue_template][section_groups_attributes][#{section_group.id}][#{group_index}][sections_attributes][#{section.id}][empty_value]" %>
Expand All @@ -20,13 +20,13 @@
<% min_value = section.min_value || 0 %>
<% max_value = section.max_value || 100 %>
<%= form.number_field :empty_value,
size: 20,
accesskey: accesskey(:edit),
no_label: true,
required: section.required,
value: value,
min: min_value,
max: max_value
size: 20,
accesskey: accesskey(:edit),
no_label: true,
required: section.required,
value: value,
min: min_value,
max: max_value
%>
<% end %>
</p>
18 changes: 9 additions & 9 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ en:
label_display_in_range: Range display
field_integer_field: "La valeur du champ entier"
new_section_options:
- ['New Instruction', 2]
- ['New Long Text Field', 1]
- ['New Short Text Field', 3]
- ['New Checkbox', 4]
- ['New Date Field', 5]
- ['New Numeric Field', 6]
- ['New Value List', 7]
- [ 'New Instruction', 2 ]
- [ 'New Long Text Field', 1 ]
- [ 'New Short Text Field', 3 ]
- [ 'New Checkbox', 4 ]
- [ 'New Date Field', 5 ]
- [ 'New Numeric Field', 6 ]
- [ 'New Value List', 7 ]
label_field_required: Required field
label_icon: Icon
label_icon: Icon
icon_name_placeholder: "Octicon name"
field_buttons_with_icons_field: Buttons with icons
label_value_at_index: "%{title} : value at index %{key} is empty"
label_copy: Copy of
label_copy: Copy of
18 changes: 9 additions & 9 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,16 @@ fr:
field_integer_field: La valeur du champ entier
field_default_value_integer: La valeur par défaut du champ entier
new_section_options:
- ['Nouvelle consigne', 2]
- ['Nouveau champ texte (long)', 1]
- ['Nouveau champ texte (court)', 3]
- ['Nouvelle checkbox', 4]
- ['Nouveau champ date', 5]
- ['Nouvelle champ entier', 6]
- ['Nouvelle liste de valeurs', 7]
label_field_required: Champ obligatoire
- [ 'Nouvelle consigne', 2 ]
- [ 'Nouveau champ texte (long)', 1 ]
- [ 'Nouveau champ texte (court)', 3 ]
- [ 'Nouvelle checkbox', 4 ]
- [ 'Nouveau champ date', 5 ]
- [ 'Nouvelle champ entier', 6 ]
- [ 'Nouvelle liste de valeurs', 7 ]
label_field_required: Champ obligatoire
label_icon: Icone
icon_name_placeholder: "Nom de l'octicon"
field_buttons_with_icons_field: Boutons avec icones
label_value_at_index: "%{title} : la valeur à l'indice %{key} est vide"
label_copy: Copie de
label_copy: Copie de
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ def change
add_column :issue_template_sections, :min_value, :integer
add_column :issue_template_sections, :max_value, :integer
end
end
end
16 changes: 8 additions & 8 deletions spec/controllers/issue_templates_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@
expect(IssueTemplate.last.section_groups.first.sections.first.icon_name).to eq("history;alert-fill")
end

it "Should successfully creates issue template section with range" do
it "creates issue template section with range" do
expect do
post :create, params: {
:issue_template => {
Expand All @@ -307,7 +307,7 @@
"min_value" => 2,
"max_value" => 4,
"empty_value" => 3,
"select_type" => "0" #display_in_range
"select_type" => "0" # display_in_range
},
]
}
Expand All @@ -323,7 +323,7 @@
expect(new_template.select_type).to eq("0")
end

it "Should not accept a default value longer than the maximum specified" do
it "does not accept a default value longer than the maximum specified" do
expect do
post :create, params: {
:issue_template => {
Expand All @@ -343,17 +343,17 @@
"min_value" => 1,
"max_value" => 2,
"empty_value" => 5,
"select_type" => "0" #display_in_range
"select_type" => "0" # display_in_range
},
]
}
]
}
}
end.to change { IssueTemplate.count }.by(0)
end.to_not change { IssueTemplate.count }
end

it "Should not accept a default value shorter than the minimum specified length" do
it "does not accept a default value shorter than the minimum specified length" do
expect do
post :create, params: {
:issue_template => {
Expand All @@ -373,14 +373,14 @@
"min_value" => 2,
"max_value" => 4,
"empty_value" => 5,
"select_type" => "0" #display_in_range
"select_type" => "0" # display_in_range
},
]
}
]
}
}
end.to change { IssueTemplate.count }.by(0)
end.to_not change { IssueTemplate.count }
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/fixtures/issue_template_sections.yml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,4 @@ field_numeric_as_range:
empty_value: "5"
issue_template_section_group_id: 5
position: 10
select_type: "0"
select_type: "0"
8 changes: 4 additions & 4 deletions spec/system/issues_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ def log_user(login, password)
end

describe "Numeric Field" do
let (:group_id) { group_id = IssueTemplate.find(6).section_groups[0].id }
let (:section) { section_id = IssueTemplate.find(6).section_groups[0].sections[8] }
let (:group_id) { IssueTemplate.find(6).section_groups[0].id }
let (:section) { IssueTemplate.find(6).section_groups[0].sections[8] }

it "Displays the expected numeric field on the 'New Issue' page with the default value" do
it "displays the expected numeric field on the 'New Issue' page with the default value" do
visit new_issue_path(project_id: project.identifier, template_id: 6)
expect(page).to have_css("input[name='issue[issue_template][section_groups_attributes][#{group_id}][0][sections_attributes][#{section.id}][empty_value]'][value='#{section.empty_value}']")
end

it "Displays the expected numeric field on the 'New Issue' page with range display" do
it "displays the expected numeric field on the 'New Issue' page with range display" do
template = IssueTemplate.find(6)
template.section_groups[0].sections[8].select_type = "1"
template.save
Expand Down

0 comments on commit b67ec26

Please sign in to comment.