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?

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:

object_name.method_name(parameters)
object_name.method_name

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 ->
  $('task_done_at').datepicker
    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

and...

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

https://rubygems.org/gems/pundit

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/

 app/controllers/products_controller.rb
if @product.user == current_user

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

 app/models/product.rb
  def user_owns?(u)
    self.user == u
  end

  def user_can_change?(u)
    self.user_owns?(u)
  end

 app/controllers/products_controller.rb
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.

http://www.rubydoc.info/github/jnicklas/capybara
https://github.com/blowmage/minitest-rails
https://github.com/blowmage/minitest-rails-capybara

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

$ rails generate
Rails:
  other generators omitted
  integration_test

Minitest:
  minitest:feature
  minitest:install

TestUnit:
  test_unit:controller
  test_unit:integration
  test_unit:model
  test_unit:scaffold

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.

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

  minitest:install
    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)

https://github.com/blowmage/minitest-rails

 Gemfile 
 source 'https://rubygems.org'

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

and go on generate “test” subdirectory

$ bundle install
$ bundle list minitest
gems/minitest-5.6.1
$ bundle list minitest-rails
gems/minitest-rails-2.1.1

 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:
https://cordova.apache.org/docs/en/edge/guide_platforms_android_index.md.html

 * 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

http://bower.io/install-bower

$ npm cache clean
$ sudo npm install -g bower

download & install node over again
https://github.com/joyent/node/wiki/installing-node.js-via-package-managerosx

https://github.com/npm/npm/issues/5470

and update npm

https://docs.npmjs.com/getting-started/installing-node

$ sudo npm install npm -g

$ npm -v
2.4.1

$ node -v
v0.10.36

now it’s working.

$ npm install -g bower

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

http://bower.io/docs/creating-packages/

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

http://karma-runner.github.io/0.12/intro/installation.html

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

$ sudo npm install karma --save-dev

error:
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

run

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"
PATH=$PATH:$ANDROID_HOME:$ANDROID_TOOLS:$ANDROID_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

https://github.com/rspec/rspec-rails

https://relishapp.com/rspec/rspec-rails/docs

https://relishapp.com/rspec/rspec-rails/docs/generators

https://github.com/jnicklas/capybara

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

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

group :test do
  gem 'capybara'
end

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 →