Merge pull request #7713 from opf/fix/custom_value_auto_saving

use default rails autosave
pull/7717/head
ulferts 5 years ago committed by GitHub
commit a10be5bea5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 19
      lib/plugins/acts_as_customizable/lib/acts_as_customizable.rb
  2. 2
      modules/my_page/spec/features/my/assigned_to_me_spec.rb
  3. 19
      spec/services/projects/update_service_intergration_spec.rb

@ -36,6 +36,7 @@ module Redmine
module ClassMethods module ClassMethods
def acts_as_customizable(options = {}) def acts_as_customizable(options = {})
return if included_modules.include?(Redmine::Acts::Customizable::InstanceMethods) return if included_modules.include?(Redmine::Acts::Customizable::InstanceMethods)
cattr_accessor :customizable_options cattr_accessor :customizable_options
self.customizable_options = options self.customizable_options = options
@ -46,11 +47,13 @@ module Redmine
.order("#{CustomField.table_name}.position") .order("#{CustomField.table_name}.position")
}, as: :customized, }, as: :customized,
dependent: :delete_all, dependent: :delete_all,
validate: false validate: false,
autosave: true
validate :validate_custom_values validate :validate_custom_values
send :include, Redmine::Acts::Customizable::InstanceMethods send :include, Redmine::Acts::Customizable::InstanceMethods
# Save custom values when saving the customized object
after_save :save_custom_field_values before_save :ensure_custom_values_complete
after_save :reset_custom_values_change_tracker
end end
end end
@ -82,7 +85,7 @@ module Redmine
# Also supports multiple values for a custom field where # Also supports multiple values for a custom field where
# instead of a single value you'd pass an array. # instead of a single value you'd pass an array.
def custom_field_values=(values) def custom_field_values=(values)
return nil unless values.is_a?(Hash) && values.any? return unless values.is_a?(Hash) && values.any?
@custom_field_values_changed = true @custom_field_values_changed = true
@ -114,7 +117,7 @@ module Redmine
def delete_obsolete_custom_values!(existing_custom_values, new_values) def delete_obsolete_custom_values!(existing_custom_values, new_values)
existing_custom_values.zip(new_values).each_with_index do |(custom_value, new_value), i| existing_custom_values.zip(new_values).each_with_index do |(custom_value, new_value), i|
if new_value.nil? if new_value.nil?
if i == 0 if i.zero?
# leave the first value but set it to nil as that's the behaviour expected # leave the first value but set it to nil as that's the behaviour expected
# by the original acts_as_customizable # by the original acts_as_customizable
custom_value.value = nil custom_value.value = nil
@ -195,9 +198,11 @@ module Redmine
end end
end end
def save_custom_field_values def ensure_custom_values_complete
self.custom_values = custom_field_values self.custom_values = custom_field_values
custom_field_values.each(&:save) end
def reset_custom_values_change_tracker
@custom_field_values_changed = false @custom_field_values_changed = false
@custom_field_values = nil @custom_field_values = nil
end end

@ -180,7 +180,7 @@ describe 'Assigned to me embedded query on my page', type: :feature, js: true do
assigned_area.resize_to(1, 2) assigned_area.resize_to(1, 2)
sleep(0.1) my_page.expect_and_dismiss_notification message: I18n.t('js.notice_successful_update')
assigned_area.expect_to_span(1, 1, 2, 3) assigned_area.expect_to_span(1, 1, 2, 3)
# has been moved down by resizing # has been moved down by resizing

@ -68,5 +68,24 @@ describe Projects::UpdateService, 'integration', type: :model do
.not_to eql later_updated_at .not_to eql later_updated_at
end end
end end
context 'if a new custom field gets a value assigned' do
let(:custom_field2) { FactoryBot.create(:text_project_custom_field) }
let(:attributes) do
{ "custom_field_#{custom_field2.id}" => 'some text' }
end
it 'touches the project after saving' do
former_updated_at = Project.pluck(:updated_on).first
service_result
later_updated_at = Project.pluck(:updated_on).first
expect(former_updated_at)
.not_to eql later_updated_at
end
end
end end
end end

Loading…
Cancel
Save