Skip to content

Commit

Permalink
Merge pull request consuldemocracy#2285 from consul/feature/2278#budg…
Browse files Browse the repository at this point in the history
…et_draft_phase

Feature/2278#budget draft phase
  • Loading branch information
bertocq authored Jan 9, 2018
2 parents 13cbd2a + 555990b commit 311c6c6
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 39 deletions.
2 changes: 2 additions & 0 deletions app/controllers/budgets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class BudgetsController < ApplicationController
include FeatureFlags
include BudgetsHelper
feature_flag :budgets

load_and_authorize_resource
Expand All @@ -9,6 +10,7 @@ class BudgetsController < ApplicationController
respond_to :html, :js

def show
raise ActionController::RoutingError, 'Not Found' unless budget_published?(@budget)
end

def index
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/budgets_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,9 @@ def current_ballot
def investment_tags_select_options
Budget::Investment.tags_on(:valuation).order(:name).select(:name).distinct
end

def budget_published?(budget)
!budget.drafting? || current_user&.administrator?
end

end
8 changes: 7 additions & 1 deletion app/models/budget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ class Budget < ActiveRecord::Base
include Measurable
include Sluggable

PHASES = %w(accepting reviewing selecting valuating balloting reviewing_ballots finished).freeze
PHASES = %w(drafting accepting reviewing selecting valuating balloting
reviewing_ballots finished).freeze
CURRENCY_SYMBOLS = %w( $ £ ¥).freeze

validates :name, presence: true, uniqueness: true
Expand All @@ -19,6 +20,7 @@ class Budget < ActiveRecord::Base
before_validation :sanitize_descriptions

