{"id":3859,"date":"2014-04-08T16:34:45","date_gmt":"2014-04-08T19:34:45","guid":{"rendered":"http:\/\/blog.plataformatec.com.br\/?p=3859"},"modified":"2014-05-16T13:13:01","modified_gmt":"2014-05-16T16:13:01","slug":"3-features-from-rails-4-1-that-im-excited-about","status":"publish","type":"post","link":"https:\/\/blog.plataformatec.com.br\/2014\/04\/3-features-from-rails-4-1-that-im-excited-about\/","title":{"rendered":"3 features from Rails 4.1 that I’m excited about"},"content":{"rendered":"

Rails 4.1 was just released this week and I already had a great experience trying out the release candidates on my latest project, so I decided to write a bit about my favorites features on this release and some things I have learned by using them so far.<\/p>\n

1) secrets.yml<\/h3>\n

Placing your configuration in a YAML file isn’t exactly a revolutionary feature, but the usage of the config\/secrets.yml<\/code> file that comes with Rails 4.1 holds a more important idea: the promise of a default approach for environment aware custom configuration on Rails applications. Over the years the community created several ways to manage such configuration so every app out there deals with this differently, but now we can use the Rails default as a standard just like we do with the app folder or the routing patterns, taking the configuration madness outside the list of things to worry about when working with Rails. So instead of dealing with multiple YAML files or constants left out inside initializers, we can go with the secrets.yml<\/code> as the default for our apps.<\/p>\n

Remember that you can place any kind of configuration – not just secrets like tokens or passwords – that need to be handled differently through your application environments, like API Endpoints or S3 bucket names. And for any gem maintainers out there, you can make your gem read these settings from the secrets.yml<\/code> automagically through an initializer<\/a> block and maybe remove a configuration step from the gem setup. Adding this to Devise on this pull request<\/a> was easier than I expected and I suggest you to try it out on your gems as well.<\/p>\n

If you want to try to organize your configuration through the secrets.yml<\/code> without having to update to Rails 4.1 right now, Andrew White backported this feature on the rails-secrets<\/a> gem for Rails 4.0 apps.<\/p>\n

So, if you are dealing with some configuration mess or aren’t using something like dotenv<\/a> for your application, I strongly suggest that you try to migrate your config to use the secrets.yml<\/code> file and see how it goes for your application.<\/p>\n

2) Action Pack Variants<\/h3>\n

Variants are proving to be a great solution to render device specific views when mixed with any device detection solution like the useragent<\/a> or browser<\/a> gems, which you integrate quickly with just a before_action<\/code> block:<\/p>\n

\nclass ApplicationController < ActionController::Base\n  before_action :set_variant\n\n  private\n\n  def set_variant\n    if browser.tablet?\n      request.variant = :tablet\n    elsif browser.mobile?\n      request.variant = :mobile\n    else\n      request.variant = :desktop\n    end\n  end\nend\n<\/pre>\n

Even though the main examples are dealing with User Agent sniffing, this feature can be used in any context where you want to have more control of which views are rendered by your application, like:<\/p>\n