diff --git a/app/controllers/organizations/page_texts_controller.rb b/app/controllers/organizations/page_texts_controller.rb index f28915405..7ac8ae7b2 100644 --- a/app/controllers/organizations/page_texts_controller.rb +++ b/app/controllers/organizations/page_texts_controller.rb @@ -8,19 +8,18 @@ def update if @page_text.update(page_text_params) redirect_to edit_page_text_path, notice: "Page text updated successfully!" else - render :edit, status: :unprocessable_entity + redirect_to edit_page_text_path, alert: @page_text.errors.full_messages.to_sentence end end private def page_text_params - params.require(:page_text).permit(:hero, :about) + params.require(:page_text).permit(:hero, :about, :hero_image, about_us_images: []) end def set_page_text - @page_text = current_user.organization.page_text - + @page_text = PageText.first authorize! @page_text end end diff --git a/app/models/page_text.rb b/app/models/page_text.rb index 1db410c01..54a9fe4d0 100644 --- a/app/models/page_text.rb +++ b/app/models/page_text.rb @@ -20,6 +20,17 @@ class PageText < ApplicationRecord acts_as_tenant(:organization) + has_one_attached :hero_image + has_many_attached :about_us_images + validates :hero, presence: true, allow_nil: true validates :about, presence: true, allow_nil: true + + validates :hero_image, content_type: ["image/png", "image/jpeg"], + limit: {max: 1}, + size: {less_than: 2.megabytes} + + validates :about_us_images, content_type: ["image/png", "image/jpeg"], + limit: {max: 2}, + size: {less_than: 2.megabytes} end diff --git a/app/views/components/_attachment_form.html.erb b/app/views/components/_attachment_form.html.erb index 09c7f0f13..5b44ef0a3 100644 --- a/app/views/components/_attachment_form.html.erb +++ b/app/views/components/_attachment_form.html.erb @@ -10,7 +10,7 @@ <%= bootstrap_form_with(model: instance, url: url, method: :post) do |form| %>
- <%= form.file_field attachment_type.to_sym, multiple: true, direct_upload: true, + <%= form.file_field attachment_type.to_sym, multiple: true, direct_upload: true, class: "custom-attachments", hide_label: true %>
@@ -21,4 +21,3 @@ <% end %>
- diff --git a/app/views/components/_image_attachment_table.html.erb b/app/views/components/_image_attachment_table.html.erb index a62bcd77a..1b42d06ca 100644 --- a/app/views/components/_image_attachment_table.html.erb +++ b/app/views/components/_image_attachment_table.html.erb @@ -10,26 +10,32 @@ - <% if @pet.images.attached? %> - <% @pet.images.each do |image| %> + <% if images.any? %> + <% images.each do |image| %>
- <%= link_to(image_tag(image, class: 'card-img'), rails_blob_path(image, disposition: 'attachment')) %> + <% if image.record.persisted? %> + <%= link_to(image_tag(image, class: 'card-img'), rails_blob_path(image, disposition: 'attachment')) %> + <% end %>
- <%= link_to image.filename.to_s, rails_blob_path(image, disposition: 'attachment') %> + <% if image.record.persisted? %> + <%= link_to image.filename.to_s, rails_blob_path(image, disposition: 'attachment') %> + <% end %> - <%= link_to t('general.delete'), - purge_attachment_path(image.id), - data: { - turbo_confirm: "Do you want to delete this image?", - turbo_method: "delete" }, - class: 'btn btn-outline-danger' %> + <% if image.record.persisted? %> + <%= link_to t('general.delete'), + purge_attachment_path(image.id), + data: { + turbo_confirm: 'Do you want to delete this image?', + turbo_method: "delete" }, + class: 'btn btn-outline-danger' %> + <% end %> <% end %> diff --git a/app/views/organizations/home/index.html.erb b/app/views/organizations/home/index.html.erb index 65fb9e972..41f9fec82 100644 --- a/app/views/organizations/home/index.html.erb +++ b/app/views/organizations/home/index.html.erb @@ -1,12 +1,12 @@
-
+

<%= Current.organization.name %>

-
-
+
+

<%= Current.organization.page_text&.hero || "Where every paw finds a home" %>

@@ -133,34 +133,38 @@
- -

About us

-
-
- - <%= image_tag('danielle_2.jpg', - class: 'rounded-5 w-100 px-4 px-lg-0') %> - -
-
-

