{"id":2527,"date":"2012-05-15T14:17:10","date_gmt":"2012-05-15T17:17:10","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=2527"},"modified":"2012-05-15T15:07:04","modified_gmt":"2012-05-15T18:07:04","slug":"say-hi-to-devise-2-1-0","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2012\/05\/say-hi-to-devise-2-1-0\/","title":{"rendered":"Say hi to Devise 2.1.0 !"},"content":{"rendered":"

After 4 months of our last major version release, we’re releasing Devise 2.1.0, which includes several bug fixes, some new features and the removal of features deprecated on Devise 2.0. If you’re eager to do the update, please check Devise’s wiki page about Upgrading from 2.0 to 2.1<\/a>. You can also check the changelog<\/a> or the commits between 2.0.4 and 2.1.0<\/a>.<\/p>\n

Encrytable is now a gem<\/h2>\n

As it was only used for old encryption algorithms like sha1 or md5, we have extracted encryptable module to a separated ruby gem. So, if you’re using the encryptable module, you should only require it on your Gemfile and you’re good to go!<\/p>\n

Check fields<\/h2>\n

To allow a developer to cherry-pick which features they want to add to their models, Devise splits its behaviors into modules. One of the consequences of such splitting is that you don’t know if the persistence layer provides all fields required by the behavior. For example, the database authenticatable module requires a encrypted_password<\/code> field. If the field does not exist, you will end up getting an error during a request. Usually those fields are automatically added to the migration when you call the devise generator, but if you decide to include a module after, you can easily forget to add the new fields.<\/p>\n

That said, in order to provide faster feedback, Devise now has a method that checks if a given class is missing any of the required fields and raises an error if so. You can call this method as follow (in case your Devise class is User<\/code>):<\/p>\n

Devise::Models.check_fields!(User)<\/pre>\n

We’ve implemented this feature in an agnostic way, to not depend on a specific ORM, but only to verify if the instance responds to the required fields. So even if your ORM does everything through method_missing<\/code>, you should be able to use this method (we’re relying that you also have implemented a working respond_to?<\/code>, which is strongly recommendeded when using method_missing<\/code>).<\/p>\n

When check_fields!<\/code> is called, Devise will detect the included modules in the given class and, if there is a missing attribute, it will raise a Devise::Models::MissingAttribute<\/code> exception with a message telling you all required fields that doesn’t exist. You can easily use that method with your favorite test framework:<\/p>\n

Devise::Models.check_fields!(User)<\/pre>\n

And then you will be able to check if your migrations have added the correct fields to your database.<\/p>\n

A message to module maintainers<\/h3>\n

If you’re a maintainer of a Devise module, you should add a method to each of your modules called self.required_fields(klass)<\/code> that returns an array of required fields. If the method is absent, you will get a deprecation warning.<\/p>\n

UPDATE<\/strong>: Fixed a class name and corrected a grammar error.<\/p>\n","protected":false},"excerpt":{"rendered":"

In this blog post we talk about a new feature upcoming on Devise 2.1 that aims to provide developers faster feedback in case a model is missing a field required by Devise behaviors. <\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[36,7],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2527"}],"collection":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/comments?post=2527"}],"version-history":[{"count":26,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2527\/revisions"}],"predecessor-version":[{"id":2725,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/posts\/2527\/revisions\/2725"}],"wp:attachment":[{"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/media?parent=2527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/categories?post=2527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.plataformatec.com.br\/wp-json\/wp\/v2\/tags?post=2527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}