Skip to content

Commit

Permalink
633 organization profile urls (#637)
Browse files Browse the repository at this point in the history
* commit annotation change

* add migrations to remove about_us column, add url columns

* remove about_us from seeds

* update strong params to accept urls

* add validations for urls

* update/add tests for urls

* add form fields for urls

* switch form layout and add placeholders to url fields
  • Loading branch information
MooseCowBear authored Apr 10, 2024
1 parent abfc91b commit 9af97a7
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ def organization_profile_params
params.require(:organization_profile).permit(
:phone_number,
:email,
:about_us,
:avatar,
:facebook_url,
:instagram_url,
:donation_url,
location_attributes: %i[city_town country province_state]
)
end
Expand Down
8 changes: 7 additions & 1 deletion app/models/organization_profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
# Table name: organization_profiles
#
# id :bigint not null, primary key
# about_us :string
# donation_url :text
# email :string
# facebook_url :text
# instagram_url :text
# phone_number :string
# created_at :datetime not null
# updated_at :datetime not null
Expand Down Expand Up @@ -34,6 +36,10 @@ class OrganizationProfile < ApplicationRecord
validates :phone_number, phone: {possible: true, allow_blank: true}
validates_format_of :email, with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i

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

delegate :name, to: :organization

private
Expand Down
12 changes: 12 additions & 0 deletions app/validators/url_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class UrlValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
return if value.blank?

uri = URI.parse(value)
unless uri.is_a?(URI::HTTPS)
record.errors.add(attribute, options[:message] || "is not a valid URL")
end
rescue URI::InvalidURIError
record.errors.add(attribute, options[:message] || "is not a valid URL")
end
end
10 changes: 5 additions & 5 deletions app/views/organizations/organization_profiles/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@
<div class="form-group">
<%= form.text_field :email, placeholder: "john@email.com", class: 'form-control' %>
</div>

<!-- About Us -->
<div class="form-group">
<%= form.text_field :about_us, placeholder: "We get pets into loving homes!", class: 'form-control' %>
</div>
</form>
<% end %>
</div>
Expand Down Expand Up @@ -71,6 +66,11 @@

<!-- Third Column -->
<div class="col-lg-6">
<%= form.text_field :facebook_url, label: "Facebook", placeholder: "https://example.com", class: 'form-control' %>
<%= form.text_field :instagram_url, label: "Instagram", placeholder: "https://example.com", class: 'form-control' %>
</div>
<div class="col-lg-6">
<%= form.text_field :donation_url, label: "Donate", placeholder: "https://example.com", class: 'form-control' %>
<%= render partial: 'partials/avatarable_form', locals: { resource: profile, form: form, picture_shape: 'rectangle' } %>
</div>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DropAboutUsFromOganizationProfiles < ActiveRecord::Migration[7.1]
def change
remove_column :organization_profiles, :about_us
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class AddUrlsToOrganizationProfiles < ActiveRecord::Migration[7.1]
def change
add_column :organization_profiles, :facebook_url, :text
add_column :organization_profiles, :instagram_url, :text
add_column :organization_profiles, :donation_url, :text
end
end
6 changes: 4 additions & 2 deletions db/schema.rb

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

2 changes: 1 addition & 1 deletion db/seeds/01_alta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@organization = Organization.create!(
name: "Alta Pet Rescue",
slug: "alta",
profile: OrganizationProfile.new(email: "alta@email.com", phone_number: "250 816 8212", location: orga_location, about_us: "We get pets into loving homes!"),
profile: OrganizationProfile.new(email: "alta@email.com", phone_number: "250 816 8212", location: orga_location),
page_text: PageText.new(hero: "Where every paw finds a hom", about: "Alta was founded by an incredible group of ladies in April of 2020. Our initial goal was to have both a rescue and a spay/neuter clinic, however, we quickly realized that it would be more efficient to separate into two organizations.")
)

Expand Down
2 changes: 1 addition & 1 deletion db/seeds/02_baja.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@organization = Organization.create!(
name: "Baja",
slug: "baja",
profile: OrganizationProfile.new(email: "baja@email.com", phone_number: "250 816 8212", location: orga_location, about_us: "We help pets find their forever homes!"),
profile: OrganizationProfile.new(email: "baja@email.com", phone_number: "250 816 8212", location: orga_location),
page_text: PageText.new(hero: "hero text", about: "about us text")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Organizations::OrganizationProfilesControllerTest < ActionDispatch::Integr

context "#update" do
setup do
@params = {about_us: Faker::Lorem.paragraph(sentence_count: 5)}
@params = {facebook_url: "https://facebook.com"}
end

should "be authorized" do
Expand Down
1 change: 0 additions & 1 deletion test/factories/organization_profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
factory :organization_profile do
email { Faker::Internet.email }
phone_number { "0000000000" }
about_us { Faker::Lorem.paragraph(sentence_count: 4) }
location
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class OrganizationProfile::EditProfileTest < ActionDispatch::IntegrationTest
organization_profile: {
email: "happy_paws_rescue@gmail.com",
phone_number: "3038947563",
about_us: "Finding pets loving homes across the front range!",
location_attributes: {
country: "United States",
province_state: "Colorado",
city_town: "Golden"
},
facebook_url: "https://example.com",
instagram_url: "https://example.com",
donation_url: "https://example.com",
avatar: fixture_file_upload("/logo.png")
}
}
Expand All @@ -52,18 +54,20 @@ class OrganizationProfile::EditProfileTest < ActionDispatch::IntegrationTest

assert_equal "happy_paws_rescue@gmail.com", @org_profile.email
assert_equal "+13038947563", @org_profile.phone_number
assert_equal "Finding pets loving homes across the front range!", @org_profile.about_us
assert_equal "United States", @org_profile.location.country
assert_equal "Colorado", @org_profile.location.province_state
assert_equal "Golden", @org_profile.location.city_town
assert_equal "logo.png", @org_profile.avatar.filename.sanitized
assert_equal "https://example.com", @org_profile.facebook_url
assert_equal "https://example.com", @org_profile.instagram_url
assert_equal "https://example.com", @org_profile.donation_url
end

test "organization profile updates with only some form fields to update" do
patch organization_profile_path(@org_profile), params: {
organization_profile: {
phone_number: "3038947542",
about_us: "Finding pets loving homes across the Denver Metro area"
donation_url: "https://example.com"
}
}
@org_profile.reload
Expand All @@ -73,7 +77,7 @@ class OrganizationProfile::EditProfileTest < ActionDispatch::IntegrationTest
assert_response :success

assert_equal "+13038947542", @org_profile.phone_number
assert_equal "Finding pets loving homes across the Denver Metro area", @org_profile.about_us
assert_equal "https://example.com", @org_profile.donation_url
end

test "organization profile does not update with a non valid phone number" do
Expand All @@ -97,4 +101,15 @@ class OrganizationProfile::EditProfileTest < ActionDispatch::IntegrationTest
assert_response :unprocessable_entity
assert_select "div.alert.alert-danger.mt-1", text: "Please fix the errors highlighted below."
end

test "organization profile does not update with an invalid url" do
patch organization_profile_path(@org_profile), params: {
organization_profile: {
facebook_url: "not a url"
}
}
@org_profile.reload
assert_response :unprocessable_entity
assert_select "div.alert.alert-danger.mt-1", text: "Please fix the errors highlighted below."
end
end
12 changes: 12 additions & 0 deletions test/models/organization_profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ class OrganizationProfileTest < ActiveSupport::TestCase

should allow_value("i_love_pets365@gmail.com").for(:email)
should_not allow_value("invalid_email.com").for(:email)

should allow_value("https://something.com").for(:facebook_url)
should allow_value("").for(:facebook_url)
should_not allow_value("http://something.com").for(:facebook_url)

should allow_value("https://something.com").for(:instagram_url)
should allow_value("").for(:instagram_url)
should_not allow_value("http://something.com").for(:instagram_url)

should allow_value("https://something.com").for(:donation_url)
should allow_value("").for(:donation_url)
should_not allow_value("http://something.com").for(:donation_url)
end

context "callbacks" do
Expand Down

0 comments on commit 9af97a7

Please sign in to comment.