Browse Source

Add organisation model and user association

pull/114/head
baarkerlounger 4 years ago
parent
commit
cf3a0baafc
  1. 31
      app/admin/organisations.rb
  2. 3
      app/models/organisation.rb
  3. 2
      app/models/user.rb
  4. 18
      db/migrate/20211126140433_create_organisations.rb
  5. 15
      db/migrate/20211126142105_user_belongs_to_organisation.rb
  6. 20
      db/schema.rb
  7. 43
      spec/controllers/admin/organisations_controller_spec.rb
  8. 11
      spec/factories/organisation.rb
  9. 1
      spec/factories/user.rb
  10. 16
      spec/models/organisation_spec.rb
  11. 12
      spec/models/user_spec.rb

31
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

3
app/models/organisation.rb

@ -0,0 +1,3 @@
class Organisation < ApplicationRecord
has_many :users
end

2
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

18
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

15
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

20
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

43
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

11
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

1
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

16
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

12
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
Loading…
Cancel
Save