Skip to content

Commit cb1563c

Browse files
authored
Website fonts and pages bugfixes
- skips font creation when name and file are blank - adds validation to ensure font name and file are present - renders :new and :edit with flash message when creating and updating fail - redirects to new website page with message when accessing pages before website is configured - adds regression specs for above scenarios
1 parent 83b562b commit cb1563c

File tree

7 files changed

+62
-12
lines changed

7 files changed

+62
-12
lines changed

app/controllers/staff/pages_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class Staff::PagesController < Staff::ApplicationController
2+
before_action :require_website
23
before_action :enable_website_subnav
34
before_action :set_page, except: :index
45
before_action :authorize_page, except: :index
@@ -98,4 +99,11 @@ def page_params
9899
:unpublished_body
99100
)
100101
end
102+
103+
def require_website
104+
return if current_website
105+
106+
redirect_to new_event_staff_website_path(current_event),
107+
alert: "Please configure your website before attempting to create pages"
108+
end
101109
end

app/controllers/staff/websites_controller.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,25 @@ class Staff::WebsitesController < Staff::ApplicationController
66
def new; end
77

88
def create
9-
@website.update(website_params)
10-
11-
flash[:success] = "Website was successfully created."
12-
redirect_to edit_event_staff_website_path(current_event)
9+
if @website.update(website_params)
10+
flash[:success] = "Website was successfully created."
11+
redirect_to edit_event_staff_website_path(current_event)
12+
else
13+
flash[:warning] = "There were errors creating your website configuration"
14+
render :new
15+
end
1316
end
1417

1518
def edit; end
1619

1720
def update
18-
@website.update(website_params)
19-
20-
flash[:success] = "Website was successfully updated."
21-
redirect_to edit_event_staff_website_path(current_event)
21+
if @website.update(website_params)
22+
flash[:success] = "Website was successfully updated."
23+
redirect_to edit_event_staff_website_path(current_event)
24+
else
25+
flash[:warning] = "There were errors updating your website configuration"
26+
render :edit
27+
end
2228
end
2329

2430
private

app/models/website.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ class Website < ApplicationRecord
88
has_many :session_formats, through: :event
99
has_many :session_format_configs
1010

11-
12-
accepts_nested_attributes_for :fonts, reject_if: :all_blank, allow_destroy: true
11+
accepts_nested_attributes_for :fonts,
12+
reject_if: ->(font) { font['name'].blank? && font['file'].blank? },
13+
allow_destroy: true
1314
accepts_nested_attributes_for :contents, reject_if: :all_blank, allow_destroy: true
1415
accepts_nested_attributes_for :meta_data
1516
accepts_nested_attributes_for :session_format_configs

app/models/website/font.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ class Website::Font < ApplicationRecord
33

44
has_one_attached :file
55

6+
validates :file, :name, presence: true
7+
68
scope :primary, -> { where(primary: true) }
79
scope :secondary, -> { where(secondary: true) }
810
end

app/views/staff/websites/_form.html.haml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@
4040
= ff.input :name
4141
= ff.hidden_field :session_format_id
4242

43-
%div{"data-controller": "nested-form"}
44-
%legend.fieldset-legend Fonts
43+
%div{"data-controller": "nested-form", class: "nested-fonts"}
44+
%h4 Fonts
4545
%template{"data-nested-form-target": "template"}
4646
= f.simple_fields_for :fonts,
4747
Website::Font.new,

spec/features/website/configuration_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,26 @@
4747
expect(current_path).to eq('/home')
4848
end
4949

50+
scenario "Organizer fails to add font file correctly", :js do
51+
website = create(:website, event: event)
52+
53+
login_as(organizer)
54+
visit edit_event_staff_website_path(event)
55+
56+
click_on("Add Font")
57+
click_on("Save")
58+
expect(page).to have_content("Website was successfully updated")
59+
60+
expect(website.fonts.count).to eq(0)
61+
62+
click_on("Add Font")
63+
within(".nested-fonts") { fill_in("Name", with: "Times") }
64+
click_on("Save")
65+
66+
expect(page).to have_content("There were errors updating your website configuration")
67+
within(".nested-fonts") { expect(page).to have_content("can't be blank") }
68+
end
69+
5070
scenario "Organizer configures tailwind with head content", :js do
5171
website = create(:website, event: event)
5272
home_page = create(:page, website: website)

spec/features/website/page_management_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@
55
let(:organizer) { create(:organizer, event: event) }
66
let!(:website) { create(:website, :with_details, event: event) }
77

8+
scenario "Organizer cannot access pages until website is created" do
9+
website.destroy
10+
login_as(organizer)
11+
12+
visit event_path(event)
13+
within('.navbar') { click_on("Website") }
14+
within('.website-subnav') { click_on("Pages") }
15+
16+
expect(page).to have_content(
17+
"Please configure your website before attempting to create pages"
18+
)
19+
end
20+
821
scenario "Organizer creates and edits a website page", :js do
922
login_as(organizer)
1023

0 commit comments

Comments
 (0)