I just wanted to have the United States, their Counties, the Cities in the Counties and the Zip codes in each City in CSV format. But I wanted that normalized. In different CSVs. So, here it is. You can also find here some Ruby on Rails sample code that loads these files. Primitive stuff, but sometimes useful.
My Contact Details
My Books
My Projects
My Achievements
United States, Counties, Cities, Zips CSV & Ruby Loading Code
Using String Column Type for Migrations
Ruby on Rails migrations are used to create the necessary tables inside our database. One of the most frequently used column type is string
.
Here is an example of an ActiveRecord::Migration
that uses this type:
The Clean Coder - a Code of Conduct for Professional Programmers - R. Martin (Pearson)
I have recently finished the book “The Clean Coder - A Code of Conduct for Professional Programmers - R. Martin (Pearson, 2011) BBS”. Here is my short review:
Overall: Fantastic. Worth reading for every developer that wants to become professional.
My Notes and Highlights
Chapter 1 - Professionalism
- Professionalism is a marker of responsibility and accountability.
- It is better to make your manager unhappy rather than your manager customers.
Twitter Bootstrap 2 Modal Scrolling Problem | Why Capybara/selenium Cannot Locate Your Visible Buttons
Twitter Bootstrap 2 has a bug while on modal dialogs. The page does not scroll as expected. So, if the browser window is not wide/tall enough to display the whole modal dialog content, the scroll bars do not make it appear.
The bug is described here:
And the answer is given a little bit below that:
This problem does not occur with jQuery modals neither with Twitter Bootstrap 3.
I am using Ruby on Rails and the less-rails-bootstrap
gem.
In order to fix the problem for my current application, and instead of moving to Twitter Bootstrap 3 or to jQuery, I have decided to fix the problem in the gem
Here is the fix:
gem 'less-rails-bootstrap', :git => 'https://github.com/pmatsinopoulos/less-rails-bootstrap.git', :branch => 'bug-fix-modal-scrolling'
Important Note This bug influences your capybara/selenium tests, because elements that are normally visible, while the browser is maximized, they cannot be located by selenium and they are reported as non-visible and cannot be interacted with, if the window browser is not open enough to display the whole modal dialog.
Model Properties - Styling
I usually come across people asking what is the preferred way of laying out the properties of a model and, in fact, of an ActiveRecord model. Shall we put first the validations and then the associations? Or first the associations and then the validations? Or shall we put first the callbacks?
One can google for rails style guide
. Will basically find only this:
which is a good resource. But is not complete.
My Rails Model Style Guidelines
we are talking here about Rails 3
Here is the list of my styling guidelines and which I use whenever I am writing a Model.
Including vs Extending a Module
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
|
But you can also include
or extend
a module
in another module
too. Read this gist by
pglombardo who has kindly appended to the above
piece of code.
How to Define and Use Class-level Variables While Mixing in Modules
There are sometimes that you might want to use class-level variables in a reusable module
. How will you do that? Here is a sample ruby program that does that:
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
If you run the above ruby script, you will get the following output:
1 2 3 4 5 6 7 8 9 10 |
|
In the above example, we keep a class level Set
of symbols (@cached_keys
). We want every class
that mixes in Cacheable
to have its own instance of that Set
.
My Solr Journey - Session 001
Why do I take this Journey?
I have been using Solr the last two years for quite some projects. My work was always done using sunspot
(via sunspot_rails
when one has Rails application), a gem that is a client library for Solr.
You need to know few things about Solr, when using sunspot
to access it and do most of the things. sunspot
is a fantastic gem and relieves you from
huge amounts of work when storing data in a Solr database.
However, there are times that I felt that I needed to know more. For example, when I was searching for University of Athens
and I got no results, but I knew
that my documents had all or any of the words “University”, “of”, “Athens” in their body, or, even worse, there were also documents that were having the whole
phrase “University of Athens” in their body. Or I wanted to query with parts of words, or parts of e-mails, and still I was not getting any results. You can see how
I ended up solving these problems reading these two: 1) Why Sunspot Solr does not bring results when I include “of” word in search query?
2) How can I set Sunspot to search for sequences of characters instead of words?
Hence, I decided to take a journey to Solr. And this will be a registration of my steps to this journey. Understanding how Solr works, will make me manage
better what I can do and achieve with sunspot
too.
And here we go
First steps
Summary
One cannot be but extremely impressed of the features and capabilities of Solr. I believe that I will use to replace many of my MySQL stores that are only used for querying.
(Next steps will soon follow)
Executing Migration Commands From Rails Console
Did you know that you can execute migration commands from rails console? Assume that you want to change the column ‘name’ of the ‘Product’ model, to have a limit of 32 characters and set it to not null. Here is what you can do:
1 2 3 4 5 6 |
|
Capistrano - Shared Directories Among Releases
Capistrano is a great tool for deploying Ruby on Rails applications on remote server machines. If you have ever used it, you may already know that it creates new directory structures for each new release that it realizes and sets “current” to point to the latest one.
This is very fine, but there are cases in your application that you might want to share the same directory data among releases. You can declare in your “deploy.rb” file the directories that will be shared among your releases. By default, the directories “log”, “public/system” and “tmp/pids” are already shared. Hence, you do not have to do anything for them. But, if, for example, you want to share the “my_custom_data” directory that you have inside your application structure, you need to add the following line inside your “deploy.rb” file:
1
|
|
This will create a link to “shared/my_custom_data” folder by executing the following command automatically:
1
|
|
Please, note that my_custom_data
should pre-exist while you are doing the deployment. But, bundle exec cap setup
will create shared folders anyway for you the first time you setup your servers.