5minutenpause

Caching collections in Rails

| Comments

If you want to speed up your Rails website you should really look into caching. The most important aspect of caching is cache invalidation. That means the moment when the cache has to be rebuilt because it became invalid (the cached objects changed).

Cache invalidation should be done by choosing the correct cache key. If you have a collection of ActiveRecord objects a nice approach could look like this.

1
2
3
4
5
6
7
8
9
articles = BlogArticles.active # or any other collection
cache_key = [blog-articles, articles.map(&:id), articles.maximum(:updated_at)]

@cached_articles = Rails.cache.fetch(cache_key) do
  # This is run if the cache is empty, e.g. on a new cache_key
  # Fetch you articles here.
end

# continue with your cached articles

Thanks for reading.

What I’ve found for March 09, 2015

tiimgreen/github-cheat-sheet :

github-cheat-sheet - A list of cool features of Git and GitHub.

Awesomplete: Ultra lightweight, highly customizable, simple autocomplete, by Lea Verou : What do you use for autocompletion? Have a look at this little widget.

Ultra lightweight, customizable, simple autocomplete widget with zero dependencies, built with modern standards for modern browsers.

HTML5 number inputs – Comma and period as decimal marks | Aeyoun : Have you ever tried to input a number with either comma or dot and it didn;t work as you expected?

will open a numeric software keyboard on modern mobile operating systems. Not every user can input decimal numbers into this convenient field without proper localization.

The Best Static Blog Generator: Middleman vs Jekyll : I use Octopress (which uses Jekyll under the hood) for this blog. When I find the time, I’ll update to either Octopress 3 or just Jekyll. I am not sure yet.

David Turnbull pits Jekyll and Middleman, his two favorite static blogging tools, against each other and looks at where each tool excels.

Rails Garbage Collection: Tuning Approaches - by Lourens of Bear Metal : Garbage collection is a topic I have yet to do anything with. On iOS it was a major topic, but I never considered it for Rails. Bookmarked for future reference…

Rails Garbage Collection: Tuning Approaches Lourens wrote this on Feb 20th, 2015 9:19 am MRI maintainers have put a tremendous amount of work into …

www.dotnetperls.com : Several examples how #split works on Strings. Useful for Ruby beginners.

What I’ve found for March 01, 2015

New Site - Thoughts and Considerations - MartinWolf.org - Freelance Web Developer : Martin redesigned his site. I always like to read about people’s workflows.:

As you may have noticed I redesigned this very site. Since going freelance I only had the blog which also acted as my portfolio of sorts

Introduction to the React JavaScript Framework -Telerik Developer Network : Perhaps you’ve never tried out React until now. Here’s your next chance: “React is a JavaScript library created by a collaboration of Facebook and Instagram. Its aim is to allow developers to create fast user interfaces easily. React makes no assumptions about the rest of the technology stack used, thus it’s easy to try it out on a small feature in an existing project.”

Your nesting is harmful — Medium : Two years ago we’ve started adding a preprocessor, Sass to be more precisely, to our toolchain. … What started as a wonderful journey at the time ended up in a not so beautiful picture about the usage of preprocessors. I’ve learned a lot in the last two years and I’ve seen some of these opportunities used in an alarming way, especially nesting.

Server-side rendering of Single Page Apps : Michael Mahemoff discusses an alternative approach to server-side rendering of Single Page Apps. I don’t really like his idea, but perhaps you do?: “But what if you don’t have, or don’t want to have, your server-side code base in JavaScript? You could still separate out a web tier microservice (it’s the future!) in JavaScript. If you don’t want to do that, you could pre-render every page using a headless browser and build it as a static HTML file. That has the advantage of being super-fast, but …

Critical CSS and Performance – David Bushell – Web Design & Front-end Development – David Bushell make websites. I help small businesses, start-ups, individuals, and fellow web agencies make the most of their web presence. : David optimized his site to show content as quick as possible. Read how: “By inlining critical CSS the browser can start to render immediately while the rest is downloaded asynchronously. What is critical CSS? Anything above the fold apparently. PageSpeed Insights is a useful tool. My original stylesheet was only 35KB so I was skeptical that I’d see any improvements. Nevertheless, I manually extracted 10KB of typographic and layout styles to inline.”

