diff --git a/app/models/container.rb b/app/models/container.rb index 96eece04f..b55141205 100644 --- a/app/models/container.rb +++ b/app/models/container.rb @@ -6,7 +6,7 @@ class Container < ActiveRecord::Base def subtitler(container) num = container.gardens.uniq.count - s = (num > 1 || num == 0) ? "s are" : " is" - return "#{num.to_s} garden#{s} using this container" + s = num > 1 || num.zero? ? "s are" : " is" + "#{num} garden#{s} using this container" end end diff --git a/app/views/containers/_actions.html.haml b/app/views/containers/_actions.html.haml index 662630b75..95bf704fe 100644 --- a/app/views/containers/_actions.html.haml +++ b/app/views/containers/_actions.html.haml @@ -2,8 +2,3 @@ = render 'shared/buttons/edit', path: edit_container_path(container) .pull-right = render 'shared/buttons/delete', path: container_path(container) - -- if can? :create, container - = link_to new_container_path, class: 'btn btn-default btn-xs' do - %span.glyphicon.glyphicon-grain{ title: "Create new container" } - Create new container diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index d2b8fd01c..ff9aa6553 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -38,6 +38,7 @@ %li= link_to t('.seeds'), seeds_path %li= link_to t('.plantings'), plantings_path %li= link_to t('.harvests'), harvests_path + %li= link_to t('.containers'), containers_path %li.dropdown< %a.dropdown-toggle{ 'data-toggle': 'dropdown', href: members_path } = t('.community') diff --git a/config/locales/en.yml b/config/locales/en.yml index c494975e7..1fccfbc49 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -144,6 +144,8 @@ en: browse_members: Browse Members community: Community community_map: Community Map + container: Container + containers: Containers crop_wrangling: Crop Wrangling crops: Crops current_memberlogin_name: "%{current_memberlogin_name}" @@ -229,6 +231,7 @@ en: planting: Please sign in or sign up to plant something. post: Please sign in or sign up to post. seed: Please sign in or sign up to add seeds. + container: Not authorized. Only admins can create containers. manage: all: Not authorized to %{action} %{subject}. read: diff --git a/spec/controllers/containers_controller_spec.rb b/spec/controllers/containers_controller_spec.rb index 2a66cecd8..8fe2951cf 100644 --- a/spec/controllers/containers_controller_spec.rb +++ b/spec/controllers/containers_controller_spec.rb @@ -3,9 +3,88 @@ require 'rails_helper' RSpec.describe ContainersController, type: :controller do include Devise::Test::ControllerHelpers let(:valid_params) { { description: 'My second Container' } } - let(:container) { FactoryBot.create :container } let(:member) { FactoryBot.create(:member) } let(:admin_member) { FactoryBot.create(:admin) } + + context "when not signed in" do + describe 'GET new' do + before { get :new, id: container.to_param } + + it { expect(response).to redirect_to(root_path) } + end + + describe 'PUT create' do + before { put :create, container: valid_params } + + it { expect(response).to redirect_to(root_path) } + end + + describe 'changing existing records' do + before do + allow(Container).to receive(:find).and_return(:container) + expect(container).not_to receive(:save) + expect(container).not_to receive(:save!) + expect(container).not_to receive(:update) + expect(container).not_to receive(:update!) + expect(container).not_to receive(:destroy) + end + + describe 'GET edit' do + before { get :edit, id: container.to_param } + + it { expect(response).to redirect_to(root_path) } + end + + describe 'POST update' do + before { post :update, id: container.to_param, container: valid_params } + + it { expect(response).to redirect_to(root_path) } + end + + describe 'DELETE' do + before { delete :destroy, id: container.to_param, params: { container: valid_params } } + + it { expect(response).to redirect_to(root_path) } + end + end + end + + context "when signed in as a member" do + before(:each) { sign_in member } + + let!(:member) { FactoryBot.create(:member) } + + describe "for any container" do + let(:any_container) { double('container') } + + before do + expect(Container).to receive(:find).and_return(:any_container) + expect(any_container).not_to receive(:save) + expect(any_container).not_to receive(:save!) + expect(any_container).not_to receive(:update) + expect(any_container).not_to receive(:update!) + expect(any_container).not_to receive(:destroy) + end + + describe 'GET edit' do + before { get :edit, id: any_container.to_param } + + it { expect(response).to redirect_to(root_path) } + end + + describe 'POST update' do + before { post :update, id: any_container.to_param, container: valid_params } + + it { expect(response).to redirect_to(root_path) } + end + + describe 'DELETE' do + before { delete :destroy, id: any_container.to_param, params: { container: valid_params } } + + it { expect(response).to redirect_to(root_path) } + end + end + end end diff --git a/spec/models/container_spec.rb b/spec/models/container_spec.rb index 4cc3b0374..bac3e8e30 100644 --- a/spec/models/container_spec.rb +++ b/spec/models/container_spec.rb @@ -1,6 +1,10 @@ require 'rails_helper' describe Container do + let(:owner) { FactoryBot.create(:member) } + let(:garden) { FactoryBot.create(:garden, owner: owner, name: 'Free Carrots') } + let(:container) { FactoryBot.create(:container, description: "fake hole in the ground") } + it "should have a description" do container = FactoryBot.build(:container, description: "organic") container.should be_valid @@ -17,7 +21,17 @@ describe Container do end it "doesn't allow a description with only spaces" do - garden = FactoryBot.build(:container, description: " ") - garden.should_not be_valid + container = FactoryBot.build(:container, description: " ") + container.should_not be_valid + end + + it "destroys plots when deleted" do + container = FactoryBot.create(:container, description: "Massive Flower Pot") + @plot1 = FactoryBot.create(:plot, garden: garden, container: container) + @plot2 = FactoryBot.create(:plot, garden: garden, container: container) + container.plots.size.should eq(2) + all = Plot.count + container.destroy + Plot.count.should eq(all - 2) end end diff --git a/spec/models/plot_spec.rb b/spec/models/plot_spec.rb index c18bc9122..4a3a16724 100644 --- a/spec/models/plot_spec.rb +++ b/spec/models/plot_spec.rb @@ -2,14 +2,16 @@ require 'rails_helper' describe Plot do let(:owner) { FactoryBot.create(:member) } - let(:garden) { FactoryBot.create(:garden, owner: owner, name: "Magic Beanstalk") } - let(:container) { FactoryBot.create(:container, description: "vertical") } + let(:garden) { FactoryBot.create(:garden) } + let(:container) { FactoryBot.create(:container) } let(:plot) { FactoryBot.create(:plot, garden: garden, container: container) } context "has valid attributes" do - it "should have a garden and container" do - plot.garden.should == garden - plot.container.should == container + it "should have a garden" do + plot.garden.description.should == "This is a **totally** cool garden" + end + it "should have a container" do + plot.container.description.should == "homemade swamp" end end end diff --git a/spec/views/containers/edit.html.haml_spec.rb b/spec/views/containers/edit.html.haml_spec.rb new file mode 100644 index 000000000..3b9d61737 --- /dev/null +++ b/spec/views/containers/edit.html.haml_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe "containers/edit" do + before(:each) do + @owner = FactoryBot.create(:admin_member) + sign_in @owner + controller.stub(:current_user) { @owner } + @container = assign(:container, FactoryBot.create(:container)) + render + end + + it "renders the edit container form" do + assert_select "form", action: containers_path, method: "post" do + assert_select "input#container_description", name: "container[description]" + end + end +end diff --git a/spec/views/containers/new.html.haml_spec.rb b/spec/views/containers/new.html.haml_spec.rb new file mode 100644 index 000000000..f56d956fb --- /dev/null +++ b/spec/views/containers/new.html.haml_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe "containers/new" do + before(:each) do + @owner = FactoryBot.create(:admin_member) + sign_in @owner + controller.stub(:current_user) { @owner } + @container = assign(:container, FactoryBot.create(:container)) + render + end + + it "renders new container form" do + assert_select "form", action: containers_path, method: "post" do + assert_select "input#container_description", name: "container[description]" + end + end +end diff --git a/spec/views/containers/show.html.haml_spec.rb b/spec/views/containers/show.html.haml_spec.rb new file mode 100644 index 000000000..a18ffdd66 --- /dev/null +++ b/spec/views/containers/show.html.haml_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe "containers/show" do + subject { render } + + let!(:container) { FactoryBot.create(:container, description: "Hot Sauce") } + + before do + controller.stub(:current_user) { nil } + assign(:container, container) + render + end + + describe "renders a container with no gardens" do + it { is_expected.to have_content "There are no gardens to display." } + end +end