Skip to content

Instantly share code, notes, and snippets.

@georgiee
Created February 8, 2024 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save georgiee/0df6bb029425ac98ec075aab8193b8cc to your computer and use it in GitHub Desktop.
Save georgiee/0df6bb029425ac98ec075aab8193b8cc to your computer and use it in GitHub Desktop.
Diff/Challenges ActiveRecord Playground
# idea to tinker around with active records with an in memory db from here:
# https://makandracards.com/makandra/32401-activerecord-how-to-use-activerecord-standalone-within-a-ruby-script
# Run this script with `$ ruby my_script.rb`
require 'sqlite3'
require 'active_record'
require 'paper_trail'
# Use `binding.pry` anywhere in this script for easy debugging
require 'pry'
# Connect to an in-memory sqlite3 database
ActiveRecord::Base.establish_connection(
adapter: 'sqlite3',
database: ':memory:'
)
# Define a minimal database schema
ActiveRecord::Schema.define do
create_table :diffs do |t|
t.string :title
t.text :source
end
create_table :challenges do |t|
t.string :title
end
create_table :reviews do |t|
t.references :challenge
t.references :diff
t.bigint :diff_version_id
end
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.json :object
t.json :object_changes
t.datetime :created_at
end
end
# Define the models
class Diff < ActiveRecord::Base
has_paper_trail only: [:source]
# include ActiveSnapshot
has_many :reviews
after_save :create_snapshot_if_reviewed
private
def create_snapshot_if_reviewed
return unless reviews.any?
reviews.find_each do |review|
review.update!(diff_version_id: versions.last.id)
end
end
end
class Challenge < ActiveRecord::Base
has_many :reviews
has_many :diffs, through: :reviews
end
class Review < ActiveRecord::Base
validates_uniqueness_of :diff_id, scope: :challenge_id
belongs_to :challenge
belongs_to :diff
def source
original_source || diff.source
end
def original_diff
self.diff.versions.find_by(id: diff_version_id)&.reify
end
def outdated?
original_diff.present?
end
def original_source
original_diff&.source
end
end
def print_stats(review)
review.reload
print <<~HEREDOC
\nStats:
review.outdated?: #{review.outdated?}
review.source: #{review.source}
actual source: #{review.diff.source}
HEREDOC
end
diff = Diff.create!(title: 'Big Bang Theory', source: 'original source')
challenge = Challenge.create!(title: 'My Challenge')
review = Review.create!(challenge: challenge, diff: diff)
p "-- diff title updated"
diff.update_attribute(:title, "new title")
print_stats(review)
p "-- diff source updated"
diff.update_attribute(:source, "new source")
print_stats(review)
# outputs:
#
# Stats:
# review.outdated?: false
# review.source: original source
# actual source: original source
#
# "-- diff source updated"
#
# Stats:
# review.outdated?: true
# review.source: original source
# actual source: new source
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment