From cf3a0baafc2d2a427ae2fb1031439bbcee257cef Mon Sep 17 00:00:00 2001 From: baarkerlounger Date: Fri, 26 Nov 2021 17:36:54 +0000 Subject: [PATCH] Add organisation model and user association --- app/admin/organisations.rb | 31 +++++++++++++ app/models/organisation.rb | 3 ++ app/models/user.rb | 2 + .../20211126140433_create_organisations.rb | 18 ++++++++ ...1126142105_user_belongs_to_organisation.rb | 15 +++++++ db/schema.rb | 20 ++++++++- .../admin/organisations_controller_spec.rb | 43 +++++++++++++++++++ spec/factories/organisation.rb | 11 +++++ spec/factories/user.rb | 1 + spec/models/organisation_spec.rb | 16 +++++++ spec/models/user_spec.rb | 12 ++++++ 11 files changed, 170 insertions(+), 2 deletions(-) create mode 100644 app/admin/organisations.rb create mode 100644 app/models/organisation.rb create mode 100644 db/migrate/20211126140433_create_organisations.rb create mode 100644 db/migrate/20211126142105_user_belongs_to_organisation.rb create mode 100644 spec/controllers/admin/organisations_controller_spec.rb create mode 100644 spec/factories/organisation.rb create mode 100644 spec/models/organisation_spec.rb create mode 100644 spec/models/user_spec.rb diff --git a/app/admin/organisations.rb b/app/admin/organisations.rb new file mode 100644 index 000000000..bbf9efe90 --- /dev/null +++ b/app/admin/organisations.rb @@ -0,0 +1,31 @@ +ActiveAdmin.register Organisation do + permit_params do + permitted = %i[name + phone + org_type + address_line1 + address_line2 + postcode + local_authorities + holds_own_stock + other_stock_owners + managing_agents] + permitted + end + + index do + selectable_column + id_column + column :name + column :org_type + column "Address Line 1", :address_line1 + column "Address Line 2", :address_line2 + column :postcode + column "Phone Number", :phone + column :local_authorities + column :holds_own_stock + column :other_stock_owners + column :managing_agents + actions + end +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb new file mode 100644 index 000000000..8874d1e58 --- /dev/null +++ b/app/models/organisation.rb @@ -0,0 +1,3 @@ +class Organisation < ApplicationRecord + has_many :users +end diff --git a/app/models/user.rb b/app/models/user.rb index 0ef982f25..095f34753 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,4 +2,6 @@ class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :recoverable, :rememberable, :validatable + + belongs_to :organisation end diff --git a/db/migrate/20211126140433_create_organisations.rb b/db/migrate/20211126140433_create_organisations.rb new file mode 100644 index 000000000..515daa97d --- /dev/null +++ b/db/migrate/20211126140433_create_organisations.rb @@ -0,0 +1,18 @@ +class CreateOrganisations < ActiveRecord::Migration[6.1] + def change + create_table :organisations do |t| + t.string :name + t.integer :phone + t.integer :org_type + t.string :address_line1 + t.string :address_line2 + t.string :postcode + t.string :local_authorities + t.boolean :holds_own_stock + t.string :other_stock_owners + t.string :managing_agents + + t.timestamps + end + end +end diff --git a/db/migrate/20211126142105_user_belongs_to_organisation.rb b/db/migrate/20211126142105_user_belongs_to_organisation.rb new file mode 100644 index 000000000..38bcb162a --- /dev/null +++ b/db/migrate/20211126142105_user_belongs_to_organisation.rb @@ -0,0 +1,15 @@ +class UserBelongsToOrganisation < ActiveRecord::Migration[6.1] + def up + change_table :users, bulk: true do |t| + t.remove :organisation + t.belongs_to :organisation + end + end + + def down + change_table :users, bulk: true do |t| + t.remove :organisation_id + t.column :organisation, :string + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 1f7ec27d6..16cae1241 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_11_25_114400) do +ActiveRecord::Schema.define(version: 2021_11_26_142105) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -169,6 +169,21 @@ ActiveRecord::Schema.define(version: 2021_11_25_114400) do t.index ["discarded_at"], name: "index_case_logs_on_discarded_at" end + create_table "organisations", force: :cascade do |t| + t.string "name" + t.integer "phone" + t.integer "org_type" + t.string "address_line1" + t.string "address_line2" + t.string "postcode" + t.string "local_authorities" + t.boolean "holds_own_stock" + t.string "other_stock_owners" + t.string "managing_agents" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false @@ -179,8 +194,9 @@ ActiveRecord::Schema.define(version: 2021_11_25_114400) do t.datetime "updated_at", precision: 6, null: false t.string "name" t.string "role" - t.string "organisation" + t.bigint "organisation_id" t.index ["email"], name: "index_users_on_email", unique: true + t.index ["organisation_id"], name: "index_users_on_organisation_id" t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end diff --git a/spec/controllers/admin/organisations_controller_spec.rb b/spec/controllers/admin/organisations_controller_spec.rb new file mode 100644 index 000000000..276742898 --- /dev/null +++ b/spec/controllers/admin/organisations_controller_spec.rb @@ -0,0 +1,43 @@ +require "rails_helper" +require_relative "../../support/devise" + +describe Admin::OrganisationsController, type: :controller do + render_views + let(:page) { Capybara::Node::Simple.new(response.body) } + let(:resource_title) { "Organisations" } + let(:valid_session) { {} } + let!(:organisation) { FactoryBot.create(:organisation) } + login_admin_user + + describe "Organisations" do + before do + get :index, session: valid_session + end + + it "returns a table of admin users" do + expect(page).to have_content(resource_title) + expect(page).to have_table("index_table_organisations") + expect(page).to have_link(organisation.id.to_s) + end + end + + describe "Create admin users" do + let(:params) { { organisation: { name: "DLUHC" } } } + + it "creates a organisation" do + expect { post :create, session: valid_session, params: params }.to change(Organisation, :count).by(1) + end + end + + describe "Update organisation" do + before do + get :edit, session: valid_session, params: { id: organisation.id } + end + + it "creates a new admin users" do + expect(page).to have_field("organisation_name") + expect(page).to have_field("organisation_org_type") + expect(page).to have_field("organisation_phone") + end + end +end diff --git a/spec/factories/organisation.rb b/spec/factories/organisation.rb new file mode 100644 index 000000000..91e4179e7 --- /dev/null +++ b/spec/factories/organisation.rb @@ -0,0 +1,11 @@ +FactoryBot.define do + factory :organisation do + name { "DLUHC" } + org_type { 1 } + address_line1 { "2 Marsham Street" } + address_line2 { "London" } + postcode { "SW1P 4DF" } + created_at { Time.zone.now } + updated_at { Time.zone.now } + end +end diff --git a/spec/factories/user.rb b/spec/factories/user.rb index c9f427a98..6509c2457 100644 --- a/spec/factories/user.rb +++ b/spec/factories/user.rb @@ -2,6 +2,7 @@ FactoryBot.define do factory :user do sequence(:email) { |i| "test#{i}@example.com" } password { "pAssword1" } + organisation created_at { Time.zone.now } updated_at { Time.zone.now } end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb new file mode 100644 index 000000000..b6414ac8e --- /dev/null +++ b/spec/models/organisation_spec.rb @@ -0,0 +1,16 @@ +require "rails_helper" + +RSpec.describe Organisation, type: :model do + describe "#new" do + let!(:user) { FactoryBot.create(:user) } + let(:organisation) { Organisation.first } + + it "has expected fields" do + expect(organisation.attribute_names).to include("name", "phone", "org_type") + end + + it "has users" do + expect(organisation.users.first).to eq(user) + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb new file mode 100644 index 000000000..999931a6c --- /dev/null +++ b/spec/models/user_spec.rb @@ -0,0 +1,12 @@ +require "rails_helper" + +RSpec.describe User, type: :model do + describe "#new" do + let(:user) { FactoryBot.create(:user) } + let(:organisation) { Organisation.first } + + it "belongs to an organisation" do + expect(user.organisation).to eq(organisation) + end + end +end