diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index b012aad38..04a94cce3 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -7,7 +7,7 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController before_action :load_geozones, only: [:new, :create, :edit, :update] def index - @polls = Poll.order(starts_at: :desc) + @polls = Poll.not_budget.order(starts_at: :desc) end def show @@ -22,7 +22,12 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController def create @poll = Poll.new(poll_params.merge(author: current_user)) if @poll.save - redirect_to [:admin, @poll], notice: t("flash.actions.create.poll") + notice = t("flash.actions.create.poll") + if @poll.budget.present? + redirect_to admin_poll_booth_assignments_path(@poll), notice: notice + else + redirect_to [:admin, @poll], notice: notice + end else render :new end @@ -63,7 +68,7 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController def poll_params attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :results_enabled, - :stats_enabled, geozone_ids: [], + :stats_enabled, :budget_id, geozone_ids: [], image_attributes: image_attributes] params.require(:poll).permit(*attributes, translation_params(Poll)) end diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index ed39862b2..769a7998c 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -6,7 +6,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController load_and_authorize_resource :question, class: "Poll::Question" def index - @polls = Poll.all + @polls = Poll.not_budget @search = search_params[:search] @questions = @questions.search(search_params).page(params[:page]).order("created_at DESC") diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index a034846a0..652a9eaae 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -11,7 +11,7 @@ class PollsController < ApplicationController ::Poll::Answer # trigger autoload def index - @polls = @polls.send(@current_filter).includes(:geozones).sort_for_list.page(params[:page]) + @polls = @polls.not_budget.send(@current_filter).includes(:geozones).sort_for_list.page(params[:page]) end def show diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index df48a08fa..e4a47557b 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -96,4 +96,16 @@ module BudgetsHelper !current_user.voted_in_group?(investment.group) && investment.group.headings.count > 1 end + + def link_to_create_budget_poll(budget) + balloting_phase = budget.phases.where(kind: "balloting").first + + link_to t("admin.budgets.index.admin_ballots"), + admin_polls_path(poll: { + name: budget.name, + budget_id: budget.id, + starts_at: balloting_phase.starts_at, + ends_at: balloting_phase.ends_at }), + method: :post + end end diff --git a/app/models/budget.rb b/app/models/budget.rb index ecfacc5c0..28fb8c5ca 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -21,6 +21,8 @@ class Budget < ActiveRecord::Base has_many :headings, through: :groups has_many :phases, class_name: Budget::Phase + has_one :poll + before_validation :sanitize_descriptions after_create :generate_phases diff --git a/app/models/budget/ballot.rb b/app/models/budget/ballot.rb index c35222f3a..7dc618360 100644 --- a/app/models/budget/ballot.rb +++ b/app/models/budget/ballot.rb @@ -70,5 +70,9 @@ class Budget investments.where(group: group).first.heading end + def casted_offline? + budget.poll&.voted_by?(user) + end + end end diff --git a/app/models/budget/investment.rb b/app/models/budget/investment.rb index b033d7404..3bf62613d 100644 --- a/app/models/budget/investment.rb +++ b/app/models/budget/investment.rb @@ -248,6 +248,7 @@ class Budget return :no_ballots_allowed unless budget.balloting? return :different_heading_assigned_html unless ballot.valid_heading?(heading) return :not_enough_money_html if ballot.present? && !enough_money?(ballot) + return :casted_offline if ballot.casted_offline? end def permission_problem(user) diff --git a/app/models/poll.rb b/app/models/poll.rb index 9276cf9fb..553baf036 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -23,6 +23,7 @@ class Poll < ActiveRecord::Base has_and_belongs_to_many :geozones belongs_to :author, -> { with_hidden }, class_name: "User", foreign_key: "author_id" + belongs_to :budget validates_translation :name, presence: true validate :date_range @@ -33,6 +34,7 @@ class Poll < ActiveRecord::Base scope :published, -> { where("published = ?", true) } scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) } scope :public_for_api, -> { all } + scope :not_budget, -> { where(budget_id: nil) } scope :sort_for_list, -> { order(:geozone_restricted, :starts_at, :name) } @@ -71,10 +73,15 @@ class Poll < ActiveRecord::Base end def votable_by?(user) + return false if user_has_an_online_ballot(user) answerable_by?(user) && not_voted_by?(user) end + def user_has_an_online_ballot(user) + budget.present? && budget.ballots.find_by(user: user)&.lines.present? + end + def self.not_voted_by(user) where("polls.id not in (?)", poll_ids_voted_by(user)) end @@ -107,4 +114,7 @@ class Poll < ActiveRecord::Base end end + def budget_poll? + budget.present? + end end diff --git a/app/views/admin/budgets/index.html.erb b/app/views/admin/budgets/index.html.erb index 2be3ae926..c63ee4789 100644 --- a/app/views/admin/budgets/index.html.erb +++ b/app/views/admin/budgets/index.html.erb @@ -17,6 +17,7 @@ <%= t("admin.budgets.index.table_investments") %> <%= t("admin.budgets.index.table_edit_groups") %> <%= t("admin.budgets.index.table_edit_budget") %> + <%= t("admin.budgets.index.table_admin_ballots") %> @@ -39,6 +40,13 @@ <%= link_to t("admin.budgets.index.edit_budget"), edit_admin_budget_path(budget) %> + + <% if budget.poll.present? %> + <%= link_to t("admin.budgets.index.admin_ballots"), admin_poll_booth_assignments_path(budget.poll) %> + <% else %> + <%= link_to_create_budget_poll(budget) %> + <% end %> + <% end %> diff --git a/app/views/admin/poll/booth_assignments/index.html.erb b/app/views/admin/poll/booth_assignments/index.html.erb index 9e99e3179..d28d98989 100644 --- a/app/views/admin/poll/booth_assignments/index.html.erb +++ b/app/views/admin/poll/booth_assignments/index.html.erb @@ -11,7 +11,7 @@ class: "button hollow float-right" %> <% if @booth_assignments.empty? %> -
+
<%= t("admin.poll_booth_assignments.index.no_booths") %>
<% else %> diff --git a/app/views/admin/poll/polls/_subnav.html.erb b/app/views/admin/poll/polls/_subnav.html.erb index 98e23003b..f72176a9e 100644 --- a/app/views/admin/poll/polls/_subnav.html.erb +++ b/app/views/admin/poll/polls/_subnav.html.erb @@ -1,18 +1,20 @@