Serj L aka Loremaster

Blog of Rails developer.

Another Guide to Upgrade to Rails 4.1

| Comments

Rails 4.1 is pretty nice release, but upgrade to this version may be painful. In this post I’ll try to help you.

First of all, you need to install the latest version of Rails.

Gemfile
1
2
gem 'rails', '4.1.2'
gem 'sass-rails', '~> 4.0.3'

After that read official rails guide to upgrade to rails 4.1, because I don’t want to talk about these details. Just don’t touch section about Spring, I’ll tell what to do with that.

How Thor Can Help You With Your Tests (or Integration of Parallel_tests and Cucumber)

| Comments

Specs it!

Today I’d like to talk about one important (kind of) area of each RoR developer. This is testing. You may wonder, “Why the heck do I even need to write these stupid tests?”. Well, first of all, you may do it because your boss wants it. Or because all other rubyists write them. What do you mean, “I’m not satisfied?”. All right, how about making sure that your app don’t crush each time you commit new feature or fix a bug? Tests also help you to upgrade libs, install new Rails and other funny stuff, FYI. I know the company, who can’t upgrade to Rails 4, because it’s app don’t have tests.

Let me suppose, that you are satisfied by that and you started to write tests… Firstly with Rspec, because test-unit is not popular solution. And then you open Cucumber for yourself. And write more and more tests. And in one day you look in your terminal and you start to think that waiting 10 minutes until your features’d finish isn’t a good idea, because you can already drink few cups of tea or coffee and with such progress soon you’ll be able to watch a “Big bang theory”.

Useful Gems for Rails Developer. Part 1: Development

| Comments

better_errors

We meet different errors almost every day. Sometimes we get exceptions and we want to debug them to find the root of the problem. And better_errors helps us with that. It shows much nicer errors. It is like superman, just in programming area. And it is not just show stack trace to you, it also allows you to interact with your code and data using the console to get feedback immediately. It is nice, and I think, that you will get this feeling soon.

Enlarge Your Capistrano

| Comments

I would like to tell you about few gems, which would make your deployment life a little bit easier. Well, it worked for me.

rvm-capistrano

Our first hero on the scene is rvm-capistrano. This hero is special, he prefers only capistrano №2. But time is changing and capistrano №3 is walking on streets of our computers, so, if you have seen it and even use it then you may select another hero: rvm1-capistrano3.

But I will talk about oldies here. What does this gem actually do? It brings integration between Rvm and capistrano. There are some discusses in the internet what to use, some people prefer rbenv, other use Rvm. Actually, rbenv is very small and it has very small code base, but it doesn’t have power of Rvm, so your choice should depend on your situation. But let me talk about hero, he is waiting to be presented!

Another Guide to Upgrade to Rails 4

| Comments

Yep, this is another guide, which’ll help you (i hope!) to upgrade your app to the Rails 4. Before start, please make sure that you are running latest Rails 3 release, i think it is 3.2.17 nowadays. Yes, i’m sorry, Rails 2 guys, but this guide’ll not help a lot, you have a lot of work to do, sorry! :)

How Install Thinking Sphinx With PostgreSQL’s Support on Mac OS X

| Comments

Thinking Sphinx is a library, which allows you use Sphinx – full text search server. But setup can be tricky, if you use PostgreSQL. In this post I will show you you how setup it.

First of all, make sure that you have installed Mysql. The best way to install it is just use homebrew:

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ brew install mysql
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/mysql-5.6.15.mavericks.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mysql-5.6.15.mavericks.bottle.tar.gz
==> Caveats
A "/etc/my.cnf" from another install may interfere with a Homebrew-built
server starting up correctly.

To connect:
    mysql -uroot

To have launchd start mysql at login:
    ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
Then to load mysql now:
    launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist
Or, if you don't want/need launchctl, you can just run:
    mysql.server start

I recommend you to copy and past commands from homebrew’s output after installation.

If you don’t want to use homebrew (but i strongly recommend to use homebrew), then you should install x64 MySQL server from official site. Make sure to use x64, you don’t want to get any weird errors (I guess that you use at least OS X 10.6).