scope :on_hold, -> { where(phase: %w(reviewing valuating reviewing_ballots")) }
scope :drafting, -> { where(phase: "drafting") }
scope :accepting, -> { where(phase: "accepting") }
scope :reviewing, -> { where(phase: "reviewing") }
scope :selecting, -> { where(phase: "selecting") }
Expand All @@ -41,6 +43,10 @@ def self.title_max_length
80
end

def drafting?
phase == "drafting"
end

def accepting?
phase == "accepting"
end
Expand Down
18 changes: 10 additions & 8 deletions app/views/budgets/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
</thead>
<tbody>
<% @budgets.each do |budget| %>
<tr>
<td>
<%= link_to budget.name, budget %>
</td>
<td>
<%= budget.translated_phase %>
</td>
</tr>
<% if budget_published?(budget) %>
<tr>
<td>
<%= link_to budget.name, budget %>
</td>
<td>
<%= budget.translated_phase %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
Expand Down
1 change: 1 addition & 0 deletions config/locales/en/budgets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ en:
unselected_title: Investments not selected for balloting phase
unselected: See investments not selected for balloting phase
phase:
drafting: Draft (Not visible to the public)
accepting: Accepting projects
reviewing: Reviewing projects
selecting: Selecting projects
Expand Down
1 change: 1 addition & 0 deletions config/locales/es/budgets.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ es:
unselected_title: Propuestas no seleccionadas para la votación final
unselected: Ver las propuestas no seleccionadas para la votación final
phase:
drafting: Borrador (No visible para el público)
accepting: Presentación de proyectos
reviewing: Revisión interna de proyectos
selecting: Fase de apoyos
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20180108182839_add_drafting_phase_to_budget.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDraftingPhaseToBudget < ActiveRecord::Migration
def change
add_column :budgets, :description_drafting, :text
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171220010000) do
ActiveRecord::Schema.define(version: 20180108182839) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -201,6 +201,7 @@
t.text "description_reviewing_ballots"
t.text "description_finished"
t.string "slug"
t.text "description_drafting"
end

create_table "campaigns", force: :cascade do |t|
Expand Down
5 changes: 5 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
sequence(:name) { |n| "Budget #{n}" }
currency_symbol "€"
phase 'accepting'
description_drafting "This budget is drafting"
description_accepting "This budget is accepting"
description_reviewing "This budget is reviewing"
description_selecting "This budget is selecting"
Expand All @@ -231,6 +232,10 @@
description_reviewing_ballots "This budget is reviewing ballots"
description_finished "This budget is finished"

trait :drafting do
phase 'drafting'
end

trait :accepting do
phase 'accepting'
end
Expand Down
40 changes: 20 additions & 20 deletions spec/features/admin/budgets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@
end

scenario 'Filters by phase' do
budget1 = create(:budget)
budget2 = create(:budget, :accepting)
budget3 = create(:budget, :selecting)
budget4 = create(:budget, :balloting)
budget5 = create(:budget, :finished)
drafting_budget = create(:budget, :drafting)
accepting_budget = create(:budget, :accepting)
selecting_budget = create(:budget, :selecting)
balloting_budget = create(:budget, :balloting)
finished_budget = create(:budget, :finished)

visit admin_budgets_path
expect(page).to have_content(budget1.name)
expect(page).to have_content(budget2.name)
expect(page).to have_content(budget3.name)
expect(page).to have_content(budget4.name)
expect(page).not_to have_content(budget5.name)
expect(page).to have_content(drafting_budget.name)
expect(page).to have_content(accepting_budget.name)
expect(page).to have_content(selecting_budget.name)
expect(page).to have_content(balloting_budget.name)
expect(page).not_to have_content(finished_budget.name)

click_link 'Finished'
expect(page).not_to have_content(budget1.name)
expect(page).not_to have_content(budget2.name)
expect(page).not_to have_content(budget3.name)
expect(page).not_to have_content(budget4.name)
expect(page).to have_content(budget5.name)
expect(page).not_to have_content(drafting_budget.name)
expect(page).not_to have_content(accepting_budget.name)
expect(page).not_to have_content(selecting_budget.name)
expect(page).not_to have_content(balloting_budget.name)
expect(page).to have_content(finished_budget.name)

click_link 'Open'
expect(page).to have_content(budget1.name)
expect(page).to have_content(budget2.name)
expect(page).to have_content(budget3.name)
expect(page).to have_content(budget4.name)
expect(page).not_to have_content(budget5.name)
expect(page).to have_content(drafting_budget.name)
expect(page).to have_content(accepting_budget.name)
expect(page).to have_content(selecting_budget.name)
expect(page).to have_content(balloting_budget.name)
expect(page).not_to have_content(finished_budget.name)
end

scenario 'Open filter is properly highlighted' do
Expand Down
63 changes: 57 additions & 6 deletions spec/features/budgets/budgets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

feature 'Budgets' do

let(:budget) { create(:budget) }
let(:level_two_user) { create(:user, :level_two) }

scenario 'Index' do
budgets = create_list(:budget, 3)
visit budgets_path
Expand All @@ -11,7 +14,6 @@
context 'Show' do

scenario "List all groups" do
budget = create(:budget)
group1 = create(:budget_group, budget: budget)
group2 = create(:budget_group, budget: budget)

Expand Down Expand Up @@ -61,9 +63,59 @@

end

context 'Accepting' do
context "In Drafting phase" do

let(:admin) { create(:administrator).user }

background do
logout
budget.update(phase: 'drafting')
end

context "Listed" do
scenario "Not listed to guest users at the public budgets list" do
visit budgets_path

expect(page).not_to have_content(budget.name)
end

scenario "Not listed to logged users at the public budgets list" do
login_as(level_two_user)
visit budgets_path

expect(page).not_to have_content(budget.name)
end

scenario "Is listed to admins at the public budgets list" do
login_as(admin)
visit budgets_path

expect(page).to have_content(budget.name)
end
end

context "Shown" do
scenario "Not accesible to guest users" do
expect { visit budget_path(budget) }.to raise_error(ActionController::RoutingError)
end

scenario "Not accesible to logged users" do
login_as(level_two_user)

let(:budget) { create(:budget) }
expect { visit budget_path(budget) }.to raise_error(ActionController::RoutingError)
end

scenario "Is accesible to admin users" do
login_as(admin)
visit budget_path(budget)

expect(page.status_code).to eq(200)
end
end

end

context 'Accepting' do

background do
budget.update(phase: 'accepting')
Expand All @@ -72,8 +124,7 @@
context "Permissions" do

scenario "Verified user" do
user = create(:user, :level_two)
login_as(user)
login_as(level_two_user)

visit budget_path(budget)

Expand All @@ -97,4 +148,4 @@

end
end
end
end
2 changes: 1 addition & 1 deletion spec/features/budgets/results_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
end

scenario "If budget is in a phase different from finished results can't be accessed" do
budget.update phase: (Budget::PHASES - ["finished"]).sample
budget.update(phase: (Budget::PHASES - ['drafting', 'finished']).sample)
visit budget_path(budget)
expect(page).not_to have_link "See results"

Expand Down
8 changes: 6 additions & 2 deletions spec/features/tags/budget_investments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let!(:heading) { create(:budget_heading, name: "More hospitals", group: group) }
let!(:tag_medio_ambiente) { create(:tag, :category, name: 'Medio Ambiente') }
let!(:tag_economia) { create(:tag, :category, name: 'Economía') }
let(:admin) { create(:administrator).user }

scenario 'Index' do
earth = create(:budget_investment, heading: heading, tag_list: tag_medio_ambiente.name)
Expand Down Expand Up @@ -185,6 +186,7 @@
Budget::PHASES.each do |phase|
budget.update(phase: phase)

login_as(admin) if budget.drafting?
visit budget_investments_path(budget, heading_id: heading.id)

within "#tag-cloud" do
Expand All @@ -204,6 +206,7 @@
end
end

login_as(admin) if budget.drafting?
visit budget_path(budget)
click_link group.name

Expand All @@ -230,6 +233,7 @@
Budget::PHASES.each do |phase|
budget.update(phase: phase)

login_as(admin) if budget.drafting?
visit budget_investments_path(budget, heading_id: heading.id)

within "#categories" do
Expand All @@ -249,6 +253,7 @@
end
end

login_as(admin) if budget.drafting?
visit budget_path(budget)
click_link group.name

Expand Down Expand Up @@ -282,8 +287,7 @@
investment.set_tag_list_on(:valuation, 'Education')
investment.save

admin = create(:administrator)
login_as(admin.user)
login_as(admin)

visit admin_budget_budget_investment_path(budget, investment)
click_link 'Edit classification'
Expand Down
9 changes: 9 additions & 0 deletions spec/models/budget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
end

it "produces auxiliary methods" do
budget.phase = "drafting"
expect(budget).to be_drafting

budget.phase = "accepting"
expect(budget).to be_accepting

Expand All @@ -63,6 +66,9 @@
end

it "on_hold?" do
budget.phase = "drafting"
expect(budget).not_to be_on_hold

budget.phase = "accepting"
expect(budget).not_to be_on_hold

Expand All @@ -86,6 +92,9 @@
end

it "balloting_or_later?" do
budget.phase = "drafting"
expect(budget).not_to be_balloting_or_later

budget.phase = "accepting"
expect(budget).not_to be_balloting_or_later

Expand Down

0 comments on commit 311c6c6

Please sign in to comment.