Clean code and new image uploader

This commit is contained in:
chris2tof 2011-09-14 15:11:50 +08:00
parent f2e3cb00db
commit 0ca354cfd0
4 changed files with 30 additions and 51 deletions

View File

@ -1,3 +1,5 @@
class Image < DesignFile class Image < DesignFile
embedded_in :design embedded_in :design
mount_uploader :file, ImageUploader
end end

View File

@ -1,47 +0,0 @@
# encoding: utf-8
class DesignFileUploader < CarrierWave::Uploader::Base
# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
# include CarrierWave::ImageScience
# Choose what kind of storage to use for this uploader:
# storage :file
# storage :s3
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"assets/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
# Create different versions of your uploaded files:
# version :thumb do
# process :scale => [50, 50]
# end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
# def extension_white_list
# %w(jpg jpeg gif png)
# end
# Override the filename of the uploaded files:
# def filename
# "something.jpg" if original_filename
# end
end

View File

@ -1,10 +1,20 @@
# encoding: utf-8 # encoding: utf-8
class DesignFileUploader < CarrierWave::Uploader::Base class ImageUploader < CarrierWave::Uploader::Base
require 'mime/types'
process :set_content_type
# Include RMagick or ImageScience support: # Include RMagick or ImageScience support:
# include CarrierWave::RMagick # include CarrierWave::RMagick
# include CarrierWave::ImageScience # include CarrierWave::ImageScience
include CarrierWave::MiniMagick
def set_content_type(*args)
content_type = file.content_type == 'binary/octet-stream' || 'application/octet-stream' || file.content_type.blank? ? MIME::Types.type_for(original_filename).first.to_s : file.content_type
self.file.instance_variable_set(:@content_type, content_type)
end
# Choose what kind of storage to use for this uploader: # Choose what kind of storage to use for this uploader:
# storage :file # storage :file
@ -13,7 +23,7 @@ class DesignFileUploader < CarrierWave::Uploader::Base
# Override the directory where uploaded files will be stored. # Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted: # This is a sensible default for uploaders that are meant to be mounted:
def store_dir def store_dir
"assets/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" "image/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end end
# Provide a default URL as a default if there hasn't been a file uploaded: # Provide a default URL as a default if there hasn't been a file uploaded:
@ -33,6 +43,10 @@ class DesignFileUploader < CarrierWave::Uploader::Base
# process :scale => [50, 50] # process :scale => [50, 50]
# end # end
version :thumb do
process :resize_to_fit => [200, 200]
end
# Add a white list of extensions which are allowed to be uploaded. # Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this: # For images you might use something like this:
# def extension_white_list # def extension_white_list
@ -44,4 +58,14 @@ class DesignFileUploader < CarrierWave::Uploader::Base
# "something.jpg" if original_filename # "something.jpg" if original_filename
# end # end
# def manipulate!
# raise current_path.inspect
# image = ::MiniMagick::Image.open(current_path)
# image = yield(image)
# image.write(current_path)
# ::MiniMagick::Image.open(current_path)
# rescue ::MiniMagick::Error, ::MiniMagick::Invalid => e
# raise CarrierWave::ProcessingError.new("Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: #{e}")
# end
end end

View File

@ -65,8 +65,8 @@ module Parser
end end
c.define_tag 'stylesheets' do |tag| c.define_tag 'stylesheets' do |tag|
res = '' res = ''
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />" if page.design.reset_css
res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' /> " res << "<link href='#{page.design.default_css.file.url}' rel='stylesheet' type='text/css' /> " if page.design.default_css
theme = page.design.themes.detect{ |d| d.id == page.theme_id } theme = page.design.themes.detect{ |d| d.id == page.theme_id }
res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme res << "<link href='#{theme.file.url}' rel='stylesheet' type='text/css' />" if theme
res res