diff --git a/app/controllers/organizations/organization_profiles_controller.rb b/app/controllers/organizations/organization_profiles_controller.rb index 2381df0b5..136d58eb7 100644 --- a/app/controllers/organizations/organization_profiles_controller.rb +++ b/app/controllers/organizations/organization_profiles_controller.rb @@ -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 diff --git a/app/models/organization_profile.rb b/app/models/organization_profile.rb index 917075d2c..eef7ba4ed 100644 --- a/app/models/organization_profile.rb +++ b/app/models/organization_profile.rb @@ -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 @@ -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 diff --git a/app/validators/url_validator.rb b/app/validators/url_validator.rb new file mode 100644 index 000000000..507a09199 --- /dev/null +++ b/app/validators/url_validator.rb @@ -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 diff --git a/app/views/organizations/organization_profiles/_form.html.erb b/app/views/organizations/organization_profiles/_form.html.erb index d13b0c1ae..c12cf1e72 100644 --- a/app/views/organizations/organization_profiles/_form.html.erb +++ b/app/views/organizations/organization_profiles/_form.html.erb @@ -29,11 +29,6 @@
<%= form.text_field :email, placeholder: "john@email.com", class: 'form-control' %>
- - -
- <%= form.text_field :about_us, placeholder: "We get pets into loving homes!", class: 'form-control' %> -
<% end %> @@ -71,6 +66,11 @@
+ <%= 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' %> +
+
+ <%= 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' } %>
diff --git a/db/migrate/20240408194400_drop_about_us_from_oganization_profiles.rb b/db/migrate/20240408194400_drop_about_us_from_oganization_profiles.rb new file mode 100644 index 000000000..aba68c27e --- /dev/null +++ b/db/migrate/20240408194400_drop_about_us_from_oganization_profiles.rb @@ -0,0 +1,5 @@ +class DropAboutUsFromOganizationProfiles < ActiveRecord::Migration[7.1] + def change + remove_column :organization_profiles, :about_us + end +end diff --git a/db/migrate/20240408194612_add_urls_to_organization_profiles.rb b/db/migrate/20240408194612_add_urls_to_organization_profiles.rb new file mode 100644 index 000000000..70bef5850 --- /dev/null +++ b/db/migrate/20240408194612_add_urls_to_organization_profiles.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index a967a329e..943f3d67c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_03_31_233944) do +ActiveRecord::Schema[7.1].define(version: 2024_04_08_194612) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -147,7 +147,9 @@ t.bigint "organization_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false - t.string "about_us" + t.text "facebook_url" + t.text "instagram_url" + t.text "donation_url" t.index ["location_id"], name: "index_organization_profiles_on_location_id" t.index ["organization_id"], name: "index_organization_profiles_on_organization_id" end diff --git a/db/seeds/01_alta.rb b/db/seeds/01_alta.rb index a3620602e..7ce8fbf41 100644 --- a/db/seeds/01_alta.rb +++ b/db/seeds/01_alta.rb @@ -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.") ) diff --git a/db/seeds/02_baja.rb b/db/seeds/02_baja.rb index bb5238444..a0c6ca696 100644 --- a/db/seeds/02_baja.rb +++ b/db/seeds/02_baja.rb @@ -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") ) diff --git a/test/controllers/organizations/organization_profiles_controller_test.rb b/test/controllers/organizations/organization_profiles_controller_test.rb index d966103fd..4506b03d3 100644 --- a/test/controllers/organizations/organization_profiles_controller_test.rb +++ b/test/controllers/organizations/organization_profiles_controller_test.rb @@ -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 diff --git a/test/factories/organization_profiles.rb b/test/factories/organization_profiles.rb index a15720eee..127e187e9 100644 --- a/test/factories/organization_profiles.rb +++ b/test/factories/organization_profiles.rb @@ -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 diff --git a/test/integration/organization_profile/organization_profile_edit_test.rb b/test/integration/organization_profile/organization_profile_edit_test.rb index 35dc1de9b..0783ea02c 100644 --- a/test/integration/organization_profile/organization_profile_edit_test.rb +++ b/test/integration/organization_profile/organization_profile_edit_test.rb @@ -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") } } @@ -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 @@ -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 @@ -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 diff --git a/test/models/organization_profile_test.rb b/test/models/organization_profile_test.rb index a23b0a395..106211cb0 100644 --- a/test/models/organization_profile_test.rb +++ b/test/models/organization_profile_test.rb @@ -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