Added Legislation Proposals controller

This commit is contained in:
María Checa
2017-09-14 12:51:36 +02:00
parent 335399e571
commit 0245605f19
3 changed files with 161 additions and 0 deletions

View File

@@ -14,6 +14,8 @@ class Legislation::ProcessesController < Legislation::BaseController
redirect_to legislation_process_draft_version_path(@process, draft_version)
elsif @process.debate_phase.enabled?
redirect_to debate_legislation_process_path(@process)
elsif @process.proposals_phase.enabled?
redirect_to proposals_legislation_process_path(@process)
else
redirect_to allegations_legislation_process_path(@process)
end
@@ -81,6 +83,17 @@ class Legislation::ProcessesController < Legislation::BaseController
end
end
def proposals
set_process
@phase = :proposals_phase
if @process.proposals_phase.started?
render :proposals
else
render :phase_not_open
end
end
private
def member_method?

View File

@@ -0,0 +1,131 @@
class Legislation::ProposalsController < ApplicationController
include CommentableActions
include FlagActions
load_and_authorize_resource :process
load_and_authorize_resource :proposal, through: :process
before_action :parse_tag_filter, only: :index
before_action :load_categories, only: [:index, :new, :create, :edit, :map, :summary]
before_action :load_geozones, only: [:edit, :map, :summary]
before_action :authenticate_user!, except: [:index, :show, :map, :summary]
invisible_captcha only: [:create, :update], honeypot: :subtitle
has_orders %w{hot_score confidence_score created_at relevance archival_date}, only: :index
has_orders %w{most_voted newest oldest}, only: :show
load_and_authorize_resource
helper_method :resource_model, :resource_name
respond_to :html, :js
def show
super
@notifications = @proposal.notifications
@document = Document.new(documentable: @proposal)
redirect_to proposal_path(@proposal), status: :moved_permanently if request.path != proposal_path(@proposal)
end
def create
@proposal = Legislation::Proposal.new(proposal_params.merge(author: current_user))
recover_documents_from_cache(@proposal)
if @proposal.save
redirect_to share_proposal_path(@proposal), notice: I18n.t('flash.actions.create.proposal')
else
render :new
end
end
def index_customization
discard_archived
load_retired
load_successful_proposals
load_featured unless @proposal_successful_exists
end
def vote
@proposal.register_vote(current_user, 'yes')
set_proposal_votes(@proposal)
end
def retire
if valid_retired_params? && @proposal.update(retired_params.merge(retired_at: Time.current))
redirect_to proposal_path(@proposal), notice: t('proposals.notice.retired')
else
render action: :retire_form
end
end
def retire_form
end
def share
if Setting['proposal_improvement_path'].present?
@proposal_improvement_path = Setting['proposal_improvement_path']
end
end
def vote_featured
@proposal.register_vote(current_user, 'yes')
set_featured_proposal_votes(@proposal)
end
def summary
@proposals = Legislation::Proposal.for_summary
@tag_cloud = tag_cloud
end
private
def proposal_params
params.require(:proposal).permit(:legislation_process_id, :title, :question, :summary, :description, :external_url, :video_url,
:responsible_name, :tag_list, :terms_of_service, :geozone_id,
documents_attributes: [:id, :title, :attachment, :cached_attachment, :user_id] )
end
def retired_params
params.require(:proposal).permit(:retired_reason, :retired_explanation)
end
def valid_retired_params?
@proposal.errors.add(:retired_reason, I18n.t('errors.messages.blank')) if params[:proposal][:retired_reason].blank?
@proposal.errors.add(:retired_explanation, I18n.t('errors.messages.blank')) if params[:proposal][:retired_explanation].blank?
@proposal.errors.empty?
end
def resource_model
Legislation::Proposal
end
def set_featured_proposal_votes(proposals)
@featured_proposals_votes = current_user ? current_user.proposal_votes(proposals) : {}
end
def discard_archived
@resources = @resources.not_archived unless @current_order == "archival_date"
end
def load_retired
if params[:retired].present?
@resources = @resources.retired
@resources = @resources.where(retired_reason: params[:retired]) if Legislation::Proposal::RETIRE_OPTIONS.include?(params[:retired])
else
@resources = @resources.not_retired
end
end
def load_featured
return unless !@advanced_search_terms && @search_terms.blank? && @tag_filter.blank? && params[:retired].blank?
@featured_proposals = Legislation::Proposal.not_archived.sort_by_confidence_score.limit(3)
if @featured_proposals.present?
set_featured_proposal_votes(@featured_proposals)
@resources = @resources.where('proposals.id NOT IN (?)', @featured_proposals.map(&:id))
end
end
def load_successful_proposals
@proposal_successful_exists = Legislation::Proposal.successful.exists?
end
end

View File

@@ -124,10 +124,27 @@ Rails.application.routes.draw do
get :draft_publication
get :allegations
get :result_publication
get :proposals
end
resources :questions, only: [:show] do
resources :answers, only: [:create]
end
resources :proposals do
member do
post :vote
post :vote_featured
put :flag
put :unflag
get :retire_form
get :share
patch :retire
end
collection do
get :map
get :suggest
get :summary
end
end
resources :draft_versions, only: [:show] do
get :go_to_version, on: :collection
get :changes