From 02174a0acbfe5685fa6fc7269ba376c407ab9e72 Mon Sep 17 00:00:00 2001 From: Brandon Baker Date: Fri, 30 Nov 2018 11:56:58 -0700 Subject: [PATCH] builds views for containers --- app/controllers/containers_controller.rb | 5 ++-- app/models/ability.rb | 4 +++ app/models/container.rb | 14 ++++----- app/views/containers/_actions.html.haml | 18 +++++------ app/views/containers/_form.html.haml | 19 ++++++++---- app/views/containers/_thumbnail.html.haml | 7 +++++ app/views/containers/edit.html.haml | 9 ++---- app/views/containers/index.html.haml | 30 ++++++++----------- app/views/containers/new.html.haml | 7 ++--- app/views/containers/show.html.haml | 17 ++++++----- .../controllers/containers_controller_spec.rb | 11 +++++++ spec/factories/containers.rb | 2 +- spec/factories/plots.rb | 4 +-- spec/models/plot_spec.rb | 14 +++++++-- 14 files changed, 97 insertions(+), 64 deletions(-) create mode 100644 app/views/containers/_thumbnail.html.haml create mode 100644 spec/controllers/containers_controller_spec.rb diff --git a/app/controllers/containers_controller.rb b/app/controllers/containers_controller.rb index 5e4489486..bfb87fb9d 100644 --- a/app/controllers/containers_controller.rb +++ b/app/controllers/containers_controller.rb @@ -1,9 +1,10 @@ class ContainersController < ApplicationController - before_action :authenticate_member!, except: %i(index show) + # before_action :authenticate_member!, except: %i(index show) + load_and_authorize_resource # GET /containers def index - @containers = Container.all + @containers = Container.all.paginate(page: params[:page]) end # GET /containers/1 diff --git a/app/models/ability.rb b/app/models/ability.rb index deff7c072..dcc3b8300 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -42,6 +42,10 @@ class Ability can :read, AlternateName do |an| an.crop.approved? end + + cannot :create, Container + cannot :update, Container + cannot :destroy, Container end def member_abilities(member) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength diff --git a/app/models/container.rb b/app/models/container.rb index aaed24aba..96eece04f 100644 --- a/app/models/container.rb +++ b/app/models/container.rb @@ -2,11 +2,11 @@ class Container < ActiveRecord::Base has_many :plots, dependent: :destroy has_many :gardens, through: :plots - validates :description, - format: { - with: /\A\w+[\w ()]+\z/ - }, - length: { maximum: 255 }, - presence: true, - uniqueness: true + validates :description, presence: true, uniqueness: true + + 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" + end end diff --git a/app/views/containers/_actions.html.haml b/app/views/containers/_actions.html.haml index d26b9a7e7..662630b75 100644 --- a/app/views/containers/_actions.html.haml +++ b/app/views/containers/_actions.html.haml @@ -1,11 +1,9 @@ -- if can?(:create, container) && can?(:edit, container) && can?(:destroy, container) - .btn-group.container-actions - - if can? :edit, container - = render 'shared/buttons/edit', path: edit_container_path(container) - - if can? :destroy, container - .pull-right= render 'shared/buttons/delete', path: container_path(container) +.btn-group.container-actions + = 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 +- 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/containers/_form.html.haml b/app/views/containers/_form.html.haml index 25dff9445..45d5b4c6d 100644 --- a/app/views/containers/_form.html.haml +++ b/app/views/containers/_form.html.haml @@ -1,4 +1,4 @@ -= form_for @container do |f| += form_for @container, html: { class: 'form-horizontal', role: "form" } do |f| - if @container.errors.any? #error_explanation %h2= "#{pluralize(@container.errors.count, "error")} prohibited this container from being saved:" @@ -6,8 +6,15 @@ - @container.errors.full_messages.each do |message| %li= message - .field - = f.label :description - = f.text_field :description - .actions - = f.submit 'Save' + %h2 Basic information + + .form-group + = f.label :description, class: 'control-label col-md-2' + .col-md-8 + = f.text_field :description, class: 'form-control' + %span.help-block + The name for the container, i.e. "organic", in English (required). + + .form-group + .form-actions.col-md-offset-2.col-md-8 + = f.submit 'Save', class: 'btn btn-primary' diff --git a/app/views/containers/_thumbnail.html.haml b/app/views/containers/_thumbnail.html.haml new file mode 100644 index 000000000..ff235a90e --- /dev/null +++ b/app/views/containers/_thumbnail.html.haml @@ -0,0 +1,7 @@ +- cache cache_key_for(Container, container.id) do + .thumbnail + .container-thumbnail + - if container + .containerinfo + .containername + = link_to container.description, container diff --git a/app/views/containers/edit.html.haml b/app/views/containers/edit.html.haml index a1ba22545..ee890d6b7 100644 --- a/app/views/containers/edit.html.haml +++ b/app/views/containers/edit.html.haml @@ -1,7 +1,4 @@ -%h1 Editing container +- content_for :title, "Edit Container" -= render 'form' - -= link_to 'Show', @container -\| -= link_to 'Back', containers_path +- if can? :update, @container + = render 'form' diff --git a/app/views/containers/index.html.haml b/app/views/containers/index.html.haml index d2e2fda34..040a892bc 100644 --- a/app/views/containers/index.html.haml +++ b/app/views/containers/index.html.haml @@ -1,21 +1,17 @@ -%h1 Listing containers +- content_for :title, 'Containers' -%table - %thead - %tr - %th Description - %th - %th - %th +%p + #{ENV['GROWSTUFF_SITE_NAME']} tracks who's growing what, where. + View any container page to see which of our members have used it. - %tbody - - @containers.each do |container| - %tr - %td= container.description - %td= link_to 'Show', container - %td= link_to 'Edit', edit_container_path(container) - %td= link_to 'Destroy', container, method: :delete, data: { confirm: 'Are you sure?' } +.row + - @containers.each do |container| + .col-md-2.six-across + = render partial: "thumbnail", locals: { container: container } -%br +- if can? :create, Container + %div + = link_to 'New Container', new_container_path, class: 'btn btn-primary' -= link_to 'New Container', new_container_path +.pagination + = will_paginate @containers diff --git a/app/views/containers/new.html.haml b/app/views/containers/new.html.haml index e4db982eb..b6e92973a 100644 --- a/app/views/containers/new.html.haml +++ b/app/views/containers/new.html.haml @@ -1,5 +1,4 @@ -%h1 New container +- content_for :title, "New Container" -= render 'form' - -= link_to 'Back', containers_path +- if can? :create, @container + = render 'form' diff --git a/app/views/containers/show.html.haml b/app/views/containers/show.html.haml index 11a7ab32c..615403575 100644 --- a/app/views/containers/show.html.haml +++ b/app/views/containers/show.html.haml @@ -1,9 +1,12 @@ -%p#notice= notice +- content_for :title, @container.description.capitalize +- content_for :subtitle, @container.subtitler(@container) -%p - %b Description: - = @container.description +- if can?(:create, @container) && can?(:edit, @container) && can?(:destroy, @container) + - content_for :buttonbar do + = render 'containers/actions', container: @container -= link_to 'Edit', edit_container_path(@container) -\| -= link_to 'Back', containers_path +- if @container.gardens.uniq.empty? + %p There are no gardens to display. +- else + - @container.gardens.uniq.each do |garden| + = render 'gardens/overview', garden: garden diff --git a/spec/controllers/containers_controller_spec.rb b/spec/controllers/containers_controller_spec.rb new file mode 100644 index 000000000..2a66cecd8 --- /dev/null +++ b/spec/controllers/containers_controller_spec.rb @@ -0,0 +1,11 @@ +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) } +end diff --git a/spec/factories/containers.rb b/spec/factories/containers.rb index 2ea319977..f6b9f8cc4 100644 --- a/spec/factories/containers.rb +++ b/spec/factories/containers.rb @@ -1,5 +1,5 @@ FactoryBot.define do factory :container do - description { "MyString" } + description { "homemade swamp" } end end diff --git a/spec/factories/plots.rb b/spec/factories/plots.rb index 0f71b74a8..9193888f3 100644 --- a/spec/factories/plots.rb +++ b/spec/factories/plots.rb @@ -1,6 +1,6 @@ FactoryBot.define do factory :plot do - garden { nil } - container { nil } + garden { garden } + container { container } end end diff --git a/spec/models/plot_spec.rb b/spec/models/plot_spec.rb index e7d018f46..c18bc9122 100644 --- a/spec/models/plot_spec.rb +++ b/spec/models/plot_spec.rb @@ -1,5 +1,15 @@ require 'rails_helper' -RSpec.describe Plot, type: :model do - pending "add some examples to (or delete) #{__FILE__}" +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(: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 + end + end end