Call Coffee Script function in Rails view

In Rails view:

<input class="my-class" type="button" value="Act!" onclick="doThisAction(<%= "'#{my_params}'" %>)" />

In Coffee Script:

@doThisAction = (params) ->
  console.log(params);
  ...

Please remember the @ sign in Coffee Script. This allow Rails view can read the function. Otherwise, you will get undefined function

RSpec Custom Devise API

Rails developers may quite familiar with Devise and RSpec. In a recent application, I need to custom Devise controllers. And of course, I need to write RSpec these. In this blog post, I will tell about RSpec custom Devise Request.
First, because I wanna create custom Devise, I need to set custom routes, like this:

  devise_for :users, :controllers => { :registrations => "users/registrations", :sessions => "users/sessions"}

And this is my custom devise for being able to response with Json request:

  # users/sessions_controller.rb
  # ofcourse, require no authentication for this
  def create
    build_resource
    resource = User.find_for_database_authentication(:email=>params[:user][:email])

    return invalid_login_attempt unless resource

    if resource.valid_password?(params[:user][:password])
      sign_in("user", resource)
      render :json=> {:success=>true, :user => resource}
      return
    end
    invalid_login_attempt
  end

Ok, I have an api for custom login via: POST: 'users/sign_in'

And this is my RSpec:

# spec/request/sessions_requests_spec.rb
require "rails_helper"

RSpec.describe "Post #create", type: :request do
  before(:all) do
    User.create(
        email: "test@example.com",
        password: 'password',
        password_confirmation: 'password',
    )
  end

  it "responds successfully with an HTTP 201 status code" do
    user_params = {user: { email: "test@example.com", password: "password"}}.to_json
    post "users/sign_in", user_params, request_headers

    expect(response).to be_success
    expect(response).to have_http_status(200)

    expect(json).to be_has_key("user")
    expect(json["user"]).to be_has_key("authentication_token")
  end

  it "responds unsuccessfully with an HTTP 401 status code" do
    user_params = {user: { email: "test@example.com", password: "wrong password"}}.to_json
    post "users/sign_in", user_params, request_headers

    expect(response).not_to be_success
    expect(response).to have_http_status(401)

    expect(json["message"]).to eql("Error with your login or password")
  end
end

In this RSpec, I used two helpers variables, json and request_headers.
I created them in a support modules for DRY spec.

# spec/supports/request_helpers.rb
module Requests
  module JsonHelpers
    def json
      @json ||= JSON.parse(response.body)
    end

    def request_headers
      {
          "Accept" => "application/json",
          "Content-Type" => "application/json"
      }
    end
  end
end

And add these helpers in configure file:

# spec/spec_helper.rb
require 'supports/request_helpers'
...
config.include Requests::JsonHelpers, type: :request

Git: Ignore files

 

When using Git to mange source code, it is very useful and usually I use .gitignore file to mange which files and folders that should not be push to Github repository.

.gitignore file is something like this:

# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal

# Ignore all logfiles and tempfiles.
/log/*.log
/tmp

/.idea


Continue reading Git: Ignore files

CentOS Basic Usage

After a long time using Ubuntu, I find Ubuntu interesting, easy to use, and convenient to programming Ruby on Rails. However, talking about Linux, I think CentOS is also the big candidate that should be mentioned and tried. Today, I want to summary some basic usages relating to CentOS only before starting to use it. I will update this post continuously during my usage of CentOS.

  • check your CentOS version

    cat /etc/redhat-release
    
  • Update repository

    yum update
    
  • List package name

    yum list 
    
  • Install package by console

    yum install 
    
  • Remove package

    yum remmove 
    
  • Know which version of application installed

    yum list installed | grep name_of_app
    
  • Install Ruby on CentOS via RVM

    yum groupinstall -y development
    
    curl -L get.rvm.io | bash -s stable
    source /etc/profile.d/rvm.sh
    
    rvm reload
    rvm install 2.1.0 
    
    ruby --version
    rvm list rubies
    rvm use 2.1.0 --default
    
    

TDD Practices with RSpec in Creating Web Service

TDD process
TDD process

 

I. TDD

I think that many of you have heard about TDD, a methodology which is becoming more famous and popular today. Although you may appreciate the process of software development, you may not have opportunity to practice TDD. This article will help you to do that. In this post, I want to give you an overview of TDD, and a tool called RSpec for writing automation testing script in Ruby.

TDD stands for ‘Test-driven development’, a software development process in which we repeat the cycle: Write test first → Code → Refactoring through the life time of project. As you can see from the above picture, first create test script, run the scripts and you should see the ‘red’ results. Then you write code to make the tests pass and you should see ‘green’ results. Finally, after passing the tests, you should refactoring your code to make it cleaner but remember keeping the ‘green’ results. That’s the reason you would be familiar with the ‘Red – Green – Refactoring’ term when talking about TDD.

Continue reading TDD Practices with RSpec in Creating Web Service