Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

redactor rails and carrierwave - picture sizes

I'm using redactor_rails gem with carrierwave. There's two places where I need text editor with picture upload, and I want to make different picture size for every editor.

If I use version then I have two sizes for every picture and I dont know how to change picture version in text field.

The main idea is to run it's own resize process for every editor uploader in redactor_rail_picture_uploader

How do I do that?

like image 804
konclave Avatar asked Nov 04 '22 07:11

konclave


1 Answers

May be it's not the perfect way, but it works.

I've made several versions of uploaded files in: redactor_rails_picture_uploader.rb

version :item_text do
  process :resize_to_limit => [478, 478]
end

version :thumb do
  process :resize_to_fill => [100, 100]
end

Created initializer and redefined method 'create' of class RedactorRails::PicturesController there. Now it saves the version that I pass with the form by 'version' param.

RedactorRails::PicturesController.class_eval do
  def create
    @picture = RedactorRails::Picture.new

    file = params[:file]
    version = params[:version]

    @picture.data = RedactorRails::Http.normalize_param(file, request)
    if @picture.respond_to?(:user_id)
      @picture.user = current_user
      @picture.assetable = current_user
    end

    if @picture.save
      if version 
        file_link = @picture.send(:url, version)
      else 
        file_link = @picture.url
      end 

      render :text => { :filelink => file_link }.to_json 

    else
      render :nothing => true
    end
  end
end

Finally added hidden input with the value of the version from uploader I want to save in this form:

%input{:id => 'redactor_version', :value => 'item_text', :type => 'hidden'}

Something like this.

like image 133
konclave Avatar answered Nov 15 '22 04:11

konclave