Rails 4 flash messages using Twitter Bootstrap 3

I’ve always wondered if there’s any better way to deal with flash messages in Ruby on Rails. In all the Ruby on Rails tutorials you see, they’ll tell you to keep the code DRY, isn’t so? Right?

So what you do? Create partial shared/_error_messages.html.erb or shared/_flash_messages.html.erb and write your flash messages displaying code into this file and call it from all other places in your views? Wait!! While keeping your flash messages in partial is a good thing, but don’t you think you can do it in more better way?? Yes, better way!

I found this gist. While I was reading this gist a thought triggered on my mind that why not remove a partial and make a method which displays the flash messages. So, I created this gist:

Now, application doesn’t have load another partial for just displaying the flash messages. Guess what? You saved few milli seconds of your action load time! Don’t believe? Try it yourself! You’re welcome!

How I started my career after graduation

I am not sure who’s going to read all the stuff I have put below. But, whatever I’ve written is my own personal experience, gained over a certain period of time.

I completed my engineering from so called computer science & engineering(I am still looking for that guy who told me that software engineers have a great life, and encouraged me to go for CSE. He also talked my father into it with the boosted words of wisdom of a software engineer, he told software engineers spend life in an AC room and earn good money for checking emails. When I find this guy then somebody’s gonna get hurt pretty badly! 😛) in July 2010. I had no job as college couldn’t afford to get any company for campus placement. So, I moved to Bangalore in a hope of getting a job and started hunting with other folks I met during that time. Later, I got a job in a pretty well established outsourcing company(organization size was about 40 employees, it wasn’t a startup though) as their first and only testing(QA) engineer after 2 months(on 28th September 2010 to be precise) of struggle. Job description was more of testing applications manually of all the developers in the company, who works on completely different technologies like PHP, Objective-C, Java, Android Applications Development, Flex, and Ruby on Rails(web application framework).

This gave me the opportunity to interact with each and every developer there, I learned in which language they code and how they code. Basically, I got the domain knowledge which I actually needed to understand how things work in a small but established IT company. So, this was the time when I was doing a lot of manual job than learning any language(or writing any code for that matter :/). And, after two months one of the developers(whom I used to talk a lot during work) from ‘Ruby on Rails’ team decided to leave(as he might have gotten a good job offer somewhere else). Now, head(YES, head as there was no manager) of the company told me to learn ‘Ruby on Rails’ from this guy who was leaving and serving his notice period. But wait, I had to learn everything when I am done with my work(which is testing others’ work), and as soon as I complete my assigned tasks, it used to be the time for everybody to leave for the day. I didn’t have any idea how to handle this situation, so after a while, I decided to stay late at night in the office and read about ‘Ruby on Rails’. I still remember how I downloaded few tutorials and doing what was shown in the videos. When I used to get stuck, I ask this guy for help next day. This worked like a charm, however it wasn’t even a month and I realized that I’ve started burning my peace of mind(staying late till 11:30 PM – 12:00 AM and then coming back to office at 9:00 AM). As the time passed, I wasn’t sure enough to work on real projects(like real real project), and time passed really fast I’d say. All I could understand was the basics on this new language and framework called “Ruby” and “Ruby on Rails”, hell, I never heard of both until I joined this company and now they want to me deliver awesome looking projects right in production for the clients. Yet, I did one thing really well in one month, which was understanding of the flow of applications(routing and mapping routes to controllers) in ‘Ruby on Rails’. 17th January, 2011 was the time when one of my cousin brothers was getting married and I had to go to my hometown, so I took leaves for 5 days. When I came back to Bangalore on 22nd january, 2011 and if I’d knew what’s coming then I would have been prepared for it but this was completely unanticipated. I got a call from a guy(who used to handle the account department in this company) on 23rd January, 2011(which was a Sunday) and he said: “There’s no need to come to office from tomorrow(Monday)”. I was surprised and shocked at the same time, I asked this guy “what happened? why?” couple of times but he repeated the same thing again and once he mentioned that he got orders from the head to convey this message to me. Completely unaware of the issue, I went to office to meet this man who happens to be the head of the company. When I reached office, I was asked to leave the office premise by this account guy(who spoke to me a day before and delivered the news over the phone) again, right in my face. But, I insisted to let me at least meet the head of the company and after couple of talks he let me enter to this ‘the head’ of the company’s cabin.

