Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/controllers/affiliations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class AffiliationsController < ApplicationController
before_action :set_affiliation, only: %i[ destroy ]

def destroy
authorize! @affiliation, to: :destroy?
affiliation = Affiliation.find(params[:id])
person = affiliation.person
destroyed = affiliation.destroy

if destroyed
flash.now[:notice] = "Person has been removed from the organization."
else
flash.now[:alert] = "Unable to remove affiliation. Please contact AWBW."
end

respond_to do |format|
format.turbo_stream do
if destroyed
render turbo_stream: turbo_stream.remove("affiliation_#{affiliation.id}")
else
render turbo_stream: turbo_stream.replace("flash_now", partial: "shared/flash_messages"),
status: :unprocessable_entity
end
end
format.html do
redirect_to generate_facilitator_user_path(person.user)
end
end
end

private

def set_affiliation
@affiliation = Affiliation.find(params[:id])
end
end
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ def flush_lifecycle_events

def preload_current_user_associations
return unless current_user&.person
@current_user_active_org_people = current_user.person
.organization_people
@current_user_active_affiliations = current_user.person
.affiliations
.active
.includes(:organization)
.load
Expand Down
36 changes: 0 additions & 36 deletions app/controllers/organization_people_controller.rb

This file was deleted.

26 changes: 13 additions & 13 deletions app/controllers/organizations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ def index
base_scope = authorized_scope(Organization.includes(:windows_type, :organization_status, :sectors, :addresses, logo_attachment: :blob))
filtered = base_scope.search_by_params(params).order(:name)
@organizations_count = filtered.count
@active_people_count = OrganizationPerson.active.where(organization_id: filtered.select(:id)).count("DISTINCT person_id, organization_id")
@active_people_count = Affiliation.active.where(organization_id: filtered.select(:id)).count("DISTINCT person_id, organization_id")
@organizations = filtered.paginate(page: params[:page], per_page: per_page)
org_ids = @organizations.map(&:id)
@affiliated_since = OrganizationPerson.where(organization_id: org_ids)
@affiliated_since = Affiliation.where(organization_id: org_ids)
.group(:organization_id)
.minimum(:start_date)
@active_people_counts = OrganizationPerson.active
@active_people_counts = Affiliation.active
.where(organization_id: org_ids)
.group(:organization_id)
.distinct
Expand Down Expand Up @@ -101,16 +101,16 @@ def set_form_variables
.map { |fn, ln, id| [ "#{fn} #{ln}", id ] }

if @organization.persisted? && @organization.errors.empty?
org_people = @organization.organization_people
org_people = org_people.includes(:person) unless org_people.loaded?
sorted = org_people.to_a
.sort_by { |op|
expired = op.inactive? || (op.end_date.present? && op.end_date < Date.current)
affiliations = @organization.affiliations
affiliations = affiliations.includes(:person) unless affiliations.loaded?
sorted = affiliations.to_a
.sort_by { |affiliation|
expired = affiliation.inactive? || (affiliation.end_date.present? && affiliation.end_date < Date.current)
[ expired ? 1 : 0,
op.person&.first_name.to_s.downcase,
op.person&.last_name.to_s.downcase ]
affiliation.person&.first_name.to_s.downcase,
affiliation.person&.last_name.to_s.downcase ]
}
@organization.organization_people.proxy_association.target.replace(sorted)
@organization.affiliations.proxy_association.target.replace(sorted)
end
end

Expand All @@ -137,7 +137,7 @@ def set_organization
@organization = Organization.includes(
:organization_status, :windows_type, :addresses,
{ sectorable_items: :sector },
organization_people: :person
affiliations: :person
).find(params[:id])
end

