Skip to content

Commit

Permalink
Merge pull request #13 from enpitut2018/account-specific-tasks
Browse files Browse the repository at this point in the history
ログインユーザー別に表示するタスク・予定を分岐させる処理を追加
  • Loading branch information
inside-hakumai authored Oct 24, 2018
2 parents 97611b9 + 5f7fccf commit 9336d77
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ApplicationController < ActionController::Base
protect_from_forgery
helper_method :get_current_user_avator, :get_current_user_id
helper_method :get_current_user_avator, :get_current_user_id, :is_logged_in

private
def get_current_user_avator
Expand Down
10 changes: 8 additions & 2 deletions app/controllers/schedule_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ def insert
start_time = Time.zone.local(s_year.to_i, s_month.to_i, s_day.to_i, s_hour.to_i, s_minute.to_i)
end_time = Time.zone.local(e_year.to_i, e_month.to_i, e_day.to_i, e_hour.to_i, e_minute.to_i)
if end_time > start_time then
Schedule.createRecord(name, start_time, end_time)

if view_context.user_signed_in? then
Schedule.createRecord(name, start_time, end_time, current_user.email)
else
Schedule.createRecord(name, start_time, end_time)
end

@err_id = "正常"
else
@err_id = "終始逆"
Expand Down Expand Up @@ -124,7 +130,7 @@ def edit
start_time = Time.zone.local(s_year.to_i, s_month.to_i, s_day.to_i, s_hour.to_i, s_minute.to_i)
end_time = Time.zone.local(e_year.to_i, e_month.to_i, e_day.to_i, e_hour.to_i, e_minute.to_i)
if end_time > start_time then
Schedule.createRecord(name, start_time, end_time)
Schedule.createRecord(name, start_time, end_time, Schedule.find_by_id(@@edit_id).user_id)
Schedule.destroyRecord(@@edit_id)
@err_id = "正常"
@@edit_id = 0
Expand Down
23 changes: 15 additions & 8 deletions app/controllers/task_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@ def is_valid_date year, month, day, hour, minute
end

def insert
@current_user = current_user
logger.debug("Current user: ")
logger.debug(@current_user)


if request.post? then
name = params['name']
year = params['year']
Expand All @@ -34,7 +29,13 @@ def insert
if (name.length <= 50 && name.length > 0) then
if (elements.all? {|t| !t.empty? && !t.nil?}) && is_valid_date(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i) then
deadline = Time.zone.local(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i)
Task.createRecord(name, deadline)

if view_context.user_signed_in? then
Task.createRecord(name, deadline, current_user.email)
else
Task.createRecord(name, deadline)
end

@err_id = "正常" # 正常に追加
else
@err_id = "日程" # 日程が異常
Expand All @@ -48,7 +49,13 @@ def insert

def display
@err_id = "初期"
@tasks = Task.all

if view_context.user_signed_in? then
@tasks = Task.where(user_id: current_user.email)
else
@tasks = Task.all
end

end

def delete
Expand Down Expand Up @@ -90,7 +97,7 @@ def edit
if (name.length <= 50 && name.length > 0) then
if (elements.all? {|t| !t.empty? && !t.nil?}) && is_valid_date(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i) then
deadline = Time.zone.local(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i)
Task.createRecord(name, deadline)
Task.createRecord(name, deadline, Task.find_by_id(@@edit_id).user_id)
Task.destroyRecord(@@edit_id)
@@edit_id = 0 # 編集処理が完了したのでedit_idを初期化
@err_id = "正常" # 正常に追加
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/schedule_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
module ScheduleHelper

def get_available_schedules
if user_signed_in? then
Schedule.where(:user_id => current_user.email)
else
Schedule.where(:user_id => nil)
end
end

end
9 changes: 9 additions & 0 deletions app/helpers/task_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
module TaskHelper

def get_available_tasks
if user_signed_in? then
Task.where(:user_id => current_user.email)
else
Task.where(:user_id => nil)
end
end

