Browse Source

create table sponsorship

tags/v1
wyx 6 years ago
parent
commit
cb089b1bb8
25 changed files with 582 additions and 0 deletions
  1. +2
    -0
      app/assets/javascripts/sponsorships.js
  2. +84
    -0
      app/assets/stylesheets/scaffolds.scss
  3. +3
    -0
      app/assets/stylesheets/sponsorships.scss
  4. +74
    -0
      app/controllers/sponsorships_controller.rb
  5. +2
    -0
      app/helpers/sponsorships_helper.rb
  6. +5
    -0
      app/models/sponsorship.rb
  7. +16
    -0
      app/views/sponsorships/_form.html.erb
  8. +2
    -0
      app/views/sponsorships/_sponsorship.json.jbuilder
  9. +6
    -0
      app/views/sponsorships/edit.html.erb
  10. +33
    -0
      app/views/sponsorships/index.html.erb
  11. +1
    -0
      app/views/sponsorships/index.json.jbuilder
  12. +5
    -0
      app/views/sponsorships/new.html.erb
  13. +24
    -0
      app/views/sponsorships/show.html.erb
  14. +1
    -0
      app/views/sponsorships/show.json.jbuilder
  15. +1
    -0
      config/routes.rb
  16. +12
    -0
      db/migrate/20200724090750_create_sponsorships.rb
  17. +141
    -0
      spec/controllers/sponsorships_controller_spec.rb
  18. +15
    -0
      spec/helpers/sponsorships_helper_spec.rb
  19. +5
    -0
      spec/models/sponsorship_spec.rb
  20. +10
    -0
      spec/requests/sponsorships_spec.rb
  21. +38
    -0
      spec/routing/sponsorships_routing_spec.rb
  22. +27
    -0
      spec/views/sponsorships/edit.html.erb_spec.rb
  23. +28
    -0
      spec/views/sponsorships/index.html.erb_spec.rb
  24. +27
    -0
      spec/views/sponsorships/new.html.erb_spec.rb
  25. +20
    -0
      spec/views/sponsorships/show.html.erb_spec.rb

+ 2
- 0
app/assets/javascripts/sponsorships.js View File

@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.

+ 84
- 0
app/assets/stylesheets/scaffolds.scss 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;
}

+ 3
- 0
app/assets/stylesheets/sponsorships.scss View File

@@ -0,0 +1,3 @@
// Place all the styles related to the Sponsorships controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

+ 74
- 0
app/controllers/sponsorships_controller.rb View File

@@ -0,0 +1,74 @@
class SponsorshipsController < ApplicationController
before_action :set_sponsorship, only: [:show, :edit, :update, :destroy]

# GET /sponsorships
# GET /sponsorships.json
def index
@sponsorships = Sponsorship.all
end

# GET /sponsorships/1
# GET /sponsorships/1.json
def show
end

# GET /sponsorships/new
def new
@sponsorship = Sponsorship.new
end

# GET /sponsorships/1/edit
def edit
end

# POST /sponsorships
# POST /sponsorships.json
def create
@sponsorship = Sponsorship.new(sponsorship_params)

