Install Capybara-Webkit on Mac Sierra

If you’re trying to stall capybara-webkit on Mac sierra. And you’re following this guide https://github.com/thoughtbot/capybara-webkit/wiki/Installing-Qt-and-compiling-capybara-webkit#xcode-80 but it doesn’t work for you. Then you can try out the steps below:

  1. Download Qt 5.5.1 from here: https://download.qt.io/archive/qt/5.5/5.5.1/qt-opensource-mac-x64-clang-5.5.1.dmg
  2. Install with the default option
  3. QMAKE=~/Qt5.5.1/5.5/clang_64/bin/qmake gem install capybara-webkit

Good luck!

Install Capybara-Webkit on Mac Sierra

Backup database from corrupted DigitalOcean droplets

So this story happened to me recently. My client got a Odoo server running on DigitalOcean for quite sometime. And suddendly one day everything just stop working. The web app is down. I can not access to the server through ssh. Hard reboot also not work. Then I contacted DO people for support.

I got the feedback from DO’s guys in an instance. He said our droplet has been corrupted. And suggested me to do is to investigate the droplet, check for bad stuff with fsck ; pretty much I did following this article https://www.digitalocean.com/community/tutorials/how-to-recover-from-file-system-corruption-using-fsck-and-a-recovery-iso

After spending nearly 2 days following the above approach I learn some quite interesting stuff. However I realize that the droplet cannot be recorvered. So I was thinking of a simpler solution which is simply get the needed data and move them to a new droplet.

And this can be done with few simple step:

*NOTE*: the database in this case is Postgresql, and no backup has been made before. And I cannot run any pg_restore or pg_dump or any other helpful commands inside corrupted droplet.

  • I need to get the droplet into the recovery mode. This step need the support DO’s guys to enable.
  • Then I need to mount the device, enable network connection, and enable SSH (this is vital step)
  • Now I can ssh into the server, and everything will be under this folder /mnt 
  • Next, I need to locate where is the data directory of postgresql is. This can be done by looking at this file /mnt/etc/postgresql/9.x/main/postgresql.conf
  • When you know the data directory. That’s the place Postgresql store everything. So all I need to do is copy all of those files to a safe place.
  • Now I need to setup a new droplet instance with Odoo available. Then paste all of the file above into the data directory of the new instance.

That’s all. Quite simply isn’t it. You might run into some small problems with the process above, such as:

  • the data directory need to be owned by posgres user instead of root
  • you also might need to shutdown the postgresql service while you do the copy and paste process.

Other than that. Enjoy a new droplet!

Backup database from corrupted DigitalOcean droplets

How to improve code quality of Rails application

When you work in a team, there always will be the case that everyone want to roll out their own style or way of doing thing. And your teams might have junior developers that have very little experience with Ruby on Rails. And your codebase might easily get mess up if you don’t have a tools to help you automat scanning for code styles, code consistency, code complexity, and security. There’re couple of great services which you can integrate to CI, such as: codeclimate.com

However in this post, I gonna show couple of free tools which can solve the problems above.

 1. Rubocop – https://github.com/bbatsov/rubocop

It’s great tool for code styles. Make sure everyone follow the same code conventions, and can be easily integrate to any CI services.

2. Fasterer – https://github.com/DamirSvrtan/fasterer

Ruby being known for not very performance language. But we still can make a performance app if we do it right.

3. Brakeman – https://github.com/presidentbeef/brakeman

Scan for security holes in your project. Great tooll, I highly recommend using brakeman. It might give you some false alert. However in general, it’s very good starting point to find out about security risk.

4. Rubycritic – https://github.com/whitesmith/rubycritic

Rubycritic is a collection of couple of different gems. It got lot of different metrix to help you easily spot the complexity of the codes with super nice report in html.

5. Couple of other reputable gems

How to improve code quality of Rails application

Understand more about full-text search for Rails developer

If you’re a Rails developer, then you might have came across to use `pg_search` gem. It’s allow you to do full-text search in a Rails app very easily. All you need to do is to add the gem to your Gemfile and do the bundle install then include the module into a Rails model that you want to search against.

After all that you tested out the search functionality and all worked very nice then you decided to improve the performance by adding full-text search functionality. For example you have the following code

