Skip to content

Commit

Permalink
Fix #{column}_cache= fails to be stored when set as a nested attribute
Browse files Browse the repository at this point in the history
Fixes #2206
  • Loading branch information
mshibuya committed Jan 15, 2023
1 parent 45520ad commit 4caf2a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/carrierwave/orm/activerecord.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ def #{column}=(new_file)
super
end
def #{column}_cache=(cache_name)
column = _mounter(:#{column}).serialization_column
__send__(:"\#{column}_will_change!") if cache_name.present?
super
end
def remove_#{column}=(value)
column = _mounter(:#{column}).serialization_column
result = super
Expand Down
36 changes: 36 additions & 0 deletions spec/orm/activerecord_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1756,4 +1756,40 @@ def initialize_dup(*)
end
end
end

describe 'when set as a nested attribute' do
before(:all) do
ActiveRecord::Base.connection.create_table('tickets', force: true) do |t|
t.column :event_id, :integer
t.column :image, :string
end
end
after(:all) { drop_table("tickets") }

before do
Event.class_eval do
has_many :tickets
accepts_nested_attributes_for :tickets
end
reset_class("Ticket")
Ticket.class_eval do
mount_uploader(:image, @uploader)
belongs_to :event
end
@ticket = Ticket.new
end
after do
Ticket.delete_all
FileUtils.rm_rf(public_path("uploads"))
end

describe '#cache_name=' do
it "should allow the image to be stored" do
cache_id = Ticket.new(image: stub_file('new.jpeg')).image_cache
@event = Event.create(tickets: [Ticket.new])
@event.update! tickets_attributes: {id: @event.tickets.first.id, image_cache: cache_id}
expect(@event.tickets.first.image.to_s).to eq '/uploads/new.jpeg'
end
end
end
end

0 comments on commit 4caf2a1

Please sign in to comment.