Simple Delegator In Ruby

Writing decorators in Ruby is made easier by the SimpleDelegator. How SimpleDelegator works is best understood by an example

A PersonDecorator object can be created by passing in the Person object in the constructor. The SimpleDelegator will call the Person object's methods for the PersonDecorator.

The magic of calling the Person object's methods without defining it in the PersonDecorator lies in the Delegator class.

The SimpleDelegator is a child class of the Delegator class. The Delegator class overrides the method_missing method and calls the Person object's methods.

Shishir is an engineer at C42 Engineering. If you liked this post, please consider...

/

How to make Turbolinks play well with jquery in rails

Let's picture this, you have started making a Rails application and you're excited to use JavaScript/jQuery in it, but when you run the app your JavaScript only works if you have reloaded the page, and not if you're navigating to it from some other page in your app. Rails is shipped with a gem called Turbolinks which prevents your browser from loading the whole page every time you send a request, making them faster. Here is how they describe the gem on their GitHub page:

Turbolinks makes following links in your web application faster. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body (or parts of) and the title in the head.

In a rails app, when you navigate from other page to your target page, instead of reloading the JavaScript again, it maintains the instance from the earlier page, and that's the reason why it doesn't work unless you reload your target page.

There are many workarounds to this problem, but the tidiest way I have come across is to add a gem called jQuery Turbolinks. Just add this line to your Gemfile and run bundle update

gem 'jquery-turbolinks'

then in your application.js file, require the javascripts in this particular order:

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//
// ... your other scripts here ...
//
//= require turbolinks

Now all you need to do is to restart your rails server, and you're good to go!

Sumit is an engineer at C42 Engineering. If you liked this post, please consider...

/

Reliable Code Coverage in rails With SimpleCov

A 100% code coverage doesn't ensure that your test suite is absolutely bulletproof. One of the reasons is that normally the controller specs are run, a significant amount of models are also covered, but we don't want that to reflect in our coverage results. SimpleCov, a complete code coverage suite which gives you options to filter, group, merge and format the results. We can also create custom Rake Tasks which will allow us to separately run the specs for our models, controllers and helpers.

Here's how you setup simplecov in your project:

gem 'simplecov', :require => false, :group => :test

Add the above line in your Gemfile and run bundle install, which will install the required gems. Now in your Rails project, open the rails_helper.rb file and add these lines above any other require statement.

Run your tests and run this command to view the report

$ open coverage/index.html

You can also configure simplecov by adding filters to it. You probably don’t want the spec or the config files to be included in your coverage results, here is how you do it.

You can also group your coverage results to be displayed in different tabs, like you can group controllers, models, views, helpers, etc.

The complete simplecov code snippet now looks like this:

Let's create a coverage.rake file under lib/tasks/ in your project and add these lines:

You can set environment variables and using those as the conditions you can run specific specs. Let's open up rails_helper.rb file and make these changes to your simplecov configuration.

add_group 'Models', "app/models"

becomes

and

add_group 'Controllers', "app/controllers"

becomes

Now head over to coverage.rake file, and write a rake task within namespace :coverage to exclusively run your model specs and its coverage.

Run the following command in terminal, followed by open coverage/index.html

$ rake spec:coverage:models

Similarly, you can also write another rake task to run your controller specs. If you haven't already, then try this out and let us know how much code you have got covered!

Sumit is an engineer at C42 Engineering. If you liked this post, please consider...

/

Tech Resume Writing 101 for Indian Grads

I've been working my way through hundreds for fresh grad resumes these last few months.

Sadly, most resumes I see actually hurt the candidate rather than help, which defeats the point of the resume and makes my life harder.

So here's some advice for fresh grads around resume building, especially if you're applying to a tech firm that actually cares about tech.

Your resume is an advertisement

Imagine your job is to go through dozens of ads every day. The ads are for different products in the same space, and you need to identify which of those products you're going to buy based on the ads.

This is what recruiters in every company do day after day, except your resume is that advertisement.

Don't bore them with a shitty ad identical in content to every other shitty ad they see.

Conformity is the enemy

The objective of your resume is to help you stand out from the crowd.

Trying to stand out by conforming to what your classmates or friends are doing is simply... stupid.

