From 84dac1d250bd5f6727844535323d75925c6f3efc Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Thu, 17 May 2012 09:30:21 +0800 Subject: [PATCH 01/10] Create FileAssetUploader --- app/models/asset.rb | 2 +- app/uploaders/asset_uploader.rb | 6 ++-- app/uploaders/file_asset.rb | 55 +++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 app/uploaders/file_asset.rb diff --git a/app/models/asset.rb b/app/models/asset.rb index 9819934f..a4065f17 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -3,7 +3,7 @@ class Asset include Mongoid::Document include Mongoid::Timestamps - mount_uploader :data, AssetUploader + mount_uploader :data, FileAssetUploader field :filename field :description diff --git a/app/uploaders/asset_uploader.rb b/app/uploaders/asset_uploader.rb index f59e20a0..7b5749c9 100644 --- a/app/uploaders/asset_uploader.rb +++ b/app/uploaders/asset_uploader.rb @@ -48,8 +48,8 @@ class AssetUploader < CarrierWave::Uploader::Base # end # Override the filename of the uploaded files: - def filename - model.filename - end + # def filename + # model.filename + # end end diff --git a/app/uploaders/file_asset.rb b/app/uploaders/file_asset.rb new file mode 100644 index 00000000..0de9e67a --- /dev/null +++ b/app/uploaders/file_asset.rb @@ -0,0 +1,55 @@ +# encoding: utf-8 + +class FileAssetUploader < CarrierWave::Uploader::Base + require 'mime/types' + + process :set_content_type + + 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 + + # 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 + model.filename + end + +end From 4467bc5e48e6df07aecf422a7a748b0046fdc30d Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Thu, 17 May 2012 09:59:49 +0800 Subject: [PATCH 02/10] Change Date to DateTime Fix filename error --- app/uploaders/{file_asset.rb => file_asset_uploader.rb} | 0 vendor/built_in_modules/announcement/app/models/bulletin.rb | 4 ++-- vendor/built_in_modules/news/app/models/news_bulletin.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename app/uploaders/{file_asset.rb => file_asset_uploader.rb} (100%) diff --git a/app/uploaders/file_asset.rb b/app/uploaders/file_asset_uploader.rb similarity index 100% rename from app/uploaders/file_asset.rb rename to app/uploaders/file_asset_uploader.rb diff --git a/vendor/built_in_modules/announcement/app/models/bulletin.rb b/vendor/built_in_modules/announcement/app/models/bulletin.rb index 8098f9a7..0bf7de64 100644 --- a/vendor/built_in_modules/announcement/app/models/bulletin.rb +++ b/vendor/built_in_modules/announcement/app/models/bulletin.rb @@ -13,8 +13,8 @@ class Bulletin has_one :text, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy has_and_belongs_to_many :tags, :class_name => "AnnouncementTag" - field :postdate , :type => Date - field :deadline , :type => Date + field :postdate , :type => DateTime + field :deadline , :type => DateTime # field :url field :create_user_id field :update_user_id, :class_name => "User" diff --git a/vendor/built_in_modules/news/app/models/news_bulletin.rb b/vendor/built_in_modules/news/app/models/news_bulletin.rb index e6e7d9fc..83bb784b 100644 --- a/vendor/built_in_modules/news/app/models/news_bulletin.rb +++ b/vendor/built_in_modules/news/app/models/news_bulletin.rb @@ -13,8 +13,8 @@ class NewsBulletin has_one :text, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy has_and_belongs_to_many :tags, :class_name => "NewsTag" - field :postdate , :type => Date - field :deadline , :type => Date + field :postdate , :type => DateTime + field :deadline , :type => DateTime # field :url field :create_user_id field :update_user_id, :class_name => "User" From 29c54e64804b08b95c13b801cc275291a05143a5 Mon Sep 17 00:00:00 2001 From: Christophe Vilayphiou Date: Thu, 17 May 2012 16:40:09 +0800 Subject: [PATCH 03/10] Add mandatory fields to assets --- app/assets/stylesheets/message.css | 2 +- app/models/asset.rb | 2 +- app/views/admin/assets/file_upload.html.erb | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/assets/stylesheets/message.css b/app/assets/stylesheets/message.css index 9ee1d45d..7206bd72 100644 --- a/app/assets/stylesheets/message.css +++ b/app/assets/stylesheets/message.css @@ -1,7 +1,7 @@ /*message*/ .error{ - color:red; + color: #B94A48; } .notice, .message{ diff --git a/app/models/asset.rb b/app/models/asset.rb index a4065f17..34ba57e5 100644 --- a/app/models/asset.rb +++ b/app/models/asset.rb @@ -10,7 +10,7 @@ class Asset has_one :i18n_variable, :as => :language_value, :autosave => true, :dependent => :destroy - validates_presence_of :filename, :data + validates_presence_of :filename, :data, :asset_category_id belongs_to :asset_category belongs_to :assetable, polymorphic: true diff --git a/app/views/admin/assets/file_upload.html.erb b/app/views/admin/assets/file_upload.html.erb index eeed414b..9d0c7fb0 100644 --- a/app/views/admin/assets/file_upload.html.erb +++ b/app/views/admin/assets/file_upload.html.erb @@ -9,7 +9,7 @@