From 5395aefa660592de90dfba00f2c78add3ce6d315 Mon Sep 17 00:00:00 2001 From: Brenda Wallace Date: Tue, 26 Mar 2019 20:48:51 +1300 Subject: [PATCH] Rename containers to garden types --- app/controllers/containers_controller.rb | 56 ------------------- app/controllers/garden_types_controller.rb | 51 +++++++++++++++++ app/controllers/gardens_controller.rb | 2 +- app/models/ability.rb | 12 ++-- app/models/container.rb | 19 ------- app/models/garden.rb | 2 +- app/models/garden_type.rb | 19 +++++++ app/models/plot.rb | 4 -- app/views/containers/_actions.html.haml | 4 -- app/views/containers/_form.html.haml | 20 ------- app/views/containers/_thumbnail.html.haml | 7 --- app/views/containers/edit.html.haml | 4 -- app/views/containers/index.html.haml | 17 ------ app/views/containers/new.html.haml | 4 -- app/views/containers/show.html.haml | 12 ---- app/views/garden_types/_actions.html.haml | 4 ++ app/views/garden_types/_form.html.haml | 20 +++++++ app/views/garden_types/_thumbnail.html.haml | 7 +++ app/views/garden_types/edit.html.haml | 4 ++ app/views/garden_types/index.html.haml | 17 ++++++ app/views/garden_types/new.html.haml | 4 ++ app/views/garden_types/show.html.haml | 12 ++++ app/views/gardens/_form.html.haml | 10 ++-- app/views/layouts/_header.html.haml | 2 +- config/routes.rb | 2 +- .../20181129171803_create_containers.rb | 15 ----- .../20190326063855_create_garden_types.rb | 17 ++++++ db/schema.rb | 28 ++++------ spec/factories/containers.rb | 5 -- spec/factories/garden_types.rb | 5 ++ spec/factories/plots.rb | 6 -- spec/models/container_spec.rb | 37 ------------ spec/models/garden_type.rb | 37 ++++++++++++ spec/models/plot_spec.rb | 17 ------ ...ontainers_spec.rb => garden_types_spec.rb} | 6 +- spec/views/containers/edit.html.haml_spec.rb | 2 +- spec/views/containers/new.html.haml_spec.rb | 2 +- 37 files changed, 228 insertions(+), 264 deletions(-) delete mode 100644 app/controllers/containers_controller.rb create mode 100644 app/controllers/garden_types_controller.rb delete mode 100644 app/models/container.rb create mode 100644 app/models/garden_type.rb delete mode 100644 app/models/plot.rb delete mode 100644 app/views/containers/_actions.html.haml delete mode 100644 app/views/containers/_form.html.haml delete mode 100644 app/views/containers/_thumbnail.html.haml delete mode 100644 app/views/containers/edit.html.haml delete mode 100644 app/views/containers/index.html.haml delete mode 100644 app/views/containers/new.html.haml delete mode 100644 app/views/containers/show.html.haml create mode 100644 app/views/garden_types/_actions.html.haml create mode 100644 app/views/garden_types/_form.html.haml create mode 100644 app/views/garden_types/_thumbnail.html.haml create mode 100644 app/views/garden_types/edit.html.haml create mode 100644 app/views/garden_types/index.html.haml create mode 100644 app/views/garden_types/new.html.haml create mode 100644 app/views/garden_types/show.html.haml delete mode 100644 db/migrate/20181129171803_create_containers.rb create mode 100644 db/migrate/20190326063855_create_garden_types.rb delete mode 100644 spec/factories/containers.rb create mode 100644 spec/factories/garden_types.rb delete mode 100644 spec/factories/plots.rb delete mode 100644 spec/models/container_spec.rb create mode 100644 spec/models/garden_type.rb delete mode 100644 spec/models/plot_spec.rb rename spec/requests/{containers_spec.rb => garden_types_spec.rb} (55%) diff --git a/app/controllers/containers_controller.rb b/app/controllers/containers_controller.rb deleted file mode 100644 index 69458da16..000000000 --- a/app/controllers/containers_controller.rb +++ /dev/null @@ -1,56 +0,0 @@ -class ContainersController < ApplicationController - # before_action :authenticate_member!, except: %i(index show) - load_and_authorize_resource - - # GET /containers - def index - @containers = Container.all.paginate(page: params[:page]) - end - - # GET /containers/1 - def show; end - - # GET /containers/new - def new - @container = Container.new - end - - # GET /containers/1/edit - def edit; end - - # POST /containers - def create - @container = Container.new(container_params) - - if @container.save - redirect_to @container, notice: 'Container was successfully created.' - else - render :new - end - end - - # PATCH/PUT /containers/1 - def update - if @container.update(container_params) - redirect_to @container, notice: 'Container was successfully updated.' - else - render :edit - end - end - - # DELETE /containers/1 - def destroy - @container.destroy - redirect_to containers_url, notice: 'Container was successfully destroyed.' - end - - private - - def set_container - @container = Container.find(params[:id]) - end - - def container_params - params.require(:container).permit(:description, :slug) - end -end diff --git a/app/controllers/garden_types_controller.rb b/app/controllers/garden_types_controller.rb new file mode 100644 index 000000000..5fade528a --- /dev/null +++ b/app/controllers/garden_types_controller.rb @@ -0,0 +1,51 @@ +class GardenTypesController < ApplicationController + respond_to :html, :json + load_and_authorize_resource + + # GET /garden_types + def index + @garden_types = GardenType.all.paginate(page: params[:page]) + respond_with @garden_types + end + + # GET /garden_types/1 + def show + respond_with @garden_type + end + + # GET /garden_types/new + def new + @garden_type = GardenType.new + end + + # GET /garden_types/1/edit + def edit; end + + # POST /garden_types + def create + @garden_type = GardenType.create(garden_type_params) + respond_with(@garden_type) + end + + # PATCH/PUT /garden_types/1 + def update + @garden_type.update(garden_type_params) + respond_with @garden_type + end + + # DELETE /garden_types/1 + def destroy + @garden_type.destroy + respond_with @garden_type + end + + private + + def set_garden_type + @garden_type = GardenType.find(params[:id]) + end + + def garden_type_params + params.require(:garden_type).permit(:description, :slug) + end +end diff --git a/app/controllers/gardens_controller.rb b/app/controllers/gardens_controller.rb index 9aaa253f1..82ed8d2a4 100644 --- a/app/controllers/gardens_controller.rb +++ b/app/controllers/gardens_controller.rb @@ -67,6 +67,6 @@ class GardensController < ApplicationController def garden_params params.require(:garden).permit(:name, :slug, :description, :active, - :location, :latitude, :longitude, :area, :area_unit, container_ids: []) + :location, :latitude, :longitude, :area, :area_unit, :garden_type_id) end end diff --git a/app/models/ability.rb b/app/models/ability.rb index dcc3b8300..66c48a8c4 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -43,9 +43,9 @@ class Ability an.crop.approved? end - cannot :create, Container - cannot :update, Container - cannot :destroy, Container + cannot :create, GardenType + cannot :update, GardenType + cannot :destroy, GardenType end def member_abilities(member) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength @@ -129,9 +129,9 @@ class Ability can :destroy, Follow cannot :destroy, Follow, followed_id: member.id # can't unfollow yourself - cannot :create, Container - cannot :update, Container - cannot :destroy, Container + cannot :create, GardenType + cannot :update, GardenType + cannot :destroy, GardenType end def admin_abilities(member) diff --git a/app/models/container.rb b/app/models/container.rb deleted file mode 100644 index a36b2099d..000000000 --- a/app/models/container.rb +++ /dev/null @@ -1,19 +0,0 @@ -class Container < ApplicationRecord - extend FriendlyId - friendly_id :description, use: %i(slugged finders) - - has_many :plots, dependent: :destroy - has_many :gardens, through: :plots - - validates :description, presence: true, uniqueness: true - - def container_slug - description.gsub!(/[^A-Za-z ]/, '') - end - - def subtitler(container) - num = container.gardens.uniq.count - s = num > 1 || num.zero? ? "s are" : " is" - "#{num} garden#{s} using this container" - end -end diff --git a/app/models/garden.rb b/app/models/garden.rb index 4e2e13efa..b64b7375b 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -9,7 +9,7 @@ class Garden < ApplicationRecord has_many :crops, through: :plantings has_many :plots, dependent: :destroy - has_many :containers, through: :plots + belongs_to :garden_type, required: false # set up geocoding geocoded_by :location diff --git a/app/models/garden_type.rb b/app/models/garden_type.rb new file mode 100644 index 000000000..da84bf01e --- /dev/null +++ b/app/models/garden_type.rb @@ -0,0 +1,19 @@ +class GardenType < ApplicationRecord + extend FriendlyId + friendly_id :name, use: %i(slugged finders) + + has_many :gardens + + validates :name, presence: true, uniqueness: true + validates :slug, presence: true, uniqueness: true + + def garden_type_slug + name.gsub!(/[^A-Za-z ]/, '') + end + + def subtitler(container) + num = container.gardens.uniq.count + s = num > 1 || num.zero? ? "s are" : " is" + "#{num} garden#{s} using this container" + end +end diff --git a/app/models/plot.rb b/app/models/plot.rb deleted file mode 100644 index c253e3d58..000000000 --- a/app/models/plot.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Plot < ApplicationRecord - belongs_to :garden - belongs_to :container -end diff --git a/app/views/containers/_actions.html.haml b/app/views/containers/_actions.html.haml deleted file mode 100644 index 95bf704fe..000000000 --- a/app/views/containers/_actions.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -.btn-group.container-actions - = render 'shared/buttons/edit', path: edit_container_path(container) - .pull-right - = render 'shared/buttons/delete', path: container_path(container) diff --git a/app/views/containers/_form.html.haml b/app/views/containers/_form.html.haml deleted file mode 100644 index f4fd7e8ba..000000000 --- a/app/views/containers/_form.html.haml +++ /dev/null @@ -1,20 +0,0 @@ -= 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:" - %ul - - @container.errors.full_messages.each do |message| - %li= message - - %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 deleted file mode 100644 index ff235a90e..000000000 --- a/app/views/containers/_thumbnail.html.haml +++ /dev/null @@ -1,7 +0,0 @@ -- 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 deleted file mode 100644 index ee890d6b7..000000000 --- a/app/views/containers/edit.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -- content_for :title, "Edit Container" - -- if can? :update, @container - = render 'form' diff --git a/app/views/containers/index.html.haml b/app/views/containers/index.html.haml deleted file mode 100644 index 040a892bc..000000000 --- a/app/views/containers/index.html.haml +++ /dev/null @@ -1,17 +0,0 @@ -- content_for :title, 'Containers' - -%p - #{ENV['GROWSTUFF_SITE_NAME']} tracks who's growing what, where. - View any container page to see which of our members have used it. - -.row - - @containers.each do |container| - .col-md-2.six-across - = render partial: "thumbnail", locals: { container: container } - -- if can? :create, Container - %div - = link_to 'New Container', new_container_path, class: 'btn btn-primary' - -.pagination - = will_paginate @containers diff --git a/app/views/containers/new.html.haml b/app/views/containers/new.html.haml deleted file mode 100644 index b6e92973a..000000000 --- a/app/views/containers/new.html.haml +++ /dev/null @@ -1,4 +0,0 @@ -- content_for :title, "New Container" - -- if can? :create, @container - = render 'form' diff --git a/app/views/containers/show.html.haml b/app/views/containers/show.html.haml deleted file mode 100644 index 615403575..000000000 --- a/app/views/containers/show.html.haml +++ /dev/null @@ -1,12 +0,0 @@ -- content_for :title, @container.description.capitalize -- content_for :subtitle, @container.subtitler(@container) - -- if can?(:create, @container) && can?(:edit, @container) && can?(:destroy, @container) - - content_for :buttonbar do - = render 'containers/actions', container: @container - -- 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/app/views/garden_types/_actions.html.haml b/app/views/garden_types/_actions.html.haml new file mode 100644 index 000000000..9dcf04028 --- /dev/null +++ b/app/views/garden_types/_actions.html.haml @@ -0,0 +1,4 @@ +.btn-group.garden_type-actions + = render 'shared/buttons/edit', path: edit_garden_type_path(garden_type) + .pull-right + = render 'shared/buttons/delete', path: garden_type_path(garden_type) diff --git a/app/views/garden_types/_form.html.haml b/app/views/garden_types/_form.html.haml new file mode 100644 index 000000000..a6d84885a --- /dev/null +++ b/app/views/garden_types/_form.html.haml @@ -0,0 +1,20 @@ += form_for @garden_type, html: { class: 'form-horizontal', role: "form" } do |f| + - if @garden_type.errors.any? + #error_explanation + %h2= "#{pluralize(@garden_type.errors.count, "error")} prohibited this garden_type from being saved:" + %ul + - @garden_type.errors.full_messages.each do |message| + %li= message + + %h2 Basic information + + .form-group + = f.label :name, class: 'control-label col-md-2' + .col-md-8 + = f.text_field :name, class: 'form-control' + %span.help-block + The name for the garden_type, 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/garden_types/_thumbnail.html.haml b/app/views/garden_types/_thumbnail.html.haml new file mode 100644 index 000000000..1004ac019 --- /dev/null +++ b/app/views/garden_types/_thumbnail.html.haml @@ -0,0 +1,7 @@ +- cache cache_key_for(GardenType, garden_type.id) do + .thumbnail + .garden_type-thumbnail + - if garden_type + .garden_typeinfo + .garden_typename + = link_to garden_type.name, garden_type diff --git a/app/views/garden_types/edit.html.haml b/app/views/garden_types/edit.html.haml new file mode 100644 index 000000000..1ec811b50 --- /dev/null +++ b/app/views/garden_types/edit.html.haml @@ -0,0 +1,4 @@ +- content_for :title, "Edit GardenType" + +- if can? :update, @garden_type + = render 'form' diff --git a/app/views/garden_types/index.html.haml b/app/views/garden_types/index.html.haml new file mode 100644 index 000000000..49352bc4c --- /dev/null +++ b/app/views/garden_types/index.html.haml @@ -0,0 +1,17 @@ +- content_for :title, 'GardenTypes' + +%p + #{ENV['GROWSTUFF_SITE_NAME']} tracks who's growing what, where. + View any garden_type page to see which of our members have used it. + +.row + - @garden_types.each do |garden_type| + .col-md-2.six-across + = render partial: "thumbnail", locals: { garden_type: garden_type } + +- if can? :create, GardenType + %div + = link_to 'New GardenType', new_garden_type_path, class: 'btn btn-primary' + +.pagination + = will_paginate @garden_types diff --git a/app/views/garden_types/new.html.haml b/app/views/garden_types/new.html.haml new file mode 100644 index 000000000..a898fef65 --- /dev/null +++ b/app/views/garden_types/new.html.haml @@ -0,0 +1,4 @@ +- content_for :title, "New GardenType" + +- if can? :create, @garden_type + = render 'form' diff --git a/app/views/garden_types/show.html.haml b/app/views/garden_types/show.html.haml new file mode 100644 index 000000000..cd9575965 --- /dev/null +++ b/app/views/garden_types/show.html.haml @@ -0,0 +1,12 @@ +- content_for :title, @garden_type.name.capitalize +- content_for :subtitle, @garden_type.subtitler(@garden_type) + +- if can?(:create, @garden_type) && can?(:edit, @garden_type) && can?(:destroy, @garden_type) + - content_for :buttonbar do + = render 'garden_types/actions', garden_type: @garden_type + +- if @garden_type.gardens.uniq.empty? + %p There are no gardens to display. +- else + - @garden_type.gardens.uniq.each do |garden| + = render 'gardens/overview', garden: garden diff --git a/app/views/gardens/_form.html.haml b/app/views/gardens/_form.html.haml index e7fbc52e3..b5510fad9 100644 --- a/app/views/gardens/_form.html.haml +++ b/app/views/gardens/_form.html.haml @@ -44,11 +44,13 @@ = f.select(:area_unit, Garden::AREA_UNITS_VALUES, { include_blank: false }, class: 'form-control') .form-group - = f.label :container, class: 'control-label col-md-2' + = f.label :garden_type, class: 'control-label col-md-2' .col-md-2 - = f.collection_check_boxes(:container_ids, Container.all, :id, :description) do |c| - = c.label class:"checkbox-inline" do - = c.check_box + c.text + = collection_select(:garden, :garden_type_id, + GardenType.all.order(:name), + :id, :name, + selected: @garden.garden_type_id, + class: 'form-control') .form-group = f.label :active, 'Active? ', class: 'control-label col-md-2' diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index 2197507b4..9daa7c14b 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -30,7 +30,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= link_to t('.garden_types'), garden_types_path %li.dropdown< %a.dropdown-toggle{ 'data-toggle': 'dropdown', href: members_path } = t('.community') diff --git a/config/routes.rb b/config/routes.rb index 5939326ff..ff1687d52 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,7 @@ Rails.application.routes.draw do get '/robots.txt' => 'robots#robots' - resources :containers + resources :garden_types resources :plant_parts devise_for :members, controllers: { diff --git a/db/migrate/20181129171803_create_containers.rb b/db/migrate/20181129171803_create_containers.rb deleted file mode 100644 index a57f9e941..000000000 --- a/db/migrate/20181129171803_create_containers.rb +++ /dev/null @@ -1,15 +0,0 @@ -class CreateContainers < ActiveRecord::Migration[4.2] - def change - create_table :containers do |t| - t.string :description - t.timestamps null: false - end - create_table :plots do |t| - t.references :garden, foreign_key: true - t.references :container, foreign_key: true - t.timestamps null: false - end - add_column :containers, :slug, :string - add_index :containers, :slug, unique: true - end -end diff --git a/db/migrate/20190326063855_create_garden_types.rb b/db/migrate/20190326063855_create_garden_types.rb new file mode 100644 index 000000000..7e3af2322 --- /dev/null +++ b/db/migrate/20190326063855_create_garden_types.rb @@ -0,0 +1,17 @@ +class CreateGardenTypes < ActiveRecord::Migration[5.2] + def change + create_table :garden_types do |t| + t.text :name, null: false, unique: true + t.text :slug, null: false, unique: true + t.timestamps null: false + end + add_column :gardens, :garden_type_id, :integer + add_index :gardens, :garden_type_id + ['organic', 'conventional', 'container', 'vertical', 'greenhouse', 'rooftop', 'no-dig', 'raised bed', + 'wicking bed', 'permaculture', 'hydroponic', 'aquaponic', 'orchard', 'food forest', + 'biodynamic'].each do |name| + say "Creating #{name}" + GardenType.create! name: name + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 84e6a994d..d0e1ac43e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2019_03_17_023129) do +ActiveRecord::Schema.define(version: 2019_03_26_063855) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -153,14 +153,6 @@ ActiveRecord::Schema.define(version: 2019_03_17_023129) do t.datetime "updated_at" end - create_table "containers", id: :serial, force: :cascade do |t| - t.string "description" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "slug" - t.index ["slug"], name: "index_containers_on_slug", unique: true - end - create_table "crops", id: :serial, force: :cascade do |t| t.string "name", null: false t.string "en_wikipedia_url" @@ -208,6 +200,13 @@ ActiveRecord::Schema.define(version: 2019_03_17_023129) do t.index ["slug"], name: "index_forums_on_slug", unique: true end + create_table "garden_types", force: :cascade do |t| + t.text "name", null: false + t.text "slug", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "gardens", id: :serial, force: :cascade do |t| t.string "name", null: false t.integer "owner_id" @@ -221,6 +220,8 @@ ActiveRecord::Schema.define(version: 2019_03_17_023129) do t.float "longitude" t.decimal "area" t.string "area_unit" + t.integer "garden_type_id" + t.index ["garden_type_id"], name: "index_gardens_on_garden_type_id" t.index ["owner_id"], name: "index_gardens_on_owner_id" t.index ["slug"], name: "index_gardens_on_slug", unique: true end @@ -402,13 +403,6 @@ ActiveRecord::Schema.define(version: 2019_03_17_023129) do t.index ["slug"], name: "index_plantings_on_slug", unique: true end - create_table "plots", id: :serial, force: :cascade do |t| - t.integer "garden_id" - t.integer "container_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - create_table "posts", id: :serial, force: :cascade do |t| t.integer "author_id", null: false t.string "subject", null: false @@ -463,7 +457,5 @@ ActiveRecord::Schema.define(version: 2019_03_17_023129) do add_foreign_key "photographings", "crops" add_foreign_key "photographings", "photos" add_foreign_key "plantings", "seeds", column: "parent_seed_id", name: "parent_seed", on_delete: :nullify - add_foreign_key "plots", "containers" - add_foreign_key "plots", "gardens" add_foreign_key "seeds", "plantings", column: "parent_planting_id", name: "parent_planting", on_delete: :nullify end diff --git a/spec/factories/containers.rb b/spec/factories/containers.rb deleted file mode 100644 index f6b9f8cc4..000000000 --- a/spec/factories/containers.rb +++ /dev/null @@ -1,5 +0,0 @@ -FactoryBot.define do - factory :container do - description { "homemade swamp" } - end -end diff --git a/spec/factories/garden_types.rb b/spec/factories/garden_types.rb new file mode 100644 index 000000000..942fd7932 --- /dev/null +++ b/spec/factories/garden_types.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :garden_type do + name { "homemade swamp" } + end +end diff --git a/spec/factories/plots.rb b/spec/factories/plots.rb deleted file mode 100644 index 9193888f3..000000000 --- a/spec/factories/plots.rb +++ /dev/null @@ -1,6 +0,0 @@ -FactoryBot.define do - factory :plot do - garden { garden } - container { container } - end -end diff --git a/spec/models/container_spec.rb b/spec/models/container_spec.rb deleted file mode 100644 index bac3e8e30..000000000 --- a/spec/models/container_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -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 - end - - it "doesn't allow a nil description" do - container = FactoryBot.build(:container, description: nil) - container.should_not be_valid - end - - it "doesn't allow a blank description" do - container = FactoryBot.build(:container, description: "") - container.should_not be_valid - end - - it "doesn't allow a description with only spaces" do - 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/garden_type.rb b/spec/models/garden_type.rb new file mode 100644 index 000000000..d237e6055 --- /dev/null +++ b/spec/models/garden_type.rb @@ -0,0 +1,37 @@ +require 'rails_helper' + +describe GardenType do + let(:owner) { FactoryBot.create(:member) } + let(:garden) { FactoryBot.create(:garden, owner: owner, name: 'Free Carrots') } + let(:container) { FactoryBot.create(:container, name: "fake hole in the ground") } + + it "should have a name" do + container = FactoryBot.build(:container, name: "organic") + container.should be_valid + end + + it "doesn't allow a nil name" do + container = FactoryBot.build(:container, name: nil) + container.should_not be_valid + end + + it "doesn't allow a blank name" do + container = FactoryBot.build(:container, name: "") + container.should_not be_valid + end + + it "doesn't allow a name with only spaces" do + container = FactoryBot.build(:container, name: " ") + container.should_not be_valid + end + + it "destroys plots when deleted" do + container = FactoryBot.create(:container, name: "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 deleted file mode 100644 index 4a3a16724..000000000 --- a/spec/models/plot_spec.rb +++ /dev/null @@ -1,17 +0,0 @@ -require 'rails_helper' - -describe Plot do - let(:owner) { FactoryBot.create(:member) } - 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" 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/requests/containers_spec.rb b/spec/requests/garden_types_spec.rb similarity index 55% rename from spec/requests/containers_spec.rb rename to spec/requests/garden_types_spec.rb index 0f61eb278..fbacd0491 100644 --- a/spec/requests/containers_spec.rb +++ b/spec/requests/garden_types_spec.rb @@ -1,9 +1,9 @@ require 'rails_helper' -RSpec.describe "Containers", type: :request do - describe "GET /containers" do +RSpec.describe "GardenTypes", type: :request do + describe "GET /garden_types" do it "works! (now write some real specs)" do - get containers_path + get garden_types_path expect(response).to have_http_status(200) end end diff --git a/spec/views/containers/edit.html.haml_spec.rb b/spec/views/containers/edit.html.haml_spec.rb index 3b9d61737..f47a8492d 100644 --- a/spec/views/containers/edit.html.haml_spec.rb +++ b/spec/views/containers/edit.html.haml_spec.rb @@ -10,7 +10,7 @@ describe "containers/edit" do end it "renders the edit container form" do - assert_select "form", action: containers_path, method: "post" do + assert_select "form", action: garden_types_path, method: "post" do assert_select "input#container_description", name: "container[description]" end end diff --git a/spec/views/containers/new.html.haml_spec.rb b/spec/views/containers/new.html.haml_spec.rb index f56d956fb..28fa68c84 100644 --- a/spec/views/containers/new.html.haml_spec.rb +++ b/spec/views/containers/new.html.haml_spec.rb @@ -10,7 +10,7 @@ describe "containers/new" do end it "renders new container form" do - assert_select "form", action: containers_path, method: "post" do + assert_select "form", action: garden_types_path, method: "post" do assert_select "input#container_description", name: "container[description]" end end