From 1a734aacf8ea383453c1eaf19ca32e31eca05b3d Mon Sep 17 00:00:00 2001 From: Luis Ramos Date: Mon, 17 Aug 2020 17:16:44 +0100 Subject: [PATCH] Allow user to upload terms and conditions PDF file to an enterprise --- app/models/enterprise.rb | 8 ++- .../api/admin/enterprise_serializer.rb | 2 +- .../permitted_attributes/enterprise.rb | 2 +- .../form/_business_details.html.haml | 10 +++ config/locales/en.yml | 2 + ...add_terms_and_conditions_to_enterprises.rb | 5 ++ db/schema.rb | 32 +++++----- .../features/admin/enterprises/images_spec.rb | 2 +- .../enterprises/terms_and_conditions_spec.rb | 61 +++++++++++++++++++ 9 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 db/migrate/20200817150002_add_terms_and_conditions_to_enterprises.rb create mode 100644 spec/features/admin/enterprises/terms_and_conditions_spec.rb diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index 407d0a71cd..971bcc4216 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -74,10 +74,16 @@ class Enterprise < ActiveRecord::Base }, url: '/images/enterprises/promo_images/:id/:style/:basename.:extension', path: 'public/images/enterprises/promo_images/:id/:style/:basename.:extension' - validates_attachment_content_type :logo, content_type: %r{\Aimage/.*\Z} validates_attachment_content_type :promo_image, content_type: %r{\Aimage/.*\Z} + has_attached_file :terms_and_conditions, + url: '/files/enterprises/terms_and_conditions/:id/:basename.:extension', + path: 'public/files/enterprises/terms_and_conditions/:id/:basename.:extension' + validates_attachment_content_type :terms_and_conditions, + content_type: "application/pdf", + message: I18n.t(:enterprise_terms_and_conditions_type_error) + include Spree::Core::S3Support supports_s3 :logo supports_s3 :promo_image diff --git a/app/serializers/api/admin/enterprise_serializer.rb b/app/serializers/api/admin/enterprise_serializer.rb index 02eff79426..5c66621744 100644 --- a/app/serializers/api/admin/enterprise_serializer.rb +++ b/app/serializers/api/admin/enterprise_serializer.rb @@ -6,7 +6,7 @@ class Api::Admin::EnterpriseSerializer < ActiveModel::Serializer :preferred_product_selection_from_inventory_only, :preferred_show_customer_names_to_suppliers, :owner, :contact, :users, :tag_groups, :default_tag_group, :require_login, :allow_guest_orders, :allow_order_changes, - :logo, :promo_image + :logo, :promo_image, :terms_and_conditions, :terms_and_conditions_file_name has_one :owner, serializer: Api::Admin::UserSerializer has_many :users, serializer: Api::Admin::UserSerializer diff --git a/app/services/permitted_attributes/enterprise.rb b/app/services/permitted_attributes/enterprise.rb index dc71d76d86..0516c9fe3e 100644 --- a/app/services/permitted_attributes/enterprise.rb +++ b/app/services/permitted_attributes/enterprise.rb @@ -25,7 +25,7 @@ module PermittedAttributes [ :id, :name, :visible, :permalink, :owner_id, :contact_name, :email_address, :phone, :is_primary_producer, :sells, :website, :facebook, :instagram, :linkedin, :twitter, - :description, :long_description, :logo, :promo_image, + :description, :long_description, :logo, :promo_image, :terms_and_conditions, :allow_guest_orders, :allow_order_changes, :require_login, :enable_subscriptions, :abn, :acn, :charges_sales_tax, :display_invoice_logo, :invoice_text, :preferred_product_selection_from_inventory_only, :preferred_shopfront_message, diff --git a/app/views/admin/enterprises/form/_business_details.html.haml b/app/views/admin/enterprises/form/_business_details.html.haml index c5953ba5ef..84686fafbe 100644 --- a/app/views/admin/enterprises/form/_business_details.html.haml +++ b/app/views/admin/enterprises/form/_business_details.html.haml @@ -31,3 +31,13 @@ = f.label :invoice_text, t('.invoice_text') .omega.eight.columns = f.text_area :invoice_text, style: "width: 100%; height: 100px;" + +.row + .alpha.three.columns + = f.label :terms_and_conditions, t('.terms_and_conditions') + + .omega.eight.columns + %a{ href: '{{ Enterprise.terms_and_conditions }}', ng: { if: 'Enterprise.terms_and_conditions' } } + = '{{ Enterprise.terms_and_conditions_file_name }}' + %p + = f.file_field :terms_and_conditions diff --git a/config/locales/en.yml b/config/locales/en.yml index 217f78f7cf..8867d8f6f7 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -690,6 +690,7 @@ en: acn_placeholder: eg. 123 456 789 display_invoice_logo: Display logo in invoices invoice_text: Add customized text at the end of invoices + terms_and_conditions: "Terms and Conditions" contact: name: Name name_placeholder: eg. Gustav Plum @@ -2433,6 +2434,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using enterprise_name_error: "has already been taken. If this is your enterprise and you would like to claim ownership, or if you would like to trade with this enterprise please contact the current manager of this profile at %{email}." enterprise_owner_error: "^%{email} is not permitted to own any more enterprises (limit is %{enterprise_limit})." enterprise_role_uniqueness_error: "^That role is already present." + enterprise_terms_and_conditions_type_error: "Only PDFs are allowed" inventory_item_visibility_error: must be true or false product_importer_file_error: "error: no file uploaded" product_importer_spreadsheet_error: "could not process file: invalid filetype" diff --git a/db/migrate/20200817150002_add_terms_and_conditions_to_enterprises.rb b/db/migrate/20200817150002_add_terms_and_conditions_to_enterprises.rb new file mode 100644 index 0000000000..57b646abd6 --- /dev/null +++ b/db/migrate/20200817150002_add_terms_and_conditions_to_enterprises.rb @@ -0,0 +1,5 @@ +class AddTermsAndConditionsToEnterprises < ActiveRecord::Migration + def change + add_attachment :enterprises, :terms_and_conditions + end +end diff --git a/db/schema.rb b/db/schema.rb index f56c95f405..83e5d66fa6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20200721135726) do +ActiveRecord::Schema.define(version: 20200817150002) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -190,8 +190,8 @@ ActiveRecord::Schema.define(version: 20200721135726) do t.integer "address_id" t.text "pickup_times" t.string "next_collection_at" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false t.text "distributor_info" t.string "logo_file_name" t.string "logo_content_type" @@ -201,22 +201,26 @@ ActiveRecord::Schema.define(version: 20200721135726) do t.string "promo_image_content_type" t.integer "promo_image_file_size" t.datetime "promo_image_updated_at" - t.boolean "visible", default: true + t.boolean "visible", default: true t.string "facebook" t.string "instagram" t.string "linkedin" - t.integer "owner_id", null: false - t.string "sells", default: "none", null: false - t.boolean "producer_profile_only", default: false - t.string "permalink", null: false - t.boolean "charges_sales_tax", default: false, null: false + t.integer "owner_id", null: false + t.string "sells", default: "none", null: false + t.boolean "producer_profile_only", default: false + t.string "permalink", null: false + t.boolean "charges_sales_tax", default: false, null: false t.string "email_address" - t.boolean "require_login", default: false, null: false - t.boolean "allow_guest_orders", default: true, null: false + t.boolean "require_login", default: false, null: false + t.boolean "allow_guest_orders", default: true, null: false t.text "invoice_text" - t.boolean "display_invoice_logo", default: false - t.boolean "allow_order_changes", default: false, null: false - t.boolean "enable_subscriptions", default: false, null: false + t.boolean "display_invoice_logo", default: false + t.boolean "allow_order_changes", default: false, null: false + t.boolean "enable_subscriptions", default: false, null: false + t.string "terms_and_conditions_file_name" + t.string "terms_and_conditions_content_type" + t.integer "terms_and_conditions_file_size" + t.datetime "terms_and_conditions_updated_at" end add_index "enterprises", ["address_id"], name: "index_enterprises_on_address_id", using: :btree diff --git a/spec/features/admin/enterprises/images_spec.rb b/spec/features/admin/enterprises/images_spec.rb index 0d4644f8c7..4dafe3df17 100644 --- a/spec/features/admin/enterprises/images_spec.rb +++ b/spec/features/admin/enterprises/images_spec.rb @@ -15,7 +15,7 @@ feature "Managing enterprise images" do visit edit_admin_enterprise_path(distributor) end - describe "images for an enterprise", js: true do + describe "images for an enterprise" do def go_to_images within(".side_menu") do click_link "Images" diff --git a/spec/features/admin/enterprises/terms_and_conditions_spec.rb b/spec/features/admin/enterprises/terms_and_conditions_spec.rb new file mode 100644 index 0000000000..93c448ef6c --- /dev/null +++ b/spec/features/admin/enterprises/terms_and_conditions_spec.rb @@ -0,0 +1,61 @@ +require "spec_helper" + +feature "Uploading Terms and Conditions PDF" do + include WebHelper + include AuthenticationHelper + + context "as an Enterprise user", js: true do + let(:enterprise_user) { create(:user, enterprise_limit: 1) } + let(:distributor) { create(:distributor_enterprise, name: "First Distributor") } + + before do + enterprise_user.enterprise_roles.build(enterprise: distributor).save! + + login_as enterprise_user + visit edit_admin_enterprise_path(distributor) + end + + describe "images for an enterprise" do + def go_to_business_details + within(".side_menu") do + click_link "Business Details" + end + end + + let(:white_pdf_file_name) { Rails.root.join("app", "assets", "images", "logo-white.pdf") } + let(:black_pdf_file_name) { Rails.root.join("app", "assets", "images", "logo-black.pdf") } + + before do + # Create fake PDFs from PNG images + FileUtils.cp(Rails.root.join("app", "assets", "images", "logo-white.png"), white_pdf_file_name) + FileUtils.cp(Rails.root.join("app", "assets", "images", "logo-black.png"), black_pdf_file_name) + + go_to_business_details + end + + scenario "uploading terms and conditions" do + # Add PDF + attach_file "enterprise[terms_and_conditions]", white_pdf_file_name + click_button "Update" + expect(page).to have_content("Enterprise \"#{distributor.name}\" has been successfully updated!") + + go_to_business_details + expect(page).to have_selector("a[href*='logo-white.pdf']") + + # Replace PDF + attach_file "enterprise[terms_and_conditions]", black_pdf_file_name + click_button "Update" + expect(page).to have_content("Enterprise \"#{distributor.name}\" has been successfully updated!") + + go_to_business_details + expect(page).to have_selector("a[href*='logo-black.pdf']") + end + + after do + # Delete fake PDFs + FileUtils.rm_f(white_pdf_file_name) + FileUtils.rm_f(black_pdf_file_name) + end + end + end +end