Skip to content

Commit

Permalink
Minor code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
nanego committed Sep 14, 2023
1 parent 85b40b4 commit e73f136
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 78 deletions.
14 changes: 6 additions & 8 deletions app/models/issue_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class IssueTemplate < ActiveRecord::Base
validates_length_of :subject, :maximum => 255
# validates_inclusion_of :done_ratio, :in => 0..100

validates :estimated_hours, :numericality => {:greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid}
validates :estimated_hours, :numericality => { :greater_than_or_equal_to => 0, :allow_nil => true, :message => :invalid }
validates :start_date, :date => true
validates :due_date, :date => true
# validate :validate_issue, :validate_required_fields
Expand Down Expand Up @@ -118,13 +118,11 @@ def assignable_users

# Overrides Redmine::Acts::Customizable::InstanceMethods#available_custom_fields
def available_custom_fields
available_custom_fields = []
#(project && tracker) ? (project.all_issue_custom_fields & tracker.custom_fields.all) : []
template_projects.each do |project|
available_custom_fields = []
template_projects.each do |project|
available_custom_fields |= project.all_issue_custom_fields.to_a
end
tracker ? available_custom_fields |= tracker.custom_fields.all.to_a : available_custom_fields

available_custom_fields |= tracker.custom_fields.all.to_a if tracker.present?
available_custom_fields
end

Expand Down Expand Up @@ -178,7 +176,7 @@ def authorized_viewer_ids
end

def assigned_to_function_id
nil #TODO Make templates compatible with this functionality
nil # TODO Make templates compatible with this functionality
end

def description_is_empty?(attributes)
Expand All @@ -197,7 +195,7 @@ def section_group_is_empty?(attributes)
return (!persisted && has_no_title && has_no_sections)
end

def safe_attribute_names(user=nil)
def safe_attribute_names(user = nil)
names = super
names -= disabled_core_fields
names
Expand Down
106 changes: 53 additions & 53 deletions app/views/issue_templates/_modal_select_projects.html.erb
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
<% template_projects = @issue_template.template_projects
allowed_projects = @issue_template.allowed_target_projects.includes(:custom_values)
custom_fields = CustomField.select("id, name").where("type = ? AND name IN (?)", "ProjectCustomField", Setting["plugin_redmine_templates"]['custom_fields'])
concerned_projects_ids = (allowed_projects | template_projects).map(&:id)

options_for_selects = {}
custom_fields.each do |field|
options_for_selects.merge!(field.name.parameterize => [])
end

custom_values = {}
CustomValue.where(customized_type: 'Project', customized_id: concerned_projects_ids, custom_field_id: custom_fields.map(&:id)).each do |v|
custom_values[v.customized_id] ||= {}
custom_values[v.customized_id][v.custom_field_id] = v.value

cf = v.custom_field
value = v.value

if value.present?
if cf.field_format == "enumeration"
enum_item = CustomFieldEnumeration.where(id: value).first
options_for_selects[cf.name.parameterize] << [enum_item.name, value]
else
if value.is_a?(Array)
value.each do |val|
options_for_selects[cf.name.parameterize] << val
end
else
options_for_selects[cf.name.parameterize] << value
end
end
end
options_for_selects[cf.name.parameterize].uniq!

end

nested_projects_list = render_project_nested_lists(allowed_projects | template_projects) do |project|
custom_fields_data = {}
if allowed_projects.include?(project)
custom_fields.each do |f|
custom_fields_data.merge!(f.name.parameterize => custom_values[project.id][f.id])
end
end
content_tag('label',
check_box_tag(
'template_project_ids[]',
project.id,
@issue_template != nil && template_projects.include?(project),
disabled: allowed_projects.include?(project) ? false : true,
:class => ("inactive" unless allowed_projects.include?(project)),
data: custom_fields_data
) + ' ' + h(project.name), :class => ("inactive" unless allowed_projects.include?(project))
)
end
allowed_projects = @issue_template.allowed_target_projects.includes(:custom_values)
custom_fields = CustomField.select("id, name").where("type = ? AND name IN (?)", "ProjectCustomField", Setting["plugin_redmine_templates"]['custom_fields'])
concerned_projects_ids = (allowed_projects | template_projects).map(&:id)

options_for_selects = {}
custom_fields.each do |field|
options_for_selects.merge!(field.name.parameterize => [])
end

custom_values = {}
CustomValue.where(customized_type: 'Project', customized_id: concerned_projects_ids, custom_field_id: custom_fields.map(&:id)).each do |v|
custom_values[v.customized_id] ||= {}
custom_values[v.customized_id][v.custom_field_id] = v.value