- <%= Current.organization.page_text&.about || "#{Current.organization.name} 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."%> -

-

- <%= link_to 'Read More', about_us_path, class: 'custom-btn-green mt-5 mb-3' %> -

-
-
- - <%= image_tag('danielle_3.jpg', class: 'w-100 rounded-5 px-4 px-lg-0') %> - -
+
+ + <% if Current.organization.page_text.about_us_images.attached? && Current.organization.page_text.about_us_images.count > 0 %> + <%= image_tag(Current.organization.page_text.about_us_images.first, class: 'rounded-5 w-100 px-4 px-lg-0') %> + <% else %> + <%= image_tag('danielle_2.jpg', class: 'rounded-5 w-100 px-4 px-lg-0') %> + <% end %> + +
+
+

+ <%= Current.organization.page_text&.about || "#{Current.organization.name} 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." %> +

+

+ <%= link_to 'Read More', about_us_path, class: 'custom-btn-green mt-5 mb-3' %> +

+
+
+ + <% if Current.organization.page_text.about_us_images.attached? && Current.organization.page_text.about_us_images.count >= 2 %> + <%= image_tag(Current.organization.page_text.about_us_images.second, class: 'w-100 rounded-5 px-4 px-lg-0') %> + <% else %> + <%= image_tag('danielle_3.jpg', class: 'w-100 rounded-5 px-4 px-lg-0') %> + <% end %> + +
diff --git a/app/views/organizations/page_texts/_form.html.erb b/app/views/organizations/page_texts/_form.html.erb index 8cdcff48b..29687e857 100644 --- a/app/views/organizations/page_texts/_form.html.erb +++ b/app/views/organizations/page_texts/_form.html.erb @@ -3,34 +3,51 @@

Landing page details

Edit your landing page. -

+

