Decent Exposure Gem – Goodbye to repeating instance variables

Introduction

Github: Decent Exposure Gem Decent Exposure allows us to create exposed variables that can be used across the controller’s actions instead repeating instance variables. Exposed variables are accessible from views as well and memorize the resultant values. In other words, it is a helper for creating declarative interfaces in controllers.

Some Points

  • Improving encapsulation
  • Variables for views can visible at the top of the controller
  • Compatible with strong_parameters, decorators
  • Exposes code smells.
Have a look here: http://c2.com/cgi/wiki?CodeSmell

Tips to play with decent exposure gem in a Rails app

  • Add gem ‘decent_exposure’ to the Gemfile and run bundle install
  • How a controller uses decent_exposure looks like:
  • By default, Decent exposure was included as a way to prevent accidentally updating sensitive model attributes and it requires all the params to be whitelisted. It has built-in support for passing strong parameters.
  • It deals with overexposure.The ideal number of exposures is two per controller (1 -> singular resource, e.g. item and 2 -> plural collections e.g. items).
  • If the top of the controller class is crowded with expose statements, two code smells might occur like exposing variables may not used in view and Controller with multiple responsibilities.
  • We can utilize expose with block feature to decorate the object: expose(:item) { |default| ItemDecorator.new(default) }
  • If the model classes are scoped by a namespace like Blog::Post we must specify it on the expose call with model:
    class Blog::PostsController < ApplicationController expose(:post, model: Blog::Post) end

Some resources

Api changes: Api Changes in new version Railscasts: Decent Exposure Decent Decoration: Decent Decoration Gem A blog: How to decrease coupling in Controllers and Views Stay tuned! Thanks,
LUBAIB CEEJEY
Sr. Ruby on Rails Developer

Subscribe For Latest Updates

Related Posts

About Post Author

Leave a Comment

Your email address will not be published. Required fields are marked *