Files
nairobi/app/controllers/valuation/spending_proposals_controller.rb
rgarcia bb3c4c6399 adds consistency to ruby code style
Keep a blank line before and after private
Keep a blank line before and after protected
Remove extra empty line at class body end
Remove extra blank line
Add final newline
Use 2 (not 3) spaces for indentation
Use 2 (not 4) spaces for indentation
Remove space before comma
Add space after comma
Remove trailing whitespaces
Remove unnecessary spacing
Use snake_case for variable names
Do not use then for multi-line if
Remove unused block argument - i
Use the new Ruby 1.9 hash syntax
Remove unused assignment to variable
Indent when as deep as case
Align attributes
Align end with def
2016-11-15 11:18:43 +01:00

80 lines
2.7 KiB
Ruby

class Valuation::SpendingProposalsController < Valuation::BaseController
include FeatureFlags
feature_flag :spending_proposals
before_action :restrict_access_to_assigned_items, only: [:show, :edit, :valuate]
has_filters %w{valuating valuation_finished}, only: :index
load_and_authorize_resource
def index
@geozone_filters = geozone_filters
if current_user.valuator?
@spending_proposals = SpendingProposal.scoped_filter(params_for_current_valuator, @current_filter).order(cached_votes_up: :desc).page(params[:page])
else
@spending_proposals = SpendingProposal.none.page(params[:page])
end
end
def valuate
if valid_price_params? && @spending_proposal.update(valuation_params)
if @spending_proposal.unfeasible_email_pending?
@spending_proposal.send_unfeasible_email
end
redirect_to valuation_spending_proposal_path(@spending_proposal), notice: t('valuation.spending_proposals.notice.valuate')
else
render action: :edit
end
end
private
def geozone_filters
spending_proposals = SpendingProposal.by_valuator(current_user.valuator.try(:id)).valuation_open.all.to_a
[ { name: t('valuation.spending_proposals.index.geozone_filter_all'),
id: nil,
pending_count: spending_proposals.size
},
{ name: t('geozones.none'),
id: 'all',
pending_count: spending_proposals.count{|x| x.geozone_id.nil?}
}
] + Geozone.all.order(name: :asc).collect do |g|
{ name: g.name,
id: g.id,
pending_count: spending_proposals.count{|x| x.geozone_id == g.id}
}
end.select{ |x| x[:pending_count] > 0 }
end
def valuation_params
params[:spending_proposal][:feasible] = nil if params[:spending_proposal][:feasible] == 'nil'
params.require(:spending_proposal).permit(:price, :price_first_year, :price_explanation, :feasible, :feasible_explanation, :time_scope, :valuation_finished, :internal_comments)
end
def params_for_current_valuator
params.merge({valuator_id: current_user.valuator.id})
end
def restrict_access_to_assigned_items
raise ActionController::RoutingError.new('Not Found') unless current_user.administrator? || ValuationAssignment.exists?(spending_proposal_id: params[:id], valuator_id: current_user.valuator.id)
end
def valid_price_params?
if /\D/.match params[:spending_proposal][:price]
@spending_proposal.errors.add(:price, I18n.t('spending_proposals.wrong_price_format'))
end
if /\D/.match params[:spending_proposal][:price_first_year]
@spending_proposal.errors.add(:price_first_year, I18n.t('spending_proposals.wrong_price_format'))
end
@spending_proposal.errors.empty?
end
end