Rails - Part1 - Employee CRUD API - Create a table , model and migration in Rails

Rails is a convention over configuration framework, Rails4 makes life simpler on how you can quickly put things in perspective.

In this Series, we will start with a Employee Model, Create its corresponding tables, migrations and then expose the Employee Model in the Rails API.

https://github.com/makrand-bkar/emp-hello

Part1 - We will create the Model, its table and corresponding Factory and test API.

Problem Statement

Lets say we want to create a Simple Employee  table

Employee {
  first_name : string,
  last_name :  string,
  icon          :  string,
  emp_no    :  integer
}

Assumptions
I am assuming that you already have rails project running and we will just adding to the existing product.

You can clone the initial project from
https://github.com/makrand-bkar/emp-hello

Step1 : Create a Model for Employee

RAILS_ENV=development rails g model Employee first_name:string last_name:string icon:string emp_id:integer


Step1 will create the model, table, rspec and factory

Step2 :
RAILS_ENV=development rake db:migrate


This will create the tables in development env and corresponding entry in schema.rb

Step3.
Now lets go ahead and ensure the factory is right, update default factory creation with sequence and timestamps

FactoryGirl.define do
  factory :employee do
    sequence(:first_name) { |n| "first_name#{n}" }
    sequence(:last_name)  { |n| "last_name#{n}" }
    sequence(:emp_id) { SecureRandom.uuid }  # this must be < 40
    icon nil
    created_at { 1.hour.ago }
    updated_at { 1.hour.ago }
  end
end


Step4. Test it in rails console
trials2/emp-hello - [master●] » RAILS_ENV=development rails c
Loading development environment (Rails 4.1.16)
2.3.5 :001 >  FactoryGirl.create(:employee) 
   (0.0ms)  begin transaction
  SQL (1.1ms)  INSERT INTO "employees" ("created_at", "emp_id", "first_name", "last_name", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", "2018-03-19 20:38:26.802950"], ["emp_id", 0], ["first_name", "first_name1"], ["last_name", "last_name1"], ["updated_at", "2018-03-19 20:38:26.803013"]]
   (0.6ms)  commit transaction
 => #<Employee id: 1, first_name: "first_name1", last_name: "last_name1", icon: nil, emp_id: 0, created_at: "2018-03-19 20:38:26", updated_at: "2018-03-19 20:38:26"> 


Everything looks good, now lets create some rspecs for the Employee

Step5. employee_spec.rb

require 'spec_helper'

describe 'Employee' do
  let(:employee) { FactoryGirl.create(:employee) }

  context 'factory' do
    it 'is a Employee' do
      employee.should be_a Employee
    end
  end

  describe '#destroy' do
    let!(:employee_id) { employee.id }

    context 'after delete' do
      let!(:destroy) { employee.destroy }
      it 'has been removed' do
        Employee.find_by_id(employee).should be_nil
      end
    end
  end
end

Run the spec
RAILS_ENV=test rspec ./spec/models/employee_spec.rb

Comments

Popular posts from this blog

Apache Airflow Wait Between Tasks

Java J2EE Security Considerations

Java Spring Interview Questions