diff --git a/Gemfile b/Gemfile index 8bd546f..2b116f7 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,7 @@ git_source(:github) do |repo_name| end # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' -gem 'rails', '~> 5.1.6' +gem 'rails', '~> 5.2' # Use sqlite3 as the database for Active Record gem 'sqlite3', '1.5' # Use Puma as the app server @@ -31,6 +31,8 @@ gem 'jbuilder', '~> 2.5' # Use ActiveModel has_secure_password gem 'bcrypt', '~> 3.1.7' +gem 'bootsnap', require: false + # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 2f28a03..dc73f4c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,47 +1,53 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.1.7) - actionpack (= 5.1.7) + actioncable (5.2.8.1) + actionpack (= 5.2.8.1) nio4r (~> 2.0) - websocket-driver (~> 0.6.1) - actionmailer (5.1.7) - actionpack (= 5.1.7) - actionview (= 5.1.7) - activejob (= 5.1.7) + websocket-driver (>= 0.6.1) + actionmailer (5.2.8.1) + actionpack (= 5.2.8.1) + actionview (= 5.2.8.1) + activejob (= 5.2.8.1) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.1.7) - actionview (= 5.1.7) - activesupport (= 5.1.7) - rack (~> 2.0) + actionpack (5.2.8.1) + actionview (= 5.2.8.1) + activesupport (= 5.2.8.1) + rack (~> 2.0, >= 2.0.8) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.1.7) - activesupport (= 5.1.7) + actionview (5.2.8.1) + activesupport (= 5.2.8.1) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (5.1.7) - activesupport (= 5.1.7) + activejob (5.2.8.1) + activesupport (= 5.2.8.1) globalid (>= 0.3.6) - activemodel (5.1.7) - activesupport (= 5.1.7) - activerecord (5.1.7) - activemodel (= 5.1.7) - activesupport (= 5.1.7) - arel (~> 8.0) - activesupport (5.1.7) + activemodel (5.2.8.1) + activesupport (= 5.2.8.1) + activerecord (5.2.8.1) + activemodel (= 5.2.8.1) + activesupport (= 5.2.8.1) + arel (>= 9.0) + activestorage (5.2.8.1) + actionpack (= 5.2.8.1) + activerecord (= 5.2.8.1) + marcel (~> 1.0.0) + activesupport (5.2.8.1) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - arel (8.0.0) + arel (9.0.0) base64 (0.2.0) bcrypt (3.1.20) bindex (0.8.1) + bootsnap (1.18.4) + msgpack (~> 1.2) builder (3.3.0) byebug (11.1.3) coffee-rails (4.2.2) @@ -56,7 +62,7 @@ GEM date (3.3.4) erubi (1.13.0) execjs (2.9.1) - ffi (1.17.0-arm64-darwin) + ffi (1.17.0) globalid (1.1.0) activesupport (>= 5.0) i18n (1.14.5) @@ -79,9 +85,12 @@ GEM net-imap net-pop net-smtp + marcel (1.0.4) method_source (1.1.0) mini_mime (1.1.5) - minitest (5.24.1) + mini_portile2 (2.8.7) + minitest (5.25.1) + msgpack (1.7.2) net-imap (0.4.14) date net-protocol @@ -92,7 +101,8 @@ GEM net-smtp (0.5.0) net-protocol nio4r (2.7.3) - nokogiri (1.15.6-arm64-darwin) + nokogiri (1.15.6) + mini_portile2 (~> 2.8.2) racc (~> 1.4) puma (6.4.2) nio4r (~> 2.0) @@ -100,17 +110,18 @@ GEM rack (2.2.9) rack-test (2.1.0) rack (>= 1.3) - rails (5.1.7) - actioncable (= 5.1.7) - actionmailer (= 5.1.7) - actionpack (= 5.1.7) - actionview (= 5.1.7) - activejob (= 5.1.7) - activemodel (= 5.1.7) - activerecord (= 5.1.7) - activesupport (= 5.1.7) + rails (5.2.8.1) + actioncable (= 5.2.8.1) + actionmailer (= 5.2.8.1) + actionpack (= 5.2.8.1) + actionview (= 5.2.8.1) + activejob (= 5.2.8.1) + activemodel (= 5.2.8.1) + activerecord (= 5.2.8.1) + activestorage (= 5.2.8.1) + activesupport (= 5.2.8.1) bundler (>= 1.3.0) - railties (= 5.1.7) + railties (= 5.2.8.1) sprockets-rails (>= 2.0.0) rails-dom-testing (2.2.0) activesupport (>= 5.0.0) @@ -119,12 +130,12 @@ GEM rails-html-sanitizer (1.6.0) loofah (~> 2.21) nokogiri (~> 1.14) - railties (5.1.7) - actionpack (= 5.1.7) - activesupport (= 5.1.7) + railties (5.2.8.1) + actionpack (= 5.2.8.1) + activesupport (= 5.2.8.1) method_source rake (>= 0.8.7) - thor (>= 0.18.1, < 2.0) + thor (>= 0.19.0, < 2.0) rake (13.2.1) rb-fsevent (0.11.2) rb-inotify (0.11.1) @@ -134,8 +145,8 @@ GEM sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sass-rails (5.0.7) - railties (>= 4.0.0, < 6) + sass-rails (5.1.0) + railties (>= 5.2.0) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) @@ -148,11 +159,12 @@ GEM base64 concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.2) - actionpack (>= 4.0) - activesupport (>= 4.0) + sprockets-rails (3.4.2) + actionpack (>= 5.2) + activesupport (>= 5.2) sprockets (>= 3.0.0) - sqlite3 (1.5.0-arm64-darwin) + sqlite3 (1.5.0) + mini_portile2 (~> 2.8.0) thor (1.3.1) thread_safe (0.3.6) tilt (2.4.0) @@ -169,22 +181,23 @@ GEM activemodel (>= 5.0) bindex (>= 0.4.0) railties (>= 5.0) - websocket-driver (0.6.5) + websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) PLATFORMS - arm64-darwin-23 + ruby DEPENDENCIES bcrypt (~> 3.1.7) + bootsnap byebug coffee-rails (~> 4.2) jbuilder (~> 2.5) jquery-rails listen (~> 3.0.5) puma - rails (~> 5.1.6) + rails (~> 5.2) sass-rails (~> 5.0) spring spring-watcher-listen (~> 2.0.0) @@ -195,4 +208,4 @@ DEPENDENCIES web-console (>= 3.3.0) BUNDLED WITH - 2.4.22 + 2.1.4 diff --git a/app/controllers/house_plants_controller.rb b/app/controllers/house_plants_controller.rb index 8ab5e61..d481063 100644 --- a/app/controllers/house_plants_controller.rb +++ b/app/controllers/house_plants_controller.rb @@ -2,4 +2,28 @@ class HousePlantsController < ApplicationController def index @house_plants = HousePlant.all end + + def show + @house_plant = HousePlant.find(params[:id]) + end + + def new + @house_plant = HousePlant.new + @rooms = Room.all + @house_plant.room_id = params[:room_id] if params[:room_id] + end + + def create + @house_plant = HousePlant.new(house_plant_params) + @rooms = Room.all + if @house_plant.save + redirect_to house_plant_path(@house_plant) + else + render :new + end + end + + def house_plant_params + params.require(:house_plant).permit(:plant_type, :height, :room_id) + end end diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index efb8527..7712be9 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -2,4 +2,8 @@ class RoomsController < ApplicationController def index @rooms = Room.all end + + def show + @room = Room.find(params[:id]) + end end diff --git a/app/models/house_plant.rb b/app/models/house_plant.rb index b98a562..4a8ff3d 100644 --- a/app/models/house_plant.rb +++ b/app/models/house_plant.rb @@ -1,3 +1,10 @@ class HousePlant < ApplicationRecord + belongs_to :room + validates :plant_type, :height, :room, presence: true + validates :height, numericality: { greater_than_or_equal_to: 1 } + + def room_name + room.name + end end diff --git a/app/models/room.rb b/app/models/room.rb index cc4a0b5..bb18b3a 100644 --- a/app/models/room.rb +++ b/app/models/room.rb @@ -1,3 +1,3 @@ class Room < ApplicationRecord - + has_many :house_plants end diff --git a/app/views/house_plants/index.html.erb b/app/views/house_plants/index.html.erb index e65cdb2..164f0c8 100644 --- a/app/views/house_plants/index.html.erb +++ b/app/views/house_plants/index.html.erb @@ -1,7 +1,9 @@

