Skip to content

Commit

Permalink
v1.6.11
Browse files Browse the repository at this point in the history
Merge pull request #544 from TechforgoodCAST/develop
  • Loading branch information
suninthesky authored Nov 23, 2017
2 parents d9c7077 + d7dabc2 commit 9226a42
Show file tree
Hide file tree
Showing 37 changed files with 581 additions and 198 deletions.
3 changes: 3 additions & 0 deletions app/admin/fund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ def find_resource
column :open_data
column "Geo", :geo_area
column "Grants", :grant_count
column "Requests" do |fund|
fund.requests.size
end
column :last_updated do |fund|
fund.updated_at.strftime("%F")
end
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/active_admin.sass
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
// For example, to change the default status-tag color:
//
// .status_tag { background: #6090DB; }
.col-geo
max-width: 120px
word-wrap: break-word
1 change: 1 addition & 0 deletions app/assets/stylesheets/v2/modules/form.sass
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
text-transform: capitalize

.field_with_errors > input
.field_with_errors > select
border-color: map-get($colors, 'red')

.label
Expand Down
5 changes: 5 additions & 0 deletions app/cells/progress/eligibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Progress
class Eligibility < Base
def initialize(*args)
super
return if @fund.stub?
@status = @proposal.eligible_status(@fund.slug)
end

Expand All @@ -10,13 +11,16 @@ def label
end

def indicator
return " #{@position}" if @status.nil?
{
-1 => 'bg-blue', 0 => 'bg-red', 1 => 'bg-green'
}[@status] << " #{@position}"
end

def message
case @status
when nil
"Missing"
when -1
link_to(
'Complete this check',
Expand All @@ -31,6 +35,7 @@ def message
end

def highlight
return '' if @status.nil?
'bg-light-blue' unless @status == 1
end
end
Expand Down
27 changes: 27 additions & 0 deletions app/cells/progress/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Progress
class Request < Base
def initialize(*args)
super
end

def label
'Update fund details'
end

def indicator
"bg-blue #{@position}"
end

def message
if @proposal.recipient.requests.find_by(fund_id: @fund.id)
tag.a('Requested', class: 'btn fs15 slate border-silver disabled')
else
link_to('Request', url_helpers.requests_path(fund: @fund), method: :post, class: 'fs15 btn white bg-blue shadow')
end
end

def highlight
'bg-light-blue'
end
end
end
9 changes: 7 additions & 2 deletions app/cells/progress/suitability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Progress
class Suitability < Base
def initialize(*args)
super
return if @fund.stub?
@status = @proposal.suitability[@fund.slug]
.all_values_for('score')
.count { |s| s > 0.2 }
Expand All @@ -12,7 +13,9 @@ def label
end

def indicator
"#{@position} " << if @status == 5
"#{@position} " << if @status.nil?
'grey'
elsif @status == 5
'bg-green'
elsif @status >= 2
'bg-yellow'
Expand All @@ -22,7 +25,9 @@ def indicator
end

def message
if @status == 5
if @status.nil?
"Missing"
elsif @status == 5
link_to('Good', '#suitability', link_opts.merge(class: 'green'))
elsif @status >= 2
link_to('Review', '#suitability', link_opts.merge(class: 'yellow'))
Expand Down
9 changes: 8 additions & 1 deletion app/cells/progress_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ def proposal_summary

def steps
opts = { proposal: model, fund: options[:fund] }
[
return [
::Progress::Eligibility.new(opts.merge(position: 'bot')),
::Progress::Suitability.new(opts.merge(position: 'top bot')),
::Progress::Apply.new(opts.merge(position: 'top'))
] unless options[:fund].stub?

[
::Progress::Request.new(opts.merge(position: 'bot')),
::Progress::Eligibility.new(opts.merge(position: 'top bot')),
::Progress::Suitability.new(opts.merge(position: 'top bot')),
::Progress::Apply.new(opts.merge(position: 'top'))
]
end
end
4 changes: 4 additions & 0 deletions app/contexts/fund_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ def self.policy_class
def featured
fund.featured
end

def stub?
fund.stub?
end
end
12 changes: 11 additions & 1 deletion app/controllers/funds_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
class FundsController < ApplicationController
before_action :ensure_logged_in, :update_legacy_suitability, except: :sources
before_action :query, only: %i[index themed]
before_action :query, :stub_query, only: %i[index themed]

def show
@fund = Fund.includes(:funder).find_by_hashid(params[:id])
authorize FundContext.new(@fund, @proposal)
render :stub if @fund.stub?
end

def index
query = @query.order_by(@proposal, params[:sort])
@fund_count = query.size
@funds = Kaminari.paginate_array(query).page(params[:page])

@fund_stubs = @stub_query.order("RANDOM()").limit(5)

end

def themed
Expand Down Expand Up @@ -46,4 +50,10 @@ def query
.eligibility(@proposal, params[:eligibility])
.duration(@proposal, params[:duration])
end

def stub_query
@stub_query = Fund.stubs
.includes(:funder)
.eligibility(@proposal, 'eligible_noquiz')
end
end
16 changes: 16 additions & 0 deletions app/controllers/requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class RequestsController < ApplicationController
before_action :ensure_logged_in

def create
authorize :request
fund = Fund.find_by_hashid(params[:fund])
Request.create(fund: fund, recipient: @recipient, message: params[:message])
redirect_to proposal_fund_path(@proposal, fund)
end

private

def user_not_authorised
redirect_to account_upgrade_path(@recipient)
end
end
4 changes: 2 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ def v2_layout? # TODO: remove @ v2
eligibilities: %i[new],
enquiries: %i[new],
errors: %i[not_found gone internal_server_error],
# feedback: %i[edit new],
feedback: %i[edit new create],
funds: %i[index themed show hidden],
pages: %i[about faq forfunders privacy terms], # preview?
password_resets: %i[new create edit update],
proposals: %i[index], # edit new update
public_funds: %i[index show themed],
recipients: %i[edit update],
# sessions: %i[new],
sessions: %i[new],
signup: %i[user create_user], # funder, granted_access, unauthorised
# signup_proposals: %i[edit new],
# signup_recipients: %i[edit new],
Expand Down
8 changes: 8 additions & 0 deletions app/models/fund.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Fund < ApplicationRecord
include ActionView::Helpers::NumberHelper

scope :active, -> { where(state: 'active') }
scope :stubs, -> { where(state: 'stub') }
scope :newer_than, ->(date) { where('updated_at > ?', date) }
scope :recent, -> { order updated_at: :desc }

Expand All @@ -10,6 +11,7 @@ class Fund < ApplicationRecord
belongs_to :funder

has_many :enquiries, dependent: :destroy
has_many :requests, dependent: :destroy

has_many :fund_themes, dependent: :destroy
has_many :themes, through: :fund_themes
Expand Down Expand Up @@ -93,6 +95,8 @@ def self.order_by(proposal, col)

def self.eligibility(proposal, state)
case state
when 'eligible_noquiz'
where slug: proposal.eligible_noquiz.keys
when 'eligible'
where slug: proposal.eligible_funds.keys
when 'ineligible'
Expand Down Expand Up @@ -144,6 +148,10 @@ def tags?
tags.count.positive?
end

def stub?
%w[stub draft].include? state
end

def key_criteria_html
markdown(key_criteria)
end
Expand Down
13 changes: 12 additions & 1 deletion app/models/proposal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,14 @@ def initial_recommendation
# Check::Suitability::Quiz.new(self, Fund.active),
]
)
check_stub_eligibility = Check::Each.new(
[
Check::Eligibility::Location.new,
Check::Eligibility::Theme.new,
]
)
update_columns(
eligibility: check_eligibility.call_each(self, Fund.active),
eligibility: check_stub_eligibility.call_each(self, Fund.stubs).merge(check_eligibility.call_each(self, Fund.active)),
suitability: check_suitability.call_each_with_total(self, Fund.active)
)
end
Expand All @@ -162,6 +168,11 @@ def suitable_funds
suitability.sort_by { |fund| fund[1]['total'] }.reverse
end

