Refactors apply_filters_and_search to inside investment model

This commit is contained in:
kikito
2016-12-14 13:39:54 +01:00
parent 4434a601d3
commit 8d60ea1d3c
3 changed files with 13 additions and 31 deletions

View File

@@ -24,8 +24,8 @@ module Budgets
respond_to :html, :js
def index
@investments = apply_filters_and_search(@investments).send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render
set_budget_investment_votes(@investments)
@investments = @investments.apply_filters_and_search(params).send("sort_by_#{@current_order}").page(params[:page]).per(10).for_render
end
def new
@@ -74,26 +74,6 @@ module Budgets
params.require(:investment).permit(:title, :description, :external_url, :heading_id, :terms_of_service)
end
def apply_filters_and_search(investments)
if params[:heading_id].blank?
@filter_heading_name = t('geozones.none')
else
@filter_heading = @budget.headings.find(params[:heading_id])
@filter_heading_name = @filter_heading.name
end
investments = investments.by_heading(params[:heading_id].presence || @budget.headings.first)
if params[:unfeasible].present?
investments = investments.unfeasible
else
investments = @budget.balloting? ? investments.feasible.valuation_finished : investments.not_unfeasible
end
investments = investments.search(params[:search]) if params[:search].present?
investments
end
def load_ballot
@ballot = Budget::Ballot.where(user: current_user, budget: @budget).first_or_create
end

View File

@@ -4,10 +4,11 @@ class Management::Budgets::InvestmentsController < Management::BaseController
load_resource :investment, through: :budget, class: 'Budget::Investment'
before_action :only_verified_users, except: :print
before_action :load_heading, only: [:index, :show, :print]
def index
@investments = apply_filters_and_search(@investments).page(params[:page])
set_investment_votes(@investments)
@investments = @investments.apply_filters_and_search(params).page(params[:page])
end
def new
@@ -35,8 +36,8 @@ class Management::Budgets::InvestmentsController < Management::BaseController
end
def print
@investments = apply_filters_and_search(@investments).order(cached_votes_up: :desc).for_render.limit(15)
set_investment_votes(@investments)
@investments = @investments.apply_filters_and_search(params).order(cached_votes_up: :desc).for_render.limit(15)
end
private
@@ -53,14 +54,8 @@ class Management::Budgets::InvestmentsController < Management::BaseController
check_verified_user t("management.budget_investments.alert.unverified_user")
end
def apply_filters_and_search(investments)
investments = params[:unfeasible].present? ? investments.unfeasible : investments.not_unfeasible
if params[:heading_id].present?
investments = investments.by_heading(params[:heading_id])
@heading = Budget::Heading.find(params[:heading_id])
end
investments = investments.search(params[:search]) if params[:search].present?
investments
def load_heading
@heading = @budget.headings.find(params[:heading_id]) if params[:heading_id].present?
end
end

View File

@@ -205,6 +205,13 @@ class Budget
budget.formatted_amount(price)
end
def self.apply_filters_and_search(params)
investments = params[:unfeasible].present? ? unfeasible : not_unfeasible
investments = investments.by_heading(params[:heading_id]) if params[:heading_id].present?
investments = investments.search(params[:search]) if params[:search].present?
investments
end
private
def set_denormalized_ids