diff --git a/app/controllers/majors_controller.rb b/app/controllers/majors_controller.rb new file mode 100644 index 0000000..c819a45 --- /dev/null +++ b/app/controllers/majors_controller.rb @@ -0,0 +1,34 @@ +class MajorsController < ApplicationController + def new + @major = current_user.build_major + end + + def update + @major = current_user.major + if @major.update(major_params) + flash[:success] = "Major updated" + else + render 'edit' + end + end + + def create + @major = current_user.build_major(major_params) + if @major.save + flash[:success] = "Major saved" + else + render 'new' + end + end + + def edit + @major = current_user.major + end + + def show + end + private + def major_params + params.require(:major).permit(:jurusan_1, :jurusan_2, :jurusan_3, :gelombang) + end +end diff --git a/app/controllers/sources_controller.rb b/app/controllers/sources_controller.rb index 4fec1d1..fe028cc 100644 --- a/app/controllers/sources_controller.rb +++ b/app/controllers/sources_controller.rb @@ -4,7 +4,7 @@ def new end def update - @source = current_user.build_source + @source = current_user.source if @source.update(source_params) flash[:success] = "source of information is updated" else @@ -22,7 +22,7 @@ def create end def edit - @source = current_user.build_source + @source = current_user.source end def show diff --git a/app/helpers/majors_helper.rb b/app/helpers/majors_helper.rb new file mode 100644 index 0000000..c1881b4 --- /dev/null +++ b/app/helpers/majors_helper.rb @@ -0,0 +1,2 @@ +module MajorsHelper +end diff --git a/app/models/user.rb b/app/models/user.rb index 722a2df..da94299 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -22,6 +22,7 @@ class User < ApplicationRecord has_one :personal has_one :source has_one :parent + has_one :major has_many :addresses has_many :languages has_many :achievements diff --git a/app/views/majors/create.html.erb b/app/views/majors/create.html.erb new file mode 100644 index 0000000..5d92fd6 --- /dev/null +++ b/app/views/majors/create.html.erb @@ -0,0 +1,2 @@ +

Majors#create

+

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

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

Majors#edit

+

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

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

Majors#new

+

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

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

Majors#show

+

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

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

Majors#update

+

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