Now you should install Sphinx. And it’s a tricky part. If you would install it without any flag or with just mysql support, you would get such weird errors:

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
$ rake ts:index
Generating configuration to /Users/serj/Projects/gearup/config/development.sphinx.conf
Sphinx 2.1.3-dev (r4319)
Copyright (c) 2001-2013, Andrew Aksyonoff
Copyright (c) 2008-2013, Sphinx Technologies Inc (http://sphinxsearch.com)

using config file '/Users/serj/Projects/gearup/config/development.sphinx.conf'...
indexing index 'service_core'...
ERROR: source 'service_core_0': unknown type 'pgsql'; skipping.
ERROR: index 'service_core': failed to configure some of the sources, will not index.
total 0 reads, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg
total 0 writes, 0.000 sec, 0.0 kb/call avg, 0.0 msec/call avg

To avoid them you should instal sphinx with mysql and postgres support:

Terminal
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
$ brew install sphinx --mysql --pgsql
==> Downloading http://sphinxsearch.com/files/sphinx-2.1.3-release.tar.gz
Already downloaded: /Library/Caches/Homebrew/sphinx-2.1.3.tar.gz
==> Downloading http://snowball.tartarus.org/dist/libstemmer_c.tgz
Already downloaded: /Library/Caches/Homebrew/sphinx--stemmer-c.tgz
==> ./configure --prefix=/usr/local/Cellar/sphinx/2.1.3 --localstatedir=/usr/local/var --with-libstemmer --with-mysql --with-pgsql
==> make install
==> Caveats
Sphinx has been compiled with libstemmer support.

Sphinx depends on either MySQL or PostreSQL as a datasource.

You can install these with Homebrew with:
  brew install mysql
    For MySQL server.

  brew install mysql-connector-c
    For MySQL client libraries only.

  brew install postgresql
    For PostgreSQL server.

We don't install these for you when you install this formula, as
we don't know which datasource you intend to use.
==> Summary
🍺  /usr/local/Cellar/sphinx/2.1.3: 16 files, 18M, built in 17 seconds

After that you can install Thinking Sphinx:

Gemfile
1
2
gem 'mysql2',          '0.3.13', :platform => :ruby
gem 'thinking-sphinx', '3.1.0'

And everything should be ok!

How Upgrade Devise 2 to 3.2

| Comments

Devise is a great gem! It really helps you, taking all authentication jobs away from you. However, upgrading Devise up to version 3.x can be a little bit tricky. In this post I’ll show you how to do that.

First of all, update your gem via bundle update OR specify version in your Gemfile:

Gemfile
1
gem "devise", "~> 3.2.2"

And run bundle install.

Since devise 3.1 platformatec announced few security improvements. One of them is secret_key. To add it open devise config and add:

config/initializers/devise.rb
1
2
3
4
# The secret key used by Devise. Devise uses this key to generate
# random tokens. Changing this key will render invalid all existing
# confirmation, reset password and unlock tokens in the database.
config.secret_key = '2710f15f11771d6692a3015d7e3dba2cb05539c1f72i6u345df5433hg535kj5x56v6er56if2566c63c2ad670d6859e536b40d87e6543b115609f0464bdd99502abbe241c4'

Of course, you should use your own secret key, so change my example to be more secure.

If you have ever generated devise’s views, then you should change it’s mailers to use @token instead of @resource.*_token:

views/devise/mailer/confirmation_instructions.html.haml
1
2
# ...
= link_to t('devise.mailer.confirmation_instructions.submit'), confirmation_url(@resource, confirmation_token: @token)
views/devise/mailer/reset_password_instructions.html.haml
1
2
# ...
= link_to t('devise.mailer.reset_password_instructions.reset_link'), edit_password_url(@resource, reset_password_token: @token)
views/devise/mailer/unlock_instructions.html.haml
1
2
# ...
= link_to t('devise.mailer.unlock_instructions.unlock_link'), unlock_url(@resource, unlock_token: @token)

After that everything should works. But take a look into your terminal: you may have deprecation errors, which you should fix!

How Set Up Sublime Text 2 as Your Default Git Editor

| Comments

If you love Sublime Text 2 as I do then you probably would like to change default git editor (which is Vim nowadays). I suppose that you have installed Sublime text.

First of all, you should install subl shortcut. To do that open your shell and print (if you already have ~/bin directory, then just use the last line of my code):

Terminal
1
2
3
$ cd ~
$ mkdir bin
$ ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

Now restart your shell and try that:

Terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ subl -h
Sublime Text 2 Build 2221

Usage: subl [arguments] [files]         edit the given files
   or: subl [arguments] [directories]   open the given directories
   or: subl [arguments] -               edit stdin

Arguments:
  --project <project>: Load the given project
  --command <command>: Run the given command
  -n or --new-window:  Open a new window
  -a or --add:         Add folders to the current window
  -w or --wait:        Wait for the files to be closed before returning
  -b or --background:  Don't activate the application
  -s or --stay:        Keep the application activated after closing the file
  -h or --help:        Show help (this message) and exit
  -v or --version:     Show version and exit

--wait is implied if reading from stdin. Use --stay to not switch back
to the terminal when a file is closed (only relevant if waiting for a file).

Filenames may be given a :line or :line:column suffix to open at a specific
location.

If you have the same output, then everything works great. Otherwise you should add ~/bin to your $PATH. To do that open your ~.bash_profile and append these lines to the end of the file:

.bash_profile
1
2
PATH=$PATH:~/bin
export PATH

And I think that you should restart your shell again.

Finally, to set up sublime as default editor just print in your shell that:

Terminal
1
$ git config --global core.editor "subl -n -w"

And that is all!