feat: add version model

This commit is contained in:
Adrien Waksberg 2018-07-22 21:45:53 +02:00
parent 72e8c908f4
commit 832bce5490
9 changed files with 58 additions and 3 deletions

View file

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

6
app/models/version.rb Normal file
View file

@ -0,0 +1,6 @@
class Version < ApplicationRecord
belongs_to :software
validates :number, presence: true
validates :date, presence: true
validates :number, uniqueness: { scope: :software }
end

View file

@ -8,8 +8,14 @@
<table class="uk-table uk-table-striped">
<thead>
<tr>
<th>Release</th>
<th>Version</th>
<th>Date</th>
</tr>
</thead>
<% @software.versions.order(date: :desc).each do |version| %>
<tr>
<td><%= version.number %></td>
<td><%= version.date.strftime('%Y-%m-%d') %></td>
</tr>
<% end %>
</table>

View file

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

View file

@ -0,0 +1,11 @@
class CreateVersions < ActiveRecord::Migration[5.2]
def change
create_table :versions do |t|
t.string :number, null: false
t.datetime :date, null: false
t.references :software, foreign_key: true
t.timestamps
end
end
end

View file

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_07_22_072217) do
ActiveRecord::Schema.define(version: 2018_07_22_183046) do
create_table "softwares", force: :cascade do |t|
t.string "name", null: false
@ -19,4 +19,13 @@ ActiveRecord::Schema.define(version: 2018_07_22_072217) do
t.datetime "updated_at", null: false
end
create_table "versions", force: :cascade do |t|
t.string "number", null: false
t.datetime "date", null: false
t.integer "software_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["software_id"], name: "index_versions_on_software_id"
end
end

View file

@ -48,5 +48,9 @@ class SoftwaresControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_select 'title', 'Kaiho'
assert_select 'h3', 'Ruby on Rails'
assert_select 'td', '1.0.0'
assert_select 'td', '2018-07-22'
assert_select 'td', '1.1.0'
assert_select 'td', '2018-12-22'
end
end

11
test/fixtures/versions.yml vendored Normal file
View file

@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
number: 1.0.0
date: 2018-07-22 20:30:46
software: two
two:
number: 1.1.0
date: 2018-12-22 20:30:46
software: two

View file

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