Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/benjaminmedia/imaginable

A gem for hooking your rails project up to the Imagination image server
https://github.com/benjaminmedia/imaginable

Last synced: 28 days ago
JSON representation

A gem for hooking your rails project up to the Imagination image server

Awesome Lists containing this project

README

        

h1. Imaginable

Imaginable is a gem for Rails, which allows your apps to store and view images with the CDN Connect cloud-based content delivery network.

h2. Compatibility

Imaginable is currently only compatible with Rails 3 and 4.

Imaginable requires that you use jQuery. You can do this easily with the jquery-rails gem, if your version of Rails doesn't already use jQuery by default.

h2. Dependencies

Imaginable requires that you have installed and configured the Fancybox2 jQuery plugin:
http://fancyapps.com/fancybox/

h2. Installation

Simply add Imaginable to your Gemfile and bundle it up:


gem 'imaginable'

Then run the generator:


$ rails generate imaginable:install

The generator will install JavaScripts, stylesheets, as well as two controllers needed for AJAX uploading/cropping of images.

Configure Imaginable in config/initializers/imaginable.rb, and set any named aspect ratios (like square, widescreen, etc.) if you need them. These are handy when fitting user-uploaded images into a standardized layout.

In addition, you will have to add the following to your routes.rb:


imaginable_routes

h2. Usage

It is really easy to make a model Imaginable.

If you are creating a new model, there are even some handy migration helpers.


class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.imaginable :photo
t.timestamps
end
end

def self.down
drop_table :articles
end
end

The @t.imaginable@ helper above, will create two columns: @photo_uuid@ and @photo_token@ which Imaginable needs to keep track of the images used by this model.

We then need to mark our model as Imaginable


class Article < ActiveRecord::Base
has_imagination :photo
end

Finally we just need to add a field to our form, which let's the user upload a photo. This is also really easy thanks to the form helper:


<%= form_for(@article) do |f| %>

<%= f.label :title %>

<%= f.text_field :title %>


<%= f.label :body %>

<%= f.text_area :body %>


<%= f.label :photo %>

<%= f.image_field :photo, :crop => :square %>


<%= f.submit %>

<% end %>

Please note, that you need to include the Imaginable javascripts in your layout, or by other means.
Here is an example of what you could do, if you had a @yield(:head)@ in the @head@ section of your layout:


<% content_for(:head) do %>
<%= imaginable_includes_tag %>
<% end %>

That's all there is to it!

h2. Validation

If you require the user to upload an image, you can use this handy validation helper.


class Article < ActiveRecord::Base

has_imagination :photo

validates_presence_of :title
validates_presence_of :body
validates_imagination :photo

end

h2. Showing images

To show an image, you simply call the imaginable method of your model.
The method will have the name that you have configured in your model.
In the above examples, this would be @@article.photo@.


<%= image_tag @article.photo.url(:crop => 'square', :width => 500) %>