Skip to content

Commit

Permalink
paper-trail - add paper trail gem and versioning of bill, user vote a…
Browse files Browse the repository at this point in the history
…nd user
  • Loading branch information
dcordz committed Jun 19, 2024
1 parent b2e4dc6 commit 8694b9e
Show file tree
Hide file tree
Showing 12 changed files with 705 additions and 13 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ gem 'shortener'
# https://medium.com/@dejanvu.developer/implementing-web-push-notifications-in-a-ruby-on-rails-application-dcd829e02df0
gem 'web-push'

# https://github.com/paper-trail-gem/paper_trail
gem 'paper_trail'

group :production do
gem 'scout_apm'
end
Expand Down
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@ GEM
openssl-signature_algorithm (1.3.0)
openssl (> 2.0)
os (1.1.4)
paper_trail (15.1.0)
activerecord (>= 6.1)
request_store (~> 1.4)
parallel (1.24.0)
parser (3.3.1.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -328,6 +331,8 @@ GEM
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
request_store (1.7.0)
rack (>= 1.4)
retriable (3.1.2)
rexml (3.2.8)
strscan (>= 3.0.9)
Expand Down Expand Up @@ -507,6 +512,7 @@ DEPENDENCIES
hotwire-livereload (~> 1.3)
inertia_rails
jbuilder
paper_trail
pry
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.2)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base
include RelyingParty
include SwayProps

before_action :redirect_if_no_current_user
before_action :redirect_if_no_current_user, :set_paper_trail_whodunnit

T::Configuration.inline_type_error_handler = lambda do |error, _opts|
Rails.logger.error error
Expand Down
24 changes: 13 additions & 11 deletions app/frontend/pages/Passkey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Passkey: React.FC = () => {
const dispatch = useDispatch();

const turnstile = useTurnstile();
const [turnstileVerified, setTurnstileVerified] = useState<boolean>(false);
const [turnstileVerified, setTurnstileVerified] = useState<boolean>(import.meta.env.DEV);
const handleTurnstileVerify = useCallback(
(token: string) => {
fetch("https://turnstile.sway.vote", {
Expand Down Expand Up @@ -183,16 +183,18 @@ const Passkey: React.FC = () => {
</BootstrapForm.Group>
<ErrorMessage name={"phone"} className="bold white" />

<div className="my-2">
<Turnstile
appearance="always"
theme="light"
action="passkey-phone"
// sitekey={"3x00000000000000000000FF"}
sitekey={import.meta.env.VITE_CLOUDFLARE_TURNSTILE_SITE_KEY}
onVerify={handleTurnstileVerify}
/>
</div>
{import.meta.env.PROD && (
<div className="my-2">
<Turnstile
appearance="always"
theme="light"
action="passkey-phone"
// sitekey={"3x00000000000000000000FF"}
sitekey={import.meta.env.VITE_CLOUDFLARE_TURNSTILE_SITE_KEY}
onVerify={handleTurnstileVerify}
/>
</div>
)}
</div>
</div>
<Animate
Expand Down
2 changes: 2 additions & 0 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
class Bill < ApplicationRecord
extend T::Sig

has_paper_trail

belongs_to :legislator
belongs_to :sway_locale

Expand Down
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
class User < ApplicationRecord
extend T::Sig

has_paper_trail

CREDENTIAL_MIN_AMOUNT = 1

attr_accessor :webauthn_id
Expand Down
3 changes: 3 additions & 0 deletions app/models/user_vote.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
class UserVote < ApplicationRecord
extend T::Sig

# https://github.com/paper-trail-gem/paper_trail?tab=readme-ov-file#1b-installation
has_paper_trail limit: nil

belongs_to :user
belongs_to :bill

Expand Down
5 changes: 5 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
require 'active_support/core_ext/integer/time'

Rails.application.configure do
# fixes '(irb):1:in `<main>': Tried to load unspecified class: ActiveSupport::TimeWithZone (Psych::DisallowedClass)'
# when calling 'Model.first.versions.first.reify'
# https://github.com/paper-trail-gem/paper_trail/issues/1364
# https://github.com/paper-trail-gem/paper_trail/pull/1451
config.active_record.use_yaml_unsafe_load = true
# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded any time
Expand Down
7 changes: 7 additions & 0 deletions config/initializers/paper_trail.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://github.com/paper-trail-gem/paper_trail?tab=readme-ov-file#1e-configuration

PaperTrail.config.enabled = true # default
PaperTrail.config.has_paper_trail_defaults = {
on: %i[create update destroy] # default
}
PaperTrail.config.version_limit = 3 # default
38 changes: 38 additions & 0 deletions db/migrate/20240619002401_create_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This migration creates the `versions` table, the only schema PT requires.
# All other migrations PT provides are optional.
class CreateVersions < ActiveRecord::Migration[7.1]

# The largest text column available in all supported RDBMS is
# 1024^3 - 1 bytes, roughly one gibibyte. We specify a size
# so that MySQL will use `longtext` instead of `text`. Otherwise,
# when serializing very large objects, `text` might not be big enough.
TEXT_BYTES = 1_073_741_823

def change
create_table :versions do |t|
t.string :item_type, null: false
t.bigint :item_id, null: false
t.string :event, null: false
t.string :whodunnit
t.text :object, limit: TEXT_BYTES

# Known issue in MySQL: fractional second precision
# -------------------------------------------------
#
# MySQL timestamp columns do not support fractional seconds unless
# defined with "fractional seconds precision". MySQL users should manually
# add fractional seconds precision to this migration, specifically, to
# the `created_at` column.
# (https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html)
#
# MySQL users should also upgrade to at least rails 4.2, which is the first
# version of ActiveRecord with support for fractional seconds in MySQL.
# (https://github.com/rails/rails/pull/14359)
#
# MySQL users should use the following line for `created_at`
# t.datetime :created_at, limit: 6
t.datetime :created_at
end
add_index :versions, %i[item_type item_id]
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8694b9e

Please sign in to comment.