From 9d0dc55ea253cdb4b3c646c258e397a007e92bca Mon Sep 17 00:00:00 2001 From: Michael Sipayung Date: Fri, 26 Jan 2024 16:45:00 +0700 Subject: [PATCH] bug fixes for allowed nill attribute --- .../usm_school_informations_controller.rb | 16 ++++++ app/helpers/usm_school_informations_helper.rb | 2 + app/models/parent.rb | 3 +- app/models/personal.rb | 2 +- app/models/pmdk_school_information.rb | 4 +- app/models/usm_school_information.rb | 22 ++++++++ app/models/utbk_school_information.rb | 4 +- .../usm_school_informations/create.html.erb | 2 + .../usm_school_informations/edit.html.erb | 2 + .../usm_school_informations/new.html.erb | 2 + .../usm_school_informations/show.html.erb | 2 + .../usm_school_informations/update.html.erb | 2 + config/routes.rb | 5 ++ ...26084849_create_usm_school_informations.rb | 15 ++++++ ..._add_user_ref_to_usm_school_information.rb | 5 ++ db/schema.rb | 17 +++++- ...usm_school_informations_controller_test.rb | 28 ++++++++++ test/fixtures/usm_school_informations.yml | 21 ++++++++ test/models/parent_test.rb | 11 ++-- test/models/personal_test.rb | 2 +- test/models/usm_school_information_test.rb | 54 +++++++++++++++++++ test/models/utbk_school_information_test.rb | 2 +- 22 files changed, 208 insertions(+), 15 deletions(-) create mode 100644 app/controllers/usm_school_informations_controller.rb create mode 100644 app/helpers/usm_school_informations_helper.rb create mode 100644 app/models/usm_school_information.rb create mode 100644 app/views/usm_school_informations/create.html.erb create mode 100644 app/views/usm_school_informations/edit.html.erb create mode 100644 app/views/usm_school_informations/new.html.erb create mode 100644 app/views/usm_school_informations/show.html.erb create mode 100644 app/views/usm_school_informations/update.html.erb create mode 100644 db/migrate/20240126084849_create_usm_school_informations.rb create mode 100644 db/migrate/20240126085158_add_user_ref_to_usm_school_information.rb create mode 100644 test/controllers/usm_school_informations_controller_test.rb create mode 100644 test/fixtures/usm_school_informations.yml create mode 100644 test/models/usm_school_information_test.rb diff --git a/app/controllers/usm_school_informations_controller.rb b/app/controllers/usm_school_informations_controller.rb new file mode 100644 index 0000000..f9470d8 --- /dev/null +++ b/app/controllers/usm_school_informations_controller.rb @@ -0,0 +1,16 @@ +class UsmSchoolInformationsController < ApplicationController + def new + end + + def update + end + + def create + end + + def edit + end + + def show + end +end diff --git a/app/helpers/usm_school_informations_helper.rb b/app/helpers/usm_school_informations_helper.rb new file mode 100644 index 0000000..d7092c1 --- /dev/null +++ b/app/helpers/usm_school_informations_helper.rb @@ -0,0 +1,2 @@ +module UsmSchoolInformationsHelper +end diff --git a/app/models/parent.rb b/app/models/parent.rb index 3363887..58245cc 100644 --- a/app/models/parent.rb +++ b/app/models/parent.rb @@ -2,7 +2,8 @@ class Parent < ApplicationRecord belongs_to :user validates :nama_ayah,:nama_ibu, presence: true, length: { minimum: 4, maximum: 25 } # validates :nama_ibu, presence: true, length: { minimum: 4, maximum: 25 } - validates :nik_ayah,:nik_ibu, presence: true, length: { minimum: 16, maximum: 16 } + validates :nik_ayah,:nik_ibu, length: { minimum: 16, maximum: 16 }, + allow_nil: true # validates :nik_ibu, presence: true, length: { minimum: 16, maximum: 16 } validates :pendidikan_ayah, presence: true, length: { minimum: 2, maximum: 20 } validates :pendidikan_ibu, presence: true, length: { minimum: 2, maximum: 20 } diff --git a/app/models/personal.rb b/app/models/personal.rb index 267e384..0c1cf1f 100644 --- a/app/models/personal.rb +++ b/app/models/personal.rb @@ -7,6 +7,6 @@ class Personal < ApplicationRecord validates :tanggal_lahir, presence: true, length: { minimum: 4, maximum: 20 } validates :nik, presence: true, length: { minimum: 16, maximum: 16 } validates :nisn, presence: true, length: { minimum: 10, maximum: 10 } - validates :no_kps, length: { minimum: 6, maximum: 6 } + validates :no_kps, length: { minimum: 6, maximum: 6 }, allow_nil: true validates :domisili, presence: true, length: { minimum: 4, maximum: 30 } end diff --git a/app/models/pmdk_school_information.rb b/app/models/pmdk_school_information.rb index 158e4b6..74de087 100644 --- a/app/models/pmdk_school_information.rb +++ b/app/models/pmdk_school_information.rb @@ -4,9 +4,9 @@ class PmdkSchoolInformation < ApplicationRecord validates :akreditas, presence: true, length: {minimum: 1, maximum: 20} validates :jumlah_nilai_un, presence: false, numericality: { greater_than_or_equal_to: 2, less_than_or_equal_to: 1000 - } + }, allow_nil: true validates :jumlah_pelajaran_un, presence: false, numericality: { only_integer: true, greater_than_or_equal_to: 2, less_than_or_equal_to: 10 - } + }, allow_nil: true end diff --git a/app/models/usm_school_information.rb b/app/models/usm_school_information.rb new file mode 100644 index 0000000..276391e --- /dev/null +++ b/app/models/usm_school_information.rb @@ -0,0 +1,22 @@ +class UsmSchoolInformation < ApplicationRecord + belongs_to :user + validates :jurusan_sekolah, presence: true, length: {minimum: 3, maximum: 50} + validates :asal_sekolah, presence: true, length: {minimum: 3, maximum: 50} + validates :akreditas, presence: true, length: {minimum: 1, maximum: 20} + validates :jumlah_nilai_un, presence: false, numericality: { + only_integer: false, + greater_than_or_equal_to: 2, less_than_or_equal_to: 1000 + }, allow_nil: true + validates :jumlah_pelajaran_un, presence: false, numericality: { + only_integer: false, greater_than_or_equal_to: 2, + less_than_or_equal_to: 10 + },allow_nil: true + validates :jumlah_pelajaran_semester_5, presence: true, numericality: { + only_integer: true, greater_than_or_equal_to: 2, + less_than_or_equal_to: 20 + } + validates :jumlah_nilai_semester_5, presence: true, numericality: { + only_integer: false, greater_than_or_equal_to: 2, + less_than_or_equal_to: 2000 + } +end diff --git a/app/models/utbk_school_information.rb b/app/models/utbk_school_information.rb index 5e9007b..c2a15fc 100644 --- a/app/models/utbk_school_information.rb +++ b/app/models/utbk_school_information.rb @@ -4,10 +4,10 @@ class UtbkSchoolInformation < ApplicationRecord validates :akreditas, presence: true, length: {minimum: 1, maximum: 20} validates :jumlah_nilai_un, presence: false, numericality: { greater_than_or_equal_to: 2, less_than_or_equal_to: 1000 - } + }, allow_nil: true validates :jumlah_pelajaran_un, presence: false, numericality: { only_integer: true, greater_than_or_equal_to: 2, less_than_or_equal_to: 10 - } + }, allow_nil: true end diff --git a/app/views/usm_school_informations/create.html.erb b/app/views/usm_school_informations/create.html.erb new file mode 100644 index 0000000..c2a6e0e --- /dev/null +++ b/app/views/usm_school_informations/create.html.erb @@ -0,0 +1,2 @@ +

UsmSchoolInformations#create

+

Find me in app/views/usm_school_informations/create.html.erb

diff --git a/app/views/usm_school_informations/edit.html.erb b/app/views/usm_school_informations/edit.html.erb new file mode 100644 index 0000000..cfb2eb2 --- /dev/null +++ b/app/views/usm_school_informations/edit.html.erb @@ -0,0 +1,2 @@ +

UsmSchoolInformations#edit

+

Find me in app/views/usm_school_informations/edit.html.erb

diff --git a/app/views/usm_school_informations/new.html.erb b/app/views/usm_school_informations/new.html.erb new file mode 100644 index 0000000..41ab8ac --- /dev/null +++ b/app/views/usm_school_informations/new.html.erb @@ -0,0 +1,2 @@ +

UsmSchoolInformations#new

+

Find me in app/views/usm_school_informations/new.html.erb

diff --git a/app/views/usm_school_informations/show.html.erb b/app/views/usm_school_informations/show.html.erb new file mode 100644 index 0000000..307825b --- /dev/null +++ b/app/views/usm_school_informations/show.html.erb @@ -0,0 +1,2 @@ +

UsmSchoolInformations#show

+

Find me in app/views/usm_school_informations/show.html.erb

diff --git a/app/views/usm_school_informations/update.html.erb b/app/views/usm_school_informations/update.html.erb new file mode 100644 index 0000000..5feb00a --- /dev/null +++ b/app/views/usm_school_informations/update.html.erb @@ -0,0 +1,2 @@ +

