-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tdd for source of information, majors
- Loading branch information
1 parent
d18aa79
commit ee3a7dc
Showing
14 changed files
with
227 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module MajorsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Majors#create</h1> | ||
<p>Find me in app/views/majors/create.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Majors#edit</h1> | ||
<p>Find me in app/views/majors/edit.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Majors#new</h1> | ||
<p>Find me in app/views/majors/new.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Majors#show</h1> | ||
<p>Find me in app/views/majors/show.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Majors#update</h1> | ||
<p>Find me in app/views/majors/update.html.erb</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters