Module: Cms::Upgrades::V3_5_0::FileStorageUpdates
- Defined in:
- lib/cms/upgrades/v3_5_0.rb
Overview
Add additional methods to ActiveRecord::Migration when this file is required.
Defined Under Namespace
Classes: AttachmentWrapper
Instance Method Summary collapse
-
#cleanup_attachment_columns(model_class) ⇒ Object
The ‘attachment_id and attachment_version are no longer stored in the model, but in the attachments table’.
-
#cleanup_attachment_columns_for_core_cms ⇒ Object
Remove the attachment_version and attachment_id columns for all core CMS blocks.
-
#cleanup_attachment_file_storage ⇒ Object
Deletes the old file storage folders from the uploads directory.
-
#cleanup_attachments_file_location ⇒ Object
Removes the cms_attachments.file_location column.
-
#migrate_attachment_files_to_new_location ⇒ Object
Move the attachment files from the old file path path to new one.
-
#migrate_attachment_for(klass) ⇒ Object
For a given class with an Attachment association (pre-3.5.0), migrate its attachment data to match the new table structure.
- #migrated_attachment_for_versioned_table(model_class, attachable_type) ⇒ Object
-
#path_for_attachment(attachment) ⇒ String
Old paths are: uploads/:year/:month/:day/:fingerprint i.e.
Instance Method Details
#cleanup_attachment_columns(model_class) ⇒ Object
The ‘attachment_id and attachment_version are no longer stored in the model, but in the attachments table’
150 151 152 153 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 150 def (model_class) remove_content_column model_class.table_name, :attachment_id if column_exists?(model_class.table_name, :attachment_id) remove_content_column model_class.table_name, :attachment_version if column_exists?(model_class.table_name, :attachment_id) end |
#cleanup_attachment_columns_for_core_cms ⇒ Object
Remove the attachment_version and attachment_id columns for all core CMS blocks.
144 145 146 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 144 def (Cms::FileBlock) end |
#cleanup_attachment_file_storage ⇒ Object
Deletes the old file storage folders from the uploads directory.
164 165 166 167 168 169 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 164 def ["2009", "2010", "2011", "2012"].each do |year| folder_to_remove = File.join(, year) FileUtils.rm_rf(folder_to_remove, :verbose => true) end end |
#cleanup_attachments_file_location ⇒ Object
Removes the cms_attachments.file_location column
data_fingerprint is used to store the file name instead now.
158 159 160 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 158 def remove_content_column :cms_attachments, :file_location if column_exists?(:cms_attachments, :file_location) end |
#migrate_attachment_files_to_new_location ⇒ Object
Move the attachment files from the old file path path to new one. Updates the Attachment record to match.
138 139 140 141 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 138 def (Cms::Attachment) (Cms::Attachment::Version) end |
#migrate_attachment_for(klass) ⇒ Object
For a given class with an Attachment association (pre-3.5.0), migrate its attachment data to match the new table structure.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 100 def (klass) klass.unscoped.find_each do |block| Cms::Attachment.unscoped.update_all({:attachable_id => block.id, :attachable_version => block.version, :attachable_type => klass.name, :attachment_name => "file", :cardinality => 'single'}, {:id => block.}) end attachable_type = klass.name # Special handling for File/Image blocks if klass == Cms::FileBlock attachable_type = "Cms::AbstractFileBlock" elsif klass == Cms::ImageBlock # Only need to do this once for both Image and File blocks since they share a table. return end (klass, attachable_type) end |
#migrated_attachment_for_versioned_table(model_class, attachable_type) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 123 def (model_class, attachable_type) version_model = model_class.version_class found = version_model.find_by_sql("SELECT original_record_id, attachment_id, attachment_version, version from #{version_model.table_name}") found.each do |version_record| Cms::Attachment::Version.unscoped.update_all({:attachable_id => version_record.original_record_id, :attachable_version => version_record.version, :attachable_type => attachable_type, :attachment_name => "file", :cardinality => 'single'}, {:original_record_id => version_record., :version => version_record.}) end end |
#path_for_attachment(attachment) ⇒ String
Old paths are:
uploads/:year/:month/:day/:fingerprint
i.e. uploads/2012/04/27/fb598.…..
New paths use paperclip’s :id_partition which creates paths like:
uploads/000/000/001/:fingerprint
where it splits the id into multiple directories
93 94 95 96 |
# File 'lib/cms/upgrades/v3_5_0.rb', line 93 def () new_id_dir = "#{Paperclip::Interpolations.id_partition(AttachmentWrapper.new(), nil)}/original" File.join(, new_id_dir, fingerprint_for_file_location()) end |