From 3cca1142b9190f893c143d7cab7204489221bde8 Mon Sep 17 00:00:00 2001 From: YOUR NAME Date: Fri, 3 Nov 2017 17:09:11 +1100 Subject: [PATCH] added listings scaffold --- app/assets/javascripts/listings.coffee | 3 + app/assets/stylesheets/listings.scss | 3 + app/assets/stylesheets/scaffolds.scss | 84 +++++++++++ app/controllers/application_controller.rb | 1 - app/controllers/home_controller.rb | 3 +- app/controllers/listings_controller.rb | 74 ++++++++++ app/helpers/listings_helper.rb | 2 + app/models/listing.rb | 2 + app/views/home/index.html.erb | 5 +- app/views/listings/_form.html.erb | 17 +++ app/views/listings/_listing.json.jbuilder | 2 + app/views/listings/edit.html.erb | 6 + app/views/listings/index.html.erb | 25 ++++ app/views/listings/index.json.jbuilder | 1 + app/views/listings/new.html.erb | 5 + app/views/listings/show.html.erb | 4 + app/views/listings/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20171103052641_create_listings.rb | 8 ++ spec/controllers/listings_controller_spec.rb | 141 +++++++++++++++++++ spec/helpers/listings_helper_spec.rb | 15 ++ spec/models/listing_spec.rb | 5 + spec/requests/listings_spec.rb | 10 ++ spec/routing/listings_routing_spec.rb | 39 +++++ spec/views/listings/edit.html.erb_spec.rb | 14 ++ spec/views/listings/index.html.erb_spec.rb | 14 ++ spec/views/listings/new.html.erb_spec.rb | 14 ++ spec/views/listings/show.html.erb_spec.rb | 11 ++ test/system/listings_test.rb | 9 ++ 29 files changed, 515 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/listings.coffee create mode 100644 app/assets/stylesheets/listings.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/listings_controller.rb create mode 100644 app/helpers/listings_helper.rb create mode 100644 app/models/listing.rb create mode 100644 app/views/listings/_form.html.erb create mode 100644 app/views/listings/_listing.json.jbuilder create mode 100644 app/views/listings/edit.html.erb create mode 100644 app/views/listings/index.html.erb create mode 100644 app/views/listings/index.json.jbuilder create mode 100644 app/views/listings/new.html.erb create mode 100644 app/views/listings/show.html.erb create mode 100644 app/views/listings/show.json.jbuilder create mode 100644 db/migrate/20171103052641_create_listings.rb create mode 100644 spec/controllers/listings_controller_spec.rb create mode 100644 spec/helpers/listings_helper_spec.rb create mode 100644 spec/models/listing_spec.rb create mode 100644 spec/requests/listings_spec.rb create mode 100644 spec/routing/listings_routing_spec.rb create mode 100644 spec/views/listings/edit.html.erb_spec.rb create mode 100644 spec/views/listings/index.html.erb_spec.rb create mode 100644 spec/views/listings/new.html.erb_spec.rb create mode 100644 spec/views/listings/show.html.erb_spec.rb create mode 100644 test/system/listings_test.rb diff --git a/app/assets/javascripts/listings.coffee b/app/assets/javascripts/listings.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/listings.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/listings.scss b/app/assets/stylesheets/listings.scss new file mode 100644 index 0000000..26576b1 --- /dev/null +++ b/app/assets/stylesheets/listings.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Listings 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 new file mode 100644 index 0000000..6045188 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,84 @@ +body { + background-color: #fff; + color: #333; + margin: 33px; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + + &:visited { + color: #666; + } + + &:hover { + color: #fff; + background-color: #000; + } +} + +th { + padding-bottom: 5px; +} + +td { + padding: 0 5px 7px; +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px 7px 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px -7px 0; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} + +label { + display: block; +} diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 89bfce6..1c07694 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,4 +1,3 @@ class ApplicationController < ActionController::Base - before_action :authenticate_user! protect_from_forgery with: :exception end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index c938d4f..154922a 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,5 +1,6 @@ class HomeController < ApplicationController + before_action :authenticate_user! def index - + end end diff --git a/app/controllers/listings_controller.rb b/app/controllers/listings_controller.rb new file mode 100644 index 0000000..a1bd4b4 --- /dev/null +++ b/app/controllers/listings_controller.rb @@ -0,0 +1,74 @@ +class ListingsController < ApplicationController + before_action :set_listing, only: [:show, :edit, :update, :destroy] + + # GET /listings + # GET /listings.json + def index + @listings = Listing.all + end + + # GET /listings/1 + # GET /listings/1.json + def show + end + + # GET /listings/new + def new + @listing = Listing.new + end + + # GET /listings/1/edit + def edit + end + + # POST /listings + # POST /listings.json + def create + @listing = Listing.new(listing_params) + + respond_to do |format| + if @listing.save + format.html { redirect_to @listing, notice: 'Listing was successfully created.' } + format.json { render :show, status: :created, location: @listing } + else + format.html { render :new } + format.json { render json: @listing.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /listings/1 + # PATCH/PUT /listings/1.json + def update + respond_to do |format| + if @listing.update(listing_params) + format.html { redirect_to @listing, notice: 'Listing was successfully updated.' } + format.json { render :show, status: :ok, location: @listing } + else + format.html { render :edit } + format.json { render json: @listing.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /listings/1 + # DELETE /listings/1.json + def destroy + @listing.destroy + respond_to do |format| + format.html { redirect_to listings_url, notice: 'Listing was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_listing + @listing = Listing.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def listing_params + params.fetch(:listing, {}) + end +end diff --git a/app/helpers/listings_helper.rb b/app/helpers/listings_helper.rb new file mode 100644 index 0000000..094f4c2 --- /dev/null +++ b/app/helpers/listings_helper.rb @@ -0,0 +1,2 @@ +module ListingsHelper +end diff --git a/app/models/listing.rb b/app/models/listing.rb new file mode 100644 index 0000000..c6f31a8 --- /dev/null +++ b/app/models/listing.rb @@ -0,0 +1,2 @@ +class Listing < ApplicationRecord +end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index e4d5b4f..c83a623 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,5 +1,6 @@

Home#index

Find me in app/views/home/index.html.erb

-<% if can? :update, @article %> +

Rently front page

+ diff --git a/app/views/listings/_form.html.erb b/app/views/listings/_form.html.erb new file mode 100644 index 0000000..63612bf --- /dev/null +++ b/app/views/listings/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_with(model: listing, local: true) do |form| %> + <% if listing.errors.any? %> +
+

<%= pluralize(listing.errors.count, "error") %> prohibited this listing from being saved:

+ + +
+ <% end %> + +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/listings/_listing.json.jbuilder b/app/views/listings/_listing.json.jbuilder new file mode 100644 index 0000000..c3128ba --- /dev/null +++ b/app/views/listings/_listing.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! listing, :id, :created_at, :updated_at +json.url listing_url(listing, format: :json) diff --git a/app/views/listings/edit.html.erb b/app/views/listings/edit.html.erb new file mode 100644 index 0000000..b980d2c --- /dev/null +++ b/app/views/listings/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Listing

+ +<%= render 'form', listing: @listing %> + +<%= link_to 'Show', @listing %> | +<%= link_to 'Back', listings_path %> diff --git a/app/views/listings/index.html.erb b/app/views/listings/index.html.erb new file mode 100644 index 0000000..50f0202 --- /dev/null +++ b/app/views/listings/index.html.erb @@ -0,0 +1,25 @@ +

<%= notice %>

+ +

Listings

+ + + + + + + + + + <% @listings.each do |listing| %> + + + + + + <% end %> + +
<%= link_to 'Show', listing %><%= link_to 'Edit', edit_listing_path(listing) %><%= link_to 'Destroy', listing, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Listing', new_listing_path %> diff --git a/app/views/listings/index.json.jbuilder b/app/views/listings/index.json.jbuilder new file mode 100644 index 0000000..ca3dc2d --- /dev/null +++ b/app/views/listings/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @listings, partial: 'listings/listing', as: :listing diff --git a/app/views/listings/new.html.erb b/app/views/listings/new.html.erb new file mode 100644 index 0000000..edb2b6c --- /dev/null +++ b/app/views/listings/new.html.erb @@ -0,0 +1,5 @@ +

New Listing

+ +<%= render 'form', listing: @listing %> + +<%= link_to 'Back', listings_path %> diff --git a/app/views/listings/show.html.erb b/app/views/listings/show.html.erb new file mode 100644 index 0000000..ca12898 --- /dev/null +++ b/app/views/listings/show.html.erb @@ -0,0 +1,4 @@ +

<%= notice %>

+ +<%= link_to 'Edit', edit_listing_path(@listing) %> | +<%= link_to 'Back', listings_path %> diff --git a/app/views/listings/show.json.jbuilder b/app/views/listings/show.json.jbuilder new file mode 100644 index 0000000..100f06e --- /dev/null +++ b/app/views/listings/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "listings/listing", listing: @listing diff --git a/config/routes.rb b/config/routes.rb index 69ce219..4ab179e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,6 @@ Rails.application.routes.draw do + resources :listings devise_for :users get 'home/index' diff --git a/db/migrate/20171103052641_create_listings.rb b/db/migrate/20171103052641_create_listings.rb new file mode 100644 index 0000000..47cfdb9 --- /dev/null +++ b/db/migrate/20171103052641_create_listings.rb @@ -0,0 +1,8 @@ +class CreateListings < ActiveRecord::Migration[5.1] + def change + create_table :listings do |t| + + t.timestamps + end + end +end diff --git a/spec/controllers/listings_controller_spec.rb b/spec/controllers/listings_controller_spec.rb new file mode 100644 index 0000000..15591a1 --- /dev/null +++ b/spec/controllers/listings_controller_spec.rb @@ -0,0 +1,141 @@ +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. +# +# Also compared to earlier versions of this generator, there are no longer any +# expectations of assigns and templates rendered. These features have been +# removed from Rails core in Rails 5, but can be added back in via the +# `rails-controller-testing` gem. + +RSpec.describe ListingsController, type: :controller do + + # This should return the minimal set of attributes required to create a valid + # Listing. As you add validations to Listing, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # 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 + # ListingsController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET #index" do + it "returns a success response" do + listing = Listing.create! valid_attributes + get :index, params: {}, session: valid_session + expect(response).to be_success + end + end + + describe "GET #show" do + it "returns a success response" do + listing = Listing.create! valid_attributes + get :show, params: {id: listing.to_param}, session: valid_session + expect(response).to be_success + end + end + + describe "GET #new" do + it "returns a success response" do + get :new, params: {}, session: valid_session + expect(response).to be_success + end + end + + describe "GET #edit" do + it "returns a success response" do + listing = Listing.create! valid_attributes + get :edit, params: {id: listing.to_param}, session: valid_session + expect(response).to be_success + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new Listing" do + expect { + post :create, params: {listing: valid_attributes}, session: valid_session + }.to change(Listing, :count).by(1) + end + + it "redirects to the created listing" do + post :create, params: {listing: valid_attributes}, session: valid_session + expect(response).to redirect_to(Listing.last) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'new' template)" do + post :create, params: {listing: invalid_attributes}, session: valid_session + expect(response).to be_success + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested listing" do + listing = Listing.create! valid_attributes + put :update, params: {id: listing.to_param, listing: new_attributes}, session: valid_session + listing.reload + skip("Add assertions for updated state") + end + + it "redirects to the listing" do + listing = Listing.create! valid_attributes + put :update, params: {id: listing.to_param, listing: valid_attributes}, session: valid_session + expect(response).to redirect_to(listing) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'edit' template)" do + listing = Listing.create! valid_attributes + put :update, params: {id: listing.to_param, listing: invalid_attributes}, session: valid_session + expect(response).to be_success + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested listing" do + listing = Listing.create! valid_attributes + expect { + delete :destroy, params: {id: listing.to_param}, session: valid_session + }.to change(Listing, :count).by(-1) + end + + it "redirects to the listings list" do + listing = Listing.create! valid_attributes + delete :destroy, params: {id: listing.to_param}, session: valid_session + expect(response).to redirect_to(listings_url) + end + end + +end diff --git a/spec/helpers/listings_helper_spec.rb b/spec/helpers/listings_helper_spec.rb new file mode 100644 index 0000000..566bc85 --- /dev/null +++ b/spec/helpers/listings_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the ListingsHelper. For example: +# +# describe ListingsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe ListingsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/listing_spec.rb b/spec/models/listing_spec.rb new file mode 100644 index 0000000..cb74171 --- /dev/null +++ b/spec/models/listing_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Listing, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/listings_spec.rb b/spec/requests/listings_spec.rb new file mode 100644 index 0000000..f492eee --- /dev/null +++ b/spec/requests/listings_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe "Listings", type: :request do + describe "GET /listings" do + it "works! (now write some real specs)" do + get listings_path + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/routing/listings_routing_spec.rb b/spec/routing/listings_routing_spec.rb new file mode 100644 index 0000000..81ad2d6 --- /dev/null +++ b/spec/routing/listings_routing_spec.rb @@ -0,0 +1,39 @@ +require "rails_helper" + +RSpec.describe ListingsController, type: :routing do + describe "routing" do + + it "routes to #index" do + expect(:get => "/listings").to route_to("listings#index") + end + + it "routes to #new" do + expect(:get => "/listings/new").to route_to("listings#new") + end + + it "routes to #show" do + expect(:get => "/listings/1").to route_to("listings#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/listings/1/edit").to route_to("listings#edit", :id => "1") + end + + it "routes to #create" do + expect(:post => "/listings").to route_to("listings#create") + end + + it "routes to #update via PUT" do + expect(:put => "/listings/1").to route_to("listings#update", :id => "1") + end + + it "routes to #update via PATCH" do + expect(:patch => "/listings/1").to route_to("listings#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/listings/1").to route_to("listings#destroy", :id => "1") + end + + end +end diff --git a/spec/views/listings/edit.html.erb_spec.rb b/spec/views/listings/edit.html.erb_spec.rb new file mode 100644 index 0000000..9a26692 --- /dev/null +++ b/spec/views/listings/edit.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "listings/edit", type: :view do + before(:each) do + @listing = assign(:listing, Listing.create!()) + end + + it "renders the edit listing form" do + render + + assert_select "form[action=?][method=?]", listing_path(@listing), "post" do + end + end +end diff --git a/spec/views/listings/index.html.erb_spec.rb b/spec/views/listings/index.html.erb_spec.rb new file mode 100644 index 0000000..d3612b2 --- /dev/null +++ b/spec/views/listings/index.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "listings/index", type: :view do + before(:each) do + assign(:listings, [ + Listing.create!(), + Listing.create!() + ]) + end + + it "renders a list of listings" do + render + end +end diff --git a/spec/views/listings/new.html.erb_spec.rb b/spec/views/listings/new.html.erb_spec.rb new file mode 100644 index 0000000..a3be8b5 --- /dev/null +++ b/spec/views/listings/new.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "listings/new", type: :view do + before(:each) do + assign(:listing, Listing.new()) + end + + it "renders new listing form" do + render + + assert_select "form[action=?][method=?]", listings_path, "post" do + end + end +end diff --git a/spec/views/listings/show.html.erb_spec.rb b/spec/views/listings/show.html.erb_spec.rb new file mode 100644 index 0000000..0a487e6 --- /dev/null +++ b/spec/views/listings/show.html.erb_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "listings/show", type: :view do + before(:each) do + @listing = assign(:listing, Listing.create!()) + end + + it "renders attributes in

" do + render + end +end diff --git a/test/system/listings_test.rb b/test/system/listings_test.rb new file mode 100644 index 0000000..7dd44f7 --- /dev/null +++ b/test/system/listings_test.rb @@ -0,0 +1,9 @@ +require "application_system_test_case" + +class ListingsTest < ApplicationSystemTestCase + # test "visiting the index" do + # visit listings_url + # + # assert_selector "h1", text: "Listing" + # end +end