Skip to content

Commit

Permalink
[WIP] keisukefunatsu#74 Userの学年を上げるメソッドを実装した
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Miyamoto committed Jun 4, 2016
1 parent c0b6017 commit f412887
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def destroy

def user_params
params.require(:user).permit(
:name,:grade, :school_name, :phone_number, :confirmed
:name, :grade_code, :school_name, :phone_number, :confirmed
)
end
end
17 changes: 17 additions & 0 deletions app/models/grade.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Grade < ActiveHash::Base
self.data = [
{code: 'ELEMENTARY_1', name: '小1', next_grade_code: 'ELEMENTARY_2'},
{code: 'ELEMENTARY_2', name: '小2', next_grade_code: 'ELEMENTARY_3'},
{code: 'ELEMENTARY_3', name: '小3', next_grade_code: 'ELEMENTARY_4'},
{code: 'ELEMENTARY_4', name: '小4', next_grade_code: 'ELEMENTARY_5'},
{code: 'ELEMENTARY_5', name: '小5', next_grade_code: 'ELEMENTARY_6'},
{code: 'ELEMENTARY_6', name: '小6', next_grade_code: 'JUNIOR_HIGH_1'},
{code: 'JUNIOR_HIGH_1', name: '中1', next_grade_code: 'JUNIOR_HIGH_2'},
{code: 'JUNIOR_HIGH_2', name: '中2', next_grade_code: 'JUNIOR_HIGH_3'},
{code: 'JUNIOR_HIGH_3', name: '中3', next_grade_code: 'HIGH_1'},
{code: 'HIGH_1', name: '高1', next_grade_code: 'HIGH_2'},
{code: 'HIGH_2', name: '高2', next_grade_code: 'HIGH_3'},
{code: 'HIGH_3', name: '高3', next_grade_code: 'GRADUATED'},
{code: 'GRADUATED', name: '卒業生', next_grade_code: 'GRADUATED'}
]
end
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class User < ActiveRecord::Base
extend ActiveHash::Associations::ActiveRecordExtensions
has_many :results, :dependent => :delete_all
has_many :report_results, :class_name => Result, :foreign_key => :author_id
has_many :tickets, :dependent => :delete_all
has_many :timecards, :dependent => :delete_all
has_many :information
has_many :participating_events, through: :tickets, source: :information
belongs_to_active_hash :grade, foreign_key: :grade_code, primary_key: :code

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable
Expand All @@ -23,6 +25,16 @@ def self.find_for_google_oauth2(auth)
user
end

def increase_grade
return nil unless grade
update(grade_code: grade.next_grade_code)
end

def increase_grade!
raise '学年が設定されていません' unless grade
update!(grade_code: grade.next_grade_code)
end

def recent_timecard
timecards.order(created_at: :desc).first
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/edit.html.slim
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

.form-group
= f.label '学年'
= f.select(:grade, ["中1","中2","中3","高1","高2","高3"], {class: 'form-control'} )
= f.select(:grade_code, Grade.all.map { |g| [g.name, g.code] }, {class: 'form-control'} )
.form-group
= f.label '氏名'
= f.text_field :name, class: 'form-control',row: 10,placeholder:'船津圭佑'
Expand Down
7 changes: 7 additions & 0 deletions db/migrate/20160604075414_change_grade_to_grade_code.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class ChangeGradeToGradeCode < ActiveRecord::Migration
def change
remove_column :users, :grade, :string
add_column :users, :grade_code, :string
add_index :users, :grade_code
end
end
13 changes: 8 additions & 5 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160604042640) do
ActiveRecord::Schema.define(version: 20160604075414) do

create_table "information", force: :cascade do |t|
t.string "title", null: false
Expand All @@ -31,11 +31,13 @@
t.integer "user_id"
t.string "title"
t.text "content"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "author_id", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "author_id"
end

add_index "results", ["author_id"], name: "index_results_on_author_id"

create_table "tickets", force: :cascade do |t|
t.integer "user_id"
t.integer "information_id", null: false
Expand Down Expand Up @@ -77,13 +79,14 @@
t.string "name"
t.string "token"
t.boolean "admin", default: false, null: false
t.string "grade"
t.string "school_name"
t.string "phone_number"
t.boolean "confirmed"
t.string "grade_code"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["grade_code"], name: "index_users_on_grade_code"
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end
2 changes: 1 addition & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# 一般ユーザの作成
["大久保","大西","田中","橘","駒田"].each.with_index(1) do |name, n|
User.create(email:"user#{n}@example.com",password:'11111111',name: name, admin: false, confirmed: true)
User.create(email:"user#{n}@example.com",password:'11111111',name: name, grade_code: Grade.all.sample.code, admin: false, confirmed: true)
end

# お知らせの作成
Expand Down
7 changes: 6 additions & 1 deletion spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
email { Faker::Internet.email }
password 'secret1234'
password_confirmation { password }

trait :student do
grade_code { Grade.all.sample.code }
admin false
end
end
end
end
37 changes: 37 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
require 'rails_helper'

describe User do
describe '#increase_grade' do
context '学年がある場合' do
subject { create(:user, :student) }
it '次の学年に更新されている' do
expected = subject.grade.next_grade_code
subject.increase_grade
expect(subject.grade_code).to eq expected
end
end

context '学年がない場合' do
subject { create(:user) }
it '学年が更新されない' do
subject.increase_grade
expect(subject.grade_code).to be_nil
end
end
end

describe '#increase_grade!' do
context '学年がある場合' do
subject { create(:user, :student) }
it '次の学年に更新されている' do
expected = subject.grade.next_grade_code
subject.increase_grade!
expect(subject.grade_code).to eq expected
end
end

context '学年がない場合' do
subject { create(:user) }
it 'エラーが発生する' do
expect{subject.increase_grade!}.to raise_error '学年が設定されていません'
end
end
end

describe '#recent_timecard' do
subject { create(:user) }
before do
Expand Down

0 comments on commit f412887

Please sign in to comment.