Module: DataMapper::FastenTheSeatBelt::InstanceMethods

Defined in:
lib/dm-fasten-the-seat-belt.rb

Instance Method Summary collapse

Instance Method Details

#absolute_path(thumb = nil) ⇒ Object



73
74
75
# File 'lib/dm-fasten-the-seat-belt.rb', line 73

def absolute_path(thumb=nil)
  File.join(complete_directory_name, filename_for_thumbnail(thumb))
end

#complete_directory_nameObject



128
129
130
# File 'lib/dm-fasten-the-seat-belt.rb', line 128

def complete_directory_name
  File.join(self.class.fasten_the_seat_belt_options[:file_system_path], directory_name)
end

#complete_file_pathObject



132
133
134
# File 'lib/dm-fasten-the-seat-belt.rb', line 132

def complete_file_path
  File.join(complete_directory_name, (self.filename || ""))
end

#create_directoryObject



136
137
138
# File 'lib/dm-fasten-the-seat-belt.rb', line 136

def create_directory
  FileUtils.mkdir_p(complete_directory_name) unless FileTest.exists?(complete_directory_name)
end

#delete_directoryObject



140
141
142
# File 'lib/dm-fasten-the-seat-belt.rb', line 140

def delete_directory 
  FileUtils.rm_rf(complete_directory_name) if FileTest.exists?(complete_directory_name)
end

#directory_nameObject



113
114
115
116
117
# File 'lib/dm-fasten-the-seat-belt.rb', line 113

def directory_name
  # you can thank Jamis Buck for this: http://www.37signals.com/svn/archives2/id_partitioning.php
  dir = ("%08d" % self.id).scan(/..../)
  File.join(dir[0], dir[1])
end

#filename_for_thumbnail(thumb = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/dm-fasten-the-seat-belt.rb', line 77

def filename_for_thumbnail(thumb=nil)
  if thumb != nil
    basename = self.filename.gsub(/\.(.*)$/, '')
    extension = self.filename.gsub(/^(.*)\./, '')
    return basename + '_' + thumb.to_s + '.' + extension
  else
    return self.filename
  end
end

#generate_thumbnails!Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/dm-fasten-the-seat-belt.rb', line 144

def generate_thumbnails!
  self.class.fasten_the_seat_belt_options[:thumbnails].each_pair do |key, value|
    resize_to = value[:size]
    quality = value[:quality].to_i
  
    image = MiniMagick::Image.from_file(complete_file_path)
    if value[:crop]
      # tw, th are target width and target height
    
      tw = resize_to.gsub(/([0-9]*)x([0-9]*)/, '\1').to_i
      th = resize_to.gsub(/([0-9]*)x([0-9]*)/, '\2').to_i
    
      # ow and oh are origin width and origin height
      ow = image[:width]
      oh = image[:height]
      
      # iw and ih and the dimensions of the cropped picture before resizing
      # there are 2 cases, iw = ow or ih = oh
      # using iw / ih = tw / th, we can determine the other values
      # we use the minimal values to determine the good case
      iw = [ow, ((oh.to_f*tw.to_f) / th.to_f)].min
      ih = [oh, ((ow.to_f*th.to_f) / tw.to_f)].min
    
      # we calculate how much image we must crop
      shave_width = ((ow.to_f - iw.to_f) / 2.0).to_i
      shave_height = ((oh.to_f - ih.to_f) / 2.0).to_i
    
  
      # specify the width of the region to be removed from both sides of the image and the height of the regions to be removed from top and bottom.
      image.shave "#{shave_width}x#{shave_height}"

      # resize of the pic
      image.resize resize_to.to_s + "!"
    else
      # no cropping
      image.resize resize_to
    end
    basename = self.filename.gsub(/\.(.*)$/, '')
    extension = self.filename.gsub(/^(.*)\./, '')

    thumb_filename = File.join(complete_directory_name, (basename + '_' + key.to_s + '.' + extension))

    # Delete thumbnail if exists
    File.delete(thumb_filename) if File.exists?(thumb_filename)
    image.write thumb_filename

    next if self.images_are_compressed == false
    
    if quality and quality!=0 and quality < 100
      compress_jpeg(thumb_filename, quality)
    end
  end
end

#path(thumb = nil) ⇒ Object

Get file path



69
70
71
# File 'lib/dm-fasten-the-seat-belt.rb', line 69

def path(thumb=nil)
  File.join(web_directory_name, filename_for_thumbnail(thumb))
end

#save_attributesObject



87
88
89
90
91
92
93
94
95
96
# File 'lib/dm-fasten-the-seat-belt.rb', line 87

def save_attributes
  return unless @file
  # The following line is solving an IE6 problem. This removes the C:\Documents and Settings\.. shit
  @file[:filename] = File.basename(@file[:filename].gsub(/\\/, '/')) if @file[:filename]

  # Setup attributes
  [:content_type, :size, :filename].each do |attribute|
    self.send("#{attribute}=", @file[attribute])
  end
end

#save_fileObject



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dm-fasten-the-seat-belt.rb', line 98

def save_file
  return unless self.filename and @file

  create_directory

  FileUtils.mv @file[:tempfile].path, complete_file_path if File.exists?(@file[:tempfile].path)

  generate_thumbnails!

  @file = nil

  # By default, images are supposed to be compressed
  self.images_are_compressed ||= true 
end

#web_directory_nameObject



119
120
121
122
123
124
125
126
# File 'lib/dm-fasten-the-seat-belt.rb', line 119

def web_directory_name
  raise "Can't return web directory name if not running in Merb" unless defined?(Merb)
  unless complete_directory_name.include?(Merb.root)
    raise "Can't return web directory name, the images aren't stored under the Merb application public directory" 
  end
  
  complete_directory_name.gsub(/^#{Merb.root}\/public/, '')
end