House Plants

+

<%= link_to 'Add New Plant', new_house_plant_path %>

+
    <% @house_plants.each do |plant| %> -
  1. <%= plant.plant_type %>
  2. +
  3. <%= link_to plant.plant_type, house_plant_path(plant) %>
  4. <% end %>
diff --git a/app/views/house_plants/new.html.erb b/app/views/house_plants/new.html.erb index e69de29..adf96f9 100644 --- a/app/views/house_plants/new.html.erb +++ b/app/views/house_plants/new.html.erb @@ -0,0 +1,36 @@ +

Add New Plant:

+ +<% if @house_plant.errors.any? %> +
+

<%= pluralize(@house_plant.errors.count, "error") %> prohibited this house plant from being saved:

+ +
+<% end %> + +<%= form_with model: @house_plant, local: true do |form| %> +
+ <%= form.label :plant_type %> + <%= form.text_field :plant_type %> +
+ +
+ <%= form.label :height %> + <%= form.number_field :height %> +
+ +
+ <%= form.label :room_id %> + <%= form.collection_select :room_id, @rooms, :id, :name, prompt: "Select a room" %> +
+ +
+ <%= form.submit "Create Plant" %> +
+<% end %> + +

<%= link_to 'Back to Plants', house_plants_path %>

+

<%= link_to 'Back to Rooms', rooms_path %>

