feat: add github repository

This commit is contained in:
Adrien Waksberg 2018-07-29 21:06:32 +02:00
parent 5154b4b632
commit 97b9a87d26
16 changed files with 172 additions and 3 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 RepositoryGithub 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,23 @@
class GithubRepositoryController < ApplicationController
def create
@software = Software.find(params[:software_id])
@repository = GithubRepository.new(repository_params)
@repository.save
redirect_to edit_software_path(@software)
end
def update
@software = Software.find(params[:software_id])
@repository = GithubRepository.find(params[:id])
@repository.update(repository_params)
redirect_to edit_software_path(@software)
end
private
def repository_params
params.require(:repository).permit(:name, :software_id)
end
end

View file

@ -13,6 +13,17 @@ class SoftwaresController < ApplicationController
def edit
@software = Software.find(params[:id])
case @software.repository_type
when 'Github'
if GithubRepository.exists?(software_id: @software)
@repository = GithubRepository.where(software_id: @software.id).take
@repository_form_path = software_github_repository_path(software_id: @software.id, id: @repository.id)
else
@repository = GithubRepository.new
@repository_form_path = software_github_repository_index_path(@software.id)
end
end
end
def update
@ -45,6 +56,6 @@ class SoftwaresController < ApplicationController
private
def software_params
params.require(:software).permit(:name, :website)
params.require(:software).permit(:name, :website, :repository_type)
end
end

View file

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

View file

@ -0,0 +1,5 @@
class GithubRepository < ApplicationRecord
belongs_to :software
validates :name, presence: true, uniqueness: true
validates :software_id, presence: true, uniqueness: true
end

View file

@ -18,7 +18,33 @@
<%= form.label :website, class: 'uk-form-label' %><br>
<%= form.text_field :website, class: 'uk-input' %>
</p>
<p>
<%= form.label :repository_type, class: 'uk-form-label' %><br>
<%= form.select :repository_type, [nil, 'Github'], {}, class: 'uk-select' %>
</p>
<p>
<%= form.submit class: 'uk-button uk-button-default uk-margin' %>
</p>
<% end %>
<% case @software.repository_type %>
<% when 'Github' %>
<h4>Github</h4>
<%= form_with(
scope: :repository,
url: @repository_form_path,
class: 'uk-form-horizontal uk-form-width-large',
model: @repository,
local: true
) do |form|
%>
<p>
<%= form.label :repository, class: 'uk-form-label' %><br>
<%= form.text_field :name, value: @repository.name, class: 'uk-input' %>
</p>
<p>
<%= form.hidden_field :software_id, value: @software.id %>
<%= form.submit class: 'uk-button uk-button-default uk-margin' %>
</p>
<% end %>
<% end %>

View file

@ -18,6 +18,10 @@
<%= form.label :website, class: 'uk-form-label' %><br>
<%= form.text_field :website, class: 'uk-input' %>
</p>
<p>
<%= form.label :repository_type, class: 'uk-form-label' %><br>
<%= form.select :repository_type, [nil, 'Github'], {}, class: 'uk-select' %>
</p>
<p>
<%= form.submit class: 'uk-button uk-button-default uk-margin' %>
</p>

View file

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

View file

@ -0,0 +1,7 @@
class AddColumnRepositoryTypeInSoftware < ActiveRecord::Migration[5.2]
def change
change_table :softwares do |t|
t.string :repository_type
end
end
end

View file

@ -0,0 +1,10 @@
class CreateGithubRepositories < ActiveRecord::Migration[5.2]
def change
create_table :github_repositories do |t|
t.string :name, uniqueness: true, null: false
t.references :software, uniqueness: true, foreign_key: true
t.timestamps
end
end
end

View file

@ -10,13 +10,22 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_07_22_183046) do
ActiveRecord::Schema.define(version: 2018_07_29_191829) do
create_table "github_repositories", force: :cascade do |t|
t.string "name", null: false
t.integer "software_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["software_id"], name: "index_github_repositories_on_software_id"
end
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
t.string "repository_type"
end
create_table "versions", force: :cascade do |t|

View file

@ -0,0 +1,45 @@
require 'test_helper'
class GithubRepositoryControllerTest < ActionDispatch::IntegrationTest
test 'should not save repository without data' do
repository = GithubRepository.new
assert_not repository.save
end
test 'should not save repository without software_id' do
data = { name: 'test/test' }
repository = GithubRepository.new(data)
assert_not repository.save
end
test 'should not save repository if a repository already exist' do
data = {
name: 'test/test',
software_id: Software.find_by_name('Ruby on Rails').id
}
repository = GithubRepository.new(data)
assert_not repository.save
end
test 'should not save repository if a repository with the same name exist' do
data = {
name: 'rails/rails',
software_id: Software.find_by_name('Kaiho').id
}
repository = GithubRepository.new(data)
assert_not repository.save
end
test 'should save repository' do
data = {
name: 'kaiho/kaiho',
software_id: Software.find_by_name('Kaiho').id
}
repository = GithubRepository.new(data)
assert repository.save
end
end

5
test/fixtures/github_repositories.yml vendored Normal file
View file

@ -0,0 +1,5 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
name: rails/rails
software: two

View file

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

View file

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