Skip to content

Commit

Permalink
213 an organization can edit update their profile (#248)
Browse files Browse the repository at this point in the history
* Add migration and new schema

* Apply changes to models and factories, add tests for it

* Add route, controller and views for new form

* Add required: true, add tests for new models

* Fix wrong named variable on new controller

* Update seeds

* Run standard linter

* Fix db seeds

* Change new controllers inheritance, to have full usage of context, add layout instead of verifying staff

* Add null false, and foreign_key true to make code more robust
  • Loading branch information
yuricarvalhop authored Oct 10, 2023
1 parent 740bbae commit e18286c
Show file tree
Hide file tree
Showing 18 changed files with 271 additions and 4 deletions.
29 changes: 29 additions & 0 deletions app/controllers/organizations/organization_profiles_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Organizations::OrganizationProfilesController < Organizations::BaseController
layout "dashboard"

def edit
@organization_profile = Current.organization.profile
end

def update
@organization_profile = Current.organization.profile

respond_to do |format|
if @organization_profile.update(organization_profile_params)
format.html { redirect_to dashboard_index_path, notice: "Profile updated" }
else
format.html { render :edit, status: :unprocessable_entity }
end
end
end

private

def organization_profile_params
params.require(:organization_profile).permit(
:phone_number,
:email,
location_attributes: %i[city_town country province_state id]
)
end
end
2 changes: 2 additions & 0 deletions app/models/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#
class Location < ApplicationRecord
has_one :adopter_profile
has_one :organization_profile

geocoded_by :address
after_validation :geocode

Expand Down
4 changes: 4 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
#
class Organization < ApplicationRecord
resourcify # rolify

has_many :staff_accounts
has_many :users, through: :staff_accounts
has_many :pets

has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true
has_one :location, through: :profile
end
39 changes: 39 additions & 0 deletions app/models/organization_profile.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# == Schema Information
#
# Table name: organization_profiles
#
# id :bigint not null, primary key
# email :string
# phone_number :string
# created_at :datetime not null
# updated_at :datetime not null
# location_id :bigint not null
# organization_id :bigint not null
#
# Indexes
#
# index_organization_profiles_on_location_id (location_id)
# index_organization_profiles_on_organization_id (organization_id)
#
# Foreign Keys
#
# fk_rails_... (location_id => locations.id)
# fk_rails_... (organization_id => organizations.id)
#
class OrganizationProfile < ApplicationRecord
belongs_to :location
belongs_to :organization

accepts_nested_attributes_for :location
validates_associated :location

before_save :normalize_phone

validates :phone_number, phone: {possible: true}

private

def normalize_phone
self.phone_number = Phonelib.parse(phone_number).full_e164.presence
end
end
5 changes: 5 additions & 0 deletions app/views/layouts/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@
<i class="fe fe-settings me-2"></i> Account Settings
<% end %>
</li>
<li>
<%= link_to edit_organization_profile_path, class: 'dropdown-item' do %>
<i class="fe fe-user me-2"></i> Profile
<% end %>
</li>
<li>
<%= button_to destroy_user_session_path, method: :delete, class: 'dropdown-item', form: {data: {turbo: false}} do %>
<i class= 'fe fe-power me-2'></i> Sign Out
Expand Down
2 changes: 1 addition & 1 deletion app/views/organizations/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<div class="row d-flex align-items-center">
<div class="col-xxl-5 col-xl-6 col-lg-6 col-12">
<div>
<h1 class="display-2 fw-bold mb-3">Rescue pets near you at <u class="text-warning"><span class="text-primary"> <%= Current.organization.name %></span></u></h1>
<h1 class="display-2 fw-bold mb-3">Rescue pets in <%= Current.organization.location.city_town %> at <u class="text-warning"><span class="text-primary"> <%= Current.organization.name %></span></u></h1>
<p class="lead mb-4">Rescue a really cute pet and be a hero</p>
<ul class="list-unstyled mb-5">
<li class="mb-2">
Expand Down
76 changes: 76 additions & 0 deletions app/views/organizations/organization_profiles/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<%= form_with model: profile, :url => organization_profile_path do |form| %>

<% if profile.errors.count > 0 %>
<div class="alert alert-danger mt-1" role="alert">
<p>
<%= t '.please_fix_the_errors' %>
</p>
</div>
<% end %>

<!--Administrative details section-->
<div class='card p-5 mb-5'>
<h3>Contact</h3>

<div class="form-group">
<%= form.label :phone_number, class: 'bigger' %>
<%= form.telephone_field :phone_number,
autofocus: true,
placeholder: "10 digit number",
class: 'form-control' %>

<% profile.errors.full_messages_for(:phone_number).each do |message| %>
<div class="alert alert-danger mt-1" role="alert"><%= message %></div>
<% end %>
</div>

<div class="form-group">
<%= form.label :email %>
<%= form.text_field :email, placeholder: "[email protected]", class: 'form-control' %>

<% profile.errors.full_messages_for(:email).each do |message| %>
<div class="alert alert-danger mt-1" role="alert"><%= message %></div>
<% end %>
</div>

<%# nested form for locations table %>
<div class="form-group bigger" data-controller="country-state">
<%= form.fields_for :location do |location| %>
<%= location.label :country %>
<%= location.select :country,
CS.countries.invert,
{ prompt: "Please select" },
'data-path': country_states_path(:country),
'data-country-state-target': 'country',
'data-action': 'change->country-state#updateStates',
'data-initial-value': location.object.country,
required: true,
class: 'form-control' %>
<% profile.location.custom_messages(:country).each do |error| %>
<div class="alert alert-danger mt-1" role="alert"><%= error.message %></div>
<% end %>

<%= location.label :province_state, 'Province/State' %>
<%= location.select :province_state,
CS.states(location.object.country).invert,
{ prompt: "Please select" },
'data-country-state-target': 'state',
required: true,
class: 'form-control' %>
<% profile.location.custom_messages(:province_state).each do |error| %>
<div class="alert alert-danger mt-1" role="alert"><%= error.message %></div>
<% end %>

<%= location.label :city_town, 'City/Town' %>
<%= location.text_field :city_town, class: 'form-control' %>
<% profile.location.custom_messages(:city_town).each do |error| %>
<div class="alert alert-danger mt-1" role="alert"><%= error.message %></div>
<% end %>
<% end %>
</div>
</div> <!--card-->

<div>
<%= form.submit 'Save profile', class: 'btn btn-outline-dark mb-3' %>
</div>
<% end %>
20 changes: 20 additions & 0 deletions app/views/organizations/organization_profiles/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!--page heading-->
<header class="pt-5 pb-5">
<div class="container">
<div class="text-center">
<h1 class="section-heading text-uppercase underline">
<%= t('.edit_profile') %>
</h1>
</div>
</div> <!--container-->
</header>

<!--rendering _form.html.erb-->
<section class="pb-5" id="account_select">
<div class="container">
<div class="col-md-8 mx-auto">
<%= render "form", profile: @organization_profile %>
<%= link_to t('general.back'), dashboard_index_path %>
</div>
</div> <!--container-->
</section>
5 changes: 5 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ en:
paused: "Applications: paused"
new:
new_pet_post: "New Pet Post"
organization_profiles:
form:
please_fix_the_errors: "Please fix the errors highlighted below."
edit:
edit_profile: "Edit Profile"
adopter_applications:
index:
adoption_applications: "Adoption Applications"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
resources :donations, only: [:create]

scope module: :organizations do
resource :organization_profile, only: %i[edit update]

resources :home, only: [:index]
resources :pets
resources :dashboard
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20231005144432_create_organization_profiles.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateOrganizationProfiles < ActiveRecord::Migration[7.0]
def up
create_table :organization_profiles do |t|
t.string :email
t.string :phone_number

t.references :location, index: true, null: false, foreign_key: true
t.references :organization, null: false, foreign_key: true

t.timestamps
end
end

def down
drop_table :organization_profiles
end
end
15 changes: 14 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
orga_location = Location.create!(
country: "USA",
province_state: "Nevada",
city_town: "AltaCity",
zipcode: "12345"
)

organization = Organization.create!(
name: "Alta Pet Rescue",
slug: "alta"
slug: "alta",
profile: OrganizationProfile.new(email: "[email protected]", phone_number: "123 456 7890", location: orga_location)
)

ActsAsTenant.with_tenant(organization) do
Expand Down
10 changes: 9 additions & 1 deletion db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
orga_location = Location.create!(
country: "USA",
province_state: "Nevada",
city_town: "BajaCity",
zipcode: "12346"
)

organization = Organization.create!(
name: "Baja",
slug: "baja"
slug: "baja",
profile: OrganizationProfile.new(email: "[email protected]", phone_number: "123 456 7891", location: orga_location)
)
ActsAsTenant.with_tenant(organization) do
@user_staff_one = User.create!(
Expand Down
14 changes: 14 additions & 0 deletions test/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@
factory :organization do
name { Faker::Company.name }
slug { Faker::Internet.domain_word }
profile { build(:organization_profile) }
end

factory :organization_profile do
email { Faker::Internet.email }
phone_number { Faker::PhoneNumber.phone_number }

location

trait :with_organization do
after :build do |profile|
profile.organization = create(:organization, profile: profile)
end
end
end

factory :pet do
Expand Down
1 change: 1 addition & 0 deletions test/models/location_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class LocationTest < ActiveSupport::TestCase
context "associations" do
should have_one(:adopter_profile)
should have_one(:organization_profile)
end

context "validations" do
Expand Down
20 changes: 20 additions & 0 deletions test/models/organization_profile_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "test_helper"

class OrganizationProfileTest < ActiveSupport::TestCase
context "associations" do
should belong_to(:organization)
should belong_to(:location)

should accept_nested_attributes_for(:location)
end

context "callbacks" do
subject { build(:organization_profile, :with_organization) }

should "call normalize phone when saving" do
assert subject.expects(:normalize_phone).at_least_once

subject.save
end
end
end
4 changes: 4 additions & 0 deletions test/models/organization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

class OrganizationTest < ActiveSupport::TestCase
context "associations" do
subject { build(:organization) }

should have_many(:staff_accounts)
should have_many(:users).through(:staff_accounts)
should have_many(:pets)

should have_one(:profile).dependent(:destroy).required
end
end

0 comments on commit e18286c

Please sign in to comment.