Module: Attacheable::ClassMethods

Defined in:
lib/attacheable.rb

Instance Method Summary collapse

Instance Method Details

#data_by_path_info(path_info) ⇒ Object

It is designed to read params, or splitted PATH_INFO in mongrel handler It assumes, that path_info is of the following format [“0000”, “0001”, “file_medium.jpg”]



101
102
103
104
105
106
107
108
109
# File 'lib/attacheable.rb', line 101

def data_by_path_info(path_info)
  id1, id2, path = path_info
  return [nil, nil] unless id1 && id2 && path
  object = find(id1.to_i*1000 + id2.to_i)
  if path = object.full_filename_by_path(path)
    return [object, File.read(path)] if File.exists?(path)
  end
  [object, nil]
end

#regenerate_thumbnails!(thumbnail = nil) ⇒ Object

You can delete all thumbnails or with selected type



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/attacheable.rb', line 83

def regenerate_thumbnails!(thumbnail = nil)
  connection.select_values("select id from #{table_name}").each do |object_id|
    object = find_by_id(object_id)
    if object && object.filename
      if thumbnail
        FileUtils.rm_f(object.full_filename_without_creation(thumbnail))
      else
        to_remove = Dir["#{File.dirname(object.full_filename_without_creation)}/*"] - [object.full_filename_without_creation]
        FileUtils.rm_f(to_remove)
      end
      #object.full_filename_with_creation(thumbnail)
    end
  end
end