From ee3beb05067f07bfd39c4ba5ffba45b79ef5381a Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Fri, 16 May 2025 10:04:02 -0400 Subject: [PATCH] Add neighborhood airtable post records --- app/models/concerns/has_table_sync.rb | 24 +++++++++++++++++++ app/models/neighborhood/post.rb | 7 ++++++ ...0250516140014_create_neighborhood_posts.rb | 11 +++++++++ db/schema.rb | 10 +++++++- 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 app/models/concerns/has_table_sync.rb create mode 100644 app/models/neighborhood/post.rb create mode 100644 db/migrate/20250516140014_create_neighborhood_posts.rb diff --git a/app/models/concerns/has_table_sync.rb b/app/models/concerns/has_table_sync.rb new file mode 100644 index 0000000..408f24b --- /dev/null +++ b/app/models/concerns/has_table_sync.rb @@ -0,0 +1,24 @@ +module HasTableSync + extend ActiveSupport::Concern + + validates :airtable_fields, presence: true + validates :airtable_id, presence: true, uniqueness: true + + class_methods do + def has_table_sync(pat:, base:, table:) + @table_sync_pat = pat + @table_sync_base = base + @table_sync_table = table + + @table = Norairrecord.table(pat, base, table) + + def pull_all_from_airtable! + records = @table.all + + records.each do |record| + find_or_initialize_by(airtable_id: record.id).update(airtable_fields: record.fields) + end + end + end + end +end diff --git a/app/models/neighborhood/post.rb b/app/models/neighborhood/post.rb new file mode 100644 index 0000000..9be81f4 --- /dev/null +++ b/app/models/neighborhood/post.rb @@ -0,0 +1,7 @@ +class Neighborhood::Post < ApplicationRecord + include HasTableSync + + has_table_sync base: "appnsN4MzbnfMY0ai", + table: "tbl0iKxglbySiEbB4", + pat: ENV["NEIGHBORHOOD_AIRTABLE_PAT"] +end diff --git a/db/migrate/20250516140014_create_neighborhood_posts.rb b/db/migrate/20250516140014_create_neighborhood_posts.rb new file mode 100644 index 0000000..221bc00 --- /dev/null +++ b/db/migrate/20250516140014_create_neighborhood_posts.rb @@ -0,0 +1,11 @@ +class CreateNeighborhoodPosts < ActiveRecord::Migration[8.0] + def change + create_table :neighborhood_posts do |t| + t.string :airtable_id, null: false + t.index :airtable_id, unique: true + t.jsonb :airtable_fields + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b2170da..bc28fde 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,9 +10,9 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_05_14_212714) do create_schema "pganalyze" +ActiveRecord::Schema[8.0].define(version: 2025_05_16_140014) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "pg_stat_statements" @@ -264,6 +264,14 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_14_212714) do t.index ["user_id"], name: "index_mailing_addresses_on_user_id" end + create_table "neighborhood_posts", force: :cascade do |t| + t.string "airtable_id", null: false + t.jsonb "airtable_fields" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["airtable_id"], name: "index_neighborhood_posts_on_airtable_id", unique: true + end + create_table "physical_mails", force: :cascade do |t| t.bigint "user_id", null: false t.integer "mission_type", null: false