From d2b621ddd61388ef1b528dbed6d0ad9e9962285c Mon Sep 17 00:00:00 2001 From: Adrien Waksberg Date: Sun, 29 Jul 2018 21:06:32 +0200 Subject: [PATCH] feat: add github repository --- .../javascripts/repository_github.coffee | 3 ++ app/assets/stylesheets/repository_github.scss | 3 ++ .../github_repository_controller.rb | 23 ++++++++++ app/controllers/softwares_controller.rb | 13 +++++- app/helpers/repository_github_helper.rb | 2 + app/models/github_repository.rb | 5 +++ app/views/softwares/edit.html.erb | 26 +++++++++++ app/views/softwares/new.html.erb | 4 ++ config/routes.rb | 4 +- ..._add_column_repository_type_in_software.rb | 7 +++ ...180729191829_create_github_repositories.rb | 10 +++++ db/schema.rb | 11 ++++- .../github_repository_controller_test.rb | 45 +++++++++++++++++++ test/fixtures/github_repositories.yml | 5 +++ test/models/github_repository_test.rb | 7 +++ test/models/repository_github_test.rb | 7 +++ 16 files changed, 172 insertions(+), 3 deletions(-) create mode 100644 app/assets/javascripts/repository_github.coffee create mode 100644 app/assets/stylesheets/repository_github.scss create mode 100644 app/controllers/github_repository_controller.rb create mode 100644 app/helpers/repository_github_helper.rb create mode 100644 app/models/github_repository.rb create mode 100644 db/migrate/20180729143537_add_column_repository_type_in_software.rb create mode 100644 db/migrate/20180729191829_create_github_repositories.rb create mode 100644 test/controllers/github_repository_controller_test.rb create mode 100644 test/fixtures/github_repositories.yml create mode 100644 test/models/github_repository_test.rb create mode 100644 test/models/repository_github_test.rb diff --git a/app/assets/javascripts/repository_github.coffee b/app/assets/javascripts/repository_github.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/repository_github.coffee @@ -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/ diff --git a/app/assets/stylesheets/repository_github.scss b/app/assets/stylesheets/repository_github.scss new file mode 100644 index 0000000..b0da3b7 --- /dev/null +++ b/app/assets/stylesheets/repository_github.scss @@ -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/ diff --git a/app/controllers/github_repository_controller.rb b/app/controllers/github_repository_controller.rb new file mode 100644 index 0000000..92f908a --- /dev/null +++ b/app/controllers/github_repository_controller.rb @@ -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 diff --git a/app/controllers/softwares_controller.rb b/app/controllers/softwares_controller.rb index 07e05c0..1a491aa 100644 --- a/app/controllers/softwares_controller.rb +++ b/app/controllers/softwares_controller.rb @@ -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(@repository) + 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 diff --git a/app/helpers/repository_github_helper.rb b/app/helpers/repository_github_helper.rb new file mode 100644 index 0000000..8499342 --- /dev/null +++ b/app/helpers/repository_github_helper.rb @@ -0,0 +1,2 @@ +module RepositoryGithubHelper +end diff --git a/app/models/github_repository.rb b/app/models/github_repository.rb new file mode 100644 index 0000000..ab80d60 --- /dev/null +++ b/app/models/github_repository.rb @@ -0,0 +1,5 @@ +class GithubRepository < ApplicationRecord + belongs_to :software + validates :name, presence: true, uniqueness: true + validates :software_id, presence: true, uniqueness: true +end diff --git a/app/views/softwares/edit.html.erb b/app/views/softwares/edit.html.erb index f6e389d..3349bd9 100644 --- a/app/views/softwares/edit.html.erb +++ b/app/views/softwares/edit.html.erb @@ -18,7 +18,33 @@ <%= form.label :website, class: 'uk-form-label' %>
<%= form.text_field :website, class: 'uk-input' %>

+

+ <%= form.label :repository_type, class: 'uk-form-label' %>
+ <%= form.select :repository_type, %w[Github], {}, class: 'uk-select' %> +

<%= form.submit class: 'uk-button uk-button-default uk-margin' %>

<% end %> + +<% case @software.repository_type %> +<% when 'Github' %> +

Github

+<%= form_with( + scope: :repository, + url: @repository_form_path, + class: 'uk-form-horizontal uk-form-width-large', + model: @repository, + local: true + ) do |form| +%> +

+ <%= form.label :repository, class: 'uk-form-label' %>
+ <%= form.text_field :name, value: @repository.name, class: 'uk-input' %> +

+

+ <%= form.hidden_field :software_id, value: @software.id %> + <%= form.submit class: 'uk-button uk-button-default uk-margin' %> +

+<% end %> +<% end %> diff --git a/app/views/softwares/new.html.erb b/app/views/softwares/new.html.erb index 7dc27b1..ce6966f 100644 --- a/app/views/softwares/new.html.erb +++ b/app/views/softwares/new.html.erb @@ -18,6 +18,10 @@ <%= form.label :website, class: 'uk-form-label' %>
<%= form.text_field :website, class: 'uk-input' %>

+

+ <%= form.label :repository_type, class: 'uk-form-label' %>
+ <%= form.select :repository_type, %w[Github], {}, class: 'uk-select' %> +

<%= form.submit class: 'uk-button uk-button-default uk-margin' %>

diff --git a/config/routes.rb b/config/routes.rb index 8b80cab..bdc2b3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do root 'home#index' - resources :softwares + resources :softwares do + resources :github_repository + end end diff --git a/db/migrate/20180729143537_add_column_repository_type_in_software.rb b/db/migrate/20180729143537_add_column_repository_type_in_software.rb new file mode 100644 index 0000000..15d07bd --- /dev/null +++ b/db/migrate/20180729143537_add_column_repository_type_in_software.rb @@ -0,0 +1,7 @@ +class AddColumnRepositoryTypeInSoftware < ActiveRecord::Migration[5.2] + def change + change_table :softwares do |t| + t.string :repository_type + end + end +end diff --git a/db/migrate/20180729191829_create_github_repositories.rb b/db/migrate/20180729191829_create_github_repositories.rb new file mode 100644 index 0000000..3173274 --- /dev/null +++ b/db/migrate/20180729191829_create_github_repositories.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1364314..16c8fe4 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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| diff --git a/test/controllers/github_repository_controller_test.rb b/test/controllers/github_repository_controller_test.rb new file mode 100644 index 0000000..7384465 --- /dev/null +++ b/test/controllers/github_repository_controller_test.rb @@ -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 diff --git a/test/fixtures/github_repositories.yml b/test/fixtures/github_repositories.yml new file mode 100644 index 0000000..f420338 --- /dev/null +++ b/test/fixtures/github_repositories.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: rails/rails + software: two diff --git a/test/models/github_repository_test.rb b/test/models/github_repository_test.rb new file mode 100644 index 0000000..b53630b --- /dev/null +++ b/test/models/github_repository_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class GithubRepositoryTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/repository_github_test.rb b/test/models/repository_github_test.rb new file mode 100644 index 0000000..e72c860 --- /dev/null +++ b/test/models/repository_github_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class RepositoryGithubTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end