He(head of the company) said: why are you here? we don’t need you to work here. You may go now.
I asked: Why? What did I do wrong that has made you to take such a decision of firing me?
He said(in angry voice, I guess): I found your RESUME on Naukri.com and I think you’re looking for a change, we can’t afford to train someone and let them find a job elsewhere. He added, I let you work here, I nurtured you(as if I was getting paid for sitting in an AC cabin to do nothing). I challenge you(me), you are not even worth getting a job in any other company. Nobody is going to hire you for even 10000 INR/month in Bangalore.

After listening to all that I came out of his cabin without a single word. I have to admit, that was an extremely bad day for me.

After two days, I got a call from this friend/colleague I used to talk in this company which threw me like a dust out of the window. I told him what happened, and I have to admit, this guy really helped me in getting out of this worse situation. He was being pursued by a startup for which he didn’t wanna work for. So, he diverted his interview call to me. I went to take up the interview, as a startup, there were just COO, CTO, and lead designer of this venture. They asked me a few questions in which I was pretty sound from my past self tranings. I finally got the job.

Here(startup) I met the most incredible people. This startup’s CTO: Prashanth HN(https://twitter.com/prashanth), COO: Vivek SP(https://twitter.com/viveksp), and the lead designer: Soujanya Sreenath(https://twitter.com/soujshri). I must say, each and every person has inspired me everyday at work.

They even gave me a certain time frame to learn things on my own on dummy projects and sometimes they used to give tips on how I should go about solving an issue. What I was, and how I turned out to be is only because of these people.

After working here, I couldn’t find a single reason to not to work for startup. Here is what I’ve learned:

1. How to deal with people.
2. How to deal with projects.
3. How to deal with code.
4. How to deal with bugs in production.
5. How to enjoy little moments in life.
6. How to be like a doer than a thinker.
7. How to take decisions which makes sense.
8. How to laugh at times on PJs.
9. How to face things, be calm and have patience.
10.How to be ready for your own startup.

And more importantly why I should be doing my own startup instead of working for someone else. Now, once should realize this themselves. Who would encourage a lost guy in the darkness of unemployment to produce employments?
After working for 2 years and 11 months, I left this startup for couple of reasons(one reason would be to try hard to do a startup of my own). Now, I am pretty much confident to do anything in life. I am no longer a boy who is scared to be left alone in the darkness of unemployment. Because, if I lose my job this time, I will be more happy than being sad and perhaps give my 100% to do something of my own. I am in the process of building something of my own now, but can not give 100% because of the duty calls.

I see all this as a butterfly effect. Every incident of my life is very well connected to anything that happens in future.

Again, this was my own personal experience and needed this golden lifetime experience to be logged somewhere so, I did it here. Thank you for being patient with me and reading all this. :)

The Observer Design Pattern in Ruby!!

Design patterns have always been a vital part of a quality software development. In fact, they let developers to work in a team effectively by setting up the common ground rules for a problem.

In this post I’d like to introduce you to The Observer Pattern. Basically, observer is a behavioral type, or publish/subscribe pattern which allows a number of observer objects to see an event. So, if an observer object is subscribed to an event on the subject, on a change/update of that event on subject will publish/reflect the changes on the observing objects as well.

For example: An electric bulb’s state(on/off) will depend on the state of the switch it is directly connected to. So, if I turn the switch on then the bulb will be turned on, and vice-a-versa.

Now, think switch and bulb as objects. These objects will have two states: either on or off. However, the bulb’s state will entirely depend on the switch’s state.

Bored reading theoretical stuff?? Let’s see some code in action:

Code above shows an Appliance class, which I thought makes more sense. Since, we have bulb, fan, A.C. which are ultimately electrical appliances which have different attributes(of course) except one, which is the state(on/off).

One thing you’ll notice when you see your switch is that, it is integrated on a switch box, so here is our switch box:

And before we have our switch, we would need the observer:

And here is our switch:

Now, all you need is to run these commands:

Just in case if you didn’t like something or wanna report an issue then here is the link to code on github repository.

Module – Include or Extend? and Mixin

Multiple Inheritance – A class is suppose to show multiple inheritance when that class can inherit characteristics and features from more than one superclass(parent class). 

Mixin – Since, Ruby does not support multiple inheritance but has an amazing implementation of achieving the same by eliminating the need of multiple inheritance using module, is also known as mixin.
Mixin can be achieved by using either include or extend keywords inside a class. The difference between include and extend is:
  • include : mixes in specified module methods as instance methods in the target class.
  • extend : mixes in specified module methods as class methods in the target class.
Here is a small code example: 

Still have a question? Feel free to contact me!!