www.leighhalliday.com : A Struct in Ruby is one of the built-in classes which basically acts a little like a normal custom user-created class, but provides some nice default functionality and shortcuts when you don’t need a full-fledged class. Below I’ll discuss some of the different places you might want to use a Struct, but first let’s look into what a Struct looks like and a comparable class.

What I’ve found for February 27, 2015

Unfortunately I can’t write a real article these days because I am sick at home. But I can at least (semi-automatically) publish the latest links I’ found during the last few days.

Enjoy.

medium.com
: An in-depth look at the new Origami release and how it’s used at Facebook.

company.myshopify.com
: Debugging is always challenging, and as programmers we can easily spend a good chunk of every day just trying to figure out what is going on with our code. Where exactly has a method been overwritten or defined in the first place? What does the inheritance chain look like for this object? Which methods are available to call from this context?

blog.lunarlogic.io
: Last week Ania Ślimak was talking about putting model on a diet. She presented a nice approach of reducing fat from models using validation factory. This solution is useful in many cases, but may not solve all the problems, so I wanted to continue this topic.

Menu to cross icon
: Perhaps you’ve seen the hamburger icon turning to a cross upon click. Here several ways to do it.

The Battle Of Build Scripts: Gulp Vs Grunt
: I’ve already written about how to get started with Gulp as well as how to get started with Grunt. They both automate our tasks, they both use Node, and they both require you to create tasks and install plugins of some sort. But do you wonder about the difference between the two, or even, which is better?

BrowserSync 2.0 - JH
: What started as a simple Grunt task capable of auto-reloading browsers and injecting CSS, has now evolved into a indispensable tool used by Designers and Developers all over the world in their quest to offer device-agnostic experiences when building for the Web.

My Sass Breakpoints Mixin
: Martin Wolf: “Working on a website nowadays almost always means that you need to use media queries to make the website adapt to certain viewport sizes or environmental changes.” This mixin makes your life easier if you don’t use any frameworks for managing your media queries.

mattgemmell/Jekyll-Multipage · GitHub
: Matt Gemmell wrote a Jekyll plugin for multi-page articles. I don’t know when or if I’ll use it, but it’s nice to have.

Different methods for merging ruby hashes - Paperless Post Dev Blog
: Benchmarking merge and merge! For methods you use that often, it makes sense to know the downsides of each.

Set Intersection in Ruby - Toby’s Ramblings
: There are times when you have two or more groups of data and you want to know what is common between them. When this happens it is good to know about set intersection.

Rails::Application class - railties (4.2.0) ruby gem documentation - Omniref
: Comprehensive documentation for the Rails::Application class including guides, examples, and source code, developed for ruby programmers.

Open Source Sunday
: Yesterday, two great things happened. 1. I published my first gem! Last week I wrote about how we implemented dynamic meta tags for sharing poster previews on social media. I decided I’d extract that into a gem. It’s pretty basic but it’s a great… | Jure Zove | Software writer.

I’ve noticed a pattern

| Comments

I’ve noticed a pattern of lately. It has to do with developer’s desks and their style to work. Perhaps I am too superficial with this. The thing I noticed is, that developers who keep their desks organized neatly and take the time to clean up their desks before they leave at night, are also the ones who clean up their code and their projects.

I have a drawer beneath my desk and every night before I leave, I put loose things in there; to have them off the desk and stored away. This way, when I come to my desk in the morning, I have a clean area and don’t have to wade through stuff from yesterday. I try to organize code in a similar manner: After I’ve written some code or implemented a feature, I take another look at it and try to find ways to optimize it. To have it better organized.

Last week I started using Pronto to help me with this job. The verdict after the first week? Big win. I showed a co-worker what I was doing and he started to try Pronto as well. Both of us liked it and will keep using it.

Do you have similar patterns?

What I’ve found for February 20, 2015

Rails on Docker : Docker is a tool which allows developers to define containers for applications; this allows for control over the operating system and software running the application. If you’re running against an older version of MySQL, a patched version of Ruby, or other dependencies which make setting up a development environment difficult, Docker may simplify development across a team. Even without complicated dependencies, forced encapsulation within a VM ensures development parity across a team and, if deploying with Docker to staging or production, across environments.

