diff --git a/app/controllers/admin/budgets_controller.rb b/app/controllers/admin/budgets_controller.rb index e5e73a346..0b373c0bd 100644 --- a/app/controllers/admin/budgets_controller.rb +++ b/app/controllers/admin/budgets_controller.rb @@ -15,7 +15,17 @@ class Admin::BudgetsController < Admin::BaseController end def new - @budget = Budget.new + end + + def edit + end + + def update + if @budget.update(budget_params) + redirect_to admin_budget_path(@budget), notice: t('admin.budgets.update.notice') + else + render :edit + end end def create @@ -30,7 +40,9 @@ class Admin::BudgetsController < Admin::BaseController private def budget_params - params.require(:budget).permit(:name, :description, :phase, :currency_symbol) + descriptions = Budget::PHASES.map{|p| "description_#{p}"}.map(&:to_sym) + valid_attributes = [:name, :phase, :currency_symbol] + descriptions + params.require(:budget).permit(*valid_attributes) end end diff --git a/app/controllers/budgets/investments_controller.rb b/app/controllers/budgets/investments_controller.rb index 490b4047d..0084e71db 100644 --- a/app/controllers/budgets/investments_controller.rb +++ b/app/controllers/budgets/investments_controller.rb @@ -80,7 +80,7 @@ module Budgets end def investment_params - params.require(:budget_investment).permit(:title, :description, :external_url, :heading_id, :terms_of_service) + params.require(:budget_investment).permit(:title, :description, :external_url, :heading_id, :terms_of_service, :location) end def load_ballot diff --git a/app/helpers/budgets_helper.rb b/app/helpers/budgets_helper.rb index aa6684b2d..0e3594b0a 100644 --- a/app/helpers/budgets_helper.rb +++ b/app/helpers/budgets_helper.rb @@ -1,7 +1,7 @@ module BudgetsHelper def budget_phases_select_options - Budget::VALID_PHASES.map { |ph| [ t("budget.phase.#{ph}"), ph ] } + Budget::PHASES.map { |ph| [ t("budget.phase.#{ph}"), ph ] } end def budget_currency_symbol_select_options diff --git a/app/models/abilities/administrator.rb b/app/models/abilities/administrator.rb index 8d7694b32..582d6a9e0 100644 --- a/app/models/abilities/administrator.rb +++ b/app/models/abilities/administrator.rb @@ -46,7 +46,7 @@ module Abilities can [:read, :create, :update, :destroy], Budget::Group can [:read, :create, :update, :destroy], Budget::Heading can [:hide, :update, :toggle_selection], Budget::Investment - can :valuate, Budget::Investment, budget: { valuating: true } + can :valuate, Budget::Investment, budget: { phase: 'valuating' } can :create, Budget::ValuatorAssignment can [:search, :edit, :update, :create, :index, :destroy], Banner diff --git a/app/models/abilities/valuator.rb b/app/models/abilities/valuator.rb index d3979a640..1462e2630 100644 --- a/app/models/abilities/valuator.rb +++ b/app/models/abilities/valuator.rb @@ -5,7 +5,7 @@ module Abilities def initialize(user) valuator = user.valuator can [:read, :update, :valuate], SpendingProposal - can [:read, :update, :valuate], Budget::Investment, id: valuator.investment_ids, budget: { valuating: true } + can [:read, :update, :valuate], Budget::Investment, id: valuator.investment_ids, budget: { phase: 'valuating' } end end end diff --git a/app/models/budget.rb b/app/models/budget.rb index 4d6f1341f..e2acf1de8 100644 --- a/app/models/budget.rb +++ b/app/models/budget.rb @@ -1,12 +1,12 @@ class Budget < ActiveRecord::Base - include Sanitizable + include Measurable - VALID_PHASES = %W{on_hold accepting selecting balloting finished} - CURRENCY_SYMBOLS = %W{€ $ £ ¥} + PHASES = %w(accepting reviewing selecting valuating balloting reviewing_ballots finished).freeze + CURRENCY_SYMBOLS = %w(€ $ £ ¥).freeze validates :name, presence: true - validates :phase, inclusion: { in: VALID_PHASES } + validates :phase, inclusion: { in: PHASES } validates :currency_symbol, presence: true has_many :investments, dependent: :destroy @@ -14,35 +14,59 @@ class Budget < ActiveRecord::Base has_many :groups, dependent: :destroy has_many :headings, through: :groups - scope :on_hold, -> { where(phase: "on_hold") } + before_validation :sanitize_descriptions + + scope :on_hold, -> { where(phase: %w(reviewing valuating reviewing_ballots")) } scope :accepting, -> { where(phase: "accepting") } + scope :reviewing, -> { where(phase: "reviewing") } scope :selecting, -> { where(phase: "selecting") } + scope :valuating, -> { where(phase: "valuating") } scope :balloting, -> { where(phase: "balloting") } + scope :reviewing_ballots, -> { where(phase: "reviewing_ballots") } scope :finished, -> { where(phase: "finished") } scope :current, -> { where.not(phase: "finished") } - scope :valuating, -> { where(valuating: true) } - def on_hold? - phase == "on_hold" + def description + self.send("description_#{self.phase}").try(:html_safe) + end + + def self.description_max_length + 2000 end def accepting? phase == "accepting" end + def reviewing? + phase == "reviewing" + end + def selecting? phase == "selecting" end + def valuating? + phase == "valuating" + end + def balloting? phase == "balloting" end + def reviewing_ballots? + phase == "reviewing_ballots" + end + def finished? phase == "finished" end + def on_hold? + reviewing? || valuating? || reviewing_ballots? + end + def current? !finished? end @@ -69,5 +93,15 @@ class Budget < ActiveRecord::Base def formatted_heading_amount_spent(heading) formatted_amount(amount_spent(heading)) end + + private + + def sanitize_descriptions + s = WYSIWYGSanitizer.new + PHASES.each do |phase| + sanitized = s.sanitize(self.send("description_#{phase}")) + self.send("description_#{phase}=", sanitized) + end + end end diff --git a/app/views/admin/budgets/_form.html.erb b/app/views/admin/budgets/_form.html.erb new file mode 100644 index 000000000..22a774b76 --- /dev/null +++ b/app/views/admin/budgets/_form.html.erb @@ -0,0 +1,18 @@ +<%= form_for [:admin, @budget] do |f| %> + + <%= f.text_field :name, maxlength: Budget.title_max_length %> + + <% Budget::PHASES.each do |phase| %> + <%= f.cktext_area "description_#{phase}", maxlength: Budget.description_max_length, ckeditor: { language: I18n.locale } %> + <% end %> + +
<%= t('admin.budgets.show.phase') %>: <%= t("budget.phase.#{@budget.phase}") %> | @@ -13,4 +13,4 @@
- <%= t("budget.investments.show.code") %> - <%= investment.id %> + <%= t("budget.investments.show.code_html", code: investment.id) %>
+ <% if investment.location.present? %> ++ <%= t("budget.investments.show.location_html", location: investment.location) %> +
+ <% end %> + <%= safe_html_with_links investment.description.html_safe %> <% if investment.external_url.present? %> diff --git a/app/views/budgets/investments/_sidebar.html.erb b/app/views/budgets/investments/_sidebar.html.erb index f57ab8ef6..173132257 100644 --- a/app/views/budgets/investments/_sidebar.html.erb +++ b/app/views/budgets/investments/_sidebar.html.erb @@ -1,12 +1,18 @@ -<%= link_to @budget, class: "back" do %> - - <%= t("spending_proposals.index.sidebar.back") %> -<% end %> - +<% if @budget.accepting? %> + <% if current_user && current_user.level_two_or_three_verified? %> + <%= link_to t("budget.investments.index.sidebar.create"), new_budget_investment_path, class: "button budget expanded" %> + <% else %> +diff --git a/app/views/budgets/show.html.erb b/app/views/budgets/show.html.erb index 66207ac7c..ce4e46c71 100644 --- a/app/views/budgets/show.html.erb +++ b/app/views/budgets/show.html.erb @@ -9,7 +9,8 @@
<%= @budget.description %>
+ + <%= @budget.description %>