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

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'

Parameters:

  • model_class (Class)

    The model to have its columns cleaned up.



150
151
152
153
# File 'lib/cms/upgrades/v3_5_0.rb', line 150

def cleanup_attachment_columns(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_cmsObject

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 cleanup_attachment_columns_for_core_cms
  cleanup_attachment_columns(Cms::FileBlock)
end

#cleanup_attachment_file_storageObject

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 cleanup_attachment_file_storage
  ["2009", "2010", "2011", "2012"].each do |year|
    folder_to_remove = File.join(attachments_dir, year)
    FileUtils.rm_rf(folder_to_remove, :verbose => true)
  end
end

#cleanup_attachments_file_locationObject

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 cleanup_attachments_file_location
  remove_content_column :cms_attachments, :file_location if column_exists?(:cms_attachments, :file_location)
end

#migrate_attachment_files_to_new_locationObject

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 migrate_attachment_files_to_new_location
  migrate_attachments_file_location(Cms::Attachment)
  migrate_attachments_file_location(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 migrate_attachment_for(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.attachment_id})
  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

  migrated_attachment_for_versioned_table(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 migrated_attachment_for_versioned_table(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.attachment_id, :version => version_record.attachment_version})
  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

Parameters:

Returns:

  • (String)

    path to location where an attachment should be (based on id)



93
94
95
96
# File 'lib/cms/upgrades/v3_5_0.rb', line 93

def path_for_attachment(attachment)
  new_id_dir = "#{Paperclip::Interpolations.id_partition(AttachmentWrapper.new(attachment), nil)}/original"
  File.join(attachments_dir, new_id_dir, fingerprint_for_file_location(attachment))
end