Skip to content

Commit

Permalink
HACK: Render lazy created poll inline with article
Browse files Browse the repository at this point in the history
Workarounds required to reference current page from within article
content via Current.page and from within the atom feed.

Also no access to the request.key_generator from within feed article
rendering, so we check for presence.
  • Loading branch information
rossta committed Dec 7, 2024
1 parent a2475fe commit 3c43de6
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ It’s basically this: computers have gotten faster, and Rails has finally figur

With the recent release of Rails 8, it’s easier than ever to choose SQLite as your primary database for production. The question is, _should you?_

<%= render Share::Polls::LazyPagePoll.new(Current.page, "SQLite on Rails Poll", "How likely are you to use SQLite in your Rails app?" => ["100%", "Strongly considering", "On the fence", "Not very, but open minded", "No way"]) %>

## SQLite3::Database.open

[Joy of Rails](/) has run on SQLite since launch, and I have been very happy with my choice—I plan to keep it that way. But SQLite isn’t for everyone.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/site_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SiteController < Sitepress::SiteController
# requested_resource is the Sitepress::Resource object that represents the current page.
#
def show
@page = Page.find_or_initialize_by(request_path: request.path)
Current.page = @page = Page.find_or_initialize_by(request_path: request.path)

super
end
Expand Down
1 change: 1 addition & 0 deletions app/models/current.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ class Current < ActiveSupport::CurrentAttributes
attribute :admin_user
attribute :user
attribute :color_scheme
attribute :page
end
6 changes: 3 additions & 3 deletions app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class Page < ApplicationRecord
scope :draft, -> { where(["published_at > ?", Time.zone.now]) }
scope :indexed, -> { where(["indexed_at < ?", Time.zone.now]) }

def self.primary_author
User.find_by!(email: Rails.configuration.x.emails.primary_author)
end
def self.primary_author = User.find_by(email: Rails.configuration.x.emails.primary_author)

def self.primary_author! = User.find_by!(email: Rails.configuration.x.emails.primary_author)

def published? = !!published_at

Expand Down
4 changes: 2 additions & 2 deletions app/models/poll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def self.generate_for(
questions = {}
)
poll = page.polls.find_by(title: title) and return poll

poll = Page.primary_author.polls.create!(title: title)
author = Page.primary_author or return nil
poll = author.polls.create!(title: title)
poll.pages << page
poll.questions = questions.map do |question_body, answers|
question = poll.questions.build(body: question_body)
Expand Down
1 change: 1 addition & 0 deletions app/views/feed/index.atom.builder
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ atom_feed do |feed|

@articles.take(50).each do |article|
next if article.resource_missing?
Current.page = article

feed.entry(
article,
Expand Down
29 changes: 29 additions & 0 deletions app/views/share/polls/lazy_page_poll.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module Share
module Polls
class LazyPagePoll < ApplicationComponent
include Phlex::Rails::Helpers::Provide
include Phlex::Rails::Helpers::Request
include Phlex::Rails::Helpers::TurboRefreshesWith

attr_reader :page, :title, :question_data
def initialize(page, title, question_data = {})
@page = page
@title = title
@question_data = question_data
end

def view_template
poll = Poll.generate_for(page, title, question_data) or return

provide :head, turbo_refreshes_with(method: :morph, scroll: :preserve)
render Share::Polls::PollComponent.new(poll, device_uuid: device_uuid)
end

def device_uuid
return "" unless helpers.request.key_generator

helpers.cookies.signed[:device_uuid]
end
end
end
end
12 changes: 11 additions & 1 deletion spec/system/content/what-you-need-to-know-about-sqlite_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
require "rails_helper"

RSpec.describe "What you need to know about SQLite", type: :system do
it "displays the article content" do
it "displays the article content if primary poll author does not exist" do
visit "/articles/what-you-need-to-know-about-sqlite"

expect(page).to have_content "What you need to know about SQLite"
end

it "displays the article content with poll by primary author" do
FactoryBot.create(:user, :primary_author)

visit "/articles/what-you-need-to-know-about-sqlite"

expect(page).to have_content "What you need to know about SQLite"

expect(page).to have_content "How likely are you to use SQLite in your Rails app?"
end
end

0 comments on commit 3c43de6

Please sign in to comment.