15 July 2014

Pagespeed insight

from the commandline

PSI is a node module that can connect google’s PageSpeed Insights and show the report on the command line, it also can work great with gulp and grunt.

Global install use npm

npm install -g psi

and run like following command:

psi http://demo.openapply.com

psi

15 July 2014

Git remerge

again a feature branch

Here is the tip about messing with the wrongly merged feature branches in git.

Suppose you have the branch my_cool_feature, and two other branches staging for the deploys to the test server and master for production deploys.

Suddenly, you make a mistake and merge your feature branch into master instead of the staging. Then you revert the merge commit, of course, and merge the feature to the staging. It gets reviewed there, and can go to prod, but the problem is that you cannot merge the feature branch into master again. It’s already up-to-date.

That said, master has all the commits from your feature branch, and it also has the revert of those. To get the changes there again, you can do the interactive rebase on your feature branch:

git rebase --interactive revision

Then we pick the first feature commit and squash all other commits into it:

p commit1
s commit2
...
s commitN

The result of this operation is the new commit, which can be merged into master, and all the feature changes will be there again.

11 July 2014

Tree command

as a better ls

There is a shell tool that I install systematically on any new machine/server, it’s tree.

apt-get install tree

or

brew install tree

Its very basic usage is to show the content of a dir in an arborescent way, and there are a lot of options. It can be a good alternative to find sometimes. But I mostly use it for having a quick overview of the content of a dir.

mose@momac > ~ > tree projects/lolita
projects/lolita
├── Gemfile
├── Gemfile.lock
├── README.md
├── config.default.yml
├── config.yml
├── lita-faria
│   ├── Gemfile
│   ├── LICENSE
│   ├── README.md
│   ├── Rakefile
│   ├── lib
│   │   ├── lita
│   │   │   └── handlers
│   │   │       └── faria.rb
│   │   └── lita-faria.rb
│   ├── lita-faria.gemspec
│   └── spec
│       ├── lita
│       │   └── handlers
│       │       └── faria_spec.rb
│       └── spec_helper.rb
├── lita_config.rb
├── log
└── tmp

9 directories, 15 files

Some options:

mose@momac > ~ > tree projects/lolita -hfih -I 'spec|lib' --du
projects/lolita
[ 164]  projects/lolita/Gemfile
[1.2K]  projects/lolita/Gemfile.lock
[1.2K]  projects/lolita/README.md
[ 177]  projects/lolita/config.default.yml
[ 222]  projects/lolita/config.yml
[2.6K]  projects/lolita/lita-faria
[  39]  projects/lolita/lita-faria/Gemfile
[1.0K]  projects/lolita/lita-faria/LICENSE
[ 357]  projects/lolita/lita-faria/README.md
[ 117]  projects/lolita/lita-faria/Rakefile
[ 769]  projects/lolita/lita-faria/lita-faria.gemspec
[1.2K]  projects/lolita/lita_config.rb
[ 102]  projects/lolita/log
[ 102]  projects/lolita/tmp

 7.4K used in 3 directories, 11 files

man tree for more (but the basic tree view is the real deal, for more detailled listings, find is better).

10 July 2014

Reflect on association

in activerecord

In rails sometimes you need to grep model’s declared associations. If you want get all :has_many relationships, instead of enum all association names, you can use reflect_on_all_associations(:has_many).

In model Student

has_many :field_group_records, :as => :record, :order => :position , :dependent => :destroy
has_many :field_string_values, :as => :record , :dependent => :destroy
has_many :field_single_option_values , :as => :record , :dependent => :destroy
has_many :field_multi_option_values , :as => :record , :dependent => :destroy
has_many :field_multi_options , :through => :field_multi_option_values

instead of hardcoding the list

contained = [ :field_group_records,
              :field_string_values,
              :field_single_option_values,
              :field_multi_option_values
            ]

we can get it dynamically

contained = Student.reflect_on_all_associations(:has_many).map do |assc|
  assc.klass if assc.name.to_s.start_with?('field') && assc.options[:dependent] == :destroy
end

doc is at http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations

10 July 2014

Activerecords transaction

to save trouble

When you want to make sure a collection of actions take place without issue, you should put them into transaction to make sure none of them has raise a exception.

Lets say John want to buy some ptt-dollar from Jade. Without transaction, it will look something like:

John.pay( 100, Jade )
Jade.transfer( 'ptt-$', John )

But if during paying procedure John don’t have any deposit cause rollback, the TRANSFER action still take place. Not good.

So we should wrap it with transaction like:

Payment.transaction
  John.pay( 100, Jade )
  Jade.transfer( 'ptt-$', John )
end

in this case any one of them fail the whole transaction will rollback. Save a lot of trouble :D

10 July 2014

Anonymous functions

in javascript

There are several way to execute an anonymous functions in javascript

(function(){ console.log('hi'); }());

(function(){ console.log('hi'); })();

var i = function(){ console.log('hi'); }();

true && function(){ console.log('hi'); }();

0, function(){ console.log('hi'); }();

!function(){ console.log('hi'); }(); // return true

~function(){ console.log('hi'); }(); // return -1

-function(){ console.log('hi'); }(); // return NaN

+function(){ console.log('hi'); }(); // return NaN

new function(){ console.log('hi'); } // return Object

Maybe you’ll never use that, but it is good to know what they do, especially when you dive into the source code by other developers.

09 July 2014

Mute cache

in rails logs

By Default, Rails.cache will write log when updating cache. But if we don’t want it to log all those records, we can use shut it up, by enclosing code with a Rails.cache.mute :

Rails.cache.mute do
  Rails.cache.write("FOO", 'bar')
end

Very easy!

09 July 2014

Nosleep

for the insomniac mac

Check NoSleep:

OS X does not provide an option to prevent laptops from entering sleep mode when the lid is closed. NoSleep allows you to close your laptop’s lid without disrupting downloads or anything else you may have running. And it’s free.

09 July 2014

Fixed table layout

to keep things under control

Table layout dynamically changes width according to its content. But it loses control if the width of the content is wider than the table column even if you give the table a fixed width or max-width property. The {table-layout: fixed;} solves this problem well.

Set {table-layout: fixed} to the table and give percentage width to td and th. Then we can still keep the dynamic width. The table width is no more expanded and {overflow: hidden} works well to td too.

See the Pen AcuKa by Oliver - Frontend Developer (@Oliverl) on CodePen.

09 July 2014

Coffee to Js in Vim

with one key down

If you are not very familiar with coffeescript you may want to know what it will look like when it is compiled to js. This is a simple script to help you do that in vim.

" Created compiled js file
function! s:Coffee2Js()
  let current_coffee = expand('%')
  let filename = substitute(expand('%:t'), ".coffee$", "", "")
  exec('! coffee -co /tmp ' . current_coffee)
  exec('split /tmp/' . filename . '.js')
endfunction
"
" Register a new command `C2J` for compile coffee to a js file
command! C2J :call <SID>Coffee2Js()

Now it is easy to check the js :)