def eligible_noquiz
# Same as eligible_funds except doesn't check for the quiz
eligibility.select { |f, fund| fund.all_values_for('eligible').exclude?(false) }
end

def eligible_funds
eligibility.select { |f, _| eligible_status(f) == 1 }
end
Expand Down
1 change: 1 addition & 0 deletions app/models/recipient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Recipient < ApplicationRecord
has_one :subscription, dependent: :destroy
has_many :users, as: :organisation, dependent: :destroy
has_many :proposals
has_many :requests
has_many :countries, -> { distinct }, through: :proposals
has_many :districts, -> { distinct }, through: :proposals
has_many :answers, as: :category, dependent: :destroy
Expand Down
8 changes: 8 additions & 0 deletions app/models/request.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Request < ApplicationRecord
belongs_to :recipient
belongs_to :fund

validates :recipient, :fund, presence: true
validates :fund, uniqueness: { scope: :recipient,
message: 'only one request per fund / recipient' }
end
4 changes: 2 additions & 2 deletions app/policies/fund_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def show?

def v1_show?
return false unless record.fund && record.proposal
return true if record.fund.featured
return true if record.fund.featured || record.fund.stub?
return true if user.subscription_active?
record.proposal
.suitable_funds
Expand All @@ -18,7 +18,7 @@ def v1_show?

def v2_show?
return false unless record
return true if record.featured
return true if record.featured || record.stub?
return true if user.subscription_active?
user.reveals.include?(record.slug)
end
Expand Down
5 changes: 5 additions & 0 deletions app/policies/request_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RequestPolicy < ApplicationPolicy
def create?
true
end
end
35 changes: 35 additions & 0 deletions app/services/check/eligibility/theme.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Check
module Eligibility
class Theme
include Check::Base

def call(proposal, fund)
validate_call proposal, fund
proposal_themes = get_proposal_themes(proposal)
match_score = match_themes(proposal_themes, fund.themes)
return eligible true if match_score.size > 0
eligible false
end

private

def get_proposal_themes(proposal)
# get all the themes from a proposal, including the related themes attached to each theme
proposal_themes = {}
proposal.themes.each do |theme|
proposal_themes[theme.name] = 1
theme.related.each do |theme_name, theme_score|
proposal_themes[theme_name] = [theme_score, proposal_themes[theme_name] || 0].max
end
end
proposal_themes
end

def match_themes(proposal_themes, fund_themes)
# return the themes from the proposal that match those from the fund
fund_theme_names = fund_themes.pluck(:name)
proposal_themes.select { |theme, score| fund_theme_names.include? theme }
end
end
end
end
Loading

0 comments on commit 9226a42

Please sign in to comment.