<%= form.fields_for :page_text do |page_text_form| %> -
- -
- <%= form.text_field :hero, - label: "Hero", - autofocus: true, - placeholder: "hero text", - class: 'form-control' %> -
- -
- <%= form.text_area :about, label: "About Us", placeholder: "About Us text", class: 'form-control' %> -
-
+
+ <%= form.text_field :hero, + label: "Hero", + autofocus: true, + placeholder: "hero text", + class: 'form-control' %> +
+
+ <%= form.file_field :hero_image, + label: "Hero Image", + class: 'form-control'%> +
+ Images must be .png or .jpeg under 2MB +
+ <%= form.text_area :about, label: "About Us", placeholder: "About Us text", class: 'form-control' %> +
+
+ <%= form.file_field :about_us_images, + multiple: true, + accept: 'image/png, image/jpeg', + label: "About Us Images", + class: 'form-control' %> + <% @page_text.about_us_images.each do |image| %> + <%= form.hidden_field :about_us_images, multiple: true, value: image.signed_id %> + <% end %> + You can upload up to 2 images.
Images must be .png or .jpeg under 2MB
<% end %> +
-
-
-
- <%= form.submit 'Save page text', class: 'btn btn-primary' %> - <%= link_to t('general.cancel'), :back, class: 'btn btn-outline-secondary' %> +
+
+ <%= form.submit 'Save page text/images', class: 'btn btn-primary' %> + <%= link_to t('general.cancel'), :back, class: 'btn btn-outline-secondary' %> +
-
+
+
+
+ <% combined_images = [@page_text.hero_image.attachment, @page_text.about_us_images].flatten.compact%> + <%= render partial: "components/image_attachment_table", locals: { images: combined_images } %> +
<% end %> - diff --git a/app/views/organizations/pets/tabs/_photos.html.erb b/app/views/organizations/pets/tabs/_photos.html.erb index 0dda18aaf..7fb271eae 100644 --- a/app/views/organizations/pets/tabs/_photos.html.erb +++ b/app/views/organizations/pets/tabs/_photos.html.erb @@ -1,4 +1,4 @@
<%= render "components/attachment_form", instance: @pet, title: 'Photos', url: attach_images_pet_path(@pet), attachment_type: 'images' %> - <%= render "components/image_attachment_table", pet: @pet %> + <%= render "components/image_attachment_table", images: @pet.images %>
diff --git a/test/controllers/organizations/home_controller_test.rb b/test/controllers/organizations/home_controller_test.rb index 00eea3f14..525f37f18 100644 --- a/test/controllers/organizations/home_controller_test.rb +++ b/test/controllers/organizations/home_controller_test.rb @@ -2,7 +2,9 @@ class Organizations::HomeControllerTest < ActionDispatch::IntegrationTest setup do - @organization = create(:organization) + @organization = create(:organization, :with_page_text) + @organization.save + Current.organization = @organization end context "GET #index" do diff --git a/test/controllers/organizations/page_texts_controller_test.rb b/test/controllers/organizations/page_texts_controller_test.rb index 73a1343df..dff81b072 100644 --- a/test/controllers/organizations/page_texts_controller_test.rb +++ b/test/controllers/organizations/page_texts_controller_test.rb @@ -46,6 +46,7 @@ class Organizations::PageTextsControllerTest < ActionDispatch::IntegrationTest context "PATCH #update" do should "update page text" do patch page_text_path(@page_text), params: {page_text: {hero: "Super Dog", about: "canine caped crusader"}} + @page_text.reload assert_response :redirect follow_redirect! assert_response :success diff --git a/test/factories/organizations.rb b/test/factories/organizations.rb index e716b8adf..eb7411fc3 100644 --- a/test/factories/organizations.rb +++ b/test/factories/organizations.rb @@ -3,5 +3,11 @@ name { Faker::Company.name } sequence(:slug) { |n| Faker::Internet.domain_word + n.to_s } profile { association :organization_profile, organization: instance } + + trait :with_page_text do + after(:create) do |organization| + create(:page_text, organization: organization) unless organization.page_text + end + end end end diff --git a/test/factories/page_text.rb b/test/factories/page_text.rb index 5b500e6fa..6fa2b3eb6 100644 --- a/test/factories/page_text.rb +++ b/test/factories/page_text.rb @@ -2,6 +2,17 @@ factory :page_text do hero { "MyString" } about { Faker::Lorem.sentence } - organization { ActsAsTenant.current_tenant } + # organization { ActsAsTenant.current_tenant } + association :organization, factory: :organization + + trait :with_image do + after(:create) do |page_text| + page_text.about_us_images.attach( + io: File.open(Rails.root.join("app", "assets", "images", "cat.jpeg")), + filename: "cat.jpeg", + content_type: "image/jpeg" + ) + end + end end end diff --git a/test/integration/adoption_application_reviews_test.rb b/test/integration/adoption_application_reviews_test.rb index 2030b53dd..c55d15854 100644 --- a/test/integration/adoption_application_reviews_test.rb +++ b/test/integration/adoption_application_reviews_test.rb @@ -8,6 +8,9 @@ class AdoptionApplicationReviewsTest < ActionDispatch::IntegrationTest @withdrawn_app = create(:adopter_application, :withdrawn) @successful_applicant_app = create(:adopter_application, status: :successful_applicant) @adoption_made_app = create(:adopter_application, status: :adoption_made) + @organization = create(:organization) + @page_text = create(:page_text, organization: @organization) + Current.organization = @organization end context "non-staff" do diff --git a/test/system/login_test.rb b/test/system/login_test.rb index d244fc018..c166f822b 100644 --- a/test/system/login_test.rb +++ b/test/system/login_test.rb @@ -4,6 +4,8 @@ class LoginTest < ApplicationSystemTestCase setup do @user = create(:staff) @organization = @user.organization + @page_text = create(:page_text, :with_image, organization: @organization) + Current.organization = @organization end context "when logging in as a staff member" do diff --git a/test/system/registration_test.rb b/test/system/registration_test.rb index 3f83c5bb4..e8c27943f 100644 --- a/test/system/registration_test.rb +++ b/test/system/registration_test.rb @@ -4,6 +4,8 @@ class RegistrationTest < ApplicationSystemTestCase setup do @user = create(:staff) @organization = @user.organization + @page_text = create(:page_text, :with_image, organization: @organization) + Current.organization = @organization visit root_url click_on "Log In" diff --git a/test/system/users_test.rb b/test/system/users_test.rb index a08139320..a56cba2fe 100644 --- a/test/system/users_test.rb +++ b/test/system/users_test.rb @@ -4,6 +4,8 @@ class UsersTest < ApplicationSystemTestCase setup do @user = create(:staff) @organization = @user.organization + @page_text = create(:page_text, :with_image, organization: @organization, hero: "Where every paw finds a home") + Current.organization = @organization end test "user can log out" do