You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. # Monkey-patch various Paperclip methods for Ruby 3.0 compatibility
  3. module Paperclip
  4. module Schema
  5. module StatementsExtensions
  6. def add_attachment(table_name, *attachment_names)
  7. raise ArgumentError, 'Please specify attachment name in your add_attachment call in your migration.' if attachment_names.empty?
  8. options = attachment_names.extract_options!
  9. attachment_names.each do |attachment_name|
  10. COLUMNS.each_pair do |column_name, column_type|
  11. column_options = options.merge(options[column_name.to_sym] || {})
  12. add_column(table_name, "#{attachment_name}_#{column_name}", column_type, **column_options)
  13. end
  14. end
  15. end
  16. end
  17. module TableDefinitionExtensions
  18. def attachment(*attachment_names)
  19. options = attachment_names.extract_options!
  20. attachment_names.each do |attachment_name|
  21. COLUMNS.each_pair do |column_name, column_type|
  22. column_options = options.merge(options[column_name.to_sym] || {})
  23. column("#{attachment_name}_#{column_name}", column_type, **column_options)
  24. end
  25. end
  26. end
  27. end
  28. end
  29. end
  30. Paperclip::Schema::Statements.prepend(Paperclip::Schema::StatementsExtensions)
  31. Paperclip::Schema::TableDefinition.prepend(Paperclip::Schema::TableDefinitionExtensions)