Kang-Kyu Lee

Ruby + Rails codewriter

Page 2


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 pundi...

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
...

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-manager#osx

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 →


setup vagrant Ubuntu 12.04 LTS

when built-in vagrant and built-in virtualbox are outdated -
And we must use system ruby but its version is 1.8 and outdated -

upgrade ruby

sudo apt-get install ruby1.9.3
sudo apt-get autoremove
sudo apt-get remove ruby1.8

upgrade virtualbox & vagrant

sudo apt-get remove virtualbox
sudo apt-get remove vagrant

and do not install them from Ubuntu Software Center, download from website most recent version of them.

such as,

sudo dpkg -i vagrant_1.6.5_x86_64.deb
sudo dpkg -i virtualbox-4.3_4.3.20-96996-Ubuntu-precise_i386.debdpkg

and then, follow regular guides and vagrant up and vagrant provision as needed.

View →


local time in Rails app

This is what I found:

when I did Time.now in Ruby, it returns my local time (my system time, I should say).
when I did Time.zone.now or Time.now.in_time_zone in any Rails file it shows the time as set it up on config/application.rb:

config.time_zone = 'Central Time (US & Canada)'

..and such friendly names here you can list up when rake time:zones:all on command line.

http://www.ruby-doc.org/core-2.1.5/Time.html
http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html

Anyway, when a user access my app it doesn’t show their local time, but the time as set up on config/application.rb. And for local use (rails server) it’s enough to call Time.now for our current time, but this is quite meaningless because when you deploy on heroku, its system uses UTC time.

And fyi Rails saves it as UTC time in the database also… even though it returns local time as it has on config file.

You...

Continue reading →