http://voltframework.com/docs : I played around with the tutorial for Volt framework. Looks interesting. Can’t hurt to try something more advanced than the tutorial, I guess.

Using events to decouple Rails applications | Redbooth Engineering : See next entry. :) Decoupling applications, microservices etc.

Scaling Complex Rails Applications | Varyonic : What is the best way to architect complex applications using Ruby on Rails? It’s a topic I am reading quite a lot about these days.

Why attr_accessor is 3x faster than getters and setters, and 8x faster than define_method - Omniref : There are 11 types of method calls defined inside the Ruby VM which have existed virtually untouched since @ko1’s first release of YARV (aka ruby 1.9) more than 5 years ago. Each type of method has it’s own characteristics. Here’s a quick benchmark demonstrating the wildly different performance characteristics between some of the most common flavors.

A small workflow for Editorial for iOS

| Comments

I don’t know if I’ve ever written about my workflow for publishing this site. I often write drafts or even complete articles while on the go. Usually when commuting to or from work. My commute is about an hour either way so that gives me plenty of time to write things for you or my newsletter.

When I am on my mobile devices I either write using the iPhone (6) or my trusty old iPad (3rd Gen with LTE).

The app I am using is Editorial for iOS. I used to write with IA Writer Pro but I had one specific problem with it: I like to use the correct typographic quotes in my writing (’ <- this one). IA Writer has a specific button for these and it looks gorgeous when writing. As I always write using Markdown I need to export it into my Octopress blog and generate HTML pages from it. Typographic quotes break everything and don’t work. So I started to use Editorial’s workflows to replace the quotes with their HTML entities. So inside my text a “’” or “’” becomes &rsquo;.

Here’s the Editorial workflow for that, in case anyone else wants to use it.

Thank you for reading.

Refactoring with Reek and RubyMine

| Comments

I recently heard of a new gem called Reek. I decided to test it in a project of mine. Because I always wanted to thoroughly test RubyMine as well, why not use RubyMine to refactor the findings of Reek?

Let’s go!

Installing and using Reek

If you clicked the link above you saw the Github repo of Reek. There is all the information you need to use Reek in your own projects. But for completeness’ sake, let’s do the gem dance:

gem install reek

Using Reek:

reek app

This gave me a very long list of things that Reek found. 134 warnings overall.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
crmapp master % reek app
app/admin/users.rb -- 9 warnings:
  [52, 52, 53, 56, 61]:create calls params[:user] 5 times (DuplicateMethodCall)
  [50]:create has approx 8 statements (TooManyStatements)
  [53]:create has the variable name 'p' (UncommunicativeVariableName)
  [77, 86]:update calls errors[:base] 2 times (DuplicateMethodCall)
  [84, 87]:update calls format.html 2 times (DuplicateMethodCall)
  [70, 71, 77]:update calls params[:user] 3 times (DuplicateMethodCall)
  [87]:update contains iterators nested 2 deep (NestedIterators)
  [66]:update has approx 13 statements (TooManyStatements)
  [71]:update has the variable name 'p' (UncommunicativeVariableName)
app/controllers/answers_controller.rb -- 1 warning:
  [1]:AnswersController has no descriptive comment (IrresponsibleModule)

# Lots of stuff omitted ...

app/models/dataset.rb -- 9 warnings:
  [34, 38]:Dataset has the variable name 'a' (UncommunicativeVariableName)
  [51, 51, 52]:Dataset#clear_times calls self.follow_up_time 3 times (DuplicateMethodCall)
  [60, 61]:Dataset#sanitized_time calls "%02d:00" % time_to_sanitize.to_i 2 times (DuplicateMethodCall)
  [60, 61]:Dataset#sanitized_time calls time_to_sanitize.to_i 2 times (DuplicateMethodCall)
  [56]:Dataset#sanitized_time doesn't depend on instance state (UtilityFunction)
  [56]:Dataset#sanitized_time has approx 9 statements (TooManyStatements)
  [63]:Dataset#sanitized_time has the variable name 'a' (UncommunicativeVariableName)
  [58]:Dataset#sanitized_time has the variable name 'i' (UncommunicativeVariableName)
  [56]:Dataset#sanitized_time refers to time_to_sanitize more than self (FeatureEnvy)