Expand All @@ -155,7 +155,7 @@ def organization_params
:sector_id,
:_destroy
],
organization_people_attributes: [
affiliations_attributes: [
:id,
:person_id,
:inactive,
Expand Down
29 changes: 14 additions & 15 deletions app/controllers/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
:avatar_attachment,
:user,
sectorable_items: :sector,
organization_people: :organization
affiliations: :organization
).references(:user))
filtered = base_scope.search_by_params(params.to_unsafe_h)
.order(:first_name, :last_name)
Expand Down Expand Up @@ -57,9 +57,9 @@ def show
when "workshop_variation_ideas"
@workshop_variation_ideas = @person.user&.workshop_variation_ideas_creator&.order(created_at: :desc)&.paginate(page: params[:page], per_page: per_page) || []
render partial: "people/sections/workshop_variation_ideas", locals: { person: @person, workshop_variation_ideas: @workshop_variation_ideas }
when "organization_people"
@organization_people = @person.organization_people.active.includes(organization: :logo_attachment).paginate(page: params[:page], per_page: per_page)
render partial: "people/sections/organization_people", locals: { person: @person, organization_people: @organization_people }
when "affiliations"
@affiliations = @person.affiliations.active.includes(organization: :logo_attachment).paginate(page: params[:page], per_page: per_page)
render partial: "people/sections/affiliations", locals: { person: @person, affiliations: @affiliations }
end
end
end
Expand All @@ -79,7 +79,7 @@ def edit
{ avatar_attachment: :blob },
{ comments: [ :created_by, :updated_by ] },
{ sectorable_items: :sector },
organization_people: { organization: :logo_attachment }
affiliations: { organization: :logo_attachment }
).find(params[:id]).decorate
authorize! @person
set_form_variables
Expand Down Expand Up @@ -175,18 +175,17 @@ def set_form_variables
set_user
# @person.build_user if @person.user.blank? # Build a fresh one if missing
if @person.persisted? && @person.errors.empty?
org_people = @person.organization_people
org_people = org_people.includes(:organization) unless org_people.loaded?
sorted = org_people.to_a
.sort_by { |op|
expired = op.inactive? || (op.end_date.present? && op.end_date < Date.current)
affiliations = @person.affiliations
affiliations = affiliations.includes(:organization) unless affiliations.loaded?
sorted = affiliations.to_a
.sort_by { |affiliation|
expired = affiliation.inactive? || (affiliation.end_date.present? && affiliation.end_date < Date.current)
[ expired ? 1 : 0,
op.start_date || Date.new(9999),
op.organization&.name.to_s.downcase ]
affiliation.organization&.name.to_s.downcase ]
}
@person.organization_people.proxy_association.target.replace(sorted)
@person.affiliations.proxy_association.target.replace(sorted)
end
@person.organization_people.build if @person.organization_people.empty?
@person.affiliations.build if @person.affiliations.empty?

