added listings scaffold

This commit is contained in:
YOUR NAME 2017-11-03 17:09:11 +11:00
parent 1e68fb2c15
commit 3cca1142b9
29 changed files with 515 additions and 4 deletions

View file

@ -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/

View file

@ -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/

View file

@ -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;
}

View file

@ -1,4 +1,3 @@
class ApplicationController < ActionController::Base
before_action :authenticate_user!
protect_from_forgery with: :exception
end

View file

@ -1,5 +1,6 @@
class HomeController < ApplicationController
before_action :authenticate_user!
def index
end
end

View file

@ -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

View file

@ -0,0 +1,2 @@
module ListingsHelper
end

2
app/models/listing.rb Normal file
View file

@ -0,0 +1,2 @@
class Listing < ApplicationRecord
end

View file

@ -1,5 +1,6 @@
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<% if can? :update, @article %>
<h2>Rently front page</h2>
<!-- <% if can? :update, @article %>
<%= link_to "Edit", edit_article_path(@article) %>
<% end %>
<% end %> -->

View file

@ -0,0 +1,17 @@
<%= form_with(model: listing, local: true) do |form| %>
<% if listing.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(listing.errors.count, "error") %> prohibited this listing from being saved:</h2>
<ul>
<% listing.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

View file

@ -0,0 +1,2 @@
json.extract! listing, :id, :created_at, :updated_at
json.url listing_url(listing, format: :json)

View file

@ -0,0 +1,6 @@
<h1>Editing Listing</h1>
<%= render 'form', listing: @listing %>
<%= link_to 'Show', @listing %> |
<%= link_to 'Back', listings_path %>

View file

@ -0,0 +1,25 @@
<p id="notice"><%= notice %></p>
<h1>Listings</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @listings.each do |listing| %>
<tr>
<td><%= link_to 'Show', listing %></td>
<td><%= link_to 'Edit', edit_listing_path(listing) %></td>
<td><%= link_to 'Destroy', listing, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Listing', new_listing_path %>

View file

@ -0,0 +1 @@
json.array! @listings, partial: 'listings/listing', as: :listing

View file

@ -0,0 +1,5 @@
<h1>New Listing</h1>
<%= render 'form', listing: @listing %>
<%= link_to 'Back', listings_path %>

View file

@ -0,0 +1,4 @@
<p id="notice"><%= notice %></p>
<%= link_to 'Edit', edit_listing_path(@listing) %> |
<%= link_to 'Back', listings_path %>

View file

@ -0,0 +1 @@
json.partial! "listings/listing", listing: @listing

View file

@ -1,5 +1,6 @@
Rails.application.routes.draw do
resources :listings
devise_for :users
get 'home/index'

View file

@ -0,0 +1,8 @@
class CreateListings < ActiveRecord::Migration[5.1]
def change
create_table :listings do |t|
t.timestamps
end
end
end

View file

@ -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

View file

@ -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

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe Listing, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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 <p>" do
render
end
end

View file

@ -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