Kang-Kyu Lee

Ruby + Rails codewriter

Page 2

Ruby block

I’ve been trying to figure out how it works. This morning I wanted to write it down my thinking process today, before I forget.


block_given? is a method of Kernel module. In other words, it’s one of the same kind of puts, gets, sleep, etc. They are at the top-level, so to speak.

The Kernel module is included by class Object, so its methods are available in every Ruby object.

And block_given? verifies if block is given when you call a method.

What do you mean by “block is given”? In normal cases (when block is not given) the codes should be like this:


Here you call methods with parameters, or without parameters. But when you say “block is given” (However this is also normal, nothing special) that means you called it with a block. A lot like a parameter.

object_name.method_name(parameter_names) { chunks of

Continue reading →

Rails, datepicker

= form_for @task do |f|
  = f.datetime_field :done_at
  = f.text_field

I thought because done_at should be saved as DateTime, the field should be datetime_field. But text_field works fine.

On top of that, for some browsers (Firefox, I can say) date type of input tag gives you a datepicker on its own (html5 datepicker), and I didn’t want to have them.

<input id="task_done_at" name="task[done_at]" type="datetime" />

Most of all, I watched an episode of Railscasts and that’s why I started working on it.

I used jquery-ui-sass-rails gem.

I put this file app/assets/javascripts/tasks.js.coffee as following the screencast 213

jQuery ->
    dateFormat: 'yy-mm-dd'

also after setting up

gem 'jquery-ui-sass-rails'

//= require jquery.ui.all
//= require jquery.ui.datepicker

@import jquery.ui.core
@import jquery.ui.theme
@import jquery.ui.datepicker


Continue reading →

Authorization with Pundit

I want share my experience in the group from our Monday meetup yesterday. Let’s refresh the memory:

“do authorization with pundit gem” – Homework here on github issue 12


We went to this page and bundle it to our Gemfile

gem 'pundit', '~> 1.0.1'

And open up these links on that page.

Homepage: https://github.com/elabs/pundit

Documentation: http://www.rubydoc.info/gems/pundit/1.0.1

Previously we have done the authorization in this way writing on the products_controller and views/products/

if @product.user == current_user

and then also implemented after that the same feature in this way

  def user_owns?(u)
    self.user == u

  def user_can_change?(u)

if @product.user_owns?(current_user)

What about pundit...

Continue reading →

how to start minitest - Ruby on Rails

From my previous post all the setup’s done for Rails, for minitest and Capybara.


I read those documentations and start testing, by generating test files, with following rails generate commands:

$ rails generate
  other generators omitted



I use these generators, I generate these test classes in which I put test methods. If I memorize all these class names, I will be able to do testing without using generators.

    Capybara::Rails::TestCase (test/features directory)

    ActiveSupport::TestCase (test/test_helper.rb)

Continue reading →

minitest setup - Ruby on Rails

I now do testing in Minitest. I came to think it’s better choice to start with minitest for testing Rails. And I thought it would make more sense when I stay out of Minitest::Spec.

my-experience-with-minitest-and-rspec by tenderlove

7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails by Brandon Hilkert

minitest-quick-reference by Matt Sears

Now I regenerate the setup process with

$ rails new setup_minitest --skip-test-unit
$ cd setup_minitest
$ rails -v
Rails 4.2.1
$ ruby -v
ruby 2.2.2

and then add minitest-rails gem (in test group; look http://bundler.io/groups.html)


 source 'https://rubygems.org'

+gem 'minitest-rails', group: :test

and go on generate “test” subdirectory

$ bundle install
$ bundle list minitest
$ bundle list minitest-rails

 now you have three more

Continue reading →

cordova setup

sudo npm install -g ionic cordova
sudo npm install -g ios-sim

Cordova.js will be generated when you build a specific platform (i.e. cordova build android)

ionic start cordovaApp tabs
cd cordovaApp

ionic platform add ios
ionic build ios
ionic emulate ios

Some quick tips:

  • cd into your project: $ cd cordovaApp
  • Setup this project to use Sass: ionic setup sass
  • Develop in the browser with live reload: ionic serve
  • Add a platform (ios or Android): ionic platform add ios [android]

Note: iOS development requires OS X currently. See the Android Platform Guide for full Android installation instructions:

 * Build your app: ionic build <PLATFORM>

 * Simulate your app: ionic emulate <PLATFORM>

 * Run your app on a device: ionic run <PLATFORM>

 * Package an app using Ionic package service: ionic package

Continue reading →

angular setup

$ npm install -g bower


$ npm cache clean
$ sudo npm install -g bower

download & install node over again


and update npm


$ sudo npm install npm -g

$ npm -v

$ node -v

now it’s working.

$ npm install -g bower

and, you don’t need bower in package.json dependencies.


$ bower install angular angular-resource angular-route angular-animate sass-bootstrap lodash jquery modernizr font-awesome --save
$ bower install angular-mocks --save-dev


 Install Karma:
$ npm install karma --save-dev

$ sudo npm install karma --save-dev

Agreeing to the Xcode/iOS license...

Continue reading →

ionic & cordova setup

install ionic

sudo npm install -g ionic cordova
sudo npm install -g ios-sim

npm dependency..

start a project

ionic start myApp blank
ionic start myApp tabs
ionic start myApp sidemenu
cd myApp


ionic platform add ios
ionic build ios
ionic emulate ios
ionic run ios

ionic run = ionic build && ionic emulate

Set up Android emulator. I was in:

os x Yosemite version 10.10.2

cordova version 4.2.0

android studio 1.0.1

Java SE Development Kit 7

set path:

 on `~/.zshrc` file (open a text editor)
export ANDROID_HOME="/Users/<user>/Library/Android/sdk/"
export ANDROID_TOOLS="/Users/<user>/Library/Android/sdk/tools"
export ANDROID_PLATFORM_TOOLS="/Users/<user>/Library/Android/sdk/platform-tools"

and then reopen terminal

install “android-19” from android SDK manager

$ android
 pick "SDK Platform Android 4.4.2,

Continue reading →

Git Merge your next Change

Sometimes when you want to merge changes made in another branch (in some remote) to your master branch. (or any branch supposed to be working)

When you trust and it works fine for sure, then it would be quite simple:

git merge  

And also we can use buttons on Github. But that’s kinda.. I wanted to make it “clear” manually. Again, when you trust this remote branch for sure:

git checkout master  
 go to your local master to begin  

git pull  
 make sure if anything in it up-to-date with remote master  

git pull origin feature_branch  
 and merge the branch you need from remote  

git push  
 and update remote master with your change  

git push origin :feature_branch  
 and remove the remote and..  

git branch -d feature_branch  
 local branch you don't need any longer  

but when you don’t trust, it’s not that you don’t trust but just in case you better test it before...

Continue reading →

rspec testing

rails new myapp –skip-test-unit

cd myapp

When you’re not going ‘minitest’ way, then you don’t need test directory. And





follow readme & documentation! For now (when I’m typing this) it says:

 in 'Gemfile' file
group :development, :test do
  gem 'rspec-rails', '~> 3.0'

group :test do
  gem 'capybara'

bundle install

rails generate rspec:install

 in 'spec/rails_helper.rb' file
require 'capybara/rails'

Now I start, and will get back to you.

View →