@all_sectors = Sector.published.order(:name)
@sectors_collection = @all_sectors.pluck(:name, :id)
Expand Down Expand Up @@ -372,7 +371,7 @@ def person_params
:zip2,
:notes
],
organization_people_attributes: [
affiliations_attributes: [
:id,
:organization_id,
:position,
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ def set_person

def set_form_variables
set_person
@user.person.organization_people.first || @user.person.organization_people.build if @user.person
@user.person.affiliations.first || @user.person.affiliations.build if @user.person
organizations = authorized_scope(Organization.all)
@organizations_array = organizations.order(:name).pluck(:name, :id)
end
Expand Down Expand Up @@ -333,8 +333,8 @@ def user_params
:phone, :phone2, :phone3, :birthday, :best_time_to_call, :notes, # legacy to remove later
#####

organization_people_attributes: [ :id, :organization_id, :position, :title, :inactive, :primary_contact, :start_date, :end_date, :_destroy ],
comments_attributes: [ :id, :body ],
affiliations_attributes: [ :id, :organization_id, :position, :title, :inactive, :primary_contact, :start_date, :end_date, :_destroy ],
)
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class OrganizationPersonDecorator < ApplicationDecorator
class AffiliationDecorator < ApplicationDecorator
def detail(length: nil)
"#{person.full_name}: #{title.presence || position} - #{organization.name}"
end
Expand Down
2 changes: 1 addition & 1 deletion app/decorators/organization_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def title
end

def badges
earliest = organization_people.minimum(:start_date) || start_date
earliest = affiliations.minimum(:start_date) || start_date
years = earliest ? (Time.zone.now.year - earliest.year) : nil
badges = []
badges << badge("Legacy Organization (10+ years)", :legacy_facilitator) if years && years >= 10
Expand Down
4 changes: 2 additions & 2 deletions app/decorators/person_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ def title
end

def detail(length: nil)
text = organization_people.active.map { |op| "#{op.title.presence || op.position}, #{op.organization.name}" }.join(", ")
text = affiliations.active.map { |affiliation| "#{affiliation.title.presence || affiliation.position}, #{affiliation.organization.name}" }.join(", ")
length ? text&.truncate(length) : text
end

Expand All @@ -30,7 +30,7 @@ def member_since_year
end

def badges
earliest = organization_people.minimum(:start_date) || member_since
earliest = affiliations.minimum(:start_date) || member_since
years = earliest ? (Time.zone.now.year - earliest.year) : nil
badges = []
badges << badge("Legacy Facilitator (10+ years)", :legacy_facilitator) if years && years >= 10
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class OrganizationPerson < ApplicationRecord
class Affiliation < ApplicationRecord
belongs_to :organization
belongs_to :person, touch: true

Expand Down Expand Up @@ -28,7 +28,7 @@ def name
private

def skip_if_duplicate
scope = OrganizationPerson.where(
scope = Affiliation.where(
organization_id: organization_id,
person_id: person_id,
start_date: start_date,
Expand All @@ -49,7 +49,7 @@ def set_inactive_from_dates

def sync_organization_affiliation_dates
org = organization
affiliations = org.organization_people.where.not(id: destroyed_by_association ? id : nil)
affiliations = org.affiliations.where.not(id: destroyed_by_association ? id : nil)

earliest_start = affiliations.minimum(:start_date)
has_active = affiliations.active.exists?
Expand All @@ -67,7 +67,7 @@ def sync_organization_affiliation_dates
end

def deactivate_organization_if_no_active_people
return if organization.organization_people.active.exists?
return if organization.affiliations.active.exists?

inactive_status = OrganizationStatus.find_by(name: "Inactive")
return unless inactive_status
Expand All @@ -81,7 +81,7 @@ def deactivate_organization_if_no_active_people
resource_id: organization.id,
resource_title: organization.name,
change: "status_set_to_inactive",
reason: "no_active_organization_people"
reason: "no_active_affiliations"
)
end
end
10 changes: 5 additions & 5 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Organization < ApplicationRecord
belongs_to :windows_type, optional: true
has_many :addresses, as: :addressable, dependent: :destroy
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
has_many :organization_people, dependent: :restrict_with_error
has_many :people, through: :organization_people
has_many :affiliations, dependent: :restrict_with_error
has_many :people, through: :affiliations
has_many :users, through: :people
has_many :reports
has_many :workshop_logs
Expand Down Expand Up @@ -35,15 +35,15 @@ class Organization < ApplicationRecord
validates :organization_status_id, presence: true
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: "must be a valid email address" }, allow_blank: true
validates :website_url, format: { with: /\Ahttps?:\/\/\S+\z/i, message: "must start with http:// or https://" }, allow_blank: true
validate :affiliation_dates_locked, if: -> { organization_people.any? && !Current.user&.super_user? }
validate :affiliation_dates_locked, if: -> { affiliations.any? && !Current.user&.super_user? }

# Nested attributes
accepts_nested_attributes_for :addresses, allow_destroy: true,
reject_if: proc { |attrs| attrs.slice("locality", "city", "state", "street_address", "zip_code").values.all?(&:blank?) }
accepts_nested_attributes_for :sectorable_items, allow_destroy: true,
reject_if: proc { |attrs| attrs["sector_id"].blank? }
after_save :remove_duplicate_sectorable_items
accepts_nested_attributes_for :organization_people, allow_destroy: true,
accepts_nested_attributes_for :affiliations, allow_destroy: true,
reject_if: proc { |attrs| attrs["person_id"].blank? }

# SearchCop
Expand Down Expand Up @@ -149,7 +149,7 @@ def affiliation_dates_locked
end

def leader
organization_people.find_by(position: 2)
affiliations.find_by(position: 2)
end

def remove_duplicate_sectorable_items
Expand Down
Loading