diff --git a/config/routes.rb b/config/routes.rb index 8f921a1..957cfef 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -52,5 +52,7 @@ resources :extras, only: [:new, :create, :edit, :update, :show] resources :organizations, only: [:new, :create, :edit, :update, :show] resources :sources, only: [:new, :create, :edit, :update, :show] + resources :majors, only: [:new, :create, :edit, :update, :show] + end diff --git a/test/controllers/majors_controller_test.rb b/test/controllers/majors_controller_test.rb new file mode 100644 index 0000000..30e981c --- /dev/null +++ b/test/controllers/majors_controller_test.rb @@ -0,0 +1,24 @@ +require "test_helper" + +class MajorsControllerTest < ActionDispatch::IntegrationTest + def setup + @user = users(:archer) + get login_path + post login_path, params: {session: { + email: @user.email, password: 'password' + }} + end + test "should get new" do + get new_major_path + assert_response :success + end + test "should get edit" do + @user = users(:michael) + get login_path + post login_path, params: {session: { + email: @user.email, password: 'password' + }} + get edit_major_path majors(:two) + assert_response :success + end +end diff --git a/test/fixtures/majors.yml b/test/fixtures/majors.yml index 1176bc8..8d98445 100644 --- a/test/fixtures/majors.yml +++ b/test/fixtures/majors.yml @@ -1,13 +1,15 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -#one: -# jurusan_1: MyString -# jurusan_2: MyString -# jurusan_3: MyString -# gelombang: MyString +one: + user: michael + jurusan_1: fisika + jurusan_2: biologi + jurusan_3: sains data + gelombang: pmdk sumut -#two: -# jurusan_1: MyString -# jurusan_2: MyString -# jurusan_3: MyString -# gelombang: MyString +two: + user: iana + jurusan_1: teknik informatika + jurusan_2: sistem informasi + jurusan_3: teknik komputer + gelombang: utbk diff --git a/test/integration/major_test.rb b/test/integration/major_test.rb new file mode 100644 index 0000000..82a583e --- /dev/null +++ b/test/integration/major_test.rb @@ -0,0 +1,101 @@ +require "test_helper" + +class MajorTest < ActionDispatch::IntegrationTest + def setup + get login_path + post login_path, params: {session: { + email: users(:archer).email, password: 'password' + }} + end + test "should create a new major" do + get new_major_path + assert_template 'majors/new' + assert_difference 'Major.count' do + post majors_path, params: {major: { + jurusan_1: 'fisika', jurusan_2: 'biology', jurusan_3: 'math', + gelombang: 'pmdk sumut' + }} + end + assert_not_nil Major.find_by_jurusan_1 'fisika' + assert_not_nil Major.find_by_jurusan_2 'biology' + assert_not_nil Major.find_by_jurusan_3 'math' + assert_not_nil Major.find_by_gelombang 'pmdk sumut' + end + test "should not create major if one major equal to each other" do + get new_major_path + assert_template 'majors/new' + assert_no_difference 'Major.count' do + post majors_path, params: {major: { + jurusan_1: 'fisika 1', jurusan_2: 'fisika 1', jurusan_3: 'math', + gelombang: 'pmdk sumut' + }} + end + assert_nil Major.find_by_jurusan_1 'fisika 1' + end + test "should not create major for invalid information" do + get new_major_path + assert_template 'majors/new' + assert_no_difference 'Major.count' do + post majors_path, params: {major: { + jurusan_1: 'fisika 1', jurusan_2: 'fisika 2', jurusan_3: 'mat', + gelombang: 'pmdk sumut' + }} + end + assert_nil Major.find_by_jurusan_1 'mat' + end + test "should reject non param while create major" do + get new_major_path + assert_template 'majors/new' + assert_difference 'Major.count' do + post majors_path, params: {major: { + jurusan_1: 'fisika 1', jurusan_2: 'fisika 2', jurusan_3: 'math', + gelombang: 'pmdk sumut', user_id: 90989098 + }} + end + assert_nil Major.find_by_user_id 90989098 + end + test "should update the major" do + get login_path + post login_path, params: {session: { + email: users(:michael).email, password: 'password' + }} + get edit_major_path majors(:one) + assert_template 'majors/edit' + patch major_path(majors(:one)), params: {major: { + jurusan_1: 'fisika 1', jurusan_2: 'fisika 2', jurusan_3: 'math 1', + gelombang: 'pmdk sumut baru' + }} + assert_equal 'fisika 1', majors(:one).reload.jurusan_1 + assert_equal 'fisika 2', majors(:one).reload.jurusan_2 + assert_equal 'math 1', majors(:one).reload.jurusan_3 + assert_equal 'pmdk sumut baru', majors(:one).reload.gelombang + end + test "should reject to update major" do + get login_path + post login_path, params: {session: { + email: users(:michael).email, password: 'password' + }} + get edit_major_path majors(:one) + assert_template 'majors/edit' + patch major_path(majors(:one)), params: {major: { + jurusan_1: '1', jurusan_2: '2', jurusan_3: 'math 1', + gelombang: 'pmdk sumut baru' + }} + assert_nil Major.find_by_jurusan_1 '1' + assert_nil Major.find_by_jurusan_2 '2' + assert_nil Major.find_by_jurusan_3 'math 1' + end + test "should reject to update for non permited params" do + get login_path + post login_path, params: {session: { + email: users(:michael).email, password: 'password' + }} + get edit_major_path majors(:one) + assert_template 'majors/edit' + patch major_path(majors(:one)), params: {major: { + jurusan_1: 'fisika walker', jurusan_2: 'fisika halidaty', jurusan_3: 'math 1', + gelombang: 'pmdk sumut baru', user_id: 19800089 + }} + assert_nil Major.find_by_user_id 19800089 + end +end diff --git a/test/integration/source_test.rb b/test/integration/source_test.rb index b434655..a949982 100644 --- a/test/integration/source_test.rb +++ b/test/integration/source_test.rb @@ -64,4 +64,43 @@ def setup assert_not_nil Source.find_by_sumber_informasi 'facebook' assert_nil Source.find_by_user_id 1231212109 end + test "should update the source" do + @user = users(:michael) + get login_path + post login_path, params: {session: {email: @user.email, password: 'password'}} + get edit_source_path sources(:one) + patch source_path(sources(:one)), params: {source: { + sumber_informasi: 'facebook', jumlah_n: 3, motivasi: 'pendidikan' + }} + assert_equal 'facebook', sources(:one).reload.sumber_informasi + assert_equal 3, sources(:one).reload.jumlah_n + assert_equal 'pendidikan', sources(:one).reload.motivasi + end + test "should update but prevent the non permited params" do + @user = users(:michael) + get login_path + post login_path, params: {session: {email: @user.email, password: 'password'}} + get edit_source_path sources(:one) + patch source_path(sources(:one)), params: {source: { + sumber_informasi: 'facebook', jumlah_n: 3, motivasi: 'pendidikan', user_id: 90890909 + }} + assert_equal 'facebook', sources(:one).reload.sumber_informasi + assert_equal 3, sources(:one).reload.jumlah_n + assert_equal 'pendidikan', sources(:one).reload.motivasi + assert_nil Source.find_by_user_id 90890909 + end + test "should reject to update for invalid information" do + @user = users(:michael) + get login_path + post login_path, params: {session: {email: @user.email, password: 'password'}} + get edit_source_path sources(:one) + patch source_path(sources(:one)), params: {source: { + sumber_informasi: 'facebook', jumlah_n: 56, motivasi: 'pendidikan' + }} + sources(:one).reload + assert_nil Source.find_by_sumber_informasi 'facebook' + assert_nil Source.find_by_motivasi 'pendidikan' + assert_nil Source.find_by_jumlah_n 20 + end + end