Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

タスクがユーザに紐づくようにした #21

Merged
merged 4 commits into from

2 participants

Takuya Kato / katton ken_c_lo / TAEKO AKATSUKA
Takuya Kato / katton
Collaborator

ログイン前のトップページのデザインを直してお使いくださいませ。

ken_c_lo / TAEKO AKATSUKA
Owner

ぬおおおー!ありがとううーー!!
collaborator 登録したよ( ˘ω˘)"

ken_c_lo / TAEKO AKATSUKA taea merged commit 8f2a4a1 into from
ken_c_lo / TAEKO AKATSUKA
Owner

オオ…、このブランチで作業しても、masterでマージしてpushすると自分が作業したぶんだけmasterブランチになってしまった… #ちょっと何いってるのか。。。

fork からの pull-requestムズカシイなー

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  .gitignore
View
@@ -1,3 +1,4 @@
+**.swp
**.orig
*.rbc
*.sassc
@@ -30,3 +31,4 @@ public/assets
public/assets-test
rerun.txt
spec/tmp
+
12 Gemfile.lock
View
@@ -112,7 +112,7 @@ GEM
activesupport (>= 3.1, < 4.1)
haml (>= 3.1, < 4.1)
railties (>= 3.1, < 4.1)
- hashie (1.2.0)
+ hashie (2.0.5)
hike (1.2.3)
hpricot (0.8.6)
html2haml (1.0.1)
@@ -125,7 +125,7 @@ GEM
jquery-rails (3.0.1)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
- json (1.8.0)
+ json (1.8.1)
kgio (2.8.0)
launchy (2.3.0)
addressable (~> 2.3)
@@ -140,17 +140,17 @@ GEM
method_source (0.8.2)
mime-types (1.25)
mini_portile (0.5.1)
- multi_json (1.8.0)
+ multi_json (1.9.0)
nokogiri (1.6.0)
mini_portile (~> 0.5.0)
oauth (0.4.7)
- omniauth (1.1.4)
+ omniauth (1.2.1)
hashie (>= 1.2, < 3)
- rack
+ rack (~> 1.0)
omniauth-oauth (1.0.1)
oauth
omniauth (~> 1.0)
- omniauth-twitter (1.0.0)
+ omniauth-twitter (1.0.1)
multi_json (~> 1.3)
omniauth-oauth (~> 1.0)
pg (0.15.1)
16 app/controllers/application_controller.rb
View
@@ -1,10 +1,24 @@
class ApplicationController < ActionController::Base
+ helper_method :current_user, :sign_in?
protect_from_forgery
+
+ private
+
def current_user
if session[:user_id]
- User.find(session[:user_id])
+ @user ||= User.find(session[:user_id])
else
nil
end
end
+
+ def sign_in?
+ current_user ? true : nil
+ end
+
+ def authenticate_user!
+ unless sign_in?
+ redirect_to root_path, notice: 'Please Sign in with Twitter'
+ end
+ end
end
4 app/controllers/sessions_controller.rb
View
@@ -5,6 +5,10 @@ def create
redirect_to '/'
end
+ def failure
+ redirect_to root_path, notice: 'OAuth Failure'
+ end
+
protected
def auth_hash
5 app/controllers/tasks_controller.rb
View
@@ -1,8 +1,10 @@
class TasksController < ApplicationController
+ before_filter :authenticate_user!, except: [:index]
+
# GET /tasks
# GET /tasks.json
def index
- @tasks = Task.find(:all, :conditions => {:status => ['unstarted', 'doing']}, :order => "status ASC, updated_at DESC")
+ @tasks = current_user.tasks.active if sign_in?
@task = Task.new
@this_iteration = Iteration.for_week
respond_to do |format|
@@ -48,6 +50,7 @@ def edit
# POST /tasks.json
def create
@task = Task.new(params[:task])
+ @task.user_id = current_user.id
respond_to do |format|
if @task.save
3  app/models/task.rb
View
@@ -1,3 +1,6 @@
class Task < ActiveRecord::Base
attr_accessible :finished_at, :memo, :point, :started_at, :status, :title, :iteration_id
+ validates_presence_of :user_id, :title
+ belongs_to :user
+ scope :active, where(status: ['unstarted', 'doing']).order("status ASC, updated_at DESC")
end
17 app/models/user.rb
View
@@ -1,5 +1,10 @@
class User < ActiveRecord::Base
attr_accessible :image, :name, :nickname, :secret, :token, :uid
+ has_many :tasks, dependent: :destroy
+
+ validates_presence_of :uid
+ validates_uniqueness_of :uid
+
def self.find_or_create_by_oauth(auth, current_user)
if user = User.find_by_oauth(auth)
user
@@ -20,12 +25,12 @@ def self.find_by_oauth(auth)
def self.create_by_oauth(auth)
user = User.new
user.uid = auth['uid']
- user.name = auth.info['name']
- user.nickname = auth.info['nickname']
- user.image = auth.info['image']
- user.token = auth.credentials['token']
- user.secret = auth.credentials['secret']
+ user.name = auth.info['name']
+ user.nickname = auth.info['nickname']
+ user.image = auth.info['image']
+ user.token = auth.credentials['token']
+ user.secret = auth.credentials['secret']
user.save
- user
+ user
end
end
12 app/views/layouts/application.html.haml
View
@@ -21,8 +21,16 @@
%header.header
%h1= link_to "K-tra!", root_path
.user
- %p.image= link_to image_tag('https://si0.twimg.com/profile_images/3703695062/b57c347a35c1fe6c4e09eca37eb7b327_normal.jpeg', alt: 'ken_c_lo'), 'auth/twitter'
- %p.name= link_to 'ken_c_lo', 'auth/twitter'
+ - if current_user
+ - # FIXME ここのaタグ、必要なさそう
+ %p.image
+ %a= image_tag current_user.image
+ %p.name
+ %a= current_user.nickname
+ - else
+ %p.name
+ = link_to '/auth/twitter' do
+ sign in
= yield
%footer.footer
%small
11 app/views/tasks/index.html.haml
View
@@ -9,7 +9,7 @@
- @task.errors.full_messages.each do |msg|
%li= msg
.field
- = f.text_field :title, class: 'task-title', placeholder: 'NEW TASK TITLE'
+ = f.text_field :title, class: 'task-title', placeholder: 'NEW TASK TITLE', required: true
%i.icon-pencil
.field.point-radio
%ul
@@ -46,13 +46,18 @@
= f.hidden_field :status, value: 'unstarted'
.actions
- = f.button "#{content_tag(:i, '', class: 'icon-plus-sign')}".html_safe, type: :submit
+ - if sign_in?
+ = f.button "#{content_tag(:i, '', class: 'icon-plus-sign')}".html_safe, type: :submit
+ - else
+ - # FIXME
+ = link_to '/auth/twitter' do
+ Sign in with Twitter
.iteration-summary
= link_to iteration_path(@this_iteration) do
= render 'iteration', iteration: @this_iteration, tasks: true
-- if @tasks.present?
+- if sign_in? && @tasks.present?
= render 'tasks'
- else
%p.error-message There's no tasks yet.
1  config/routes.rb
View
@@ -9,6 +9,7 @@
end
root :to => 'tasks#index'
get '/auth/twitter/callback', to: 'sessions#create'
+ get '/auth/failure' => 'sessions#failure'
# The priority is based upon order of creation:
# first created -> highest priority.
6 db/migrate/20140306103013_add_user_id_to_tasks.rb
View
@@ -0,0 +1,6 @@
+class AddUserIdToTasks < ActiveRecord::Migration
+ def change
+ add_column :tasks, :user_id, :integer
+ add_index :tasks, :user_id
+ end
+end
5 db/schema.rb
View
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130815115856) do
+ActiveRecord::Schema.define(:version => 20140306103013) do
create_table "iterations", :force => true do |t|
t.date "start_date"
@@ -30,8 +30,11 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "iteration_id"
+ t.integer "user_id"
end
+ add_index "tasks", ["user_id"], :name => "index_tasks_on_user_id"
+
create_table "users", :force => true do |t|
t.string "uid", :null => false
t.string "name"
1  spec/factories/tasks.rb
View
@@ -2,6 +2,7 @@
factory :task, class: 'Task' do
title { Faker::Name.title }
point { 1 }
+ user_id { 1 }
memo { Faker::Lorem.paragraphs.join("\n") }
end
end
Something went wrong with that request. Please try again.