Skip to content

Commit

Permalink
README improved
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Sokolov committed Sep 9, 2011
1 parent 774bfb3 commit 03984e1
Showing 1 changed file with 129 additions and 9 deletions.
138 changes: 129 additions & 9 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@ Add the following line to your Gemfile:
include CarrierWave::RMagick
include CarrierWave::Meta

def store_dir
"tmp/store"
end

def cache_dir
"tmp/cache"
end

storage :file

process :store_meta
Expand Down Expand Up @@ -87,8 +79,136 @@ content type.

Uploader tries to retrieve metadata from a model's column first.

= Integrating CarrierWave & JCrop using carrierwave-meta
= model_delegate_attribute

class DelegateTestModel
attr_accessor :processed
attr_accessor :a_processed
attr_accessor :a_b_processed
end

class DelegateTestUploader < CarrierWave::Uploader::Base
model_delegate_attribute :uploaded

set_processed

version :a do
set_processed
version :b do
set_processed
end
end

def set_processed
self.processed = true
end
end

model = DelegateTestModel.new
uploader = DelegateTestUploader.new(model, :image)
file = File.open('test.jpg')

uploader.store!(file)

model.processed # true
model.a_processed # true
model.a_b_processed # true

model.a_processed = false

uploader.processed # true
uploader.a_processed # false
uploader.a_b_processed # true

Is used to synchronize data between uploader and mounted model. Model is used like values cache.

When model is mounted to uploader:

1. When assigned inside uploader, assigns corresponding property in model.
2. When retrieved from uploader instance first checks if value is defined in model and returns it. Otherwise returns uploader's instance variable.
3. When file is deleted, value becomes nil.

Otherwise acts as regular uploader's instance variables.

This is very useful for:

= Integrating CarrierWave with JCrop

Let implement the behavior like at this demo: http://deepliquid.com/projects/Jcrop/demos.php?demo=thumbnail

The uploader:

class PostImageUploader < SobakaUploader
include CarrierWave::Meta

# Crop source is a source image converted from original (left image in the example).
version :crop_source do
process :resize_to_fit => [538, 538]
process :store_meta

# This is the cropped version of parent image. Let crop to 50x50 square.
version :big do
process :crop_to => [50, 50]
end
end

# Defines crop area dimensions.
# This should be assigned before store! and cache! called and should be saved in the model's instance.
# If crop ared dimensions are'nt assigned, uploader creates default cropped image and saves it's dimensions.
# Otherwise cropped image would be lost after recreate_versions! is called.
model_delegate_attribute :x
model_delegate_attribute :y
model_delegate_attribute :w
model_delegate_attribute :h

def crop_to(width, height)
# Checks that crop area is defined and crop should be done.
if ((crop_args[0] == crop_args[2]) || (crop_args[1] == crop_args[3]))
# If not creates default image and saves it's dimensions.
resize_to_fill_and_save_dimensions(width, height)
else
args = crop_args + [width, height]
crop_and_resize(*args)
end
end

def crop_and_resize(x, y, width, height, new_width, new_height)
manipulate! do |img|
cropped_img = img.crop(x, y, width, height)
new_img = cropped_img.resize_to_fill(new_width, new_height)
destroy_image(cropped_img)
destroy_image(img)
new_img
end
end

# Creates the default crop image.
# Here the original cropped image dimensions are restored and assigned to the model's instance.
def resize_to_fill_and_save_dimensions(new_width, new_height)
manipulate! do |img|
width, height = img.columns, img.rows
new_img = img.resize_to_fill(new_width, new_height)
destroy_image(img)

w_ratio = width.to_f / new_width.to_f
h_ratio = height.to_f / new_height.to_f

ratio = [w_ratio, h_ratio].min

self.w = ratio * new_width
self.h = ratio * new_height
self.x = (width - self.w) / 2
self.y = (height - self.h) / 2

new_img
end
end

private
def crop_args
%w(x y w h).map { |accessor| send(accessor).to_i }
end
end

= TODO

Expand Down

0 comments on commit 03984e1

Please sign in to comment.