diff --git a/db/migrate/20240303091643_add_updated_at_to_pages.rb b/db/migrate/20240303091643_add_updated_at_to_pages.rb new file mode 100644 index 0000000..01938a9 --- /dev/null +++ b/db/migrate/20240303091643_add_updated_at_to_pages.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +ROM::SQL.migration do + up do + execute <<-SQL + CREATE OR REPLACE FUNCTION update_updated_at_column() + RETURNS TRIGGER AS $$ + BEGIN + NEW.updated_at = (now() at time zone 'utc'); + RETURN NEW; + END; + $$ language 'plpgsql'; + SQL + + alter_table :pages do + add_column :updated_at, :timestamp + end + + run <<-SQL + CREATE TRIGGER update_pages_updated_at BEFORE UPDATE + ON pages FOR EACH ROW EXECUTE PROCEDURE + update_updated_at_column(); + SQL + end + + down do + execute("DROP FUNCTION IF EXISTS update_updated_at_column();") + + run <<-SQL + DROP TRIGGER IF EXISTS update_pages_updated_at ON pages; + SQL + + alter_table :pages do + drop_column :updated_at + end + end +end diff --git a/slices/main/templates/pages/show.html.slim b/slices/main/templates/pages/show.html.slim index 1781935..04b9d71 100644 --- a/slices/main/templates/pages/show.html.slim +++ b/slices/main/templates/pages/show.html.slim @@ -5,4 +5,6 @@ article class="mb-12 prose dark:prose-invert max-w-prose mx-auto text-gray-800 d h1= page_name == page_content + - if page_updated_at + ="Last updated: #{page_updated_at}" div class="max-w-screen-md mx-auto border-t border-solid border-gray-200 dark:border-gray-600" diff --git a/slices/main/views/pages/show.rb b/slices/main/views/pages/show.rb index d01c7ce..ac0d119 100644 --- a/slices/main/views/pages/show.rb +++ b/slices/main/views/pages/show.rb @@ -12,6 +12,10 @@ module Main page.name end + expose :page_updated_at do |page| + page.updated_at&.strftime("%m/%d/%Y") + end + private_expose :page do |slug:| page_repo.fetch!(slug: slug) end