feat: add softwares controller

This commit is contained in:
Adrien Waksberg 2018-07-22 17:44:29 +02:00
parent cac31ff12c
commit 72e8c908f4
17 changed files with 259 additions and 0 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 softwares 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,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

View file

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

3
app/models/software.rb Normal file
View file

@ -0,0 +1,3 @@
class Software < ApplicationRecord
validates :name, presence: true, uniqueness: true
end

View file

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

View 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 %>

View 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 %>

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

View 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 %>

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

View file

@ -1,3 +1,4 @@
Rails.application.routes.draw do
root 'home#index'
resources 'softwares'
end

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

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

View file

@ -0,0 +1,7 @@
require 'test_helper'
class SoftwareTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end