respond_to do |format|
if @sponsorship.save
format.html { redirect_to @sponsorship, notice: 'Sponsorship was successfully created.' }
format.json { render :show, status: :created, location: @sponsorship }
else
format.html { render :new }
format.json { render json: @sponsorship.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /sponsorships/1
# PATCH/PUT /sponsorships/1.json
def update
respond_to do |format|
if @sponsorship.update(sponsorship_params)
format.html { redirect_to @sponsorship, notice: 'Sponsorship was successfully updated.' }
format.json { render :show, status: :ok, location: @sponsorship }
else
format.html { render :edit }
format.json { render json: @sponsorship.errors, status: :unprocessable_entity }
end
end
end

# DELETE /sponsorships/1
# DELETE /sponsorships/1.json
def destroy
@sponsorship.destroy
respond_to do |format|
format.html { redirect_to sponsorships_url, notice: 'Sponsorship was successfully destroyed.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_sponsorship
@sponsorship = Sponsorship.find(params[:id])
end

# Only allow a list of trusted parameters through.
def sponsorship_params
params.require(:sponsorship).permit(:amount, :visible, :sponsor_id, :developer_id)
end
end

+ 2
- 0
app/helpers/sponsorships_helper.rb View File

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

+ 5
- 0
app/models/sponsorship.rb View File

@@ -0,0 +1,5 @@
class Sponsorship < ApplicationRecord
belongs_to :sponsor, class_name: 'User'
belongs_to :developer, class_name: 'User'
validates :amount, presence: true
end

+ 16
- 0
app/views/sponsorships/_form.html.erb View File

@@ -0,0 +1,16 @@

<%= simple_form_for(@sponsorship) do |f| %>
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

<div class="form-inputs">
<%= f.input :amount %>
<%= f.input :visible %>
<%= f.input :sponsor_id %>
<%= f.input :developer_id %>
</div>

<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>

+ 2
- 0
app/views/sponsorships/_sponsorship.json.jbuilder View File

@@ -0,0 +1,2 @@
json.extract! sponsorship, :id, :amount, :visible, :sponsor_id, :developer_id, :created_at, :updated_at
json.url sponsorship_url(sponsorship, format: :json)

+ 6
- 0
app/views/sponsorships/edit.html.erb View File

@@ -0,0 +1,6 @@
<h1>Editing Sponsorship</h1>

<%= render 'form', sponsorship: @sponsorship %>

<%= link_to 'Show', @sponsorship %> |
<%= link_to 'Back', sponsorships_path %>

+ 33
- 0
app/views/sponsorships/index.html.erb View File

@@ -0,0 +1,33 @@
<p id="notice"><%= notice %></p>

<h1>Sponsorships</h1>

<table>
<thead>
<tr>
<th>Amount</th>
<th>Visible</th>
<th>Sponsor</th>
<th>Developer</th>
<th colspan="3"></th>
</tr>
</thead>

<tbody>
<% @sponsorships.each do |sponsorship| %>
<tr>
<td><%= sponsorship.amount %></td>
<td><%= sponsorship.visible %></td>
<td><%= sponsorship.sponsor_id %></td>
<td><%= sponsorship.developer_id %></td>
<td><%= link_to 'Show', sponsorship %></td>
<td><%= link_to 'Edit', edit_sponsorship_path(sponsorship) %></td>
<td><%= link_to 'Destroy', sponsorship, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>

<br>

<%= link_to 'New Sponsorship', new_sponsorship_path %>

+ 1
- 0
app/views/sponsorships/index.json.jbuilder View File

@@ -0,0 +1 @@
json.array! @sponsorships, partial: "sponsorships/sponsorship", as: :sponsorship

+ 5
- 0
app/views/sponsorships/new.html.erb View File

@@ -0,0 +1,5 @@
<h1>New Sponsorship</h1>

<%= render 'form', sponsorship: @sponsorship %>

<%= link_to 'Back', sponsorships_path %>

+ 24
- 0
app/views/sponsorships/show.html.erb View File

@@ -0,0 +1,24 @@
<p id="notice"><%= notice %></p>

<p>
<strong>Amount:</strong>
<%= @sponsorship.amount %>
</p>

<p>
<strong>Visible:</strong>
<%= @sponsorship.visible %>
</p>

<p>
<strong>Sponsor:</strong>
<%= @sponsorship.sponsor_id %>
</p>

<p>
<strong>Developer:</strong>
<%= @sponsorship.developer_id %>
</p>

<%= link_to 'Edit', edit_sponsorship_path(@sponsorship) %> |
<%= link_to 'Back', sponsorships_path %>

+ 1
- 0
app/views/sponsorships/show.json.jbuilder View File

@@ -0,0 +1 @@
json.partial! "sponsorships/sponsorship", sponsorship: @sponsorship

+ 1
- 0
config/routes.rb View File

@@ -1,5 +1,6 @@
Rails.application.routes.draw do
resources :sponsorships
require 'sidekiq/web'
require 'admin_constraint'


+ 12
- 0
db/migrate/20200724090750_create_sponsorships.rb View File

@@ -0,0 +1,12 @@
class CreateSponsorships < ActiveRecord::Migration[5.2]
def change
create_table :sponsorships do |t|
t.integer :amount
t.integer :visible
t.integer :sponsor_id
t.integer :developer_id

t.timestamps
end
end
end

+ 141
- 0
spec/controllers/sponsorships_controller_spec.rb 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 SponsorshipsController, type: :controller do

# This should return the minimal set of attributes required to create a valid
# Sponsorship. As you add validations to Sponsorship, 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
# SponsorshipsController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe "GET #index" do
it "returns a success response" do
Sponsorship.create! valid_attributes
get :index, params: {}, session: valid_session
expect(response).to be_successful
end
end

describe "GET #show" do
it "returns a success response" do
sponsorship = Sponsorship.create! valid_attributes
get :show, params: {id: sponsorship.to_param}, session: valid_session
expect(response).to be_successful
end
end

describe "GET #new" do
it "returns a success response" do
get :new, params: {}, session: valid_session
expect(response).to be_successful
end
end

describe "GET #edit" do
it "returns a success response" do
sponsorship = Sponsorship.create! valid_attributes
get :edit, params: {id: sponsorship.to_param}, session: valid_session
expect(response).to be_successful
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new Sponsorship" do
expect {
post :create, params: {sponsorship: valid_attributes}, session: valid_session
}.to change(Sponsorship, :count).by(1)
end

it "redirects to the created sponsorship" do
post :create, params: {sponsorship: valid_attributes}, session: valid_session
expect(response).to redirect_to(Sponsorship.last)
end
end

context "with invalid params" do
it "returns a success response (i.e. to display the 'new' template)" do
post :create, params: {sponsorship: invalid_attributes}, session: valid_session
expect(response).to be_successful
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 sponsorship" do
sponsorship = Sponsorship.create! valid_attributes
put :update, params: {id: sponsorship.to_param, sponsorship: new_attributes}, session: valid_session
sponsorship.reload
skip("Add assertions for updated state")
end

it "redirects to the sponsorship" do
sponsorship = Sponsorship.create! valid_attributes
put :update, params: {id: sponsorship.to_param, sponsorship: valid_attributes}, session: valid_session
expect(response).to redirect_to(sponsorship)
end
end

context "with invalid params" do
it "returns a success response (i.e. to display the 'edit' template)" do
sponsorship = Sponsorship.create! valid_attributes
put :update, params: {id: sponsorship.to_param, sponsorship: invalid_attributes}, session: valid_session
expect(response).to be_successful
end
end
end

describe "DELETE #destroy" do
it "destroys the requested sponsorship" do
sponsorship = Sponsorship.create! valid_attributes
expect {
delete :destroy, params: {id: sponsorship.to_param}, session: valid_session
}.to change(Sponsorship, :count).by(-1)
end

it "redirects to the sponsorships list" do
sponsorship = Sponsorship.create! valid_attributes
delete :destroy, params: {id: sponsorship.to_param}, session: valid_session
expect(response).to redirect_to(sponsorships_url)
end
end

end

+ 15
- 0
spec/helpers/sponsorships_helper_spec.rb View File

@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the SponsorshipsHelper. For example:
#
# describe SponsorshipsHelper 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 SponsorshipsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end

+ 5
- 0
spec/models/sponsorship_spec.rb View File

@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Sponsorship, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

+ 10
- 0
spec/requests/sponsorships_spec.rb View File

@@ -0,0 +1,10 @@
require 'rails_helper'

RSpec.describe "Sponsorships", type: :request do
describe "GET /sponsorships" do
it "works! (now write some real specs)" do
get sponsorships_path
expect(response).to have_http_status(200)
end
end
end

+ 38
- 0
spec/routing/sponsorships_routing_spec.rb View File

@@ -0,0 +1,38 @@
require "rails_helper"

RSpec.describe SponsorshipsController, type: :routing do
describe "routing" do
it "routes to #index" do
expect(:get => "/sponsorships").to route_to("sponsorships#index")
end

it "routes to #new" do
expect(:get => "/sponsorships/new").to route_to("sponsorships#new")
end

it "routes to #show" do
expect(:get => "/sponsorships/1").to route_to("sponsorships#show", :id => "1")
end

it "routes to #edit" do
expect(:get => "/sponsorships/1/edit").to route_to("sponsorships#edit", :id => "1")
end


it "routes to #create" do
expect(:post => "/sponsorships").to route_to("sponsorships#create")
end

it "routes to #update via PUT" do
expect(:put => "/sponsorships/1").to route_to("sponsorships#update", :id => "1")
end

it "routes to #update via PATCH" do
expect(:patch => "/sponsorships/1").to route_to("sponsorships#update", :id => "1")
end

it "routes to #destroy" do
expect(:delete => "/sponsorships/1").to route_to("sponsorships#destroy", :id => "1")
end
end
end

+ 27
- 0
spec/views/sponsorships/edit.html.erb_spec.rb View File

@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe "sponsorships/edit", type: :view do
before(:each) do
@sponsorship = assign(:sponsorship, Sponsorship.create!(
:amount => 1,
:visible => 1,
:sponsor_id => 1,
:developer_id => 1
))
end

it "renders the edit sponsorship form" do
render

assert_select "form[action=?][method=?]", sponsorship_path(@sponsorship), "post" do

assert_select "input[name=?]", "sponsorship[amount]"

assert_select "input[name=?]", "sponsorship[visible]"

assert_select "input[name=?]", "sponsorship[sponsor_id]"

assert_select "input[name=?]", "sponsorship[developer_id]"
end
end
end

+ 28
- 0
spec/views/sponsorships/index.html.erb_spec.rb View File

@@ -0,0 +1,28 @@
require 'rails_helper'

RSpec.describe "sponsorships/index", type: :view do
before(:each) do
assign(:sponsorships, [
Sponsorship.create!(
:amount => 2,
:visible => 3,
:sponsor_id => 4,
:developer_id => 5
),
Sponsorship.create!(
:amount => 2,
:visible => 3,
:sponsor_id => 4,
:developer_id => 5
)
])
end

it "renders a list of sponsorships" do
render
assert_select "tr>td", :text => 2.to_s, :count => 2
assert_select "tr>td", :text => 3.to_s, :count => 2
assert_select "tr>td", :text => 4.to_s, :count => 2
assert_select "tr>td", :text => 5.to_s, :count => 2
end
end

+ 27
- 0
spec/views/sponsorships/new.html.erb_spec.rb View File

@@ -0,0 +1,27 @@
require 'rails_helper'

RSpec.describe "sponsorships/new", type: :view do
before(:each) do
assign(:sponsorship, Sponsorship.new(
:amount => 1,
:visible => 1,
:sponsor_id => 1,
:developer_id => 1
))
end

it "renders new sponsorship form" do
render

assert_select "form[action=?][method=?]", sponsorships_path, "post" do

assert_select "input[name=?]", "sponsorship[amount]"

assert_select "input[name=?]", "sponsorship[visible]"

assert_select "input[name=?]", "sponsorship[sponsor_id]"

assert_select "input[name=?]", "sponsorship[developer_id]"
end
end
end

+ 20
- 0
spec/views/sponsorships/show.html.erb_spec.rb View File

@@ -0,0 +1,20 @@
require 'rails_helper'

RSpec.describe "sponsorships/show", type: :view do
before(:each) do
@sponsorship = assign(:sponsorship, Sponsorship.create!(
:amount => 2,
:visible => 3,
:sponsor_id => 4,
:developer_id => 5
))
end

it "renders attributes in <p>" do
render
expect(rendered).to match(/2/)
expect(rendered).to match(/3/)
expect(rendered).to match(/4/)
expect(rendered).to match(/5/)
end
end

Loading…
Cancel
Save