From f0197b4323c1ebb37cc320e0fc3a7fded4176545 Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Tue, 10 Mar 2015 16:12:34 +0100 Subject: [PATCH] style --- lib/api/decorators/single.rb | 2 -- lib/api/v3/types/type_representer.rb | 1 - .../lib/acts_as_customizable.rb | 24 ++++++++++++++----- .../form/work_package_form_resource_spec.rb | 4 ++-- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/lib/api/decorators/single.rb b/lib/api/decorators/single.rb index 5037afc554..b9a162ca6d 100644 --- a/lib/api/decorators/single.rb +++ b/lib/api/decorators/single.rb @@ -103,8 +103,6 @@ module API private - - def datetime_formatter API::V3::Utilities::DateTimeFormatter end diff --git a/lib/api/v3/types/type_representer.rb b/lib/api/v3/types/type_representer.rb index 4cb16b6ba7..dcfaa7909b 100644 --- a/lib/api/v3/types/type_representer.rb +++ b/lib/api/v3/types/type_representer.rb @@ -31,7 +31,6 @@ module API module V3 module Types class TypeRepresenter < ::API::Decorators::Single - self_link property :id diff --git a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb index 880d8bf15c..87896bac5a 100644 --- a/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb +++ b/lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb @@ -82,12 +82,21 @@ module Redmine @custom_field_values_changed = true values = values.stringify_keys custom_field_values.each do |custom_value| - custom_value.value = values[custom_value.custom_field_id.to_s] if values.has_key?(custom_value.custom_field_id.to_s) + if values.has_key?(custom_value.custom_field_id.to_s) + custom_value.value = values[custom_value.custom_field_id.to_s] + end end if values.is_a?(Hash) end def custom_field_values - @custom_field_values ||= available_custom_fields.map { |x| custom_values.detect { |v| v.custom_field == x } || custom_values.build(customized: self, custom_field: x, value: nil) } + @custom_field_values ||= available_custom_fields.map do |custom_field| + existing_cv = custom_values.detect { |v| v.custom_field == custom_field } + if existing_cv + existing_cv + else + custom_values.build(customized: self, custom_field: custom_field, value: nil) + end + end end def visible_custom_field_values @@ -113,14 +122,17 @@ module Redmine def reset_custom_values! @custom_field_values = nil @custom_field_values_changed = true - values = custom_values.inject({}) { |h, v| h[v.custom_field_id] = v.value; h } + values = custom_values.inject({}) do |hash, custom_value| + hash[custom_value.custom_field_id] = custom_value.value + hash + end custom_values.each { |cv| cv.destroy unless custom_field_values.include?(cv) } end def validate_custom_values - custom_field_values.reject(&:marked_for_destruction?).select(&:invalid?).each do |custom_value| - custom_value.errors.each do |_, message| - errors.add(custom_value.custom_field.accessor_name.to_sym, message) + custom_field_values.reject(&:marked_for_destruction?).select(&:invalid?).each do |cv| + cv.errors.each do |_, message| + errors.add(cv.custom_field.accessor_name.to_sym, message) end end end diff --git a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb index 675d9804a8..c0bde43999 100644 --- a/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb +++ b/spec/requests/api/v3/work_packages/form/work_package_form_resource_spec.rb @@ -647,9 +647,9 @@ describe 'API v3 Work package form resource', type: :request do it 'should list the types' do expect(subject.body).to be_json_eql(type_link.to_json) - .at_path("#{links_path}/allowedValues/1/href") + .at_path("#{links_path}/allowedValues/1/href") expect(subject.body).to be_json_eql(other_type_link.to_json) - .at_path("#{links_path}/allowedValues/0/href") + .at_path("#{links_path}/allowedValues/0/href") end end