1 Create a media
2 Media attachment
2.1 Add media to a model
In your model, add:
media_attachment :resources, :size => N, :types => %w{image}
For example:
class ExampleModel < ActiveRecord::Base
media_attachment :images, :size => 2, :types => %w{image}
# .......
end
size and types options are optional. By default is sized 1 and any type.
You can use :many value with size option specifing that you want a undetermined number of assets.
For convention, the attribute name should be always plural no matter if it contains a single element (size option is 1). The media_attachment accessor always returns an array.
2.1.1 Minimum amount of assets
media_attachments able to set the maximum amount of related assets.
If you need to set the minimum, add something like this to your model:
validates_length_of(
:images_ids,
:minimum => 1
)
You can add a custom error message adding a :message option to the validates_length_of
2.1.2 Media types
The available types are in the AssetType model, you can get it running that in your script/console:
$ script/console
Loading development environment (Rails 2.3.2)
>> pp AssetType.all.inject({}){|acc, at| acc[at.key] = at.name; acc}
{"audio"=>"Audio",
"doc"=>"Document",
"image"=>"Image",
"video"=>"Video",
"other"=>"Other",
"flash"=>"Flash"}
=> nil
The types of the :type option of the media_attachment method are the keys of the hash printed in the script/console example.
Also, if you want to use all types, you can set it to :ALL. It’s an special value.
2.2 Insert media selector to their form
In the ubiquo you have to add a media selector. To do that just call the helper:
<% form_for ... do |form| %>
....
<%= media_selector form, :images, :visibility => 'public' %>
....
<% end %>
The first attribute is the form object. The second is the name of the media attachment. Visibility option can be ‘public’ or ‘protected’. This visibility determines if the resource can be accessed from anywhere(public) or requires to login in Ubiquo(protected). Default is public.
2.3 Get the resource url
To get the url of a resource you can use url_for_media_attachment helper. In that example there is a link to a resource:
<%= link_to(
"a link to the first image",
url_for_media_attachment(object.images.first)
) %>
2.4 Get the resource name
When a resource is associated with something, that relation can be renamed. This name is unique for the relation.
This is useful when a generic name is added when uploading the asset but in a relation it must be named with a different name.
In that example there are a link to a resource showing their name:
<%= link_to(
object.name_for_asset(:images, object.images.first),
url_for_media_attachment(object.images.first)
) %>
2.5 Get the thumbnail url
The url_for_media_attachment accepts an argument specifying a resource version. Versions are defined in the configuration. To see it just put that in script/console:
$ script/console
Loading development environment (Rails 2.3.2)
>> Ubiquo::Config.context(:ubiquo_media).get(:media_styles_list)
=> {:thumb=>"100x100>"}
Then, to get this version url just type something like that:
<%= link_to(
"a link to the first image thumbnail",
url_for_media_attachment(object.images.first, :thumb)
) %>
3 Media selector
When adding a media selectior initially you will see something like that:

Here, you can add new media or select existent.
3.1 Upload media
If the ‘add new media’ option selected, a sub-form will appear.

Here you have to add a file and their name. When clicked in the ‘save’ button, the file will be uploaded with AJAX and will be selected automatically
3.2 Select media
If the ‘select existent media’ option selected, a list will appear.

Here you can search existent assets by their name and select it.
3.3 Rename selection
When an asset is selected it has it’s own name and a ‘selection
name’. This name is only for that relation and can be changed from the
selected item view:

just click in ‘change text’ link.
4 Ubiquo show helpers
For show pages, there are a couple of helpers that can help you to brainlessly print a list of images and documents.
Images
ubiquo_show_media_attachment_images(
@instance,
:media_field,
'Images list'
)
Documents
ubiquo_show_media_attachment_docs(
@instance,
:media_field,
'Documents list'
)
5 Media processors
5.1 Add media processors
Processors let you pass the uploaded image or file through n:Number Processors, returning the processed file/image. By default, Ubiquo has the :thumbnail processor. In order to add a new processor, for example one named “watermark”, in config/initializers/ubiquo_config.rb you should add:
default_media_processors = Ubiquo::Config.context(:ubiquo_media).get(:media_processors_list)
app_processors = [:watermark]
Ubiquo::Config.context(:ubiquo_media).set do |config|
config.media_processors_list = app_processors + default_media_processors
end
5.2 Add a watermark
Continuing with the watermark example, in order to add that watermark functionality that corresponds to the processor, you should create the folder RAILS_ROOT/lib/paperclip_processors and add a file named “watermark.rb” to that folder with that code:
module Paperclip
class Watermark < Processor
class InstanceNotGiven < ArgumentError; end
def initialize(file, options = {},attachment = nil)
super
@file = file
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@watermark = RAILS_ROOT + "/public/images/rails.png"
@current_geometry = Geometry.from_file file # This is pretty slow
@watermark_geometry = watermark_dimensions
end
def watermark_dimensions
return @watermark_dimensions if @watermark_dimensions
@watermark_dimensions = Geometry.from_file @watermark
end
def make
dst = Tempfile.new([@basename, @format].compact.join("."))
watermark = " \\( #{@watermark} -extract #{@current_geometry.width.to_i}x#{@current_geometry.height.to_i}+#{@watermark_geometry.height.to_i / 2}+#{@watermark_geometry.width.to_i / 2} \\) "
command = "-gravity center " + watermark + File.expand_path(@file.path) + " " +File.expand_path(dst.path)
begin
success = Paperclip.run("composite", command.gsub(/\s+/, " "))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny_thumbnails
end
dst
end
end
end
This code was taken from http://kairichardkoenig.de/2009/07/30/watermarking-images-with-paperclip-in-rails/