# Lots of stuff omitted ...

134 total warnings

As you can read in between, I omitted several lines. This is just supposed to be an example after all. I decided to show you three types of warnings:

  • An admin model app/admin/users.rb
  • A controller app/controllers/answers_controller.rb
  • An Active Record model app/models/dataset.rb (one God Object in this project…)

I will go through each one of these and try to reason about Reek’s warnings. I will use RubyMine to refactor parts of the code to get rid of the warnings and to have better code.

One more thought before I start. If you use tools like Reek, Brakeman or Flay and Flog please don’t just follow their advice for the sake of following it. Always try to weigh on what you would like to achieve with your refactoring. My thoughts for this projects are:

  • I often have multiple week-long breaks where I don’t work on this code/project.
  • Everything I can refactor to make it easier for me to understand code I’ve written should help me.
  • I don’t have 100% test coverage for this project, so I can only refactor code that is covered by tests.
  • There are always exceptions to these “rules”

Refactoring the admin model

These were the warnings.

1
2
3
4
5
6
7
8
9
10
app/admin/users.rb -- 9 warnings:
  [52, 52, 53, 56, 61]:create calls params[:user] 5 times (DuplicateMethodCall)
  [50]:create has approx 8 statements (TooManyStatements)
  [53]:create has the variable name 'p' (UncommunicativeVariableName)
  [77, 86]:update calls errors[:base] 2 times (DuplicateMethodCall)
  [84, 87]:update calls format.html 2 times (DuplicateMethodCall)
  [70, 71, 77]:update calls params[:user] 3 times (DuplicateMethodCall)
  [87]:update contains iterators nested 2 deep (NestedIterators)
  [66]:update has approx 13 statements (TooManyStatements)
  [71]:update has the variable name 'p' (UncommunicativeVariableName)

Refactoring the #create action

I’ll start from the top. [52, 52, 53, 56, 61]:create calls params[:user] 5 times (DuplicateMethodCall) tells me that the create action calls params[:user] 5 times. The lines were it is called are given at the start of the warning. Here’s the code for the create action:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def create # this is line 50
  generated_password = Devise.friendly_token.first(8)
  if params[:user][:invite_user].present? && params[:user][:password].blank?
    [:password,:password_confirmation,:current_password].collect{ |p| params[:user][p] = generated_password }
  end

  user = User.create(params[:user])
  if UserConfirmation.new.confirm_user(user.id)
    flash[:notice] = "Eine Einladung wurde an #{user.email} gesendet."
    redirect_to admin_users_path
  else
    flash[:error] = "Die E-Mail an #{params[:user][:email]} konnte nicht gesendet werden."
    redirect_to new_admin_user_path
  end
end

I will extract the calls to params[:user] as a variable. The screenshots show how to do this in RubyMine.

After calling the extraction you’ll have to name it. I decided to simply call it user_params.

The modified lines now look like this.

1
2
3
4
5
6
7
8
  # ...
  user_params = params[:user]
  if user_params[:invite_user].present? && user_params[:password].blank?
    [:password,:password_confirmation,:current_password].collect{ |p| user_params[p] = generated_password }
  end

  user = User.create(user_params)
  # ...

You probably already noticed the lines with the if statement. This is a perfect example for a good spot for refactoring. If you remember my goals from the beginning, there was this one:

Everything I can refactor to make it easier for me to understand code I’ve written should help me.

The if statement and the following line generates passwords and injects them into the user_params if there aren’t any. Why not take these lines and extract a method from them? I will name the method accordingly to what it does and everything will be clearer and neater. While extracting the method I noticed, that I created a variable called generated_password at the beginning of the create action. This variable is only used with the injection method I just extracted. Instead of providing the variable as an argument to the method (as RubyMine suggested), I can move the whole line into the method. Then I provide the original params[:user] to the method, return the modified user_params from the method and assign them inside the create action to a local user_params variable.

What used to be four lines that didn’t reveal their intent unless you read every single one of them, is now one line with very clear intentions:

1
2
# sure, one could argue about the method name...
user_params = inject_generated_passwords_into(params[:user])

