Skip to content

Commit

Permalink
allocation models
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Aug 21, 2024
1 parent c1d4d30 commit 2320e3c
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 4 deletions.
16 changes: 16 additions & 0 deletions app/models/allocation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Allocation < ApplicationRecord
belongs_to :fund
has_many :project_allocations

def calculate_funded_projects
# get a list of all possible projects for this fund

# calculate scores for each project

# sort projects by score

# allocate funds to the top projects

# save the project allocations
end
end
2 changes: 2 additions & 0 deletions app/models/fund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Fund < ApplicationRecord
validates :name, presence: true
validates :slug, presence: true, uniqueness: true

has_many :allocations

def self.import_from_topic(topic)
topic_url = "https://awesome.ecosyste.ms/api/v1/topics/#{topic}"

Expand Down
9 changes: 6 additions & 3 deletions app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
require 'csv'

class Project < ApplicationRecord

validates :url, presence: true, uniqueness: { case_sensitive: false }

has_many :project_allocations

scope :active, -> { where("(repository ->> 'archived') = ?", 'false') }
scope :archived, -> { where("(repository ->> 'archived') = ?", 'true') }

Expand Down Expand Up @@ -56,7 +56,8 @@ def first_created
end

def sync
check_url
status = check_url
return if status.blank?
fetch_repository
fetch_owner
fetch_dependencies
Expand Down Expand Up @@ -88,8 +89,10 @@ def check_url
puts "Duplicate url #{url}"
puts e.class
destroy
return nil
rescue
puts "Error checking url for #{url}"
return nil
end

def combine_keywords
Expand Down
7 changes: 7 additions & 0 deletions app/models/project_allocation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ProjectAllocation < ApplicationRecord
belongs_to :project
belongs_to :allocation
belongs_to :fund


end
13 changes: 13 additions & 0 deletions db/migrate/20240821145347_create_allocations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateAllocations < ActiveRecord::Migration[7.2]
def change
create_table :allocations do |t|
t.integer :fund_id
t.integer :year
t.integer :month
t.integer :total_cents
t.integer :funded_projects_count

t.timestamps
end
end
end
13 changes: 13 additions & 0 deletions db/migrate/20240821145853_create_project_allocations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateProjectAllocations < ActiveRecord::Migration[7.2]
def change
create_table :project_allocations do |t|
t.integer :allocation_id
t.integer :project_id
t.integer :fund_id
t.integer :amount_cents
t.float :score

t.timestamps
end
end
end
22 changes: 21 additions & 1 deletion db/schema.rb

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

15 changes: 15 additions & 0 deletions test/fixtures/allocations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
fund_id: 1
year: 1
month: 1
total_cents: 1
funded_projects_count: 1

two:
fund_id: 1
year: 1
month: 1
total_cents: 1
funded_projects_count: 1
15 changes: 15 additions & 0 deletions test/fixtures/project_allocations.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
allocation_id: 1
project_id: 1
fund_id: 1
amount_cents: 1
score: 1.5

two:
allocation_id: 1
project_id: 1
fund_id: 1
amount_cents: 1
score: 1.5
7 changes: 7 additions & 0 deletions test/models/allocation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

class AllocationTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/project_allocation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require "test_helper"

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

0 comments on commit 2320e3c

Please sign in to comment.