As I've said before, "Doing what most do leads to an average case outcome".

Your 'Objective' is bullshit

The objective seems to be a standard in most resumes, and is often extremely off-putting. This is awful given that it is the first thing about you that the recruiter sees.

Here's a typical objective:

To contribute my skills sets to the organization to achieve the goals and targets that enhance my professional and personal growth.

Mmm. Ok. You and everybody else.

A content-free objective section tells a recruiter is that you lack an objective.

Please take the time to carefully think this through - it's the first thing in your resume and sets the tone for everything that follows. I would go so far as to recommend writing a fresh, carefully crafted objective for each company you're applying to that speaks to that company's needs.

Academic performance

This section is often the only useful section in the resumes of most candidates.

Don't mess with this.

Prior art matters

This should be obvious: If you're applying to a tech startup, showcase your skills. Make sure you put all the code you've written front and centre.

Open source contributions, hobby projects and anything else that prove your ability to code result in an instant interview with us at C42, irrespective of academic performance.

Finally, this is 2015. A strong online presence is de rigueur for a tech resume. You should be active on Github, StackOverflow, Open Source User Groups, TopCoder and so on.

Extra curricular

I just went through ~20 resumes from a batch where everyone had exactly the same extra curricular activities.

Stop copy-pasting. It is hurting you.

Extra curricular activities help build up a more complete picture of you, so they are useful, but please put successes at the top and give them attention.

Please also try to avoid citing achievements from your fourth standard painting contest.

The achievements must be significant, recent or (ideally) both.

If you've succeeded in tech related activities, please highlight these.

Conclusion

If your goal is to get a prospective employer's attention through your resume, don't do it by making your resume look like everyone else's.

This doesn't mean changing what colours you use. It means understanding that you're making an advertisement - a tasteful, understated one, but an advertisement nonetheless.


Sidu is a Partner at C42 Engineering. If you liked this post, please consider...

/

Automating Octopress Deployment on Heroku

Octopress is a framework designed for Jekyll, the static blogging engine powering Github pages. It also makes it easier to deploy on other platforms like Web Hosting or Heroku. Heroku deployment requires the least amount of effort; just check-in your Octopress blog repository on Heroku and you're good.

This Heroku deployment strategy comes with a few constraints:

  • Harder get contribution for your blog repository from other hackers.
  • Every small change will require you to push code onto Heroku Git.
  • Harder to track changes if a build breaks. Which happens whenever you are trying out a new theme or modifying current config.
  • Its preferred to associate a single piece of infrastructure with only one responsibility; but with Heroku we have a single monolithic repository is serving both as blog generating engine and blog itself.

Here are my solutions to these constraints:

  • To make contribution easier you can have blog source code on Github or Bitbucket. These services equip you with a project wiki, a issue tracker and version control system like Git or Mercurial; with blog source code open to other hackers, they can easily contribute to your blog by creating pull requests.
  • I prefer to use git-flow to track changes and add new features to project. It is helpful in creating feature branches and releases using git tags.
  • It is preferred to keep a separate staging and production environment, so if a build breaks it'll only affects staging. I separate by syncing Github repository's 'develop' branch with Heroku 'staging' environment and 'master' branch with Heroku 'production' environment.
  • Singularity in infrastructure can be achieved by keeping blog source code on Github and generated static pages with essential config on Heroku Git.

INITIAL STEP

For these solutions to work you need to make some modifications to octopress default Rakefile.

Aside from that you have to create a barebones Heroku application. Also keep thing simple, we are only going to focus on Heroku production environment i.e. no setup for staging.

NEXT STEP

Checking-in a placeholder index and 404 error page to Heroku Git.

rake setup_heroku[git@heroku.com:example.git]

FINAL STEP

Finally deployment is easy with:

rake deploy

It will generate static pages and commit changes to Heroku Git.

REFERENCES

  1. http://octopress.org/docs/deploying/heroku
  2. http://joshuawood.net/how-to-deploy-jekyll-slash-octopress-to-heroku
  3. http://www.jackiejohnston.us/blog/setting-up-an-octopress-blog-on-heroku
  4. https://github.com/raeoks/sophisticated-elephant

Ranjeet is an engineer at C42 Engineering. If you liked this post, please consider...

/