Skip to content

Commit

Permalink
Add funding source associations and project count methods to Allocati…
Browse files Browse the repository at this point in the history
…on model
  • Loading branch information
andrew committed Nov 18, 2024
1 parent 04de993 commit 81f0a22
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
21 changes: 21 additions & 0 deletions app/models/allocation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Allocation < ApplicationRecord
belongs_to :fund
has_many :project_allocations, dependent: :destroy
has_many :projects, through: :project_allocations
has_many :funding_sources, through: :project_allocations

scope :displayable, -> { where('funded_projects_count > 0') }

Expand Down Expand Up @@ -133,6 +134,10 @@ def total_allocated_cents
project_allocations.sum(:amount_cents)
end

def funders_count
1 # TODO
end

def find_possible_projects
fund.possible_projects.active.with_license
end
Expand All @@ -151,4 +156,20 @@ def group_projects_by_funding_source_and_platform
def payout
project_allocations.find_each(&:choose_payout_method)
end

def github_sponsored_projects_count
funding_sources.select{|fs| fs.platform == 'github.com'}.length
end

def open_collective_projects_count
funding_sources.select{|fs| fs.platform == 'opencollective.com'}.length
end

def other_projects_count
funding_sources.approved.reject{|fs| ['opencollective.com', 'github.com'].include?(fs.platform) }.length
end

def invited_projects_count
project_allocations.select{|pa| pa.funding_source.blank?}.length
end
end
9 changes: 5 additions & 4 deletions app/models/funding_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ class FundingSource < ApplicationRecord
has_many :projects
has_many :project_allocations

scope :platform, ->(platform) { where(platform: platform) }

APPROVED_PLATFORMS = ['opencollective.com', 'github.com', 'patreon.com',
'liberapay.com', 'ko-fi.com', 'funding.communitybridge.org', 'buymeacoffee.com']

'liberapay.com', 'ko-fi.com', 'funding.communitybridge.org', 'buymeacoffee.com', 'paypal.com']

scope :platform, ->(platform) { where(platform: platform) }
scope :approved, -> { where(platform: APPROVED_PLATFORMS) }

def approved?
APPROVED_PLATFORMS.include?(platform)
end
Expand Down
39 changes: 37 additions & 2 deletions app/views/funds/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,47 @@
</p>

<% if @allocation %>
<h2>Most Recent Allocation</h2>
<h2><%= @allocation.created_at.strftime('%B %d, %Y') %></h2>

<strong>Allocation Details</strong>

<p>
Total funded:
<%= number_to_currency @allocation.total_cents/100.0 %>
</p>

<p>
Funders:
<%= @allocation.funders_count %>
</p>

<p>
Projects: <%= @allocation.funded_projects_count %>
</p>

<strong>Distribution Details</strong>

<p>
<%= number_to_currency @allocation.total_cents/100.0 %> on <%= @allocation.created_at.strftime('%B %d, %Y') %>
GitHub Sponsors:
<%= @allocation.github_sponsored_projects_count %>
</p>

<p>
Open Collective:
<%= @allocation.open_collective_projects_count %>
</p>

<p>
Other Sources:
<%= @allocation.other_projects_count %>
</p>

<p>
Projects Invited
<%= @allocation.invited_projects_count %>
</p>


<table class="table">
<thead>
<tr>
Expand Down

0 comments on commit 81f0a22

Please sign in to comment.