Sha256: ce4444389c318fe3ad71cdcc0006206822d2d0d6fc0161754c8a229eb3bb4988

Contents?: true

Size: 1.36 KB

Versions: 4

Compression:

Stored size: 1.36 KB

Contents

module Croppable
  extend ActiveSupport::Concern

  included do
    attr_accessor :crop_x, :crop_y, :crop_w, :crop_h

    if self.name.include? 'Controller'
      before_action :set_crop_params, only: [:update]
      after_action :set_crop_params, only: [:create]

      def set_crop_params
        model_name = self.class.name.gsub('Controller', '').singularize.underscore
        if params[model_name][:crop_x].present?
          record = instance_variable_get "@#{model_name}"
          record.crop_x = params[model_name][:crop_x]
          record.crop_y = params[model_name][:crop_y]
          record.crop_w = params[model_name][:crop_w]
          record.crop_h = params[model_name][:crop_h]
          if action_name == 'create'
            uploaded_file = params[model_name].select { |k,v| v.class == ActionDispatch::Http::UploadedFile }.keys.first
            record.send(uploaded_file).crop
            record.save
          end
        end
      end
    elsif self.name.include? 'Uploader'
      include CarrierWave::MiniMagick
      process :crop

      def crop
        if model.respond_to?(:crop_x) and model.crop_x.present?
          manipulate! do |img|
            x = model.crop_x
            y = model.crop_y
            w = model.crop_w
            h = model.crop_h
            img.crop("#{w}x#{h}+#{x}+#{y}")
            img
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
upload-image-0.1.3 app/concerns/croppable.rb
upload-image-0.1.2 app/concerns/croppable.rb
upload-image-0.1.1 app/concerns/croppable.rb
upload-image-0.1.0 app/concerns/croppable.rb