From 6ee79d28ad49a48ed3046b660a74b4d90df3d09c Mon Sep 17 00:00:00 2001 From: Andrew Nesbitt Date: Mon, 25 Nov 2024 19:00:31 +0000 Subject: [PATCH] Add sponsorship model and associations; implement sponsorship synchronization in Account model --- app/models/account.rb | 24 +++++++++ app/models/sponsorship.rb | 7 +++ app/views/accounts/show.html.erb | 49 +++++++++++++++++++ .../20241125184831_create_sponsorships.rb | 11 +++++ db/schema.rb | 15 +++++- test/fixtures/sponsorships.yml | 11 +++++ test/models/sponsorship_test.rb | 7 +++ 7 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 app/models/sponsorship.rb create mode 100644 db/migrate/20241125184831_create_sponsorships.rb create mode 100644 test/fixtures/sponsorships.yml create mode 100644 test/models/sponsorship_test.rb diff --git a/app/models/account.rb b/app/models/account.rb index 471a038..f6af118 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,6 +1,12 @@ class Account < ApplicationRecord validates :login, presence: true + has_many :sponsorships_as_funder, class_name: "Sponsorship", foreign_key: :funder_id + has_many :sponsorships_as_maintainer, class_name: "Sponsorship", foreign_key: :maintainer_id + + has_many :maintained_accounts, through: :sponsorships_as_funder, source: :maintainer + has_many :funder_accounts, through: :sponsorships_as_maintainer, source: :funder + scope :has_sponsors_listing, -> { where(has_sponsors_listing: true) } scope :has_sponsors_profile, -> { where('length(sponsor_profile::text) > 2') } @@ -143,4 +149,22 @@ def fetch_all_sponsors(filter: nil) return sponsors end + + def sync_sponsorships + sponsors = fetch_all_sponsors(filter: 'active') + sponsors.each do |login| + funder = Account.find_or_create_by(login: login) + # TODO sync funder + s = Sponsorship.find_or_create_by(funder: funder, maintainer: self) + s.update(status: 'active') + end + + past_sponsors = fetch_all_sponsors(filter: 'inactive') + past_sponsors.each do |login| + funder = Account.find_or_create_by(login: login) + # TODO sync funder + s = Sponsorship.find_or_create_by(funder: funder, maintainer: self) + s.update(status: 'inactive') + end + end end diff --git a/app/models/sponsorship.rb b/app/models/sponsorship.rb new file mode 100644 index 0000000..55c5fe8 --- /dev/null +++ b/app/models/sponsorship.rb @@ -0,0 +1,7 @@ +class Sponsorship < ApplicationRecord + belongs_to :funder, class_name: 'Account' + belongs_to :maintainer, class_name: 'Account' + + scope :active, -> { where(status: 'active') } + scope :inactive, -> { where(status: 'inactive') } +end diff --git a/app/views/accounts/show.html.erb b/app/views/accounts/show.html.erb index 9bf90e4..7d36780 100644 --- a/app/views/accounts/show.html.erb +++ b/app/views/accounts/show.html.erb @@ -65,5 +65,54 @@ <% end %> <% end %> + + <% if @account.sponsorships_as_maintainer.any? %> +

Sponsors

+ +
Active Sponsors
+ + + +
Past Sponsors
+ + + <% end %> <% end %> + + <% if @account.sponsorships_as_funder.any? %> +

Sponsorships

+ +
Active Sponsorships
+ + + +
Past Sponsorships
+ + + <% end %> + \ No newline at end of file diff --git a/db/migrate/20241125184831_create_sponsorships.rb b/db/migrate/20241125184831_create_sponsorships.rb new file mode 100644 index 0000000..4de03dc --- /dev/null +++ b/db/migrate/20241125184831_create_sponsorships.rb @@ -0,0 +1,11 @@ +class CreateSponsorships < ActiveRecord::Migration[8.0] + def change + create_table :sponsorships do |t| + t.references :funder, null: false, foreign_key: { to_table: :accounts } + t.references :maintainer, null: false, foreign_key: { to_table: :accounts } + t.string :status + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 6a4a58d..d33636f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_11_25_141413) do +ActiveRecord::Schema[8.0].define(version: 2024_11_25_184831) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -25,4 +25,17 @@ t.datetime "updated_at", null: false t.jsonb "sponsor_profile", default: {} end + + create_table "sponsorships", force: :cascade do |t| + t.bigint "funder_id", null: false + t.bigint "maintainer_id", null: false + t.string "status" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["funder_id"], name: "index_sponsorships_on_funder_id" + t.index ["maintainer_id"], name: "index_sponsorships_on_maintainer_id" + end + + add_foreign_key "sponsorships", "accounts", column: "funder_id" + add_foreign_key "sponsorships", "accounts", column: "maintainer_id" end diff --git a/test/fixtures/sponsorships.yml b/test/fixtures/sponsorships.yml new file mode 100644 index 0000000..7bfc261 --- /dev/null +++ b/test/fixtures/sponsorships.yml @@ -0,0 +1,11 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + funder: one + maintainer: one + status: MyString + +two: + funder: two + maintainer: two + status: MyString diff --git a/test/models/sponsorship_test.rb b/test/models/sponsorship_test.rb new file mode 100644 index 0000000..93291d9 --- /dev/null +++ b/test/models/sponsorship_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class SponsorshipTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end