pg_search_scope :search_by_name, against: :name, using: {
  tsearch: {
    tsvector_column: "tsvector_name_tsearch",
    dictionary: "english",
    prefix: true
  }
}

Now you’re happy with the performance boost. But you’re also realize that the search doesn’t return all result as before. For example: you have an article with the following name `Business landing page` and the search `landi` or `landin` doesn’t return any result at all but `landing` return the correct result. So why is that ?

If you’re notice a little bit, you can see that in the code above we’re using dictionary is english as the way to do the index. It means that when you have the following content `Business landing page` then here is the list of tokens that Postgresql will do the index


"Business landing page" =>
  ["business",
   "businesses",
   "land",
   "lands",
   "landed"
   "landing",
   "page",
   "paged",
   "pages",
   "paging"
  ]

`landi` or `landin` are not the correct English words and they never got indexed so that why they never got return.

More  about dictionaries

  • Simple dictionary: The simple dictionary template operates by converting the input token to lower case and checking it against a file of stop words. If it is found in the file then an empty array is returned, causing the token to be discarded. If not, the lower-cased form of the word is returned as the normalized lexeme.
  • Synonym dictionary: This dictionary template is used to create dictionaries that replace a word with a synonym.
  • Thesaurus dictionary: A thesaurus dictionary is a collection of words that includes information about the relationships of words and phrases
  • Ispell dictionary: The Ispell dictionary template supports morphological dictionaries, which can normalize many different linguistic forms of a word into the same lexeme. For example, an English Ispell dictionary can match all declensions and conjugations of the search term bank, e.g., banking, banked,banks, banks', and bank's.

 

#References

Understand more about full-text search for Rails developer

Replace S3 with DigitalOcean as asset server for Spree commerce

In this post I will show steps need to setup DigitalOcean as asset server for Spree projects. So before going into any detail, I want to explain a bit the reason I did it. S3 was great for me. It’s simple to setup and use. Until one day my bill for Amazon S3 go up to ~7k USD/month. I was freak out. And the reason was, one of our dev guys have use aws key for testing purpose and he was not careful enough. And the rest of the story you already know what was going on. Then I decided to setup the asset server on DigitalOcean with only 5$/month and I got 20Gb of SSD storage. Which I think pretty good.

Ok. So here is what you need to make it happen.

First, add the following gem to your Gemfile `paperclip-storage-ftp` at https://github.com/xing/paperclip-storage-ftp. Second, you gonna need to do the config for Spree. You can add a file called `spree_ftp.rb` under `initializers` folder

attachment_config = {
  storage: :ftp,
  path: "/home/your_project_path/files/:attachment/:id/:style/:filename",
  url: "http://X.X.X.X/:attachment/:id/:style/:filename",
  styles: {
    mini: "48x48>",
    small: "100x100>",
    product: "240x240>",
    large: "600x600>"
  },
  default_style: "product",
    ftp_servers: [
      {
        host: "X.X.X.X",
        user: "your-ftp-user-name",
        password: "your-ftp-password"
      }
    ],
  ftp_connect_timeout: 5,
  ftp_ignore_failing_connections: true,
  ftp_keep_empty_directories: true
}

attachment_config.each do |key, value|
  Spree::Image.attachment_definitions[:attachment][key.to_sym] = value
end

That is all for Spree part. Now for DigitalOcean, you gonna need 2 things.

  • 1 is a nginx server – to serve the static content only (png, jpg,..)
  • 2 ftp server – for your Spree project upload asset to

Setup an nginx server on DigitalOcean quite easy and straight forward. One thing to keep in mind that you need to config Nginx to serve only static content (nothing else) and only authorize public users to access to the asset folder which you defined in the Spree config above.

For FTP server setup, you can follow great tutorial here https://www.digitalocean.com/community/tutorials/how-to-set-up-vsftpd-on-ubuntu-12-04 – This can be easily setup within 15 – 30 minutes. Then one last thing you need to do the config for FTP server also. By edit the following file `/etc/vsftpd.conf`. Here is my config as an example

Screen Shot 2016-03-15 at 9.15.46 PM

That’s all. Now I can upload all of my assets into DigitalOcean. The result was pretty great for me. Nginx handle static images very fast. I happy with it and now I gonna move all images from S3 to DigitalOcean.

Replace S3 with DigitalOcean as asset server for Spree commerce