How to allow users to crop images in Rails 4

For cropping images I use Jcrop, a plugin for jQuery. It works pretty well and there are already some tutorials for integrating it into a Rails app. So this post is almost a simple update to what’s already available in the internet. What follows is just my setup.

1: create an Upload model, i.e. a class for holding uploaded files, not only images. This is fairly straightforward.

{[ .migration-basic | 1.hljs(=ruby=) ]}

{[ .upload-class-basic | 1.hljs(=ruby=) ]}

2: attach Paperclip to the Upload model.

{[ .gem-cargo | 1.hljs(=ruby=) ]}

{[ .migration-cargo | 1.hljs(=ruby=) ]}

Note that in my setup, contrary to what’s generally advised, I’m trashing the really original image in favor of a (small and) predictable version of it. (width, height and weight)

{[ .upload-class-cargo | 1.hljs(=ruby=) ]}

3: add exif data support

{[ .gem-exif | 1.hljs(=ruby=) ]}

{[ .migration-exif | 1.hljs(=ruby=) ]}

{[ .upload-class-exif | 1.hljs(=ruby=) ]}

4: add cropping support

Jcrop must be installed in Rails.

  1. I added a /vendor/assets/jquery-jcrop/0.9.2 directory with all of its files in it.
  2. I copied js/jquery-Jcrop.js, css/jquery-Jcrop.css and Jcrop.gif into the /vendor/assets/jquery-jcrop directory.
  3. I added a = require jquery.Jcrop line into both the /app/assets/javascripts/application.js and /app/assets/stylesheets/application.css manifests.

{[ .manual-thumbnail | 1.hljs(=ruby=) ]}

{[ .migration-crop | 1.hljs(=ruby=) ]}

{[ .upload-class-crop | 1.hljs(=ruby=) ]}

5: modify the generated controller such that

  1. all necessary params are whitelisted
  2. upon image creation the user is given a chance to crop it
  3. upon image update it gets cropped

{[ .uploads-controller | 1.hljs(=ruby=) ]}

6: modify the form helper

{[ .form-helper | 1.hilite(=html=) ]}

7: modify the show template

{[ .show-template | 1.hilite(=html=) ]}

How to manage users in Rails 4 (using devise)

Devise is currently the most used gem for authentication in Rails, and version 3.0.0 is compatible with Rails 4. For this, and for being powerful and configurable and staying out of the way (at least initially), I’ve chosen devise for my current project.

To generate a User model I left it to devise itself by issuing

$ rails generate devise MODEL
$ rake db:migrate

Al went fine, but when I wanted to be able to also do some CRUD myself on User models, I issued

$ rails generate scaffold_controller User –skip

At this point I had to fix the form helper and a couple of views, for adding email, password and password_confirmation fields. Then the controller too needed some adjustments that were a little more difficult go get properly, but the model was already fine.

The problem with the controller was changing the update action so that I’d be able to edit the email and/or the password independently.

Here is my new update

{[ .update1 | 1.hljs(=ruby=) ]}

And here is the private defs for user_params and needs_password?

{[ .update2 | 1.hljs(=ruby=) ]}

So the difficult part here was that you have to use the normal update method when you also want to change the password and the special update_without_password method (provided by devise) when you don’t want to change the password.

Devise also provides an update_with_password method, but that’s misleading because it requires the current_password field, only useful when you want the user herself to be able to edit her data. It should be renamed to update_with_current_password…

 

How to validate dates in Rails 4

During the past few days I’ve been busy looking for existing gems doing date validation in Rails 4. I’ve tried a couple of the most famous, but they were compatible with Rails only up to 3.2, so I decided to write a solution myself.

It works fine for me. I’m not going to convert it to a gem because I don’t have time to learn how to right now. If you want to help, you’re welcome.

Docs

It’s all very straightforward.

  • The validator symbol is :date.
  • The validator really does nothing if you don’t provide any options…
  • Supported options are :after, :before, :on_or_after, and :on_or_before, plus :message.
  • If the message is not provided a fine default one is used instead.
  • Values of supported options can be date-castable objects, lambdas, or symbols.
  • Symbols must be method names of the validating object or its class.
  • Values are computed (if needed) and converted to date on each validation.

Code

Here is my DateValidator class, to be put into the app/validators folder.

{[ .date-validator | 1.hljs(=ruby=) ]}

Here is an example of how to use it into a model.

{[ .user-profile | 1.hljs(=ruby=) ]}

Here is the form helper snippet (only what differs from scaffolding).

{[ .form-helper | 1.hilite(=html=) ]}

Here is the controller snippet (only what differs from scaffolding).

{[ .controller | 1.hljs(=ruby=) ]}