cf = v.custom_field
value = v.value

if value.present?
if cf.field_format == "enumeration"
enum_item = CustomFieldEnumeration.where(id: value).first
options_for_selects[cf.name.parameterize] << [enum_item.name, value]
else
if value.is_a?(Array)
value.each do |val|
options_for_selects[cf.name.parameterize] << val
end
else
options_for_selects[cf.name.parameterize] << value
end
end
end
options_for_selects[cf.name.parameterize].uniq!

end

nested_projects_list = render_project_nested_lists(allowed_projects | template_projects) do |project|
custom_fields_data = {}
if allowed_projects.include?(project)
custom_fields.each do |f|
custom_fields_data.merge!(f.name.parameterize => custom_values[project.id][f.id])
end
end
content_tag('label',
check_box_tag(
'template_project_ids[]',
project.id,
@issue_template != nil && template_projects.include?(project),
disabled: allowed_projects.include?(project) ? false : true,
:class => ("inactive" unless allowed_projects.include?(project)),
data: custom_fields_data
) + ' ' + h(project.name), :class => ("inactive" unless allowed_projects.include?(project))
)
end
%>

<h3 class="title"><%= l(:field_project).pluralize %> TEMPLATE</h3>
Expand Down
12 changes: 6 additions & 6 deletions app/views/issue_templates/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<div id="all_attributes">
<%= render :partial => 'issue_templates/form' %>
</div>
<script type="text/javascript">
$(function(){
// Call this method because of there is a selected tracker by default
updateIssueTemplateFrom("<%= issue_template_form_path(:id => @issue_template.id, :format => 'js')%>");
});
</script>
<script type="text/javascript">
$(function () {
// Call this method because of there is a selected tracker by default
updateIssueTemplateFrom("<%= issue_template_form_path(:id => @issue_template.id, :format => 'js')%>");
});
</script>
22 changes: 11 additions & 11 deletions spec/system/issue_templates_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ def log_user(login, password)
include IssuesHelper

fixtures :projects, :users, :email_addresses, :roles, :members, :member_roles,
:trackers, :projects_trackers, :enabled_modules, :issue_statuses, :issues,
:enumerations, :custom_fields, :custom_values, :custom_fields_trackers,
:workflows, :issue_templates, :issue_template_projects, :issue_template_section_groups, :issue_template_sections
:trackers, :projects_trackers, :enabled_modules, :issue_statuses, :issues,
:enumerations, :custom_fields, :custom_values, :custom_fields_trackers,
:workflows, :issue_templates, :issue_template_projects, :issue_template_section_groups, :issue_template_sections

let(:template_with_sections) { IssueTemplate.find(3) }
let(:template_4) { IssueTemplate.find(4) }
Expand All @@ -35,15 +35,15 @@ def log_user(login, password)
end

describe "Display all custom fields used in issue_template view" do
it "When selecting a project" do
it "displays all project's custom-fields when selecting a project" do
custom_field = CustomField.create(name: "CustomField_project2", field_format: "string", visible: "1", type: "IssueCustomField", projects: [project])

visit new_issue_template_path

expect(page).to_not have_selector("label", text: custom_field.name)

find("#link_update_project_list").click

within '#ajax-modal' do
page.find("label", :text => project.name).click
page.find("#button_apply_projects").click
Expand All @@ -56,21 +56,21 @@ def log_user(login, password)
expect(page).to have_selector("label", text: custom_field.name)
end

it "When selecting a tracker" do
it "displays all tracker's custom-field when selecting a tracker" do
tracker_1 = Tracker.find(1)
tracker_2 = Tracker.find(2)
custom_field_1 = CustomField.create(name: "CustomField_tracker1", field_format: "string", visible: "1", type: "IssueCustomField", trackers: [tracker_1])
custom_field_2 = CustomField.create(name: "CustomField_tracker2", field_format: "string", visible: "1", type: "IssueCustomField", trackers: [tracker_2])

visit new_issue_template_path

tracker_1.custom_fields.all.each do |field|
expect(page).to have_selector("label", text: field.name)
end
expect(page).to_not have_selector("label", text: custom_field_2.name)

select "Feature request", :from => "issue_template_tracker_id"

tracker_2.custom_fields.all.each do |field|
expect(page).to have_selector("label", text: field.name)
end
Expand Down

0 comments on commit e73f136

Please sign in to comment.