feat: add softwares controller
This commit is contained in:
parent
cac31ff12c
commit
72e8c908f4
17 changed files with 259 additions and 0 deletions
3
app/assets/javascripts/softwares.coffee
Normal file
3
app/assets/javascripts/softwares.coffee
Normal 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/
|
3
app/assets/stylesheets/softwares.scss
Normal file
3
app/assets/stylesheets/softwares.scss
Normal file
|
@ -0,0 +1,3 @@
|
|||
// Place all the styles related to the softwares controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
50
app/controllers/softwares_controller.rb
Normal file
50
app/controllers/softwares_controller.rb
Normal file
|
@ -0,0 +1,50 @@
|
|||
class SoftwaresController < ApplicationController
|
||||
def index
|
||||
@softwares = Software.all
|
||||
end
|
||||
|
||||
def new
|
||||
@software = Software.new
|
||||
end
|
||||
|
||||
def show
|
||||
@software = Software.find(params[:id])
|
||||
end
|
||||
|
||||
def edit
|
||||
@software = Software.find(params[:id])
|
||||
end
|
||||
|
||||
def update
|
||||
@software = Software.find(params[:id])
|
||||
|
||||
if @software.update(software_params)
|
||||
redirect_to @software
|
||||
else
|
||||
render 'edit'
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
@software = Software.new(software_params)
|
||||
|
||||
if @software.save
|
||||
redirect_to @software
|
||||
else
|
||||
render 'new'
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@software = Software.find(params[:id])
|
||||
@software.destroy
|
||||
|
||||
redirect_to softwares_path
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def software_params
|
||||
params.require(:software).permit(:name, :website)
|
||||
end
|
||||
end
|
2
app/helpers/softwares_helper.rb
Normal file
2
app/helpers/softwares_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module SoftwaresHelper
|
||||
end
|
3
app/models/software.rb
Normal file
3
app/models/software.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Software < ApplicationRecord
|
||||
validates :name, presence: true, uniqueness: true
|
||||
end
|
|
@ -17,6 +17,7 @@
|
|||
<div class="uk-margin-left uk-margin-right">
|
||||
<div class="uk-navbar-left">
|
||||
<a class="uk-navbar-item uk-logo" href="/">Kaiho</a>
|
||||
<%= link_to 'Softwares', softwares_path, class: 'uk-navbar-item' %>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
|
14
app/views/softwares/_form_errors.html.erb
Normal file
14
app/views/softwares/_form_errors.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
|||
<% if @software.errors.any? %>
|
||||
<div id="error_explanation" class="uk-alert-warning" uk-alert>
|
||||
<a class="uk-alert-close" uk-close></a>
|
||||
<p>
|
||||
<%= pluralize(@software.errors.count, 'error') %> prohibited
|
||||
this provider from being saved:
|
||||
</p>
|
||||
<ul>
|
||||
<% @software.errors.full_messages.each do |msg| %>
|
||||
<li><%= msg %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
24
app/views/softwares/edit.html.erb
Normal file
24
app/views/softwares/edit.html.erb
Normal file
|
@ -0,0 +1,24 @@
|
|||
<h3>Edit software</h3>
|
||||
|
||||
<%= render 'form_errors' %>
|
||||
|
||||
<%= form_with(
|
||||
scope: :software,
|
||||
url: software_path(@software),
|
||||
class: 'uk-form-horizontal uk-form-width-large',
|
||||
model: @software,
|
||||
local: true
|
||||
) do |form|
|
||||
%>
|
||||
<p>
|
||||
<%= form.label :name, class: 'uk-form-label' %><br>
|
||||
<%= form.text_field :name, class: 'uk-input' %>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.label :website, class: 'uk-form-label' %><br>
|
||||
<%= form.text_field :website, class: 'uk-input' %>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.submit class: 'uk-button uk-button-default uk-margin' %>
|
||||
</p>
|
||||
<% end %>
|
19
app/views/softwares/index.html.erb
Normal file
19
app/views/softwares/index.html.erb
Normal file
|
@ -0,0 +1,19 @@
|
|||
<h3>Softwares</h3>
|
||||
<p class="uk-align-right">
|
||||
<span uk-icon="icon: plus-circle"></span>
|
||||
<%= link_to 'Add software', new_software_path(@software), class: 'uk-icon-link' %>
|
||||
</p>
|
||||
<table class="uk-table uk-table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Website</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<% @softwares.each do |software| %>
|
||||
<tr>
|
||||
<td><%= link_to software.name, software %></td>
|
||||
<td><%= link_to software.website, software.website %></td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
24
app/views/softwares/new.html.erb
Normal file
24
app/views/softwares/new.html.erb
Normal file
|
@ -0,0 +1,24 @@
|
|||
<h3>Add a new software</h3>
|
||||
|
||||
<%= render 'form_errors' %>
|
||||
|
||||
<%= form_with(
|
||||
scope: :software,
|
||||
url: softwares_path,
|
||||
class: 'uk-form-horizontal uk-form-width-large',
|
||||
model: @software,
|
||||
local: true
|
||||
) do |form|
|
||||
%>
|
||||
<p>
|
||||
<%= form.label :name, class: 'uk-form-label' %><br>
|
||||
<%= form.text_field :name, class: 'uk-input' %>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.label :website, class: 'uk-form-label' %><br>
|
||||
<%= form.text_field :website, class: 'uk-input' %>
|
||||
</p>
|
||||
<p>
|
||||
<%= form.submit class: 'uk-button uk-button-default uk-margin' %>
|
||||
</p>
|
||||
<% end %>
|
15
app/views/softwares/show.html.erb
Normal file
15
app/views/softwares/show.html.erb
Normal file
|
@ -0,0 +1,15 @@
|
|||
<p class="uk-align-right">
|
||||
<span uk-icon="icon: pencil"></span>
|
||||
<%= link_to 'Edit', edit_software_path(@software), class: 'uk-icon-link' %>
|
||||
<span uk-icon="icon: trash"></span>
|
||||
<%= link_to 'Delete', software_path(@software), method: :delete, class: 'uk-icon-link', data: { confirm: 'Are you sure?' } %>
|
||||
</p>
|
||||
<h3><%= @software.name %></h3>
|
||||
<table class="uk-table uk-table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Release</th>
|
||||
<th>Date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
</table>
|
|
@ -1,3 +1,4 @@
|
|||
Rails.application.routes.draw do
|
||||
root 'home#index'
|
||||
resources 'softwares'
|
||||
end
|
||||
|
|
10
db/migrate/20180722072217_create_softwares.rb
Normal file
10
db/migrate/20180722072217_create_softwares.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
class CreateSoftwares < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :softwares do |t|
|
||||
t.string :name, uniqueness: true, null: false
|
||||
t.string :website
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
22
db/schema.rb
Normal file
22
db/schema.rb
Normal file
|
@ -0,0 +1,22 @@
|
|||
# This file is auto-generated from the current state of the database. Instead
|
||||
# of editing this file, please use the migrations feature of Active Record to
|
||||
# incrementally modify your database, and then regenerate this schema definition.
|
||||
#
|
||||
# Note that this schema.rb definition is the authoritative source for your
|
||||
# database schema. If you need to create the application database on another
|
||||
# system, you should be using db:schema:load, not running all the migrations
|
||||
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
|
||||
# you'll amass, the slower it'll run and the greater likelihood for issues).
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2018_07_22_072217) do
|
||||
|
||||
create_table "softwares", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "website"
|
||||
t.datetime "created_at", null: false
|
||||
t.datetime "updated_at", null: false
|
||||
end
|
||||
|
||||
end
|
52
test/controllers/softwares_controller_test.rb
Normal file
52
test/controllers/softwares_controller_test.rb
Normal file
|
@ -0,0 +1,52 @@
|
|||
require 'test_helper'
|
||||
|
||||
class SoftwaresControllerTest < ActionDispatch::IntegrationTest
|
||||
test 'should not save software without name' do
|
||||
software = Software.new
|
||||
assert_not software.save
|
||||
end
|
||||
|
||||
test 'save software without website' do
|
||||
data = { name: 'MySoft' }
|
||||
software = Software.new(data)
|
||||
|
||||
assert software.save
|
||||
assert_equal data[:name], software.name
|
||||
end
|
||||
|
||||
test 'save software with website' do
|
||||
data = { name: 'MySoft', website: 'https://mysoft.local' }
|
||||
software = Software.new(data)
|
||||
|
||||
assert software.save
|
||||
assert_equal data[:name], software.name
|
||||
end
|
||||
|
||||
test 'should not save software with same name' do
|
||||
data = { name: 'Ruby on Rails' }
|
||||
|
||||
software = Software.new(data)
|
||||
assert_not software.save
|
||||
end
|
||||
|
||||
test 'should get index' do
|
||||
get softwares_url
|
||||
|
||||
assert_response :success
|
||||
assert_select 'title', 'Kaiho'
|
||||
assert_select 'h3', 'Softwares'
|
||||
assert_select 'td', 'Ruby on Rails'
|
||||
assert_select 'td', 'https://rubyonrails.org'
|
||||
assert_select 'td', 'Kaiho'
|
||||
assert_select 'td', 'https://kaiho.local'
|
||||
end
|
||||
|
||||
test 'should get show software' do
|
||||
software = Software.find_by_name('Ruby on Rails')
|
||||
get software_path(software)
|
||||
|
||||
assert_response :success
|
||||
assert_select 'title', 'Kaiho'
|
||||
assert_select 'h3', 'Ruby on Rails'
|
||||
end
|
||||
end
|
9
test/fixtures/softwares.yml
vendored
Normal file
9
test/fixtures/softwares.yml
vendored
Normal file
|
@ -0,0 +1,9 @@
|
|||
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
name: Kaiho
|
||||
website: https://kaiho.local
|
||||
|
||||
two:
|
||||
name: Ruby on Rails
|
||||
website: https://rubyonrails.org
|
7
test/models/software_test.rb
Normal file
7
test/models/software_test.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
require 'test_helper'
|
||||
|
||||
class SoftwareTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
Loading…
Add table
Reference in a new issue