Skip to content

Commit

Permalink
Spike of user with many organisations (and a dropdown to choose an or…
Browse files Browse the repository at this point in the history
…ganisation)
  • Loading branch information
benshimmin committed Nov 27, 2024
1 parent ca99c9a commit 7f7500e
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ class ApplicationController < ActionController::Base
include Auth
include Ip

before_action -> {
@organisation_list = current_user.organisations

if session[:current_organisation]
Current.organisation = session[:current_organisation]
end
}, if: :user_signed_in?

private

def add_breadcrumb(name, path, options = {})
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/organisation_session_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class OrganisationSessionController < ApplicationController
def update
if params[:current_organisation]
if current_user.organisations.pluck(:id).include?(params[:current_organisation])
session[:current_organisation] = params[:current_organisation]
end
redirect_to current_user.service_owner? ? request.referer : root_path
end
end
end
3 changes: 3 additions & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Current < ActiveSupport::CurrentAttributes
attribute :organisation
end
1 change: 1 addition & 0 deletions app/models/organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Organisation < ApplicationRecord

strip_attributes only: [:iati_reference]
has_many :users
has_and_belongs_to_many :users
has_many :funds
has_many :reports
has_many :org_participations
Expand Down
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class User < ApplicationRecord
otp_secret_encryption_key: ENV["SECRET_KEY_BASE"]

belongs_to :organisation
has_and_belongs_to_many :organisations
has_many :historical_events
validates_presence_of :name, :email
validates :email, with: :email_cannot_be_changed_after_create, on: :update
Expand All @@ -18,6 +19,17 @@ class User < ApplicationRecord

delegate :service_owner?, :partner_organisation?, to: :organisation

def organisation
if Current.organisation
return Organisation.find(Current.organisation)
end
super
end

def primary_organisation
Organisation.find(self.organisation_id)
end

def active_for_authentication?
active
end
Expand Down
10 changes: 10 additions & 0 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@
= t("header.feedback.html")

= render 'layouts/messages'

- if authenticated? && current_user.primary_organisation.service_owner?
.govuk-grid-row
.govuk-grid-column-full{:class => "govuk-!-padding-top-4"}

=form_with url: organisation_session_path, method: :patch do |form|
=form.label "Available organisations", class: "govuk-label", for: "current_organisation"
=form.select :current_organisation, options_for_select(@organisation_list.pluck(:name, :id), Current.organisation), {}, class: "govuk-select"
=form.submit "Set organisation", class: "govuk-button", "data-module": "govuk-button"

= breadcrumb_tags

= yield
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
resources :users, except: [:index]
resources :activities, only: [:index]

patch "/organisation_session" => "organisation_session#update"

roles = %w[implementing_organisations partner_organisations matched_effort_providers external_income_providers]
constraints role: /#{roles.join("|")}/ do
get "organisations/(:role)", to: "organisations#index", defaults: {role: "partner_organisations"}, as: :organisations
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20241126225521_create_join_table_organisation_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateJoinTableOrganisationUser < ActiveRecord::Migration[6.1]
def change
create_join_table :organisations, :users, column_options: {type: :uuid} do |t|
t.index [:organisation_id, :user_id]
t.index [:user_id, :organisation_id]
end

User.all.each do |user|
user.organisations << user.organisation
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2024_01_15_183402) do
ActiveRecord::Schema.define(version: 2024_11_26_225521) do

# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
Expand Down Expand Up @@ -285,6 +285,13 @@
t.index ["iati_reference"], name: "index_organisations_on_iati_reference", unique: true
end

create_table "organisations_users", id: false, force: :cascade do |t|
t.uuid "organisation_id", null: false
t.uuid "user_id", null: false
t.index ["organisation_id", "user_id"], name: "index_organisations_users_on_organisation_id_and_user_id"
t.index ["user_id", "organisation_id"], name: "index_organisations_users_on_user_id_and_organisation_id"
end

create_table "outgoing_transfers", id: :uuid, default: -> { "public.gen_random_uuid()" }, force: :cascade do |t|
t.uuid "source_id", null: false
t.uuid "destination_id", null: false
Expand Down

0 comments on commit 7f7500e

Please sign in to comment.