-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
213 an organization can edit update their profile (#248)
* 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
1 parent
740bbae
commit e18286c
Showing
18 changed files
with
271 additions
and
4 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
app/controllers/organizations/organization_profiles_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
app/views/organizations/organization_profiles/_form.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
app/views/organizations/organization_profiles/edit.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters