diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 00000000..b1b25a5f --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.2.2 diff --git a/Gemfile b/Gemfile index 26cf25d9..d17d402b 100644 --- a/Gemfile +++ b/Gemfile @@ -14,7 +14,7 @@ gem 'pg' # Use Puma as the app server gem 'puma', '~> 3.0' # Use SCSS for stylesheets -gem 'sass-rails', '~> 5.0' +gem 'sass-rails', '>= 5.0.6' # Use Uglifier as compressor for JavaScript assets gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .coffee assets and views diff --git a/Gemfile.lock b/Gemfile.lock index 529d2037..a0695444 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -184,7 +184,7 @@ GEM rspec-support (~> 3.5.0) rspec-support (3.5.0) sass (3.4.23) - sass-rails (5.0.5) + sass-rails (5.0.6) railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) @@ -256,7 +256,7 @@ DEPENDENCIES rails (~> 5.0.1) rails-controller-testing rspec-rails (~> 3.5) - sass-rails (~> 5.0) + sass-rails (>= 5.0.6) seed_dump spring spring-watcher-listen (~> 2.0.0) diff --git a/app/assets/javascripts/districts.coffee b/app/assets/javascripts/districts.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/districts.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/schools.coffee b/app/assets/javascripts/schools.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/schools.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/districts.scss b/app/assets/stylesheets/districts.scss new file mode 100644 index 00000000..54a6e37d --- /dev/null +++ b/app/assets/stylesheets/districts.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Districts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss index c1f28f5a..4137aa83 100644 --- a/app/assets/stylesheets/scaffolds.scss +++ b/app/assets/stylesheets/scaffolds.scss @@ -36,6 +36,7 @@ header { a, a:hover, a:visited { color: $darkgrey; + text-decoration: none; } } diff --git a/app/assets/stylesheets/schools.scss b/app/assets/stylesheets/schools.scss new file mode 100644 index 00000000..813d2d05 --- /dev/null +++ b/app/assets/stylesheets/schools.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Schools controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/districts_controller.rb b/app/controllers/districts_controller.rb new file mode 100644 index 00000000..89c95611 --- /dev/null +++ b/app/controllers/districts_controller.rb @@ -0,0 +1,74 @@ +class DistrictsController < ApplicationController + before_action :set_district, only: [:show, :edit, :update, :destroy] + + # GET /districts + # GET /districts.json + def index + @districts = District.all + end + + # GET /districts/1 + # GET /districts/1.json + def show + end + + # GET /districts/new + def new + @district = District.new + end + + # GET /districts/1/edit + def edit + end + + # POST /districts + # POST /districts.json + def create + @district = District.new(district_params) + + respond_to do |format| + if @district.save + format.html { redirect_to @district, notice: 'District was successfully created.' } + format.json { render :show, status: :created, location: @district } + else + format.html { render :new } + format.json { render json: @district.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /districts/1 + # PATCH/PUT /districts/1.json + def update + respond_to do |format| + if @district.update(district_params) + format.html { redirect_to @district, notice: 'District was successfully updated.' } + format.json { render :show, status: :ok, location: @district } + else + format.html { render :edit } + format.json { render json: @district.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /districts/1 + # DELETE /districts/1.json + def destroy + @district.destroy + respond_to do |format| + format.html { redirect_to districts_url, notice: 'District was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_district + @district = District.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def district_params + params.require(:district).permit(:name, :state_id) + end +end diff --git a/app/controllers/recipients_controller.rb b/app/controllers/recipients_controller.rb index be2db737..2da06e35 100644 --- a/app/controllers/recipients_controller.rb +++ b/app/controllers/recipients_controller.rb @@ -1,10 +1,11 @@ class RecipientsController < ApplicationController + before_action :set_school before_action :set_recipient, only: [:show, :edit, :update, :destroy] # GET /recipients # GET /recipients.json def index - @recipients = Recipient.all + @recipients = @school.recipients.all end # GET /recipients/1 @@ -14,7 +15,7 @@ class RecipientsController < ApplicationController # GET /recipients/new def new - @recipient = Recipient.new + @recipient = @school.recipients.new end # GET /recipients/1/edit @@ -24,11 +25,11 @@ class RecipientsController < ApplicationController # POST /recipients # POST /recipients.json def create - @recipient = Recipient.new(recipient_params) + @recipient = @school.recipients.new(recipient_params) respond_to do |format| if @recipient.save - format.html { redirect_to @recipient, notice: 'Recipient was successfully created.' } + format.html { redirect_to school_recipient_path(@school, @recipient), notice: 'Recipient was successfully created.' } format.json { render :show, status: :created, location: @recipient } else format.html { render :new } @@ -42,7 +43,7 @@ class RecipientsController < ApplicationController def update respond_to do |format| if @recipient.update(recipient_params) - format.html { redirect_to @recipient, notice: 'Recipient was successfully updated.' } + format.html { redirect_to school_recipient_path(@school, @recipient), notice: 'Recipient was successfully updated.' } format.json { render :show, status: :ok, location: @recipient } else format.html { render :edit } @@ -56,15 +57,20 @@ class RecipientsController < ApplicationController def destroy @recipient.destroy respond_to do |format| - format.html { redirect_to recipients_url, notice: 'Recipient was successfully destroyed.' } + format.html { redirect_to @school, notice: 'Recipient was successfully destroyed.' } format.json { head :no_content } end end private + # Use callbacks to share common setup or constraints between actions. + def set_school + @school = School.find(params[:school_id]) + end + # Use callbacks to share common setup or constraints between actions. def set_recipient - @recipient = Recipient.find(params[:id]) + @recipient = @school.recipients.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb new file mode 100644 index 00000000..1248d14c --- /dev/null +++ b/app/controllers/schools_controller.rb @@ -0,0 +1,74 @@ +class SchoolsController < ApplicationController + before_action :set_school, only: [:show, :edit, :update, :destroy] + + # GET /schools + # GET /schools.json + def index + @schools = School.all + end + + # GET /schools/1 + # GET /schools/1.json + def show + end + + # GET /schools/new + def new + @school = School.new + end + + # GET /schools/1/edit + def edit + end + + # POST /schools + # POST /schools.json + def create + @school = School.new(school_params) + + respond_to do |format| + if @school.save + format.html { redirect_to @school, notice: 'School was successfully created.' } + format.json { render :show, status: :created, location: @school } + else + format.html { render :new } + format.json { render json: @school.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /schools/1 + # PATCH/PUT /schools/1.json + def update + respond_to do |format| + if @school.update(school_params) + format.html { redirect_to @school, notice: 'School was successfully updated.' } + format.json { render :show, status: :ok, location: @school } + else + format.html { render :edit } + format.json { render json: @school.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /schools/1 + # DELETE /schools/1.json + def destroy + @school.destroy + respond_to do |format| + format.html { redirect_to schools_url, notice: 'School was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_school + @school = School.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def school_params + params.require(:school).permit(:name, :district_id) + end +end diff --git a/app/helpers/districts_helper.rb b/app/helpers/districts_helper.rb new file mode 100644 index 00000000..3029f2c9 --- /dev/null +++ b/app/helpers/districts_helper.rb @@ -0,0 +1,2 @@ +module DistrictsHelper +end diff --git a/app/helpers/schools_helper.rb b/app/helpers/schools_helper.rb new file mode 100644 index 00000000..e1893f43 --- /dev/null +++ b/app/helpers/schools_helper.rb @@ -0,0 +1,2 @@ +module SchoolsHelper +end diff --git a/app/models/district.rb b/app/models/district.rb new file mode 100644 index 00000000..1b2126aa --- /dev/null +++ b/app/models/district.rb @@ -0,0 +1,5 @@ +class District < ApplicationRecord + has_many :schools + + validates :name, presence: true +end diff --git a/app/models/recipient.rb b/app/models/recipient.rb index abf1da26..d5e580f7 100644 --- a/app/models/recipient.rb +++ b/app/models/recipient.rb @@ -1,2 +1,7 @@ class Recipient < ApplicationRecord + belongs_to :school + validates_associated :school + + validates :name, presence: true + end diff --git a/app/models/school.rb b/app/models/school.rb new file mode 100644 index 00000000..d4a0113f --- /dev/null +++ b/app/models/school.rb @@ -0,0 +1,7 @@ +class School < ApplicationRecord + belongs_to :district + has_many :recipients + + validates :name, presence: true + +end diff --git a/app/views/districts/_form.html.haml b/app/views/districts/_form.html.haml new file mode 100644 index 00000000..33a8f43f --- /dev/null +++ b/app/views/districts/_form.html.haml @@ -0,0 +1,17 @@ += form_for(district) do |f| + - if district.errors.any? + #error_explanation + %h2 + = pluralize(district.errors.count, "error") + prohibited this district from being saved: + %ul + - district.errors.full_messages.each do |message| + %li= message + .field + = f.label :name + = f.text_field :name + .field + = f.label :state_id + = f.number_field :state_id + .actions + = f.submit diff --git a/app/views/districts/edit.html.haml b/app/views/districts/edit.html.haml new file mode 100644 index 00000000..a21fc64b --- /dev/null +++ b/app/views/districts/edit.html.haml @@ -0,0 +1,5 @@ +%h1 Editing District += render 'form', district: @district += link_to 'Show', @district +| += link_to 'Back', districts_path diff --git a/app/views/districts/index.html.haml b/app/views/districts/index.html.haml new file mode 100644 index 00000000..211ef57d --- /dev/null +++ b/app/views/districts/index.html.haml @@ -0,0 +1,18 @@ +%p#notice= notice +%h1 Districts +%table + %thead + %tr + %th Name + %th State + %th{:colspan => "3"} + %tbody + - @districts.each do |district| + %tr + %td= district.name + %td= district.state_id + %td= link_to 'Show', district + %td= link_to 'Edit', edit_district_path(district) + %td= link_to 'Destroy', district, method: :delete, data: { confirm: 'Are you sure?' } +%br/ += link_to 'New District', new_district_path diff --git a/app/views/districts/index.json.jbuilder b/app/views/districts/index.json.jbuilder new file mode 100644 index 00000000..b7a5d132 --- /dev/null +++ b/app/views/districts/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@districts) do |district| + json.extract! district, :id, :name, :state_id + json.url district_url(district, format: :json) +end diff --git a/app/views/districts/new.html.haml b/app/views/districts/new.html.haml new file mode 100644 index 00000000..8a1497d2 --- /dev/null +++ b/app/views/districts/new.html.haml @@ -0,0 +1,3 @@ +%h1 New District += render 'form', district: @district += link_to 'Back', districts_path diff --git a/app/views/districts/show.html.haml b/app/views/districts/show.html.haml new file mode 100644 index 00000000..a317b859 --- /dev/null +++ b/app/views/districts/show.html.haml @@ -0,0 +1,10 @@ +%p#notice= notice +%p + %strong Name: + = @district.name +%p + %strong State: + = @district.state_id += link_to 'Edit', edit_district_path(@district) +| += link_to 'Back', districts_path diff --git a/app/views/districts/show.json.jbuilder b/app/views/districts/show.json.jbuilder new file mode 100644 index 00000000..5db3a823 --- /dev/null +++ b/app/views/districts/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @district, :id, :name, :state_id, :created_at, :updated_at diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 586eb286..42479355 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -2,7 +2,7 @@ %html %head %meta{:content => "text/html; charset=UTF-8", "http-equiv" => "Content-Type"}/ - %title Edcontext + %title EdContext = csrf_meta_tags = stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' = stylesheet_link_tag 'https://fonts.googleapis.com/css?family=Roboto:300' diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb deleted file mode 100644 index 37f0bddb..00000000 --- a/app/views/layouts/mailer.text.erb +++ /dev/null @@ -1 +0,0 @@ -<%= yield %> diff --git a/app/views/layouts/mailer.text.haml b/app/views/layouts/mailer.text.haml new file mode 100644 index 00000000..0a90f092 --- /dev/null +++ b/app/views/layouts/mailer.text.haml @@ -0,0 +1 @@ += yield diff --git a/app/views/recipients/_form.html.erb b/app/views/recipients/_form.html.erb deleted file mode 100644 index c9bcf414..00000000 --- a/app/views/recipients/_form.html.erb +++ /dev/null @@ -1,67 +0,0 @@ -<%= form_for(recipient) do |f| %> - <% if recipient.errors.any? %> -
<%= notice %>
- -| Name | -Phone | -Birth date | -Gender | -Race | -Ethnicity | -Home language | -Income | -Opted out | -School | -- | ||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| <%= recipient.name %> | -<%= recipient.phone %> | -<%= recipient.birth_date %> | -<%= recipient.gender %> | -<%= recipient.race %> | -<%= recipient.ethnicity %> | -<%= recipient.home_language_id %> | -<%= recipient.income %> | -<%= recipient.opted_out %> | -<%= recipient.school_id %> | -<%= link_to 'Show', recipient %> | -<%= link_to 'Edit', edit_recipient_path(recipient) %> | -<%= link_to 'Destroy', recipient, method: :delete, data: { confirm: 'Are you sure?' } %> | -
<%= notice %>
- -- Name: - <%= @recipient.name %> -
- -- Phone: - <%= @recipient.phone %> -
- -- Birth date: - <%= @recipient.birth_date %> -
- -- Gender: - <%= @recipient.gender %> -
- -- Race: - <%= @recipient.race %> -
- -- Ethnicity: - <%= @recipient.ethnicity %> -
- -- Home language: - <%= @recipient.home_language_id %> -
- -- Income: - <%= @recipient.income %> -
- -- Opted out: - <%= @recipient.opted_out %> -
- -- School: - <%= @recipient.school_id %> -
- -<%= link_to 'Edit', edit_recipient_path(@recipient) %> | -<%= link_to 'Back', recipients_path %> diff --git a/app/views/recipients/show.html.haml b/app/views/recipients/show.html.haml new file mode 100644 index 00000000..d89fe625 --- /dev/null +++ b/app/views/recipients/show.html.haml @@ -0,0 +1,33 @@ +%p + %strong Name: + = @recipient.name +%p + %strong Phone: + = @recipient.phone +%p + %strong Birth date: + = @recipient.birth_date +%p + %strong Gender: + = @recipient.gender +%p + %strong Race: + = @recipient.race +%p + %strong Ethnicity: + = @recipient.ethnicity +%p + %strong Home language: + = @recipient.home_language_id +%p + %strong Income: + = @recipient.income +%p + %strong Opted out: + = @recipient.opted_out +%p + %strong School: + = @recipient.school_id += link_to 'Edit', edit_school_recipient_path(@school, @recipient) +| += link_to 'Back', school_path(@school) diff --git a/app/views/schools/_form.html.haml b/app/views/schools/_form.html.haml new file mode 100644 index 00000000..bd4ad5f8 --- /dev/null +++ b/app/views/schools/_form.html.haml @@ -0,0 +1,17 @@ += form_for(school) do |f| + - if school.errors.any? + #error_explanation + %h2 + = pluralize(school.errors.count, "error") + prohibited this school from being saved: + %ul + - school.errors.full_messages.each do |message| + %li= message + .form-group + = f.label :name + = f.text_field :name, class: 'form-control' + -# .form-group + -# = f.label :district_id + -# = f.number_field :district_id, class: 'form-control' + .form-group + = f.submit 'Save School', class: 'btn btn-primary' diff --git a/app/views/schools/edit.html.haml b/app/views/schools/edit.html.haml new file mode 100644 index 00000000..8c69ff7f --- /dev/null +++ b/app/views/schools/edit.html.haml @@ -0,0 +1,5 @@ +%h1 Editing School += render 'form', school: @school += link_to 'Show', @school +| += link_to 'Back', schools_path diff --git a/app/views/schools/index.html.haml b/app/views/schools/index.html.haml new file mode 100644 index 00000000..53fe1f8b --- /dev/null +++ b/app/views/schools/index.html.haml @@ -0,0 +1,17 @@ +%h1 Schools +%table + %thead + %tr + %th Name + %th District + %th{:colspan => "3"} + %tbody + - @schools.each do |school| + %tr + %td= school.name + %td= school.district_id + %td= link_to 'Show', school + %td= link_to 'Edit', edit_school_path(school) + %td= link_to 'Destroy', school, method: :delete, data: { confirm: 'Are you sure?' } +%br/ += link_to 'New School', new_school_path diff --git a/app/views/schools/index.json.jbuilder b/app/views/schools/index.json.jbuilder new file mode 100644 index 00000000..507de846 --- /dev/null +++ b/app/views/schools/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@schools) do |school| + json.extract! school, :id, :name, :district_id + json.url school_url(school, format: :json) +end diff --git a/app/views/schools/new.html.haml b/app/views/schools/new.html.haml new file mode 100644 index 00000000..352dff2e --- /dev/null +++ b/app/views/schools/new.html.haml @@ -0,0 +1,6 @@ +.row + .offset-sm-2.col-sm-8 + %h3 Create A New School + = render 'form', school: @school + %br + %p= link_to 'Back', schools_path diff --git a/app/views/schools/show.html.haml b/app/views/schools/show.html.haml new file mode 100644 index 00000000..18be3f2c --- /dev/null +++ b/app/views/schools/show.html.haml @@ -0,0 +1,13 @@ +%p + %strong Name: + = @school.name +%p + %strong District: + = @school.district_id + +%p= link_to "Add Recipient", new_school_recipient_path(@school) + + += link_to 'Edit', edit_school_path(@school) +| += link_to 'Back', schools_path diff --git a/app/views/schools/show.json.jbuilder b/app/views/schools/show.json.jbuilder new file mode 100644 index 00000000..00db5171 --- /dev/null +++ b/app/views/schools/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @school, :id, :name, :district_id, :created_at, :updated_at diff --git a/app/views/welcome/index.html.haml b/app/views/welcome/index.html.haml index 20bf9702..0f1af1eb 100644 --- a/app/views/welcome/index.html.haml +++ b/app/views/welcome/index.html.haml @@ -1 +1,6 @@ -WELCOME +%p= link_to "Create A New District", new_district_path + +%p= link_to "Create A New School", new_school_path + +- @schools.each do |school| + = link_to school.name, school diff --git a/config/routes.rb b/config/routes.rb index 0461e809..5b6711eb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,7 +1,14 @@ Rails.application.routes.draw do - resources :recipients + resources :districts + + resources :schools do + resources :recipients + end + devise_for :users # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + + root to: "welcome#index" end diff --git a/db/migrate/20170228164212_create_schools.rb b/db/migrate/20170228164212_create_schools.rb new file mode 100644 index 00000000..db7baf5d --- /dev/null +++ b/db/migrate/20170228164212_create_schools.rb @@ -0,0 +1,10 @@ +class CreateSchools < ActiveRecord::Migration[5.0] + def change + create_table :schools do |t| + t.string :name + t.integer :district_id + + t.timestamps + end + end +end diff --git a/db/migrate/20170228164245_create_districts.rb b/db/migrate/20170228164245_create_districts.rb new file mode 100644 index 00000000..ed592147 --- /dev/null +++ b/db/migrate/20170228164245_create_districts.rb @@ -0,0 +1,10 @@ +class CreateDistricts < ActiveRecord::Migration[5.0] + def change + create_table :districts do |t| + t.string :name + t.integer :state_id + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index ce57e096..49c795c3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,18 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170225150432) do +ActiveRecord::Schema.define(version: 20170228164245) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "districts", force: :cascade do |t| + t.string "name" + t.integer "state_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "recipients", force: :cascade do |t| t.string "name" t.string "phone" @@ -30,6 +37,13 @@ ActiveRecord::Schema.define(version: 20170225150432) do t.datetime "updated_at", null: false end + create_table "schools", force: :cascade do |t| + t.string "name" + t.integer "district_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "users", force: :cascade do |t| t.string "email", default: "", null: false t.string "encrypted_password", default: "", null: false diff --git a/spec/controllers/districts_controller_spec.rb b/spec/controllers/districts_controller_spec.rb new file mode 100644 index 00000000..2a53ac6a --- /dev/null +++ b/spec/controllers/districts_controller_spec.rb @@ -0,0 +1,159 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +RSpec.describe DistrictsController, type: :controller do + + # This should return the minimal set of attributes required to create a valid + # District. As you add validations to District, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + {name: 'District'} + } + + let(:invalid_attributes) { + {name: ''} + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # DistrictsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET #index" do + it "assigns all districts as @districts" do + district = District.create! valid_attributes + get :index, params: {}, session: valid_session + expect(assigns(:districts)).to eq([district]) + end + end + + describe "GET #show" do + it "assigns the requested district as @district" do + district = District.create! valid_attributes + get :show, params: {id: district.to_param}, session: valid_session + expect(assigns(:district)).to eq(district) + end + end + + describe "GET #new" do + it "assigns a new district as @district" do + get :new, params: {}, session: valid_session + expect(assigns(:district)).to be_a_new(District) + end + end + + describe "GET #edit" do + it "assigns the requested district as @district" do + district = District.create! valid_attributes + get :edit, params: {id: district.to_param}, session: valid_session + expect(assigns(:district)).to eq(district) + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new District" do + expect { + post :create, params: {district: valid_attributes}, session: valid_session + }.to change(District, :count).by(1) + end + + it "assigns a newly created district as @district" do + post :create, params: {district: valid_attributes}, session: valid_session + expect(assigns(:district)).to be_a(District) + expect(assigns(:district)).to be_persisted + end + + it "redirects to the created district" do + post :create, params: {district: valid_attributes}, session: valid_session + expect(response).to redirect_to(District.last) + end + end + + context "with invalid params" do + it "assigns a newly created but unsaved district as @district" do + post :create, params: {district: invalid_attributes}, session: valid_session + expect(assigns(:district)).to be_a_new(District) + end + + it "re-renders the 'new' template" do + post :create, params: {district: invalid_attributes}, session: valid_session + expect(response).to render_template("new") + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + {name: 'New District'} + } + + it "updates the requested district" do + district = District.create! valid_attributes + put :update, params: {id: district.to_param, district: new_attributes}, session: valid_session + district.reload + expect(district.name).to eq('New District') + end + + it "assigns the requested district as @district" do + district = District.create! valid_attributes + put :update, params: {id: district.to_param, district: valid_attributes}, session: valid_session + expect(assigns(:district)).to eq(district) + end + + it "redirects to the district" do + district = District.create! valid_attributes + put :update, params: {id: district.to_param, district: valid_attributes}, session: valid_session + expect(response).to redirect_to(district) + end + end + + context "with invalid params" do + it "assigns the district as @district" do + district = District.create! valid_attributes + put :update, params: {id: district.to_param, district: invalid_attributes}, session: valid_session + expect(assigns(:district)).to eq(district) + end + + it "re-renders the 'edit' template" do + district = District.create! valid_attributes + put :update, params: {id: district.to_param, district: invalid_attributes}, session: valid_session + expect(response).to render_template("edit") + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested district" do + district = District.create! valid_attributes + expect { + delete :destroy, params: {id: district.to_param}, session: valid_session + }.to change(District, :count).by(-1) + end + + it "redirects to the districts list" do + district = District.create! valid_attributes + delete :destroy, params: {id: district.to_param}, session: valid_session + expect(response).to redirect_to(districts_url) + end + end + +end diff --git a/spec/controllers/recipients_controller_spec.rb b/spec/controllers/recipients_controller_spec.rb index be2daa76..81fb7772 100644 --- a/spec/controllers/recipients_controller_spec.rb +++ b/spec/controllers/recipients_controller_spec.rb @@ -20,16 +20,20 @@ require 'rails_helper' RSpec.describe RecipientsController, type: :controller do + let(:school) { School.create!(name: 'School') } + # This should return the minimal set of attributes required to create a valid # Recipient. As you add validations to Recipient, be sure to # adjust the attributes here as well. let(:valid_attributes) { - skip("Add a hash of attributes valid for your model") + { + name: 'Recipient Name', + phone: '111-222-3333', + school_id: school.id + } } - let(:invalid_attributes) { - skip("Add a hash of attributes invalid for your model") - } + let(:invalid_attributes) { {name: '', phone: '111-222-3333'} } # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in @@ -39,7 +43,7 @@ RSpec.describe RecipientsController, type: :controller do describe "GET #index" do it "assigns all recipients as @recipients" do recipient = Recipient.create! valid_attributes - get :index, params: {}, session: valid_session + get :index, params: {school_id: school.to_param}, session: valid_session expect(assigns(:recipients)).to eq([recipient]) end end @@ -47,14 +51,14 @@ RSpec.describe RecipientsController, type: :controller do describe "GET #show" do it "assigns the requested recipient as @recipient" do recipient = Recipient.create! valid_attributes - get :show, params: {id: recipient.to_param}, session: valid_session + get :show, params: {school_id: school.to_param, id: recipient.to_param}, session: valid_session expect(assigns(:recipient)).to eq(recipient) end end describe "GET #new" do it "assigns a new recipient as @recipient" do - get :new, params: {}, session: valid_session + get :new, params: {school_id: school.id}, session: valid_session expect(assigns(:recipient)).to be_a_new(Recipient) end end @@ -62,7 +66,7 @@ RSpec.describe RecipientsController, type: :controller do describe "GET #edit" do it "assigns the requested recipient as @recipient" do recipient = Recipient.create! valid_attributes - get :edit, params: {id: recipient.to_param}, session: valid_session + get :edit, params: {school_id: school.to_param, id: recipient.to_param}, session: valid_session expect(assigns(:recipient)).to eq(recipient) end end @@ -71,30 +75,30 @@ RSpec.describe RecipientsController, type: :controller do context "with valid params" do it "creates a new Recipient" do expect { - post :create, params: {recipient: valid_attributes}, session: valid_session + post :create, params: {school_id: school.to_param, recipient: valid_attributes}, session: valid_session }.to change(Recipient, :count).by(1) end it "assigns a newly created recipient as @recipient" do - post :create, params: {recipient: valid_attributes}, session: valid_session + post :create, params: {school_id: school.to_param, recipient: valid_attributes}, session: valid_session expect(assigns(:recipient)).to be_a(Recipient) expect(assigns(:recipient)).to be_persisted end it "redirects to the created recipient" do - post :create, params: {recipient: valid_attributes}, session: valid_session - expect(response).to redirect_to(Recipient.last) + post :create, params: {school_id: school.to_param, recipient: valid_attributes}, session: valid_session + expect(response).to redirect_to(school_recipient_path(school, Recipient.last)) end end context "with invalid params" do it "assigns a newly created but unsaved recipient as @recipient" do - post :create, params: {recipient: invalid_attributes}, session: valid_session + post :create, params: {school_id: school.to_param, recipient: invalid_attributes}, session: valid_session expect(assigns(:recipient)).to be_a_new(Recipient) end it "re-renders the 'new' template" do - post :create, params: {recipient: invalid_attributes}, session: valid_session + post :create, params: {school_id: school.to_param, recipient: invalid_attributes}, session: valid_session expect(response).to render_template("new") end end @@ -103,39 +107,40 @@ RSpec.describe RecipientsController, type: :controller do describe "PUT #update" do context "with valid params" do let(:new_attributes) { - skip("Add a hash of attributes valid for your model") + {name: 'New Name'} } it "updates the requested recipient" do recipient = Recipient.create! valid_attributes - put :update, params: {id: recipient.to_param, recipient: new_attributes}, session: valid_session + put :update, params: {school_id: school.to_param, id: recipient.to_param, recipient: new_attributes}, session: valid_session recipient.reload - skip("Add assertions for updated state") + expect(recipient.name).to eq('New Name') + expect(recipient.phone).to eq('111-222-3333') end it "assigns the requested recipient as @recipient" do recipient = Recipient.create! valid_attributes - put :update, params: {id: recipient.to_param, recipient: valid_attributes}, session: valid_session + put :update, params: {school_id: school.to_param, id: recipient.to_param, recipient: valid_attributes}, session: valid_session expect(assigns(:recipient)).to eq(recipient) end it "redirects to the recipient" do recipient = Recipient.create! valid_attributes - put :update, params: {id: recipient.to_param, recipient: valid_attributes}, session: valid_session - expect(response).to redirect_to(recipient) + put :update, params: {school_id: school.to_param, id: recipient.to_param, recipient: valid_attributes}, session: valid_session + expect(response).to redirect_to(school_recipient_url(school, recipient)) end end context "with invalid params" do it "assigns the recipient as @recipient" do recipient = Recipient.create! valid_attributes - put :update, params: {id: recipient.to_param, recipient: invalid_attributes}, session: valid_session + put :update, params: {school_id: school.to_param, id: recipient.to_param, recipient: invalid_attributes}, session: valid_session expect(assigns(:recipient)).to eq(recipient) end it "re-renders the 'edit' template" do recipient = Recipient.create! valid_attributes - put :update, params: {id: recipient.to_param, recipient: invalid_attributes}, session: valid_session + put :update, params: {school_id: school.to_param, id: recipient.to_param, recipient: invalid_attributes}, session: valid_session expect(response).to render_template("edit") end end @@ -145,14 +150,14 @@ RSpec.describe RecipientsController, type: :controller do it "destroys the requested recipient" do recipient = Recipient.create! valid_attributes expect { - delete :destroy, params: {id: recipient.to_param}, session: valid_session + delete :destroy, params: {school_id: school.to_param, id: recipient.to_param}, session: valid_session }.to change(Recipient, :count).by(-1) end it "redirects to the recipients list" do recipient = Recipient.create! valid_attributes - delete :destroy, params: {id: recipient.to_param}, session: valid_session - expect(response).to redirect_to(recipients_url) + delete :destroy, params: {school_id: school.to_param, id: recipient.to_param}, session: valid_session + expect(response).to redirect_to(school) end end diff --git a/spec/controllers/schools_controller_spec.rb b/spec/controllers/schools_controller_spec.rb new file mode 100644 index 00000000..7442e17f --- /dev/null +++ b/spec/controllers/schools_controller_spec.rb @@ -0,0 +1,159 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +RSpec.describe SchoolsController, type: :controller do + + # This should return the minimal set of attributes required to create a valid + # School. As you add validations to School, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + {name: 'School'} + } + + let(:invalid_attributes) { + {name: ''} + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # SchoolsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET #index" do + it "assigns all schools as @schools" do + school = School.create! valid_attributes + get :index, params: {}, session: valid_session + expect(assigns(:schools)).to eq([school]) + end + end + + describe "GET #show" do + it "assigns the requested school as @school" do + school = School.create! valid_attributes + get :show, params: {id: school.to_param}, session: valid_session + expect(assigns(:school)).to eq(school) + end + end + + describe "GET #new" do + it "assigns a new school as @school" do + get :new, params: {}, session: valid_session + expect(assigns(:school)).to be_a_new(School) + end + end + + describe "GET #edit" do + it "assigns the requested school as @school" do + school = School.create! valid_attributes + get :edit, params: {id: school.to_param}, session: valid_session + expect(assigns(:school)).to eq(school) + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new School" do + expect { + post :create, params: {school: valid_attributes}, session: valid_session + }.to change(School, :count).by(1) + end + + it "assigns a newly created school as @school" do + post :create, params: {school: valid_attributes}, session: valid_session + expect(assigns(:school)).to be_a(School) + expect(assigns(:school)).to be_persisted + end + + it "redirects to the created school" do + post :create, params: {school: valid_attributes}, session: valid_session + expect(response).to redirect_to(School.last) + end + end + + context "with invalid params" do + it "assigns a newly created but unsaved school as @school" do + post :create, params: {school: invalid_attributes}, session: valid_session + expect(assigns(:school)).to be_a_new(School) + end + + it "re-renders the 'new' template" do + post :create, params: {school: invalid_attributes}, session: valid_session + expect(response).to render_template("new") + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + {name: 'New School'} + } + + it "updates the requested school" do + school = School.create! valid_attributes + put :update, params: {id: school.to_param, school: new_attributes}, session: valid_session + school.reload + expect(school.name).to eq('New School') + end + + it "assigns the requested school as @school" do + school = School.create! valid_attributes + put :update, params: {id: school.to_param, school: valid_attributes}, session: valid_session + expect(assigns(:school)).to eq(school) + end + + it "redirects to the school" do + school = School.create! valid_attributes + put :update, params: {id: school.to_param, school: valid_attributes}, session: valid_session + expect(response).to redirect_to(school) + end + end + + context "with invalid params" do + it "assigns the school as @school" do + school = School.create! valid_attributes + put :update, params: {id: school.to_param, school: invalid_attributes}, session: valid_session + expect(assigns(:school)).to eq(school) + end + + it "re-renders the 'edit' template" do + school = School.create! valid_attributes + put :update, params: {id: school.to_param, school: invalid_attributes}, session: valid_session + expect(response).to render_template("edit") + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested school" do + school = School.create! valid_attributes + expect { + delete :destroy, params: {id: school.to_param}, session: valid_session + }.to change(School, :count).by(-1) + end + + it "redirects to the schools list" do + school = School.create! valid_attributes + delete :destroy, params: {id: school.to_param}, session: valid_session + expect(response).to redirect_to(schools_url) + end + end + +end diff --git a/spec/routing/districts_routing_spec.rb b/spec/routing/districts_routing_spec.rb new file mode 100644 index 00000000..e00d6da4 --- /dev/null +++ b/spec/routing/districts_routing_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe DistrictsController, type: :routing do + describe "routing" do + + it "routes to #index" do + expect(:get => "/districts").to route_to("districts#index") + end + + it "routes to #new" do + expect(:get => "/districts/new").to route_to("districts#new") + end + + it "routes to #show" do + expect(:get => "/districts/1").to route_to("districts#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/districts/1/edit").to route_to("districts#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/districts").to route_to("districts#create") + end + + it "routes to #update via PUT" do + expect(:put => "/districts/1").to route_to("districts#update", :id => "1") + end + + it "routes to #update via PATCH" do + expect(:patch => "/districts/1").to route_to("districts#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/districts/1").to route_to("districts#destroy", :id => "1") + end + + end +end diff --git a/spec/routing/recipients_routing_spec.rb b/spec/routing/recipients_routing_spec.rb index 1dc4d291..df2c0b51 100644 --- a/spec/routing/recipients_routing_spec.rb +++ b/spec/routing/recipients_routing_spec.rb @@ -2,37 +2,43 @@ require "rails_helper" RSpec.describe RecipientsController, type: :routing do describe "routing" do + before(:each) do + @school = School.create!( + :name => "MyString", + :district_id => 1 + ) + end it "routes to #index" do - expect(:get => "/recipients").to route_to("recipients#index") + expect(:get => "schools/#{@school.id}/recipients").to route_to("recipients#index", school_id: @school.id.to_s) end it "routes to #new" do - expect(:get => "/recipients/new").to route_to("recipients#new") + expect(:get => "schools/#{@school.id}/recipients/new").to route_to("recipients#new", school_id: @school.id.to_s) end it "routes to #show" do - expect(:get => "/recipients/1").to route_to("recipients#show", :id => "1") + expect(:get => "schools/#{@school.id}/recipients/1").to route_to("recipients#show", school_id: @school.id.to_s, :id => "1") end it "routes to #edit" do - expect(:get => "/recipients/1/edit").to route_to("recipients#edit", :id => "1") + expect(:get => "schools/#{@school.id}/recipients/1/edit").to route_to("recipients#edit", school_id: @school.id.to_s, :id => "1") end it "routes to #create" do - expect(:post => "/recipients").to route_to("recipients#create") + expect(:post => "schools/#{@school.id}/recipients").to route_to("recipients#create", school_id: @school.id.to_s) end it "routes to #update via PUT" do - expect(:put => "/recipients/1").to route_to("recipients#update", :id => "1") + expect(:put => "schools/#{@school.id}/recipients/1").to route_to("recipients#update", school_id: @school.id.to_s, :id => "1") end it "routes to #update via PATCH" do - expect(:patch => "/recipients/1").to route_to("recipients#update", :id => "1") + expect(:patch => "schools/#{@school.id}/recipients/1").to route_to("recipients#update", school_id: @school.id.to_s, :id => "1") end it "routes to #destroy" do - expect(:delete => "/recipients/1").to route_to("recipients#destroy", :id => "1") + expect(:delete => "schools/#{@school.id}/recipients/1").to route_to("recipients#destroy", school_id: @school.id.to_s, :id => "1") end end diff --git a/spec/routing/schools_routing_spec.rb b/spec/routing/schools_routing_spec.rb new file mode 100644 index 00000000..6980bb9e --- /dev/null +++ b/spec/routing/schools_routing_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe SchoolsController, type: :routing do + describe "routing" do + + it "routes to #index" do + expect(:get => "/schools").to route_to("schools#index") + end + + it "routes to #new" do + expect(:get => "/schools/new").to route_to("schools#new") + end + + it "routes to #show" do + expect(:get => "/schools/1").to route_to("schools#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/schools/1/edit").to route_to("schools#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/schools").to route_to("schools#create") + end + + it "routes to #update via PUT" do + expect(:put => "/schools/1").to route_to("schools#update", :id => "1") + end + + it "routes to #update via PATCH" do + expect(:patch => "/schools/1").to route_to("schools#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/schools/1").to route_to("schools#destroy", :id => "1") + end + + end +end diff --git a/spec/views/districts/edit.html.erb_spec.rb b/spec/views/districts/edit.html.erb_spec.rb new file mode 100644 index 00000000..74e0df82 --- /dev/null +++ b/spec/views/districts/edit.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe "districts/edit", type: :view do + before(:each) do + @district = assign(:district, District.create!( + :name => "MyString", + :state_id => 1 + )) + end + + it "renders the edit district form" do + render + + assert_select "form[action=?][method=?]", district_path(@district), "post" do + + assert_select "input#district_name[name=?]", "district[name]" + + assert_select "input#district_state_id[name=?]", "district[state_id]" + end + end +end diff --git a/spec/views/districts/index.html.erb_spec.rb b/spec/views/districts/index.html.erb_spec.rb new file mode 100644 index 00000000..aa28e2d3 --- /dev/null +++ b/spec/views/districts/index.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "districts/index", type: :view do + before(:each) do + assign(:districts, [ + District.create!( + :name => "Name", + :state_id => 2 + ), + District.create!( + :name => "Name", + :state_id => 2 + ) + ]) + end + + it "renders a list of districts" do + render + assert_select "tr>td", :text => "Name".to_s, :count => 2 + assert_select "tr>td", :text => 2.to_s, :count => 2 + end +end diff --git a/spec/views/districts/new.html.erb_spec.rb b/spec/views/districts/new.html.erb_spec.rb new file mode 100644 index 00000000..8e149f0e --- /dev/null +++ b/spec/views/districts/new.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe "districts/new", type: :view do + before(:each) do + assign(:district, District.new( + :name => "MyString", + :state_id => 1 + )) + end + + it "renders new district form" do + render + + assert_select "form[action=?][method=?]", districts_path, "post" do + + assert_select "input#district_name[name=?]", "district[name]" + + assert_select "input#district_state_id[name=?]", "district[state_id]" + end + end +end diff --git a/spec/views/districts/show.html.erb_spec.rb b/spec/views/districts/show.html.erb_spec.rb new file mode 100644 index 00000000..06831d9f --- /dev/null +++ b/spec/views/districts/show.html.erb_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +RSpec.describe "districts/show", type: :view do + before(:each) do + @district = assign(:district, District.create!( + :name => "Name", + :state_id => 2 + )) + end + + it "renders attributes in" do + render + expect(rendered).to match(/Name/) + expect(rendered).to match(/2/) + end +end diff --git a/spec/views/recipients/edit.html.erb_spec.rb b/spec/views/recipients/edit.html.erb_spec.rb index 4c6310bd..c1130661 100644 --- a/spec/views/recipients/edit.html.erb_spec.rb +++ b/spec/views/recipients/edit.html.erb_spec.rb @@ -2,6 +2,10 @@ require 'rails_helper' RSpec.describe "recipients/edit", type: :view do before(:each) do + @school = assign(:recipient, School.create!( + :name => "School" + )) + @recipient = assign(:recipient, Recipient.create!( :name => "MyString", :phone => "MyString", @@ -11,14 +15,14 @@ RSpec.describe "recipients/edit", type: :view do :home_language_id => 1, :income => "MyString", :opted_out => false, - :school_id => 1 + :school_id => @school.id )) end it "renders the edit recipient form" do render - assert_select "form[action=?][method=?]", recipient_path(@recipient), "post" do + assert_select "form[action=?][method=?]", school_recipient_path(@school, @recipient), "post" do assert_select "input#recipient_name[name=?]", "recipient[name]" @@ -34,9 +38,6 @@ RSpec.describe "recipients/edit", type: :view do assert_select "input#recipient_income[name=?]", "recipient[income]" - assert_select "input#recipient_opted_out[name=?]", "recipient[opted_out]" - - assert_select "input#recipient_school_id[name=?]", "recipient[school_id]" end end end diff --git a/spec/views/recipients/index.html.erb_spec.rb b/spec/views/recipients/index.html.erb_spec.rb index b284f4ed..82250e7b 100644 --- a/spec/views/recipients/index.html.erb_spec.rb +++ b/spec/views/recipients/index.html.erb_spec.rb @@ -2,6 +2,10 @@ require 'rails_helper' RSpec.describe "recipients/index", type: :view do before(:each) do + @school = assign(:school, School.create!( + name: 'School' + )) + assign(:recipients, [ Recipient.create!( :name => "Name", @@ -12,7 +16,7 @@ RSpec.describe "recipients/index", type: :view do :home_language_id => 2, :income => "Income", :opted_out => false, - :school_id => 3 + :school_id => @school.to_param ), Recipient.create!( :name => "Name", @@ -23,7 +27,7 @@ RSpec.describe "recipients/index", type: :view do :home_language_id => 2, :income => "Income", :opted_out => false, - :school_id => 3 + :school_id => @school.to_param ) ]) end @@ -38,6 +42,6 @@ RSpec.describe "recipients/index", type: :view do assert_select "tr>td", :text => 2.to_s, :count => 2 assert_select "tr>td", :text => "Income".to_s, :count => 2 assert_select "tr>td", :text => false.to_s, :count => 2 - assert_select "tr>td", :text => 3.to_s, :count => 2 + assert_select "tr>td", :text => @school.to_param, :count => 2 end end diff --git a/spec/views/recipients/new.html.erb_spec.rb b/spec/views/recipients/new.html.erb_spec.rb index 255ec613..a152c41a 100644 --- a/spec/views/recipients/new.html.erb_spec.rb +++ b/spec/views/recipients/new.html.erb_spec.rb @@ -2,7 +2,11 @@ require 'rails_helper' RSpec.describe "recipients/new", type: :view do before(:each) do - assign(:recipient, Recipient.new( + @school = assign(:school, School.create!( + name: 'School' + )) + + @recipient = assign(:recipient, Recipient.new( :name => "MyString", :phone => "MyString", :gender => "MyString", @@ -11,14 +15,14 @@ RSpec.describe "recipients/new", type: :view do :home_language_id => 1, :income => "MyString", :opted_out => false, - :school_id => 1 + :school_id => @school.to_param )) end it "renders new recipient form" do render - assert_select "form[action=?][method=?]", recipients_path, "post" do + assert_select "form[action=?][method=?]", school_recipients_path(@school, @recipient), "post" do assert_select "input#recipient_name[name=?]", "recipient[name]" @@ -34,9 +38,6 @@ RSpec.describe "recipients/new", type: :view do assert_select "input#recipient_income[name=?]", "recipient[income]" - assert_select "input#recipient_opted_out[name=?]", "recipient[opted_out]" - - assert_select "input#recipient_school_id[name=?]", "recipient[school_id]" end end end diff --git a/spec/views/recipients/show.html.erb_spec.rb b/spec/views/recipients/show.html.erb_spec.rb index 9921821b..10bd4e46 100644 --- a/spec/views/recipients/show.html.erb_spec.rb +++ b/spec/views/recipients/show.html.erb_spec.rb @@ -2,6 +2,10 @@ require 'rails_helper' RSpec.describe "recipients/show", type: :view do before(:each) do + @school = assign(:school, School.create!( + name: 'School' + )) + @recipient = assign(:recipient, Recipient.create!( :name => "Name", :phone => "Phone", @@ -11,7 +15,7 @@ RSpec.describe "recipients/show", type: :view do :home_language_id => 2, :income => "Income", :opted_out => false, - :school_id => 3 + :school_id => @school.to_param )) end @@ -25,6 +29,6 @@ RSpec.describe "recipients/show", type: :view do expect(rendered).to match(/2/) expect(rendered).to match(/Income/) expect(rendered).to match(/false/) - expect(rendered).to match(/3/) + expect(rendered).to match(/#{@school.to_param}/) end end diff --git a/spec/views/schools/edit.html.erb_spec.rb b/spec/views/schools/edit.html.erb_spec.rb new file mode 100644 index 00000000..efd2019a --- /dev/null +++ b/spec/views/schools/edit.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.describe "schools/edit", type: :view do + before(:each) do + @school = assign(:school, School.create!( + :name => "MyString", + :district_id => 1 + )) + end + + it "renders the edit school form" do + render + + assert_select "form[action=?][method=?]", school_path(@school), "post" do + + assert_select "input#school_name[name=?]", "school[name]" + + end + end +end diff --git a/spec/views/schools/index.html.erb_spec.rb b/spec/views/schools/index.html.erb_spec.rb new file mode 100644 index 00000000..0ee5886c --- /dev/null +++ b/spec/views/schools/index.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "schools/index", type: :view do + before(:each) do + assign(:schools, [ + School.create!( + :name => "Name", + :district_id => 2 + ), + School.create!( + :name => "Name", + :district_id => 2 + ) + ]) + end + + it "renders a list of schools" do + render + assert_select "tr>td", :text => "Name".to_s, :count => 2 + assert_select "tr>td", :text => 2.to_s, :count => 2 + end +end diff --git a/spec/views/schools/new.html.erb_spec.rb b/spec/views/schools/new.html.erb_spec.rb new file mode 100644 index 00000000..7304621f --- /dev/null +++ b/spec/views/schools/new.html.erb_spec.rb @@ -0,0 +1,20 @@ +require 'rails_helper' + +RSpec.describe "schools/new", type: :view do + before(:each) do + assign(:school, School.new( + :name => "MyString", + :district_id => 1 + )) + end + + it "renders new school form" do + render + + assert_select "form[action=?][method=?]", schools_path, "post" do + + assert_select "input#school_name[name=?]", "school[name]" + + end + end +end diff --git a/spec/views/schools/show.html.erb_spec.rb b/spec/views/schools/show.html.erb_spec.rb new file mode 100644 index 00000000..99f7081f --- /dev/null +++ b/spec/views/schools/show.html.erb_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +RSpec.describe "schools/show", type: :view do + before(:each) do + @school = assign(:school, School.create!( + :name => "Name", + :district_id => 2 + )) + end + + it "renders attributes in
" do + render + expect(rendered).to match(/Name/) + expect(rendered).to match(/2/) + end +end