Extract concern to handle investments filters
We were defining the same filters in three different controllers. We were also adding a method in the ApplicationController which only made sense in the same three controllers.
This commit is contained in:
@@ -111,14 +111,6 @@ class ApplicationController < ActionController::Base
|
||||
end
|
||||
end
|
||||
|
||||
def set_default_budget_filter
|
||||
if @budget&.balloting? || @budget&.publishing_prices?
|
||||
params[:filter] ||= "selected"
|
||||
elsif @budget&.finished?
|
||||
params[:filter] ||= "winners"
|
||||
end
|
||||
end
|
||||
|
||||
def current_budget
|
||||
Budget.current
|
||||
end
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
module Budgets
|
||||
class GroupsController < ApplicationController
|
||||
include InvestmentFilters
|
||||
|
||||
before_action :load_budget
|
||||
before_action :load_group
|
||||
authorize_resource :budget
|
||||
authorize_resource :group, class: "Budget::Group"
|
||||
|
||||
before_action :set_default_budget_filter, only: :show
|
||||
has_filters %w[not_unfeasible feasible unfeasible unselected selected winners], only: [:show]
|
||||
before_action :set_default_investment_filter, only: :show
|
||||
has_filters investment_filters, only: [:show]
|
||||
|
||||
def show
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ module Budgets
|
||||
include RandomSeed
|
||||
include ImageAttributes
|
||||
include Translatable
|
||||
include InvestmentFilters
|
||||
|
||||
PER_PAGE = 10
|
||||
|
||||
@@ -20,7 +21,7 @@ module Budgets
|
||||
before_action :load_heading, only: [:index, :show]
|
||||
before_action :set_random_seed, only: :index
|
||||
before_action :load_categories, only: [:index, :new, :create, :edit, :update]
|
||||
before_action :set_default_budget_filter, only: :index
|
||||
before_action :set_default_investment_filter, only: :index
|
||||
before_action :set_view, only: :index
|
||||
before_action :load_content_blocks, only: :index
|
||||
|
||||
@@ -31,8 +32,7 @@ module Budgets
|
||||
has_orders %w[most_voted newest oldest], only: :show
|
||||
has_orders ->(c) { c.instance_variable_get(:@budget).investments_orders }, only: :index
|
||||
|
||||
valid_filters = %w[not_unfeasible feasible unfeasible unselected selected winners]
|
||||
has_filters valid_filters, only: [:index, :show, :suggest]
|
||||
has_filters investment_filters, only: [:index, :show, :suggest]
|
||||
|
||||
invisible_captcha only: [:create, :update], honeypot: :subtitle, scope: :budget_investment
|
||||
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
class BudgetsController < ApplicationController
|
||||
include FeatureFlags
|
||||
include BudgetsHelper
|
||||
include InvestmentFilters
|
||||
feature_flag :budgets
|
||||
|
||||
before_action :load_budget, only: :show
|
||||
load_and_authorize_resource
|
||||
before_action :set_default_budget_filter, only: :show
|
||||
has_filters %w[not_unfeasible feasible unfeasible unselected selected winners], only: :show
|
||||
before_action :set_default_investment_filter, only: :show
|
||||
has_filters investment_filters, only: :show
|
||||
|
||||
respond_to :html, :js
|
||||
|
||||
|
||||
17
app/controllers/concerns/investment_filters.rb
Normal file
17
app/controllers/concerns/investment_filters.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module InvestmentFilters
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
class_methods do
|
||||
def investment_filters
|
||||
%w[not_unfeasible feasible unfeasible unselected selected winners]
|
||||
end
|
||||
end
|
||||
|
||||
def set_default_investment_filter
|
||||
if @budget&.balloting? || @budget&.publishing_prices?
|
||||
params[:filter] ||= "selected"
|
||||
elsif @budget&.finished?
|
||||
params[:filter] ||= "winners"
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user