Skip to content

Commit

Permalink
848 - Merge OrganizationProfile with Organization (#870)
Browse files Browse the repository at this point in the history
* Migration to merge profile into organization and related seed file changes

* Updated Details in footer and sidebar from Organization Profile to Organization

* Updated model, controller, policy from OrganizationProfile to Organization

* Fix test cases after merging organization_profile with organization

* Migration to remove OrganizationProfile

* Modularize code

* Remove unwanted comments

* Migration changes for location assocation

* Seed file, association changes for location

* Remove not null contraint for organization_id as if conflicts with existing data

* Test cases changes for updated location association

* Fixes for location nested form as per association changes

* Migration to drop organization_profiles table

* Fix attahment deletion issue

* Lint fix

* Fixed organization attachment issue

* Lint fix
  • Loading branch information
sarvaiyanidhi authored Jul 24, 2024
1 parent da451fc commit 3047dc9
Show file tree
Hide file tree
Showing 35 changed files with 537 additions and 511 deletions.

This file was deleted.

40 changes: 40 additions & 0 deletions app/controllers/organizations/staff/organizations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Organizations
module Staff
class OrganizationsController < Organizations::BaseController
layout "dashboard"

before_action :set_organization, only: %i[edit update]

def edit
end

def update
if @organization.update(organization_params)
redirect_to edit_staff_organization_path, notice: t(".success")
else
render :edit, status: :unprocessable_entity
end
end

private

def organization_params
params.require(:organization).permit(
:phone_number,
:email,
:avatar,
:facebook_url,
:instagram_url,
:donation_url,
locations_attributes: %i[city_town country province_state]
)
end

def set_organization
@organization = Organization.first

authorize! @organization
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/concerns/authorizable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def permission?(name)
ADMIN_PERMISSIONS + %i[
activate_staff
invite_staff
manage_organization_profile
manage_organization
manage_custom_page
manage_staff
change_user_roles
Expand Down
25 changes: 17 additions & 8 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,26 @@
#
# Table name: locations
#
# id :bigint not null, primary key
# city_town :string
# country :string
# province_state :string
# zipcode :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# city_town :string
# country :string
# province_state :string
# zipcode :string
# created_at :datetime not null
# updated_at :datetime not null
# organization_id :bigint
#
# Indexes
#
# index_locations_on_organization_id (organization_id)
#
# Foreign Keys
#
# fk_rails_... (organization_id => organizations.id)
#
class Location < ApplicationRecord
acts_as_tenant(:organization)
has_one :adopter_foster_profile
has_one :organization_profile

validates :country, presence: true, length: {maximum: 50, message: "50 characters maximum"}
validates :city_town, presence: true, length: {maximum: 50, message: "50 characters maximum"}
Expand Down
39 changes: 31 additions & 8 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@
#
# Table name: organizations
#
# id :bigint not null, primary key
# name :string
# slug :string
# created_at :datetime not null
# updated_at :datetime not null
# id :bigint not null, primary key
# donation_url :text
# email :string
# facebook_url :text
# instagram_url :text
# name :string
# phone_number :string
# slug :string
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_organizations_on_slug (slug) UNIQUE
#
class Organization < ApplicationRecord
include Avatarable
include Phoneable

# Rolify resource
resourcify

Expand All @@ -25,9 +33,24 @@ class Organization < ApplicationRecord

has_many :form_answers, dependent: :destroy
has_many :people

has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true
has_one :location, through: :profile
has_one :form_submission, dependent: :destroy
has_one :custom_page, dependent: :destroy

has_many :locations
accepts_nested_attributes_for :locations

before_save :normalize_phone

validates :phone_number, phone: {possible: true, allow_blank: true}
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, allow_blank: true

validates :facebook_url, url: true, allow_blank: true
validates :instagram_url, url: true, allow_blank: true
validates :donation_url, url: true, allow_blank: true

private

def normalize_phone
self.phone_number = Phonelib.parse(phone_number).full_e164.presence
end
end
51 changes: 0 additions & 51 deletions app/models/organization_profile.rb

This file was deleted.

11 changes: 10 additions & 1 deletion app/policies/active_storage/attachment_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ def purge_avatar?
private

def organization
@organization || record.record.organization
@organization || record_organization
end

# `record` should be an ActiveStorage Attachment which responds to `#record`.
def record_organization
if record.record.is_a?(Organization)
record.record
else
record.record.organization
end
end
end
9 changes: 9 additions & 0 deletions app/policies/organizations/organization_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Organizations
class OrganizationPolicy < ApplicationPolicy
pre_check :verify_active_staff!

def manage?
permission?(:manage_organization)
end
end
end
8 changes: 0 additions & 8 deletions app/policies/organizations/organization_profile_policy.rb

This file was deleted.

33 changes: 16 additions & 17 deletions app/services/organizations/create_service.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# class to create a new location, organization, organization profile, user, and staff account with role admin
# class to create a new location, organization, user, and staff account with role admin
# email is sent to admin user if all steps are successful
# call with Organizations::CreateService.new.signal(args)
# sample args:
Expand All @@ -22,15 +22,15 @@
class Organizations::CreateService
def signal(args)
ActiveRecord::Base.transaction do
create_organization(
args[:organization][:name],
args[:organization][:slug]
)
create_location(
args[:location][:country],
args[:location][:city_town],
args[:location][:province_state]
)
create_organization_and_profile(
args[:organization][:name],
args[:organization][:slug]
)
create_user(
args[:user][:email],
args[:user][:first_name],
Expand All @@ -47,22 +47,21 @@ def signal(args)

private

def create_location(country, city_town, province_state)
@location = Location.create!(
country: country,
city_town: city_town,
province_state: province_state
def create_organization(name, slug)
@organization = Organization.create!(
name: name,
slug: slug
)
end

def create_organization_and_profile(name, slug)
@organization = Organization.create!(
name: name,
slug: slug,
profile: OrganizationProfile.new(
location_id: @location.id
def create_location(country, city_town, province_state)
ActsAsTenant.with_tenant(@organization) do
@location = Location.create!(
country: country,
city_town: city_town,
province_state: province_state
)
)
end
end

def create_user(email, first_name, last_name)
Expand Down
8 changes: 4 additions & 4 deletions app/views/layouts/dashboard/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<!-- Brand logo -->
<p class="navbar-brand flex-column">
<%= link_to home_index_path, class: "nav-link" do %>
<% if Current.organization.profile.avatar.attached? %>
<%= image_tag Current.organization.profile.avatar, alt: current_organization_name, title: current_organization_name, class: 'rounded-1' %>
<% if Current.organization.avatar.attached? %>
<%= image_tag Current.organization.avatar, alt: current_organization_name, title: current_organization_name, class: 'rounded-1' %>
<% else %>
<%= current_organization_name %>
<% end %>
Expand Down Expand Up @@ -68,11 +68,11 @@
<% end %>
</li>
<% end %>
<% if allowed_to?(:edit?, OrganizationProfile, namespace: Organizations,
<% if allowed_to?(:edit?, Organization, namespace: Organizations,
context: {organization: Current.organization})
%>
<li class="nav-item">
<%= active_link_to edit_staff_organization_profile_path, class: "nav-link" do %>
<%= active_link_to edit_staff_organization_path, class: "nav-link" do %>
<i class="nav-icon fe fe-settings me-2"></i>Settings
<% end %>
</li>
Expand Down
18 changes: 9 additions & 9 deletions app/views/layouts/shared/_footer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
<!-- list -->
<h3 class="fw-bold mb-3"><%= t('.company') %></h3>
<ul class="list-unstyled nav nav-footer flex-column nav-x-0">
<% if Current.tenant&.profile.facebook_url.present? || Current.tenant&.profile.instagram_url.present? %>
<% if Current.tenant&.facebook_url.present? || Current.tenant&.instagram_url.present? %>
<li class="d-flex gap-3">
<% if Current.tenant&.profile.facebook_url.present? %>
<%= link_to Current.tenant&.profile.facebook_url, class: 'nav-link', target: '_blank', aria: {label: "Find us on facebook" } do %>
<% if Current.tenant&.facebook_url.present? %>
<%= link_to Current.tenant&.facebook_url, class: 'nav-link', target: '_blank', aria: {label: "Find us on facebook" } do %>
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32"
fill="currentColor" class="bi bi-facebook"
viewBox="0 0 16 16">
Expand All @@ -35,8 +35,8 @@
</svg>
<% end %>
<% end %>
<% if Current.tenant&.profile.instagram_url.present? %>
<%= link_to Current.tenant&.profile.instagram_url, class: 'nav-link', target: '_blank', aria: {label: "Find us on instagram"} do %>
<% if Current.tenant&.instagram_url.present? %>
<%= link_to Current.tenant&.instagram_url, class: 'nav-link', target: '_blank', aria: {label: "Find us on instagram"} do %>
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32"
fill="currentColor" class="bi bi-instagram"
viewBox="0 0 16 16">
Expand Down Expand Up @@ -70,10 +70,10 @@
<!-- contact info -->
<div class="mb-4">
<h3 class="fw-bold mb-3">Get in touch</h3>
<p><%= Current.tenant.location.city_town %>, <%= Current.tenant.location.province_state %> <%= Current.tenant.location.country %></p>
<p class="mb-1">Email: <a href="mailto:<%= Current.tenant.profile.email %>"><%= Current.tenant.profile.email %></a></p>
<% if Current.tenant.profile.phone_number.present? %>
<p>Phone: <span class="text-dark fw-semibold"><%= Current.tenant.profile.formatted_phone_number %></span></p>
<p><%= Current.tenant.locations.last&.city_town %>, <%= Current.tenant.locations.last&.province_state %> <%= Current.tenant.locations.last&.country %></p>
<p class="mb-1">Email: <a href="mailto:<%= Current.tenant.email %>"><%= Current.tenant.email %></a></p>
<% if Current.tenant.phone_number.present? %>
<p>Phone: <span class="text-dark fw-semibold"><%= Current.tenant.formatted_phone_number %></span></p>
<% end %>
</div>
</div>
Expand Down
Loading

0 comments on commit 3047dc9

Please sign in to comment.