From be554a629c00ae2cb9943bf6cb148d317b2a70d7 Mon Sep 17 00:00:00 2001 From: rgarcia Date: Mon, 15 Jan 2018 18:37:32 +0100 Subject: [PATCH] Make current_budget accessible in controller and views --- app/controllers/application_controller.rb | 5 +++++ .../application_controller_spec.rb | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 spec/controllers/application_controller_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 495a25314..f03a05239 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -25,6 +25,7 @@ class ApplicationController < ActionController::Base layout :set_layout respond_to :html + helper_method :current_budget private @@ -120,4 +121,8 @@ class ApplicationController < ActionController::Base params[:filter] ||= "selected" end end + + def current_budget + Budget.current + end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 000000000..f11068b65 --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' + +describe ApplicationController do + + describe "#current_budget" do + + it "returns the last budget that is not in draft phase" do + old_budget = create(:budget, phase: "finished", created_at: 2.years.ago) + previous_budget = create(:budget, phase: "accepting", created_at: 1.year.ago) + current_budget = create(:budget, phase: "accepting", created_at: 1.month.ago) + next_budget = create(:budget, phase: "drafting", created_at: 1.week.ago) + + budget = subject.instance_eval{ current_budget } + expect(budget).to eq(current_budget) + end + + end + +end \ No newline at end of file