Clean code and new image uploader
This commit is contained in:
parent
f2e3cb00db
commit
0ca354cfd0
|
@ -1,3 +1,5 @@
|
|||
class Image < DesignFile
|
||||
embedded_in :design
|
||||
|
||||
mount_uploader :file, ImageUploader
|
||||
end
|
||||
|
|
|
@ -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
|
|
@ -1,10 +1,20 @@
|
|||
# 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 CarrierWave::RMagick
|
||||
# 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:
|
||||
# storage :file
|
||||
|
@ -13,7 +23,7 @@ class DesignFileUploader < CarrierWave::Uploader::Base
|
|||
# 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}"
|
||||
"image/#{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:
|
||||
|
@ -32,6 +42,10 @@ class DesignFileUploader < CarrierWave::Uploader::Base
|
|||
# version :thumb do
|
||||
# process :scale => [50, 50]
|
||||
# end
|
||||
|
||||
version :thumb do
|
||||
process :resize_to_fit => [200, 200]
|
||||
end
|
||||
|
||||
# Add a white list of extensions which are allowed to be uploaded.
|
||||
# For images you might use something like this:
|
||||
|
@ -43,5 +57,15 @@ class DesignFileUploader < CarrierWave::Uploader::Base
|
|||
# def filename
|
||||
# "something.jpg" if original_filename
|
||||
# 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
|
|
@ -65,8 +65,8 @@ module Parser
|
|||
end
|
||||
c.define_tag 'stylesheets' do |tag|
|
||||
res = ''
|
||||
res << "<link href='#{page.design.reset_css.file.url}' rel='stylesheet' type='text/css' />"
|
||||
res << "<link href='#{page.design.default_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' /> " if page.design.default_css
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue