From 70513330282008d034acd3faf024e3981c0364e6 Mon Sep 17 00:00:00 2001 From: kikito Date: Fri, 3 Jun 2016 18:50:28 +0200 Subject: [PATCH] Adds actions to application_controller and investments_controller --- app/controllers/application_controller.rb | 4 + .../budgets/investments_controller.rb | 97 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 app/controllers/budgets/investments_controller.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 3e1b78d31..6384f19e3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -80,6 +80,10 @@ class ApplicationController < ActionController::Base @spending_proposal_votes = current_user ? current_user.spending_proposal_votes(spending_proposals) : {} end + def set_budget_investment_votes(budget_investments) + @budget_investments_votes = current_user ? current_user.budget_investment_votes(budget_investments) : {} + end + def set_comment_flags(comments) @comment_flags = current_user ? current_user.comment_flags(comments) : {} end diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb new file mode 100644 index 000000000..7ab31b0da --- /dev/null +++ b/app/controllers/budgets/investments_controller.rb @@ -0,0 +1,97 @@ +module Budgets + class InvestmentsController < ApplicationController + include FeatureFlags + include CommentableActions + include FlagActions + + before_action :authenticate_user!, except: [:index, :show] + + load_and_authorize_resource :budget + load_and_authorize_resource :investment, through: :budget, class: "Budget::Investment" + + before_action -> { flash.now[:notice] = flash[:notice].html_safe if flash[:html_safe] && flash[:notice] } + before_action :load_ballot, only: [:index, :show] + before_action :load_heading, only: [:index, :show] + + feature_flag :budgets + + has_orders %w{most_voted newest oldest}, only: :show + has_orders ->(c){ c.instance_variable_get(:@budget).balloting? ? %w{random price} : %w{random confidence_score} }, only: :index + + invisible_captcha only: [:create, :update], honeypot: :subtitle + + 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) + end + + def new + end + + def show + @commentable = @investment + @comment_tree = CommentTree.new(@commentable, params[:page], @current_order) + set_comment_flags(@comment_tree.comments) + set_budget_investment_votes(@investment) + end + + def create + @investment.author = current_user + + if @investment.save + notice = t('flash.actions.create.budget_investment', activity: "#{t('layouts.header.my_activity_link')}") + redirect_to @investment, notice: notice, flash: { html_safe: true } + else + render :new + end + end + + def destroy + investment.destroy + redirect_to user_path(current_user, filter: 'budget_investments'), notice: t('flash.actions.destroy.budget_investment') + end + + def vote + @investment.register_selection(current_user) + set_budget_investment_votes(@investment) + end + + private + + def investment_params + 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) + + 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 + + def load_heading + @heading = @budget.headings.find(params[:heading_id]) if params[:geozone_id].present? + end + + end + +end