ruby carrierwave.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby carrierwave.rb相关的知识,希望对你有一定的参考价值。

require 'uuid'

class ItemsController < ApplicationController

  def new
    @uuid = UUID.new
    @temp_name = @uuid.generate(:compact).object_id
    @item = Item.new
    @item.build_item_detail
    @item.build_dropship_item if @dropship
    @item.clone_from_other_item(params[:i]) if params[:i]
    @item.set_from_user(current_user)
    @item.clone_from_template(params[:t]) if params[:t] and !params[:i]
    5.times { @item.images.build }
  end

  def create
    @item = Item.new(params[:item])    
    unless @dropship
      @item.user = current_user
    else
      @item.dropship_item_attributes = params[:item][:dropship_item_attributes]
      @item.synchronized_shop_id = params[:item][:synchronized_shop_id]
    end
    if @item.save
      redirect_to_with(:notice, @dropship ? dropship_items_url : item_url(current_language, current_country, @item), Item.successfully_created)
    else
      render 'new'
    end
  end

end
class Image < ActiveRecord::Base

  has_many :images_items
  has_many :items, :through => :images_items
  belongs_to :item_detail 
  
  mount_uploader :name, ImageUploader, :delayed=> true 
  
  default_scope :order => "position ASC" 
  
  attr_accessor :splitted_title, :temp_name_name, :name_content_type, :name_file_name, :name_cache_name
  
  attr_accessible :name, :remote_name_url, :name_cache, :remove_name, :item_detail_id, :position, :temporary_file_name
  
  validates_size_of :name, :maximum => 4.megabyte, :message => I18n.t('models.images.too_big')
  
  before_save :save_cache_column
  after_save :send_store_column , :if=>:check_for_file
  
  def save_cache_column
    self.cache_name = name.cache_name
  end

  def send_store_column
    self.send_later(:store_name!)
  end

  def check_for_file
    self.name.filename
  end

end
# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or ImageScience support
  # include CarrierWave::RMagick
  # include CarrierWave::ImageScience
  include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader
  # storage :file
  # storage :s3
  # storage :cloud_files

  # 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
  #   "images/#{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

  def default_url
    "/images/no_image_#{version_name}.png"
  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
  process :resize_to_fit => [800, 600]

  process :optimize_image

  process :watermark => "#{RAILS_ROOT}/public/images/neonisi-watermark.png"

  version :thumb do
    process :resize_to_fit => [80, 80]
    process :optimize_image
  end

  version :medium do
    process :resize_to_fit => [160, 160]
    process :optimize_image
  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
    return nil if @original_filename.blank?
    name = model["temp_#{self.mounted_as}_name"]
    name.present? ? name : renaming
  end

  private

  def renaming
      # store the image name based on random words in the item_detail title
      if model.item_detail.present?
        model.splitted_title ||= model.item_detail.title.split(/[^-a-zA-Z0-9]/).delete_if{ |w| w == '' or w == '-' }.map!{ |w| w + '-' }
        words = model.splitted_title.sort_by { rand }
        model["temp_#{self.mounted_as}_name"] = words[0..4].to_s[0..-2] + '.jpg'
      else
        item_detail = ItemDetail.find_by_internal_id(model.temporary_file_name)
        model.splitted_title ||= item_detail.title.split(/[^-a-zA-Z0-9]/).delete_if{ |w| w == '' or w == '-' }.map!{ |w| w + '-' }
        words = model.splitted_title.sort_by { rand }
        model["temp_#{self.mounted_as}_name"] = words[0..4].to_s[0..-2] + '.jpg'
      end
  end

  def watermark(path_to_file)
    manipulate! do |img|
      img = img.composite(MiniMagick::Image.open(path_to_file), "jpg") do |c|
        c.gravity "SouthEast"
      end
    end
  end

  def optimize_image
    manipulate! do |img|
      img.format('JPEG')
      img = img.auto_orient
      img = img.quality 80
      img
    end
  end
end
#config/initializers/carrierwave.rb

CarrierWave.configure do |config|
  if Rails.env.production? or Rails.env.development?
    config.storage :cloud_files
    config.cloud_files_username = "your_username"
    config.cloud_files_api_key = "your_key"
    config.cloud_files_container = "test"
    config.cloud_files_cdn_host = "c0012345.cdnn.cloudfiles.rackspacecloud.com"
    def store_dir
      "images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  else
    config.storage = :file
    def store_dir
      "vendor/plugins/syncher/public/images/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  end
end

以上是关于ruby carrierwave.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby运算符

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee