public
Description: Tog Platform Social Component
Homepage: http://www.toghq.com
Clone URL: git://github.com/tog/tog_social.git
tog_social / db / migrate / 003_add_attachments_image_to_group.rb
100644 57 lines (46 sloc) 1.983 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
class AddAttachmentsImageToGroup < ActiveRecord::Migration
  def self.up
    add_column :groups, :image_file_name, :string
    add_column :groups, :image_content_type, :string
    add_column :groups, :image_file_size, :integer
    add_column :groups, :image_updated_at, :datetime
 
    rename_column :groups, :image, :old_file_name
 
    add_crop("plugins.tog_social.group.image.versions.big")
    add_crop("plugins.tog_social.group.image.versions.medium")
    add_crop("plugins.tog_social.group.image.versions.small")
    add_crop("plugins.tog_social.group.image.versions.tiny")
 
    Group.all.each do |p|
      unless p.old_file_name.blank?
        p.image = File.new("public/system/group_photos/group/image/#{p.id}/#{p.old_file_name}") if File.exists?("public/system/group_photos/group/image/#{p.id}/#{p.old_file_name}")
        p.save!
      end
    end
    FileUtils.rm_rf "public/system/group_photos/"
 
    remove_column :groups, :old_file_name
  end
 
  def self.down
    add_column :groups, :image, :string
 
    remove_crop("plugins.tog_social.group.image.versions.big")
    remove_crop("plugins.tog_social.group.image.versions.medium")
    remove_crop("plugins.tog_social.group.image.versions.small")
    remove_crop("plugins.tog_social.group.image.versions.tiny")
 
    Group.all.each do |p|
      unless p.image_file_name.blank?
        p.image = File.new("public/system/groups/images/#{p.id}/#{p.image_file_name}") if File.exists?("public/system/groups/images/#{p.id}/#{p.image_file_name}")
        p.save!
      end
    end
    FileUtils.rm_rf "public/system/groups"
 
    remove_column :groups, :image_file_name
    remove_column :groups, :image_content_type
    remove_column :groups, :image_file_size
    remove_column :groups, :image_updated_at
  end
 
  private
  def self.add_crop(key)
    Tog::Config[key]="#{Tog::Config[key]}#" unless Tog::Config[key] =~ /#|%|@|!|<|>|\^/
  end
 
  def self.remove_crop(key)
    Tog::Config[key]=Tog::Config[key].gsub("#",'')
  end
end