UsmSchoolInformations#update

+

Find me in app/views/usm_school_informations/update.html.erb

diff --git a/config/routes.rb b/config/routes.rb index 2c1131f..dc8a0b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,9 @@ Rails.application.routes.draw do + get 'usm_school_informations/new' + get 'usm_school_informations/update' + get 'usm_school_informations/create' + get 'usm_school_informations/edit' + get 'usm_school_informations/show' resources :accreditation_school_lists resources :pmdk_school_lists resources :all_school_lists diff --git a/db/migrate/20240126084849_create_usm_school_informations.rb b/db/migrate/20240126084849_create_usm_school_informations.rb new file mode 100644 index 0000000..0035498 --- /dev/null +++ b/db/migrate/20240126084849_create_usm_school_informations.rb @@ -0,0 +1,15 @@ +class CreateUsmSchoolInformations < ActiveRecord::Migration[7.1] + def change + create_table :usm_school_informations do |t| + t.string :asal_sekolah + t.string :akreditas + t.integer :jumlah_pelajaran_un + t.decimal :jumlah_nilai_un + t.string :jurusan_sekolah + t.integer :jumlah_pelajaran_semester_5 + t.decimal :jumlah_nilai_semester_5 + + t.timestamps + end + end +end diff --git a/db/migrate/20240126085158_add_user_ref_to_usm_school_information.rb b/db/migrate/20240126085158_add_user_ref_to_usm_school_information.rb new file mode 100644 index 0000000..84151c4 --- /dev/null +++ b/db/migrate/20240126085158_add_user_ref_to_usm_school_information.rb @@ -0,0 +1,5 @@ +class AddUserRefToUsmSchoolInformation < ActiveRecord::Migration[7.1] + def change + add_reference :usm_school_informations, :user, null: false, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 1d42fd1..2d7364f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_01_25_035732) do +ActiveRecord::Schema[7.1].define(version: 2024_01_26_085158) do create_table "accreditation_school_lists", force: :cascade do |t| t.string "akreditasi" t.datetime "created_at", null: false @@ -394,6 +394,20 @@ t.index ["email"], name: "index_users_on_email", unique: true end + create_table "usm_school_informations", force: :cascade do |t| + t.string "asal_sekolah" + t.string "akreditas" + t.integer "jumlah_pelajaran_un" + t.decimal "jumlah_nilai_un" + t.string "jurusan_sekolah" + t.integer "jumlah_pelajaran_semester_5" + t.decimal "jumlah_nilai_semester_5" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "user_id", null: false + t.index ["user_id"], name: "index_usm_school_informations_on_user_id" + end + create_table "utbk_school_informations", force: :cascade do |t| t.string "asal_sekolah" t.string "akreditas" @@ -438,6 +452,7 @@ add_foreign_key "pmdk_school_informations", "users" add_foreign_key "pmdk_total_score_informations", "users" add_foreign_key "sources", "users" + add_foreign_key "usm_school_informations", "users" add_foreign_key "utbk_school_informations", "users" add_foreign_key "utbk_scores", "users" end diff --git a/test/controllers/usm_school_informations_controller_test.rb b/test/controllers/usm_school_informations_controller_test.rb new file mode 100644 index 0000000..07ac5ca --- /dev/null +++ b/test/controllers/usm_school_informations_controller_test.rb @@ -0,0 +1,28 @@ +require "test_helper" + +class UsmSchoolInformationsControllerTest < ActionDispatch::IntegrationTest + test "should get new" do + get usm_school_informations_new_url + assert_response :success + end + + test "should get update" do + get usm_school_informations_update_url + assert_response :success + end + + test "should get create" do + get usm_school_informations_create_url + assert_response :success + end + + test "should get edit" do + get usm_school_informations_edit_url + assert_response :success + end + + test "should get show" do + get usm_school_informations_show_url + assert_response :success + end +end diff --git a/test/fixtures/usm_school_informations.yml b/test/fixtures/usm_school_informations.yml new file mode 100644 index 0000000..841bebd --- /dev/null +++ b/test/fixtures/usm_school_informations.yml @@ -0,0 +1,21 @@ +# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +usm_one: + user: archer + asal_sekolah: MyString + akreditas: MyString + jumlah_pelajaran_un: 1 + jumlah_nilai_un: 9.99 + jurusan_sekolah: MyString + jumlah_pelajaran_semester_5: 1 + jumlah_nilai_semester_5: 9.99 + +usm_two: + user: iana + asal_sekolah: MyString + akreditas: MyString + jumlah_pelajaran_un: 1 + jumlah_nilai_un: 9.99 + jurusan_sekolah: MyString + jumlah_pelajaran_semester_5: 1 + jumlah_nilai_semester_5: 9.99 diff --git a/test/models/parent_test.rb b/test/models/parent_test.rb index d95bb1a..eb00aee 100644 --- a/test/models/parent_test.rb +++ b/test/models/parent_test.rb @@ -3,8 +3,7 @@ class ParentTest < ActiveSupport::TestCase def setup @user = users(:michael) - @parent = Parent.new(user_id: @user.id, nama_ayah: 'example', nama_ibu: 'example', - nik_ayah: 1234567891234567, nik_ibu: 1234567891234567, pendidikan_ayah: 'sma', + @parent = Parent.new(user_id: @user.id, nama_ayah: 'example', nama_ibu: 'example', pendidikan_ayah: 'sma', pendidikan_ibu: 'sma', pekerjaan_ayah: 'wiraswasta', pekerjaan_ibu: 'wiraswasta', tanggal_lahir_ayah: '1997-12-12', tanggal_lahir_ibu: '1997-12-12') end @@ -35,9 +34,9 @@ def setup @parent.nama_ibu = "a"*26 assert_not @parent.valid? end - test "nik_ayah should be present" do + test "nik_ayah should not be present" do @parent.nik_ayah = "" - assert_not @parent.valid? + assert @parent.valid? end test "nik_ayah should not be too short" do @parent.nik_ayah = "a"*15 @@ -47,9 +46,9 @@ def setup @parent.nik_ayah = "a"*17 assert_not @parent.valid? end - test "nik_ibu should be present" do + test "nik_ibu should not be present" do @parent.nik_ibu = "" - assert_not @parent.valid? + assert @parent.valid? end test "nik_ibu should not be too short" do @parent.nik_ibu = "a"*15 diff --git a/test/models/personal_test.rb b/test/models/personal_test.rb index bcd8ce7..ae17be1 100644 --- a/test/models/personal_test.rb +++ b/test/models/personal_test.rb @@ -5,7 +5,7 @@ def setup @user = users(:michael) @personal = Personal.new(user_id: @user.id, nama_lengkap: "Example User", agama: 'islam', jenis_kelamin: 'laki-laki', tempat_lahir: 'jakarta', tanggal_lahir: '1997-12-12', - nik: 1234567890123456, nisn: 1234567890, no_kps: 123456, domisili: 'jakarta') + nik: 1234567890123456, nisn: 1234567890, domisili: 'jakarta') end test "should be valid" do assert @personal.valid? diff --git a/test/models/usm_school_information_test.rb b/test/models/usm_school_information_test.rb new file mode 100644 index 0000000..b93c89b --- /dev/null +++ b/test/models/usm_school_information_test.rb @@ -0,0 +1,54 @@ +require "test_helper" + +class UsmSchoolInformationTest < ActiveSupport::TestCase + def setup + @usm_school = + UsmSchoolInformation.new(user_id: users(:michael).id, jurusan_sekolah: 'ipa', + asal_sekolah: 'sma santo tomas', akreditas: 'terakreditasi', + jumlah_pelajaran_semester_5: 8, jumlah_nilai_semester_5: 78.9) + end + test "should be a valid utbk information" do + assert @usm_school.valid? + end + test "should reject for invalid asal sekolah - usm" do + @usm_school.asal_sekolah='a'*2 + assert_not @usm_school.valid? + @usm_school.asal_sekolah=' ' + assert_not @usm_school.valid? + @usm_school.asal_sekolah='a'*51 + assert_not @usm_school.valid? + end + test "should reject for invalid jumlah nilai un-usm" do + @usm_school.jumlah_nilai_un= 1000.5 + assert_not @usm_school.valid? + @usm_school.jumlah_nilai_un= -12 + assert_not @usm_school.valid? + end + test "should reject for invalid jumlah pelajaran un-usm" do + @usm_school.jumlah_pelajaran_un = 50 + assert_not @usm_school.valid? + @usm_school.jumlah_pelajaran_un = -1 + assert_not @usm_school.valid? + end + test "should reject for invalid akreditas-usm" do + @usm_school.akreditas = 'a'*21 + assert_not @usm_school.valid? + @usm_school.akreditas = ' ' + assert_not @usm_school.valid? + end + test "should reject for invalid jumlah pelajaran semester 5-usm" do + @usm_school.jumlah_pelajaran_semester_5 = 21 + assert_not @usm_school.valid? + @usm_school.jumlah_pelajaran_semester_5 = -1 + assert_not @usm_school.valid? + end + test "should reject for invalid jumlah nilai semester 5-usm" do + @usm_school.jumlah_nilai_semester_5 =-30.9 + assert_not @usm_school.valid? + @usm_school.jumlah_nilai_semester_5 =2001 + assert_not @usm_school.valid? + end + test "should allow non mandatory attribute to be empty-usm" do + assert @usm_school.valid? + end +end diff --git a/test/models/utbk_school_information_test.rb b/test/models/utbk_school_information_test.rb index c2260ba..0b22547 100644 --- a/test/models/utbk_school_information_test.rb +++ b/test/models/utbk_school_information_test.rb @@ -2,7 +2,7 @@ class UtbkSchoolInformationTest < ActiveSupport::TestCase def setup - @utbk_information = UtbkSchoolInformation.new( + @usm_school = UtbkSchoolInformation.new( user_id: users(:michael).id, asal_sekolah: 'sma tarutung', jumlah_pelajaran_un: 5, jumlah_nilai_un: 50.5, akreditas: 'A' )