Skip to main content

Don't over concern your rails project

Rails Concern is a good feature, don't make it evil . John McDowall has written an excellent article about how we are misusing rails concern. 

Rails concern are not meant to make your fat model skinny, its meant for making your life easy when you want to include, extend  and use included callback ( all the same time) in a module.

`Skinny Controller and Fat Model` is not enough for writing quality code. Fat Model is pain for maintenance and enhancement. Its nightmare for new developer on the project.

We use Code climate to keep a check on code smell.

Code climate complains when your model start becoming fat and simple option to shut code climate is to pull out some methods related to same logic and dump in a module or concern and code climate gives you green signal.

Even though code climate says your code quality improved but in reality you have just deteriorated your code quality. According to code climate team `Pulling out methods from model to some module is not refactoring ` but code climate is not smart enough to catch this. I think that's why they quote :

“Any application with an app/concerns directory is concerning.”

 Solution for writing quality code is to follow Single Responsibility Principle . 

 - Model should be responsible for defining validations, callbacks and methods directly related to class.
- Avoid Callbacks
   - only use callbacks for preparing object.
   - after_* callbacks are Bad. Once model is saved, purpose of model is done.

- Add Service(plain ruby) object whenever you can, to solve a particular problem.



Comments

Popular posts from this blog

Resolve OpenSSL::SSL::SSLError on Yosemite

If you are using rvm installed ruby on Yosemite  OSX  and getting error : OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed then reinstalling ruby with ` --disable-binary` may fix this Issue. rvm reinstall 2.1.2 --disable-binary  Reason: Ruby installed with packaged binary in rvm has OpenSSL path: /etc/openssl  Path of installed openssl is :  /usr/local/etc/openssl To check openssl directory: > irb > require 'openssl' > "SSL_CERT_DIR: %s" % OpenSSL::X509::DEFAULT_CERT_DIR

Creating an ember cli component addon for javascript library

Create addon with generator command ember addon <addon-name> example: ember addon ember-cli-holderjs Add bower dependency  create blueprint to install dependency  ember g blueprint <addon-name> example: ember g blueprint ember-cli-holderjs Two files will be created installing blueprint   create blueprints/.jshintrc   create blueprints/ember-cli-holderjs/index.js Edit blueprints/<addon-name>/index.js Add afterInstall hook to add javascript library package to bower.json afterInstall: function(options) {     return this.addBowerPackageToProject('holderjs', '2.9.0');   } Generate component ember g component <addon-name> example: ember g component ember-cli-holderjs This will create following files installing component   create addon/components/ember-cli-holderjs.js   create addon/templates/components/ember-cli-holderjs.hbs installing ...

Letter To Ruby Conf India Organisers

Disclaimer : This article consist of my brutally honest review about Ruby Conf India 2015, If you are prone to depression, heart attack while reading criticism, please STOP reading this letter now  and move on. I know organizers worked hard to organize this event and I thank you for that but this does not mean I am happy with the way it was organised. I have complaints, so do lots of people I know. Someone has to raise voice, for now I am taking this responsibility. 1. Why Discriminate ? I was given accommodation in Justa-panjim while most of the conf attendees were in Cidade-goa, those two hotel are incomparable. Justa-panjim didn't have enough water for bathing, forget about swimming pool, beach. I didn't had complained if everyone was given accommodation in Justa-panjim, Yes I am being jealous here..!!! I paid conf fees(lite tickets.. yes thats how early I booked my ticket.) and I deserve to be treated equally , why discriminate ? 2. Where's Ruby ? In some o...