end
4 changes: 2 additions & 2 deletions app/models/schedule.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Schedule < ApplicationRecord
def self.createRecord name, start_time, end_time
Schedule.create(:name => name, :start_time => start_time, :end_time => end_time)
def self.createRecord name, start_time, end_time, user_id=nil
Schedule.create(:user_id => user_id, :name => name, :start_time => start_time, :end_time => end_time)
end

def self.destroyRecord id
Expand Down
4 changes: 2 additions & 2 deletions app/models/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def self.calc_rate_busy id
f_time / d_time
end

def self.createRecord name, deadline
Task.create(:name => name, :deadline => deadline)
def self.createRecord name, deadline, user_id=nil
Task.create(:user_id => user_id, :name => name, :deadline => deadline)
end

def self.destroyRecord id
Expand Down
2 changes: 1 addition & 1 deletion app/views/schedule/_schedule.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</tr>
</thead>
<tbody>
<% schedules = Schedule.all.order("start_time ASC") %>
<% schedules = get_available_schedules.order("start_time ASC") %>
<%= render :partial => "listingschedule.html.erb",
:collection => schedules %>
</tbody>
Expand Down
2 changes: 1 addition & 1 deletion app/views/task/_task.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</tr>
</thead>
<tbody>
<% tasks = Task.all.order("deadline ASC") %>
<% tasks = get_available_tasks.order("deadline ASC") %>
<%= render :partial => "listingtask.html.erb",
:collection => tasks %>
</tbody>
Expand Down
1 change: 1 addition & 0 deletions db/migrate/20180726042230_create_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CreateTasks < ActiveRecord::Migration[5.2]
def change
create_table :tasks do |t|
t.string :user_id, null: true, default: nil
t.string :name
t.datetime :deadline

Expand Down
1 change: 1 addition & 0 deletions db/migrate/20180726042240_create_schedules.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
class CreateSchedules < ActiveRecord::Migration[5.2]
def change
create_table :schedules do |t|
t.string :user_id, null: true, default: nil
t.string :name
t.datetime :start_time
t.datetime :end_time
Expand Down
4 changes: 2 additions & 2 deletions db/migrate/20181010145021_devise_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def self.up
create_table :users do |t|
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :email, null: false
t.string :encrypted_password, null: false

#added by Sumiya
t.string :users, :name
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
enable_extension "plpgsql"

create_table "schedules", force: :cascade do |t|
t.string "user_id"
t.string "name"
t.datetime "start_time"
t.datetime "end_time"
Expand All @@ -24,6 +25,7 @@
end

create_table "tasks", force: :cascade do |t|
t.string "user_id"
t.string "name"
t.datetime "deadline"
t.datetime "created_at", null: false
Expand Down
29 changes: 23 additions & 6 deletions test/controllers/sessions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest
test "should get login" do
get sessions_login_url
assert_response :success

test 'should not get login' do
# ログイン用のURLは "/login" に設定されており, "/sessions/login" は使用できない
assert_raises(ActionController::RoutingError) do
get '/sessions/login'
end
end

test 'should not get logout' do
# ログアウト用のURLは "/logout" に設定されており, "/sessions/logout" は使用できない
assert_raises(ActionController::RoutingError) do
get '/sessions/logout'
end
end

test 'should be redirected to google authentication page' do
# ログイン用のURLにアクセスするとGoogleの認証ページにリダイレクトされる
get '/login'
assert_response :redirect
end

test "should get logout" do
get sessions_logout_url
assert_response :success
test 'should get logout' do
# ログアウト用のURLに(未ログイン状態で)アクセスするとトップページにリダイレクトされる
get '/logout'
assert_response :redirect
end

end
8 changes: 6 additions & 2 deletions test/fixtures/users.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}

# テスト環境におけるUserテーブルの状態はこのファイルで定義されるが,空エントリは挿入時にemailとencrypted_passwordのNULL非許可設定に
# 引っかかってエラーを吐くので全てコメントアウト

# one: {}
# column: value
#
two: {}
# two: {}
# column: value

0 comments on commit 9336d77

Please sign in to comment.