The controller action is now arguably better/clearer than before. The first two lines deal with creating the object. The rest is for rendering the appropriate response. There are people who don’t like explicit calls to render or redirect_to in their controllers. But once again I believe that they are perfect for revealing what happens and why does it happen. I prefer to have two or three more lines in my code, then having to scratch my head every time I see the code as to why things happen.

1
2
3
4
5
6
7
8
9
10
11
12
def create
  user_params = inject_generated_passwords_into(params[:user])
  user = User.create(user_params)

  if UserConfirmation.new.confirm_user(user.id)
    flash[:notice] = "Eine Einladung wurde an #{user.email} gesendet."
    redirect_to admin_users_path
  else
    flash[:error] = "Die E-Mail an #{user_params[:email]} konnte nicht gesendet werden."
    redirect_to new_admin_user_path
  end
end

By doing this we dealt with these three warnings:

1
2
3
  [52, 52, 53, 56, 61]:create calls params[:user] 5 times (DuplicateMethodCall)
  [50]:create has approx 8 statements (TooManyStatements)
  [53]:create has the variable name 'p' (UncommunicativeVariableName)

You may wonder what I did about the last warning. The answer is nothing. The warning is because of this line:

1
[:password, :password_confirmation, :current_password].collect { |p| user_params[p] = generated_password }

I will not change the p to somethings else, as this variable is only visible inside the collect block and it’s perfectly clear what it does. So I won’t change it, just because Reek doesn’t like it. Deal with it, Reek!

Refactoring the #update action

Once again, these were the warnings:

1
2
3
4
5
6
[77, 86]:update calls errors[:base] 2 times (DuplicateMethodCall)
[84, 87]:update calls format.html 2 times (DuplicateMethodCall)
[70, 71, 77]:update calls params[:user] 3 times (DuplicateMethodCall)
[87]:update contains iterators nested 2 deep (NestedIterators)
[66]:update has approx 13 statements (TooManyStatements)
[71]:update has the variable name 'p' (UncommunicativeVariableName)

And this is what #update looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def update
    @user = User.find(params[:id])
  errors = {}

  if params[:user][:password].blank?
    [:password,:password_confirmation,:current_password].collect{|p| params[:user].delete(p) }
  # else
  #   errors[:base] = "The password you entered is incorrect" unless User.find_by_email(params[:user][:email]).valid_password?(params[:user][:current_password])
  end

  respond_to do |format|
    if errors[:base].blank? and @user.update_attributes(params[:user])
      if (@user == current_user)
        flash[:notice] = "Your account has been updated"
      else
        flash[:notice] = "The account #{@user.email} has been updated"
      end
      sign_in(:user, @user)
      format.html { redirect_to admin_users_path }
    else
      flash[:error] = "#{errors[:base]}"
      format.html { render :action => :edit, :status => :unprocessable_entity }
    end
  end
end

This looks bad, doesn’t it? Why hide it, it’s code I’ve written about a year ago. Things change and people get better. Now I have the chance to refactor it. Let’s see what can be done.

The first line, where I assign @user is indented 4 spaces. This is corrected very quickly. In the next line I instantiate an empty hash and name it errors. I suppose this was done to have a place were I can shove any errors that occur during the saving of the user. The only problem with all this is: It doesn’t work!

I will delete everything but the neccessary parts of this method.

After:

1
2
3
4
5
6
7
8
9
10
11
12
13
def update
  @user = User.find(params[:id])

  respond_to do |format|
    if @user.update_attributes(params[:user])
      flash[:notice] = "The account #{@user.email} has been updated."
      format.html { redirect_to admin_users_path }
    else
      flash[:error] = "#{errors[:base]}"
      format.html { render action: :edit, status: :unprocessable_entity }
    end
  end
end

I use Active Admin for the admin backend. If something isn’t saved properly Active Admin takes care of notifying the user. I don’t have to do this myself. The whole “delete password if empty” dance was superfluous and didn’t work properly. Hidden inside the successful if branch was a sign_in(:user, @user). This had the effect that if an admin updated another user account, the admin was logged out and imidiately signed in as this other user. HAVOC!

What a bad design. If you look at the update action above, what do you see?

Right. Nothing special!. As I said, I use Active Admin. If the update action doesn’t do anything special it has no right to exist in the first place. Active Admin does all that by itself already. The best code refactoring is deleting code. So this is the update action after the complete refactoring:

