Showing posts with label ruby on rails. Show all posts
Showing posts with label ruby on rails. Show all posts

Wednesday, January 25, 2017

setting up automatic deployment via git

If you wish to have automatic deployment with rails and thin, here is an example:

1st, clone your repository on server to a path.
Next, vi .git/hooks/post-merge
add in your bash script to run after git is pull
example:
bash -lc /pathtoroot/deploy.sh

chmod a+x .git/hooks/post-merge

in your git repository, add a file called: deploy.sh, you may code any deployment command after git pull, example:
RAILS_ENV=production
cd /pathtoroot
echo "deploy.sh: bundle install"
bundle install
echo "deploy.sh: rake db:migrate"
bundle exec rake db:migrate
echo "deploy.sh: rake assets:precompile"
bundle exec rake assets:precompile
echo "deploy.sh: restarting"
bundle exec thin -C config/thin.yml restart
bundle exec sidekiqctl stop tmp/sidekiq.pid 0
bundle exec sidekiq -d -P tmp/sidekiq.pid -L log/sidekiq.log
echo "deploy.sh: Done!"

makesure you have chmod a+x deploy.sh and commit this file via git on your local machine.

on server, add a cron job to automatically pull on a period basis:
crontab -e
---
* * * * * bash -lc "cd /pathtoroot/ && git pull"
* * * * * sleep 30 && bash -lc "cd /pathtoroot/ && git pull"
---

2nd command allows you to run git pull within 30seconds.

Once git pull is successful, it will auto trigger post-merge and run your deploy.sh

hope it helps :)





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

example:
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 :)

https://forum.upcase.com/t/rails-doesnt-recognize-a-request-as-patch/5115

http://eileencodes.com/posts/actioncontroller-parameters-now-returns-an-object-instead-of-a-hash/

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.