diff --git a/app/views/house_plants/show.html.erb b/app/views/house_plants/show.html.erb index e69de29..7479309 100644 --- a/app/views/house_plants/show.html.erb +++ b/app/views/house_plants/show.html.erb @@ -0,0 +1,7 @@ +

<%= @house_plant.plant_type %>

+ +

<%= @house_plant.height %> inches

+ +

<%= link_to @house_plant.room_name, room_path(@house_plant.room) %>

+ +

<%= link_to 'Back to Plants', house_plants_path %>

diff --git a/app/views/rooms/index.html.erb b/app/views/rooms/index.html.erb index b2ea9a9..4982601 100644 --- a/app/views/rooms/index.html.erb +++ b/app/views/rooms/index.html.erb @@ -2,6 +2,10 @@
    <% @rooms.each do |room| %> -
  1. <%= room.name %>
  2. +
  3. <%= link_to room.name, room_path(room) %>
  4. <% end %>
+ + + +

<%= link_to 'Create New Plant', new_house_plant_path %>

diff --git a/app/views/rooms/show.html.erb b/app/views/rooms/show.html.erb index e69de29..93ffb1a 100644 --- a/app/views/rooms/show.html.erb +++ b/app/views/rooms/show.html.erb @@ -0,0 +1,14 @@ +

<%= @room.name %>

+ +

Occupancy:

+

<%= @room.occupancy %> people

+ +

Plants:

+ + +

<%= link_to 'Add New Plant to this Room', new_house_plant_path(room_id: @room.id) %>

+

<%= link_to 'Back to Rooms', rooms_path %>

diff --git a/config/routes.rb b/config/routes.rb index 3e1cebe..61ed7de 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ Rails.application.routes.draw do - resources :rooms, only: [:index] - resources :house_plants, only: [:index] + resources :rooms, only: [:index, :show] + resources :house_plants, only: [:index, :show, :new, :create] end diff --git a/db/migrate/20190610081818_create_house_plants.rb b/db/migrate/20190610081818_create_house_plants.rb index 6710ce8..2beb852 100644 --- a/db/migrate/20190610081818_create_house_plants.rb +++ b/db/migrate/20190610081818_create_house_plants.rb @@ -3,6 +3,7 @@ def change create_table :house_plants do |t| t.string :plant_type t.integer :height + t.references :room end end end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..5539835 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,27 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2019_06_10_081955) do + + create_table "house_plants", force: :cascade do |t| + t.string "plant_type" + t.integer "height" + t.integer "room_id" + t.index ["room_id"], name: "index_house_plants_on_room_id" + end + + create_table "rooms", force: :cascade do |t| + t.string "name" + t.integer "occupancy" + end + +end