refactors investment filters

This commit is contained in:
rgarcia
2017-05-06 03:37:29 +02:00
parent fa825185f5
commit c0488c3b3c
12 changed files with 85 additions and 59 deletions

View File

@@ -46,8 +46,8 @@ class Budget
scope :not_unfeasible, -> { where.not(feasibility: "unfeasible") }
scope :undecided, -> { where(feasibility: "undecided") }
scope :with_supports, -> { where('cached_votes_up > 0') }
scope :selected, -> { where(selected: true) }
scope :unselected, -> { where(selected: false) }
scope :selected, -> { feasible.where(selected: true) }
scope :unselected, -> { feasible.where(selected: false) }
scope :last_week, -> { where("created_at >= ?", 7.days.ago)}
scope :by_group, -> (group_id) { where(group_id: group_id) }
@@ -234,17 +234,9 @@ class Budget
budget.formatted_amount(price)
end
def self.apply_filters_and_search(budget, params)
def self.apply_filters_and_search(budget, params, current_filter=nil)
investments = all
if budget.balloting? && params[:unfeasible].blank? && params[:unselected].blank?
investments = investments.selected
elsif budget.balloting? && params[:unfeasible].blank? && params[:unselected].present?
investments = investments.feasible.unselected
else
investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible
end
investments = investments.send(current_filter) if current_filter.present?
investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present?
investments = investments.search(params[:search]) if params[:search].present?
investments
@@ -256,5 +248,6 @@ class Budget
self.group_id = self.heading.try(:group_id) if self.heading_id_changed?
self.budget_id ||= self.heading.try(:group).try(:budget_id)
end
end
end