It’s gone. Poof!

That means I’ve dealt with all warnings Reek gave me for the admin/users.rb file. Yeah! I rock. :-D

I’ve written in the beginning, that I want to deal with the warnings for two different files as well, but I won’t write about it this time. So please come back another time and read about the rest. And I will try to use RubyMine a bit more for the other files. In the end it wasn’t a fair competition for RubyMine. Every editor is the same when it comes to deleting code. So I can’t yet say wether RubyMine is good for refactoring.

If you don’t want to miss the second part of this post, you should subscribe to my newsletter. And you should probably follow me on Twitter. Although I post rarely over there.

Thank you for reading.

A disproportionate amount of happy

| Comments

At the start of January I sat down to think about what I’d like to achieve in 2015. There were a few goals on my list (and I have to pay attention to not wanting to do too much). One of my goals has to do with Rails, the framework. I’ve been working with Rails for almost five years now and it is the foundation of my career as a software developer.

Back to my goals: Whenever I go to a conference and they introduce the speakers, there’s this one thing that some speakers have that I wanted to have as well. They are introduced as a Rails Contributor. I know that being a contributor is hard work and not achieved easily. But I knew that this was doable and I knew I had to start somewhere. So I downloaded the Rails source code from Github and began to read it. I am a competent Rubyist but far from the Greats. So I was surprised to see how readable the source is. There were few parts that seemed overwhelming and only some that I didn’t understand (and didn’t [yet] bothered to try to understand).

How to contribute

So how could I contribute to this source code? There would be a lot of reading ahead to make sense of it all and to be able to have a meaningful contribution to the code. I had to find a second, quicker way! I remembered that I recently searched in one of my own projects for the use of #map and a subsequent #flatten because I had learned that the call of #flat_map was more idiomatic and performed better(See also the following slide for the numbers.). So I did a full text search for /\.map.*\.flatten/. To my surprise I really found an occurrence and quickly changed it to use #flat_map. A bit further down the file I noticed a superfluous return statement and deleted that.

git commit -a and a useful commit message were the next steps. Finally I forked the project to my Github account, pushed the changes to this new remote and opened the pull request. This was exciting!

Of course my changes really were minor. And of course I used full text search to be able to contribute. But in my opinion this doesn’t matter. Having this commit merged into Rails just makes me happy. I improved the code and I did the first step to be a Rails contributor. And I believe that’s all that counts.

Where to go from here?

I will continue with reading the source. There already were some things I learned (ActionMailer is for sending email, you know that. But what surprised me is that you can use it for receiving mails as well). As my journey through Rails continues I will learn more things (e.g. Rails introduces mattr_accessible and cattr_accessible for Module and Class, respectively.), improve my understanding of Ruby and learn how to improve my usage of the framework.

I believe it’s important to start with small things to contribute and build momentum from there.

  • Do I want to continue to contribute? Sure.
  • Do I know when my next commit and pull request are going to happen? Uncertain.
  • Will it be as small/easy as this one? Probably not.

But I am proud to be on the list.

Next steps

So what’s next in 2015? Another big goal of mine is to improve my ability to teach software development. As a result I will get deeper into teaching and I will try out different approaches. This will mostly happen with the subscribers on my list. You should join today.

Know Your Frameworks

| Comments

One of the goals of using frameworks is to reduce the amount of work you have to do yourself. A prerequisite to this is that you have to know the framework you’re using. I have been guilty of it myself and I see other developers doing it as well:

Rebuild functionality that the framework already gives you for free.

A small example

Let’s say you’re using Twitter Bootstrap as your frontend framework. It’s common to have things float to the left or right, I am telling you nothing new here. But what I’ve seen lately is people give their HTML elements a custom class, go to their CSS/Scss/whatever file and enter

1
2
3
.my-custom-class {
  float: right;
}

You don’t need to do this.

Bootstrap already has this built in! All you have to do is give your element the correct class. In this example it would be pull-right.

You can read more about these small helper classes in Bootstrap’s documentation. There are quite a lot of helpers.

Because I am guilty of doing this myself I will try to make it a habit to document on my blog whenever I realize that I tried to work around a framework. Perhaps you’ll find yourself doing it as well.