Thursday, May 5, 2016

beego bee tool migration error?

beego default migration tool does not show any error,
its very annoying.

here is a fix, either
go get


clone this repository instead,
and goto $gopath
go build
go install

and now, rerun your bee migrate

Sunday, April 24, 2016

rails 5 hstore hash

Rails 5 now returns params as a class instance, no longer a hash.
due to the requirement to have random hashs key based on user input,
the only way to do this is to force this value by calling .to_unsafe_h

res = params.require(:detail).permit(:name, :year)
res[:specification] = params[: detail][:specification].to_unsafe_h

but if you do know your hash key, call this instead:
permit(:specification => { :keyname1, keyname2 }

if you have any suggestion, please comment :)

Tuesday, April 12, 2016

speed up rails development

One of the main concern with rails development is the speed.
its very slow indeed when your stack grows.

I found several reasons for it to be slow:

  1. assets, when debug is enabled, each individual js and css file will be loaded individually
  2. navigation menu with permission triggers query and outputting links to admin requires some loading time
  3. crud - list and read is rendered everytime even though record is not updated, this can save more time
  4. sassc - compiling and loading sass is very slow in ruby

1. Assets

If you aren't planning to debug any scripting or css, set
config.assets.debug = false # set this to false if you dont plan to debug your css or js
config.assets.digest = true # this recommended permanently to allow cache on browser

2. Navigation menu

Navigation menu via cancancan gem requires some query to the database. Often this file is not changed frequently, its almost once setup, its forgotten. Therefore it make sense to cache this file based on user session. One way todo this is to cache this based on template name with prefix and current user id.

- cache("template_layout_admin_nav_#{ rescue 0}") do 
  nav. ...
     ... (more code here)

3. List & read

Since most record will not be changed, it make sense to cache each individual view fragment for respective model to be cached. They call this russian doll caching. Rails cache is smart enough to update cache when model updated_at changes. This can be applied to both listing and show.

example listing template:

  - @rows.each do |row|
    - cache row do
      td =
      td ...

example show template:
- cache @record do

4. Use sassc

Sass compilation on rails is slow. Recently, we have found an alternative for faster sass compilation. Make use of sassc, add this into Gemfile.
gem 'sassc-rails', group: [:development, :test, :staging]

It is not recommended to run assets precompile on production server. Its slow and takes a long time to update on server. One way to do this is to compile assets locally.

Before every deployment, run this to update your assets. But because we do not wish to load static assets on our development, add this to config/environment/development.rb
config.assets.prefix = '/dev-assets'

in config/application.rb
config.assets.initialize_on_precompile = false # this ensure local assets precompile for production works without the need to connect to production database

This configuration ensure all assets is delivered via dev-assets on local development, while production assets is generated into public/assets and retrieved via /assets

To compile assets for production, run this:
export RAILS_ENV=production; bundle exec rake assets:precompile

If you have relative uri to your rails instance, call this:
export RAILS_ENV=production; export RAILS_RELATIVE_URL_ROOT=/shop; bundle exec rake assets:precompile
# where /shop = uri to your site

To enable cache in development

in config/environment/development.rb,
config.action_controller.perform_caching = true
config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { size: 64.megabytes }
# config.cache_store = :memory_store, { size: 64.megabytes }
    config.public_file_server.headers = {
      'Cache-Control' => 'public, max-age=172800'

It is recommended to set cache memory_store, so that every time you wish to clear cache, simply restart the rails instance. But if you want to have it persistant on every time, use file_store cache.
But if you ever change the template file in navigation, you will need to clear the cache directory
rm -Rf tmp/cache/#
# = represent the cached folder (ls -lat to get the current directory name)

For further speed tuning, please follow the instruction below:

Tuesday, March 22, 2016

turbolinks 5 and ckeditor

turbolinks 5 gives some pretty neat features.
it no longer needs turbolinks-jquery.

but in some way, it doesnt work properly due to cache within turbolinks.
a work around this issue would be calling:

jQuery(document).on('turbolinks:load', (e)->
    #do your initialize here
    if Turbolinks

calling turbolinks cache helps to keep the rendered form intact.
but for some reason, this doesn't work with ckeditor.

the work around is to add this in the turbolinks:load function

    if $(this).css('visibility') != 'hidden'
      # console.log(this)

hope it helps :)

Thursday, February 18, 2016

rails local assets precompile

if you have spring, you may not run bin/rake or bin/rails assets:precompile.

here is a way todo it right

RAILS_ENV=production bundle exec rake assets:precompile

ensure that you have (config/application.rb) set
config.assets.initialize_on_precompile = false

and for development:
config.assets.prefix = '/dev-assets' # to avoid assets precompile everytime u change a css or js

Wednesday, January 13, 2016

gem install mysql -v 0.3

ive issues with rails 4.1 that it does not play well with mysql2 v 0.4 .
im forced to use mysql2 v 0.3.20

when i ran gem install mysql -v 0.3.20
i came to this error:

errmsg.h is missing

ive already had mysql-dev installed.
after searching around, i found the issue.
cpanel mysql-config is still using mysql 5 dev library.
when i ran which mysql-config, and mysql-config, ive gotten cpanel 3rd party bin with v5.

so i renamed cpanel own mysql-config, and relink the correct mysql-config to cpanel/bin
mv /usr/local/cpanel/3rdparty/bin/mysql_config /usr/local/cpanel/3rdparty/bin/mysql_config5

ln -s /usr/bin/mysql_config /usr/local/cpanel/3rdparty/bin/mysql_config

reran gem install and bundle, and it works great now!

Sunday, January 10, 2016

rails admin date time input format

after upgrading to rails admin 0.8.1,
took me few hours trying to figure why all my datetime input fail to work.

it turned out that rails admin have forced their datetime input format based on
this can be set in en.yml

otherwise, you may set the datetime by declaring in the configuration, etc:
field :created_at do
    date_format do


for more formats:

Monday, January 4, 2016

capistrano is slow, alternative: blazing

capistrano seems to be quite slow to deploy. it works great for production, as it export entire git into independent directory and relink directory from shared directory.

In some of my smaller projects,  i would prefer to maintain only 1 folder, and do a git hard reset and push the update directly from my local git.
my work around is to use blazing. since blazing 0.5, recipes is no longer supported.
blazing by default works great! but until you realize you are using spring, and passenger

ive customize blazing with spring generated bin removed from bin.

to deploy:
  1. create to $HOME/rails/shared
  2. upload shared/config/application.yml (for figaro)
  3. mkdir shared/log
  4. mkdir shared/public/uploads
  5. mkdir shared/public/system
  6. mkdir shared/tmp
  7. mkdir shared/vendor/bundle

next, in your project,
  1. blazing init
  2. edit config/blazing.rb
    rake :"server:deploy"
  3. copy this file to lib/tasks/ #if you use mod passenger
  4. blazing setup production
    # this will deploy git after deploy scripts on server
  5. blazing update production # to update later if changed blazing config

to deploy updates, simply:
  1. git push production master # remote: production, local branch: master