[from now] 2014/01/24 22:02:41

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 85c3cd2..042c6d3 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -3,11 +3,23 @@ class ApplicationController < ActionController::Base
   # For APIs, you may want to use :null_session instead.
   protect_from_forgery with: :exception

-  def user_id
+  def gyazo_id
     params['id'] || new_id
   end

   def new_id
     Digest::MD5.hexdigest(request[:remote_addr] + Time.now.to_s)
   end
+
+  def new_user?
+    params[:id].present?
+  end
+
+  def current_user
+    User.find(gyazo_id: gyazo_id) || User.create(gyazo_id: gyazo_id)
+  end
+
+  def hoge
+    header['X-Gyazo-Id'] = gyazo_id if new_user?
+  end
 end
[from now] 2014/02/08 21:26:04

diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index 042c6d3..864a1ca 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -3,6 +3,10 @@ class ApplicationController < ActionController::Base
   # For APIs, you may want to use :null_session instead.
   protect_from_forgery with: :exception

+  def user_id
+  end
+
+
   def gyazo_id
     params['id'] || new_id
   end
@@ -16,7 +20,7 @@ class ApplicationController < ActionController::Base
   end

   def current_user
-    User.find(gyazo_id: gyazo_id) || User.create(gyazo_id: gyazo_id)
+    User.where(gyazo_id: gyazo_id).first || User.create(gyazo_id: gyazo_id)
   end

   def hoge
diff --git a/app/controllers/screenshots_controller.rb b/app/controllers/screenshots_controller.rb
index e303abe..0e00fda 100644
--- a/app/controllers/screenshots_controller.rb
+++ b/app/controllers/screenshots_controller.rb
@@ -3,7 +3,7 @@ class ScreenshotsController < ApplicationController
   skip_before_action :verify_authenticity_token, only: [:create]

   def index
-    @screenshots = Screenshot.all
+    @screenshots = Screenshot.where(user_id: current_user)
   end

   def show
@@ -12,6 +12,7 @@ class ScreenshotsController < ApplicationController
   def create
     @screenshot = Screenshot.new
     @screenshot.image = params[:imagedata]
+    @screenshot.user = current_user
     @screenshot.save
     render text: screenshot_url(@screenshot)
   end
diff --git a/app/models/screenshot.rb b/app/models/screenshot.rb
index 43dc60d..a8d52ee 100644
--- a/app/models/screenshot.rb
+++ b/app/models/screenshot.rb
@@ -1,5 +1,8 @@
 class Screenshot < ActiveRecord::Base
   extend FriendlyId
+
+  belongs_to :user
+
   has_attached_file :image, styles: { thumb: '100x100>' }, default_url: '/images/:style/missing.png',
     path: ':rails_root/public/screenshots/:style/:id_sha1.png',
     url:  "#{ActionController::Base.relative_url_root}/screenshots/:style/:id_sha1.png"
diff --git a/db/schema.rb b/db/schema.rb
index 223be14..3862393 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: 20140111015619) do
+ActiveRecord::Schema.define(version: 20140124131340) do

   create_table "screenshots", force: true do |t|
     t.datetime "created_at"
@@ -22,8 +22,16 @@ ActiveRecord::Schema.define(version: 20140111015619) do
     t.datetime "image_updated_at"
     t.string   "title"
     t.string   "slug"
+    t.integer  "user_id"
   end

   add_index "screenshots", ["slug"], name: "index_screenshots_on_slug", unique: true

+  create_table "users", force: true do |t|
+    t.string   "gyazo_id"
+    t.string   "name"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
+
 end
[from now] 2014/02/08 21:26:24

diff --git a/app/models/user.rb b/app/models/user.rb
new file mode 100644
index 0000000..438a355
--- /dev/null
+++ b/app/models/user.rb
@@ -0,0 +1,3 @@
+class User < ActiveRecord::Base
+  has_many :screenshots
+end
diff --git a/db/migrate/20140124130331_create_users.rb b/db/migrate/20140124130331_create_users.rb
new file mode 100644
index 0000000..47f1a62
--- /dev/null
+++ b/db/migrate/20140124130331_create_users.rb
@@ -0,0 +1,10 @@
+class CreateUsers < ActiveRecord::Migration
+  def change
+    create_table :users do |t|
+      t.string :gyazo_id
+      t.string :name
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/migrate/20140124131340_add_user_id_to_screenshot.rb b/db/migrate/20140124131340_add_user_id_to_screenshot.rb
new file mode 100644
index 0000000..2798ff0
--- /dev/null
+++ b/db/migrate/20140124131340_add_user_id_to_screenshot.rb
@@ -0,0 +1,5 @@
+class AddUserIdToScreenshot < ActiveRecord::Migration
+  def change
+    add_column :screenshots, :user_id, :integer
+  end
+end
diff --git a/spec/factories/users.rb b/spec/factories/users.rb
new file mode 100644
index 0000000..c64bf73
--- /dev/null
+++ b/spec/factories/users.rb
@@ -0,0 +1,8 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+  factory :user do
+    gyazo_id "MyString"
+    name "MyString"
+  end
+end
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
new file mode 100644
index 0000000..44032b4
--- /dev/null
+++ b/spec/models/user_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe User do
+  pending "add some examples to (or delete) #{__FILE__}"
+end
drone