Skip to content

Commit

Permalink
Add Consulate model
Browse files Browse the repository at this point in the history
  • Loading branch information
johncarney committed Jul 17, 2019
1 parent b94b28d commit a7bb709
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 4 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--require spec_helper
--format documentation
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ gem "bootsnap", ">= 1.1.0", require: false
group :development, :test do
# Call "byebug" anywhere in the code to stop execution and get a debugger console
gem "byebug", platforms: [:mri, :mingw, :x64_mingw]
gem "pry-byebug"
gem "rspec-rails"
end

Expand All @@ -58,6 +59,7 @@ group :test do
gem "selenium-webdriver"
# Easy installation and use of chromedriver to run system tests with Chrome
gem "chromedriver-helper"
gem "shoulda-matchers"
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
Expand Down
11 changes: 11 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ GEM
chromedriver-helper (2.1.1)
archive-zip (~> 0.10)
nokogiri (~> 1.8)
coderay (1.1.2)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
Expand Down Expand Up @@ -106,6 +107,12 @@ GEM
nokogiri (1.10.3)
mini_portile2 (~> 2.4.0)
pg (1.1.4)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-byebug (3.7.0)
byebug (~> 11.0)
pry (~> 0.10)
public_suffix (3.1.1)
puma (3.12.1)
rack (2.0.7)
Expand Down Expand Up @@ -173,6 +180,8 @@ GEM
selenium-webdriver (3.142.3)
childprocess (>= 0.5, < 2.0)
rubyzip (~> 1.2, >= 1.2.2)
shoulda-matchers (4.1.1)
activesupport (>= 4.2.0)
spring (2.1.0)
spring-commands-rspec (1.0.4)
spring (>= 0.9.1)
Expand Down Expand Up @@ -219,11 +228,13 @@ DEPENDENCIES
jbuilder (~> 2.5)
listen (>= 3.0.5, < 3.2)
pg (>= 0.18, < 2.0)
pry-byebug
puma (~> 3.11)
rails (~> 5.2.3)
rspec-rails
sass-rails (~> 5.0)
selenium-webdriver
shoulda-matchers
spring
spring-commands-rspec
spring-watcher-listen (~> 2.0.0)
Expand Down
4 changes: 4 additions & 0 deletions app/models/consulate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Consulate < ApplicationRecord
validates :code, presence: true
validates :name, presence: true, uniqueness: { scope: :code, case_sensitive: false }
end
14 changes: 14 additions & 0 deletions db/migrate/20190717204054_create_consulates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateConsulates < ActiveRecord::Migration[5.2]
def change
enable_extension :citext

create_table :consulates do |t|
t.citext :name, null: false
t.citext :code, null: false

t.timestamps

t.index %i[ code name ], unique: true
end
end
end
27 changes: 27 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_07_17_204054) do

# These are extensions that must be enabled in order to support this database
enable_extension "citext"
enable_extension "plpgsql"

create_table "consulates", force: :cascade do |t|
t.citext "name", null: false
t.citext "code", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["code", "name"], name: "index_consulates_on_code_and_name", unique: true
end

end
26 changes: 26 additions & 0 deletions spec/models/consulate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

RSpec.describe Consulate, type: :model do
it { is_expected.to validate_presence_of :name }
it { is_expected.to validate_presence_of :code }

describe "uniqueness validation" do
subject { described_class.create(code: "xxx", name: "Calgary") }

it { is_expected.to validate_uniqueness_of(:name).scoped_to(:code).case_insensitive }
end

describe "#name" do
it "is case-insensitive" do
consulate = described_class.create(code: "xxx", name: "Calgary")
expect(described_class.find_by(name: "calgary")).to eq consulate
end
end

describe "#code" do
it "is case-insensitive" do
consulate = described_class.create(code: "XXX", name: "Calgary")
expect(described_class.find_by(code: "xxx")).to eq consulate
end
end
end
18 changes: 14 additions & 4 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
require 'spec_helper'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require "spec_helper"

ENV["RAILS_ENV"] ||= "test"
# require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'rspec/rails'

require "rspec/rails"
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand All @@ -30,6 +32,7 @@
puts e.to_s.strip
exit 1
end

RSpec.configure do |config|
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
Expand Down Expand Up @@ -59,3 +62,10 @@
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")
end

Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end

0 comments on commit a7bb709

Please sign in to comment.