Papertrail and Carrierwave

前端 未结 3 1473
醉话见心
醉话见心 2021-02-15 01:41

I have a model that use both: Carrierwave for store photos, and PaperTrail for versioning.

I also configured Carrierwave for store diferent files when upda

3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-15 02:10

    You can override item_before_change on your versioned model so you don't call the uploader accesor directly and use write_attribute instead. Alternatively, since you might want to do that for several models, you can monkey-patch the method directly, like this:

    module PaperTrail
      module Model
        module InstanceMethods
          private
            def item_before_change
              previous = self.dup
              # `dup` clears timestamps so we add them back.
              all_timestamp_attributes.each do |column|
                previous[column] = send(column) if respond_to?(column) && !send(column).nil?
              end
              previous.tap do |prev|
                prev.id = id
                changed_attributes.each do |attr, before|
                  if defined?(CarrierWave::Uploader::Base) && before.is_a?(CarrierWave::Uploader::Base)
                    prev.send(:write_attribute, attr, before.url && File.basename(before.url))
                  else
                    prev[attr] = before
                  end
                end
              end
            end
        end
      end
    end
    

    Not sure if it's the best solution, but it seems to work.

提交回复
热议问题