Skip to content

Commit

Permalink
Add sponsorship model and associations; implement sponsorship synchro…
Browse files Browse the repository at this point in the history
…nization in Account model
  • Loading branch information
andrew committed Nov 25, 2024
1 parent 08dc58d commit 6ee79d2
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 1 deletion.
24 changes: 24 additions & 0 deletions app/models/account.rb
Original file line number Diff line number Diff line change
@@ -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') }

Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions app/models/sponsorship.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions app/views/accounts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,54 @@
<% end %>
</div>
<% end %>
<% if @account.sponsorships_as_maintainer.any? %>
<h2>Sponsors</h2>

<h5>Active Sponsors</h5>

<ul>
<% @account.sponsorships_as_maintainer.active.includes(:funder).each do |sponsorship| %>
<li>
<%= link_to sponsorship.funder.login, account_path(sponsorship.funder.login) %>
</li>
<% end %>
</ul>

<h5>Past Sponsors</h5>

<ul>
<% @account.sponsorships_as_maintainer.inactive.includes(:funder).each do |sponsorship| %>
<li>
<%= link_to sponsorship.funder.login, account_path(sponsorship.funder.login) %>
</li>
<% end %>
</ul>
<% end %>
<% end %>
<% if @account.sponsorships_as_funder.any? %>
<h2>Sponsorships</h2>

<h5>Active Sponsorships</h5>

<ul>
<% @account.sponsorships_as_funder.active.includes(:maintainer).each do |sponsorship| %>
<li>
<%= link_to sponsorship.maintainer.login, account_path(sponsorship.maintainer.login) %>
</li>
<% end %>
</ul>

<h5>Past Sponsorships</h5>

<ul>
<% @account.sponsorships_as_funder.inactive.includes(:maintainer).each do |sponsorship| %>
<li>
<%= link_to sponsorship.maintainer.login, account_path(sponsorship.maintainer.login) %>
</li>
<% end %>
</ul>
<% end %>

</div>
11 changes: 11 additions & 0 deletions db/migrate/20241125184831_create_sponsorships.rb
Original file line number Diff line number Diff line change
@@ -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
15 changes: 14 additions & 1 deletion db/schema.rb

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

11 changes: 11 additions & 0 deletions test/fixtures/sponsorships.yml
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions test/models/sponsorship_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class SponsorshipTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 6ee79d2

Please sign in to comment.