Skip to content

Commit 5937b92

Browse files
add body_published_at to pages table (#295)
* add body_published_at to pages table As a website editor I would like to know when a page was last edited, and which pages have updates that have yet to be published Solution - add body_published_at column to the pages table. - when a page is published, set the value of body_published_at. - If a page has unpublished changes, disply this on the index with a icon. * add color to 'not published text' * update page factory for testing * Bugfix for automatic caching when page is updated - uses website#save with after_save_commit callback instead of `belongs_to :website, touch: :purged_at` so that purged_at is not touched when caching is not automatic Co-authored-by: Jonathan Greenberg <[email protected]>
1 parent 20f3831 commit 5937b92

File tree

8 files changed

+33
-7
lines changed

8 files changed

+33
-7
lines changed

Diff for: app/controllers/staff/pages_controller.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ def update
3939
def preview; end
4040

4141
def publish
42-
@page.update(published_body: @page.unpublished_body)
42+
@page.update(published_body: @page.unpublished_body,
43+
body_published_at: Time.current)
4344
flash[:success] = "#{@page.name} Page was successfully published."
4445
redirect_to event_staff_pages_path(current_event)
4546
end

Diff for: app/models/page.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ class Page < ApplicationRecord
44
'home' => { },
55
}
66

7-
belongs_to :website, touch: :purged_at
7+
belongs_to :website
8+
after_save_commit :purge_website_cache
89

910
scope :published, -> { where.not(published_body: nil).where(hide_page: false) }
1011
scope :in_footer, -> { published.where.not(footer_category: [nil, ""]) }
@@ -33,6 +34,16 @@ def self.promote(page)
3334
def self.from_template(key, attrs)
3435
new(TEMPLATES[key].merge(template: key, name: key.titleize, slug: key, **attrs))
3536
end
37+
38+
def unpublished_changes?
39+
published_body != unpublished_body
40+
end
41+
42+
private
43+
44+
def purge_website_cache
45+
website.save
46+
end
3647
end
3748

3849
# == Schema Information

Diff for: app/models/website.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ class Website < ApplicationRecord
2121
has_one_attached :background
2222
has_one_attached :favicon
2323

24-
after_touch :purge_cache, if: :caching_automatic?
2524
around_update :purge_cache, if: :caching_automatic?
2625

2726
DEFAULT = 'default'.freeze
@@ -34,11 +33,9 @@ def manual_purge
3433
purge_cache { save }
3534
end
3635

37-
private
38-
3936
def purge_cache
4037
self.purged_at = Time.current
41-
yield if block_given?
38+
yield
4239
FastlyService.service&.purge_by_key(event.slug)
4340
end
4441
end

Diff for: app/views/staff/pages/_page.html.haml

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44
%td
55
- if page.published?
66
%span.glyphicon.glyphicon-ok
7+
%td
8+
- if page.published?
9+
= time_ago_in_words(page.body_published_at || page.updated_at)
10+
- else
11+
%span.label-warning Not Published
12+
%td
13+
- if page.unpublished_changes?
14+
%span.glyphicon.glyphicon-bell
715
%td
816
- if page.landing?
917
%span.glyphicon.glyphicon-ok

Diff for: app/views/staff/pages/index.html.haml

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
%th Name
1515
%th Slug
1616
%th Published
17+
%th Last Published
18+
%th Unpublished Changes
1719
%th Landing Page
1820
%th
1921
Actions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddBodyPublishedAtToPages < ActiveRecord::Migration[6.1]
2+
def change
3+
add_column :pages, :body_published_at, :datetime, default: nil
4+
end
5+
end

Diff for: db/schema.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2022_05_12_134224) do
13+
ActiveRecord::Schema.define(version: 2022_05_23_185412) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
@@ -115,6 +115,7 @@
115115
t.boolean "hide_footer", default: false, null: false
116116
t.boolean "hide_page", default: false, null: false
117117
t.string "footer_category"
118+
t.datetime "body_published_at"
118119
t.index ["website_id"], name: "index_pages_on_website_id"
119120
end
120121

Diff for: spec/factories/pages.rb

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
sequence(:slug) { |i| "home#{i if i > 1}" }
66
unpublished_body { "<p>#{Faker::Lorem.paragraph}</p>" }
77
published_body { "<p>#{Faker::Lorem.paragraph}</p>" }
8+
body_published_at { DateTime.now - 1.day }
89
end
910
end

0 commit comments

Comments
 (0)