example:
views/layout/admin/_nav.html.slim
- cache("template_layout_admin_nav_#{current_user.id 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:

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

example show template:
- cache @record do
  .panel-body.resource.box.p-a
    .row
      .col-md-12
        = @record.id

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:
https://www.nateberkopec.com/2015/08/05/rack-mini-profiler-the-secret-weapon.html








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:

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

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

$('textarea.ckeditor').each(->
    if $(this).css('visibility') != 'hidden'
      # console.log(this)
      CKEDITOR.replace(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/environment/development.rb
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
en.time.formats.long
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
      :short
    end

end


for more formats:
http://apidock.com/ruby/DateTime/strftime


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.

https://github.com/u007/blazing

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/
    http://pastebin.com/13cTyvGg #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

Sunday, December 27, 2015

undefined method `register_preprocessor' for nil:NilClass

seems to occur on sprocket-rails v3.0 sprockets-rails (2.3.3) works great! sprocketrails 2.3.3 + sprockets (3.5.2) it was due to less-rails https://github.com/rails/rails/issues/22647

Monday, November 23, 2015

undefined local variable or method `try_spree_current_user'

If you create your custom spree controller,

beware that you will need to add in some spree controller helper to make the magic works

include Spree::Core::ControllerHelpers::Auth
include Spree::Core::ControllerHelpers::Store
include Spree::Core::ControllerHelpers::Order

or

you may extends from Spree::BaseController





Friday, October 23, 2015

authorize! for update

just realize calling model.update after authorize! is wrong!

the right way is to run an authorize!, then model.assign_attributes, then rerun authorize! again, 
and finally model.save

Tuesday, October 20, 2015

running rvm based cron job

rvm is requires a bash login to work.
in order to execute cron jobs, run this:

*/5 * * * * 'sh /home/myuser/versions/current/runme.sh'

create a shell file,
change working directory to rails root,
and run the required job, such as delayed_job once and exit

vi /home/myuser/versions/current/runme.sh
cd /home/myuser/versions/current
/bin/bash -l -c 'RAILS_ENV=production bundle exec rake jobs:workoff'



Thursday, July 30, 2015

connecting to sql server with rails

sql server / sql express is by default cannot be connected by a fixed port
to login to sql server, first, assign a password for sa user,
or any user, and map the database to the user.
you may use db_owner for default role mapping.

then open sql configuration manager
select sql server netwrok configuration > Protocols for localhost / express / your instance name
select TCP/IP
scroll to bottom, and set:
all ports: 1433
dynamic port: (empty, not even a zero)
then goto control panel > services > sql server
restart service,
enable sql browser (auto start delayed) and start service

this shall help to make it work with rails or tiny_tds

Thursday, May 21, 2015

deploying to production via capistrano with passenger and sub uri

due to digested assets requirement in production environment,
the url of images cannot be use in relative to path to css file.

example normal assets:
/assets/frontend/all.css

example of digested assets

/assets/all.XXX.css

due to this, the only way to accurately point to images in css is to add relative path to the url.
one way todo it is to use ENV['RAILS_RELATIVE_URL_ROOT'],
first, you will need to change .css file to .css.erb.
and add in <%=ENV['RAILS_RELATIVE_URL_ROOT']%> to the url path.
example: background-image: url('<%=ENV['RAILS_RELATIVE_URL_ROOT']%>/img/logo.png');

but since we are precompiling via capistrano, it is a must to add this environment into deploy/production.rb
example:
set :bundle_flags, '--deployment' # set this to ensure traces of assets compilation can be shown via --trace
set :default_env, {
  'RAILS_RELATIVE_URL_ROOT' => '/shop'
}

and then run:
cap production deploy --trace

took me hours to try to figure all the issues in place to resolve this.
hope it helps you :)

Thursday, April 30, 2015

iis 7 and rails

after struggling to get things running,
i finally found my best solution.

HttpPlatformHandler

If you are running iis 8, just use httpplatformhandler, it save you a lot of time. but you will need to get webmatrix running. but this is not supported on iis 7/7.5, a bummer :(

WebMatrix

so moving on to try microsoft web platform.
https://www.microsoft.com/web/webmatrix

also several issues,
webmatrix does not play well with proxy, ke-damn!

Some odd / weird results when trying to install webmatrix,
first, it requires iis express, and ive no idea why do it need it.
but after running install and it failed, we are still able to open the webmatrix installer options.
Just ignore the webmatrix installation issue.

webmatrix and proxy?

setting http proxy in environment does not help in one of my client case,
you might want to consider setting up this:
http://ntlmaps.sourceforge.net/ - works like a charm for me :)

Okay, please please please do not install helicon method. Their ruby and rails are outdated!
This will save you the hassle from installing old ruby 1.8 and 1.9 on your machines. And this may conflict with railsinstaller ruby.

So, what now? how do we run rails on /rails uri?
Yes, install Application Request Routing (ARR).
Inside webmatrix, search for arr, install it :)
After install, restart your iis manager (not refreshed if you have it opened while installing).

Okay, unless you want to setup entire host to proxy to another port, dont setup ARR on root level.

In my case, ive setup uri "/rails" to proxy over to my own rails instance.
Todo so, create a application "rails" under Default sites.
Then click into rails, and click on "Rewrite rules".
Then "Add rule" > "Reverse proxy"
Key in "127.0.0.1:3000" for server name,
After adding, double click on the rules, and ensure that you have similar configuration alike below. Notice the {R:1} appended as suffix of the rewrite url.

Setting up Rails + Ruby

to setup ruby, goto http://railsinstaller.org/en (thank god engine yard started something for us).
makesure to install with devkits and git and ssh.
it makes life much easier.
It will install to c:\RailsInstaller
all the paths should have been set for you, thank god :)

Then next, you might realize your gem install throws sslv3 certificate error with rubygems.org, another bummer. It turn out as of today 30th april 2015, certificate has been renewed on rubygems but it wasnt updated to the trusted repository of gems.
Follow here:
https://gist.github.com/luislavena/f064211759ee0f806c88

Since im using 2.1.5, ive followed to 2.0.15 instruction (not sure why the 2.0).
https://github.com/rubygems/rubygems/releases/tag/v2.0.15

Download the .gem, and runs:
gem install --local rubygems-update-2.0.15.gem
update_rubygems --no-ri --no-rdoc

you may run:
gem update (to test if it works)

You will realize latest rails is already installed by railsinstaller.
Create a new app to try
c:\Sites\rails new app1

then create a startup file (this is using webrick, best to replace using either puma or thin)
@echo off
set RAILS_ENV=production
set RAILS_RELATIVE_URL_ROOT=/rails
set SECRET_KEY_BASE=yoursecretkeyshere
rails s

and edit config.ru
and change `run Rails.application` to:
---------
map "/rails" do
  run Rails.application
end

map "/" do
  run Rails.application
end
---------

Notice the uri rails is used to indicated a uri so that all proxy over from iis can be used with this uri.

Installing Mysql2 gem

ensure your mysql2 gem is 0.3.18 or later

goto:
https://dev.mysql.com/downloads/connector/c/
install the connector of your platform and 32/64bits (install the installer if you want to use path below)

next, run:
path to connector: C:\Program Files\MySQL\MySQL Connector C #version
example: v6.1
gem install mysql2 --platform=ruby -- '--with-mysql-dir="C:\Program Files\MySQL\MySQL Connector C 6.1"'

Gems requirements on windows

Due to bugs and issues on windows, these are the min require versions of gem if you need to use them on windows:

gem 'eventmachine', '>=1.0.7'
gem 'bcrypt', '>= 3.1.10'
gem 'mysql2', '>=0.3.18'





Friday, September 12, 2014

overriding spree javascripts

to overwrite spree javascripts, such as spree.js.coffee.erb,

first , enable assets path in config/application.rb

add:
config.assets.paths << "#{Rails.root}/assets"

Next, copy the assets file to the same directory path as the assets on spree.
eg.
spree / core / app / assets / javascripts / spree.js.coffee.erb
to:
assets/javascripts/spree.js.coffee.erb

Then run this on server, if its in production mode:
bundle exec rake tmp:clear

then restart the server,
thin restart, or what ever server you are using. you may stop and start.


if you need to add more file, you may edit:
vendor/assets/javascripts/spree/frontend (or backend) / all.js

hope it helps :)

Thursday, July 24, 2014

undefined method ... path due to polymorphic_url

i came across this non meaningful error,
as it turn out to be a mis named params

example: mispelled params[:nam] for params[:name]

Wednesday, June 25, 2014

Circular dependency detected while autoloading constant

When face upon this issue,
its caused by naming controller or calling model name incorrectly.

possible issue are naming are case sensitive.
example:
#model
class Oi


end

But calling
OI.find #this lead to circular dependency error

Tuesday, May 20, 2014

Spree new.before?

Spree has nifty callbacks for controller,
create.before
create.after
update.before,
but what about new.before?

new_action.before :method_name

Friday, December 27, 2013

Unable to download data from https://rubygems.org/ - SSL_connect


If you are getting:

Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed ...

Its most likely your gem gem is outdated.
Try updating via:
gem update --system

then try to install your gem as required

if it still fails, try using raggi brew update
brew tap raggi/ale
$ brew install openssl-osx-ca
as referred from here: