From f412887b7255640859f812aa0f41724c3eb09f15 Mon Sep 17 00:00:00 2001 From: Takashi Miyamoto Date: Sat, 4 Jun 2016 17:40:57 +0900 Subject: [PATCH] =?UTF-8?q?[WIP]=20#74=20User=E3=81=AE=E5=AD=A6=E5=B9=B4?= =?UTF-8?q?=E3=82=92=E4=B8=8A=E3=81=92=E3=82=8B=E3=83=A1=E3=82=BD=E3=83=83?= =?UTF-8?q?=E3=83=89=E3=82=92=E5=AE=9F=E8=A3=85=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/users_controller.rb | 2 +- app/models/grade.rb | 17 +++++++++ app/models/user.rb | 12 ++++++ app/views/users/edit.html.slim | 2 +- ...160604075414_change_grade_to_grade_code.rb | 7 ++++ db/schema.rb | 13 ++++--- db/seeds.rb | 2 +- spec/factories/users.rb | 7 +++- spec/models/user_spec.rb | 37 +++++++++++++++++++ 9 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 app/models/grade.rb create mode 100644 db/migrate/20160604075414_change_grade_to_grade_code.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 22de4c9..c24e996 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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 diff --git a/app/models/grade.rb b/app/models/grade.rb new file mode 100644 index 0000000..c209cfe --- /dev/null +++ b/app/models/grade.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index a761417..50143ba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ -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 diff --git a/app/views/users/edit.html.slim b/app/views/users/edit.html.slim index 7c8020f..b40fcd5 100644 --- a/app/views/users/edit.html.slim +++ b/app/views/users/edit.html.slim @@ -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:'船津圭佑' diff --git a/db/migrate/20160604075414_change_grade_to_grade_code.rb b/db/migrate/20160604075414_change_grade_to_grade_code.rb new file mode 100644 index 0000000..688f294 --- /dev/null +++ b/db/migrate/20160604075414_change_grade_to_grade_code.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 78b2533..01e2c2a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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 @@ -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 @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index 9dc9e8b..f605490 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 # お知らせの作成 diff --git a/spec/factories/users.rb b/spec/factories/users.rb index 51d9051..08355c0 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -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 \ No newline at end of file +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 82f2594..8ae7fde 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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