Merge pull request #5964 from opf/fix/custom_option_cache

Fix/custom option cache
pull/5976/head
ulferts 7 years ago committed by GitHub
commit 7fce2dcb30
  1. 7
      app/controllers/custom_fields_controller.rb
  2. 10
      app/models/custom_field.rb
  3. 39
      spec/models/custom_field_spec.rb
  4. 7
      spec/models/custom_option_spec.rb

@ -123,8 +123,11 @@ class CustomFieldsController < ApplicationController
def prepare_custom_option_position
return unless params[:custom_field][:custom_options_attributes]
params[:custom_field][:custom_options_attributes].each_with_index do |(_id, attributes), index|
attributes[:position] = index + 1
index = 0
params[:custom_field][:custom_options_attributes].each do |_id, attributes|
attributes[:position] = (index = index + 1)
end
end

@ -32,7 +32,15 @@ class CustomField < ActiveRecord::Base
include CustomField::OrderStatements
has_many :custom_values, dependent: :delete_all
has_many :custom_options, -> { order(position: :asc) }, dependent: :delete_all
# WARNING: the inverse_of option is also required in order
# for the 'touch: true' option on the custom_field association in CustomOption
# to work as desired.
# Without it, the after_commit callbacks of acts_as_list will prevent the touch to happen.
# https://github.com/rails/rails/issues/26726
has_many :custom_options,
-> { order(position: :asc) },
dependent: :delete_all,
inverse_of: 'custom_field'
accepts_nested_attributes_for :custom_options
acts_as_list scope: 'type = \'#{self.class}\''

@ -243,4 +243,43 @@ describe CustomField, type: :model do
end
end
end
describe 'nested attributes for custom options' do
let(:option) { FactoryGirl.build(:custom_option) }
let(:options) { [option] }
let(:field) { FactoryGirl.build :custom_field, field_format: 'list', custom_options: options }
before do
field.save!
end
shared_examples_for 'saving updates field\'s updated_at' do
it 'updates updated_at' do
# mysql does not store milliseconds so we have to slow down the tests by orders of magnitude
timestamp_before = field.updated_at
sleep 1
field.save
expect(field.updated_at).not_to eql(timestamp_before)
end
end
context 'after adding a custom option' do
before do
field.attributes = { 'custom_options_attributes' => { '0' => option.attributes,
'1' => { value: 'blubs' } } }
end
it_behaves_like 'saving updates field\'s updated_at'
end
context 'after changing a custom option' do
before do
attributes = option.attributes.merge(value: 'new_value')
field.attributes = { 'custom_options_attributes' => { '0' => attributes } }
end
it_behaves_like 'saving updates field\'s updated_at'
end
end
end

@ -63,6 +63,13 @@ describe CustomOption, type: :model do
expect(CustomOption.where(id: custom_option.id).count)
.to eql 0
end
it "updates the custom_field's timestamp" do
timestamp_before = custom_field.updated_at
sleep 1
custom_option.destroy
expect(custom_field.reload.updated_at).not_to eql(timestamp_before)
end
end
context 'with only one option for the cf' do

Loading…
Cancel
Save