Sha256: be2b2395181d1783e95ed1009797ffe0cb6d9e4213cf2f8c59a4c16f206d9122

Contents?: true

Size: 1.85 KB

Versions: 19

Compression:

Stored size: 1.85 KB

Contents

module ModelReconstruction
  def reset_class class_name
    ActiveRecord::Base.send(:include, Paperclip::Glue)
    Object.send(:remove_const, class_name) rescue nil
    klass = Object.const_set(class_name, Class.new(ActiveRecord::Base))

    klass.class_eval do
      include Paperclip::Glue
    end

    klass.reset_column_information
    klass.connection_pool.clear_table_cache!(klass.table_name) if klass.connection_pool.respond_to?(:clear_table_cache!)
    klass.connection.schema_cache.clear_table_cache!(klass.table_name) if klass.connection.respond_to?(:schema_cache)
    klass
  end

  def reset_table table_name, &block
    block ||= lambda { |table| true }
    ActiveRecord::Base.connection.create_table :dummies, {force: true}, &block
  end

  def modify_table table_name, &block
    ActiveRecord::Base.connection.change_table :dummies, &block
  end

  def rebuild_model options = {}
    ActiveRecord::Base.connection.create_table :dummies, force: true do |table|
      table.column :title, :string
      table.column :other, :string
      table.column :avatar_file_name, :string
      table.column :avatar_content_type, :string
      table.column :avatar_file_size, :integer
      table.column :avatar_updated_at, :datetime
      table.column :avatar_fingerprint, :string
    end
    rebuild_class options
  end

  def rebuild_class options = {}
    reset_class("Dummy").tap do |klass|
      klass.has_attached_file :avatar, options
      klass.do_not_validate_attachment_file_type :avatar
      Paperclip.reset_duplicate_clash_check!
    end
  end

  def rebuild_meta_class_of obj, options = {}
    meta_class_of(obj).tap do |metaklass|
      metaklass.has_attached_file :avatar, options
      metaklass.do_not_validate_attachment_file_type :avatar
      Paperclip.reset_duplicate_clash_check!
    end
  end

  def meta_class_of(obj)
    class << obj
      self
    end
  end
end

Version data entries

19 entries across 17 versions & 3 rubygems

Version Path
paperclip-4.3.7 spec/support/model_reconstruction.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/paperclip-4.3.6/spec/support/model_reconstruction.rb
paperclip-4.3.6 spec/support/model_reconstruction.rb
paperclip-4.3.5 spec/support/model_reconstruction.rb
paperclip-4.3.4 spec/support/model_reconstruction.rb
paperclip-4.3.3 spec/support/model_reconstruction.rb
paperclip-4.3.2 spec/support/model_reconstruction.rb
paperclip-4.3.1 spec/support/model_reconstruction.rb
solidus_backend-1.0.0.pre3 vendor/bundle/gems/paperclip-4.2.4/spec/support/model_reconstruction.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/paperclip-4.2.4/spec/support/model_reconstruction.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.4/spec/support/model_reconstruction.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.2/spec/support/model_reconstruction.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/paperclip-4.2.3/spec/support/model_reconstruction.rb
paperclip-4.3.0 spec/support/model_reconstruction.rb
paperclip-4.2.4 spec/support/model_reconstruction.rb
paperclip-4.2.3 spec/support/model_reconstruction.rb
paperclip-4.2.2 spec/support/model_reconstruction.rb
paperclip-4.2.1 spec/support/model_reconstruction.rb
paperclip-4.2.0 spec/support/model_reconstruction.rb