Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Add stranger and initial stay seconds at first and default is 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Guo committed Mar 30, 2015
1 parent bb72d00 commit 31e154c
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -2,4 +2,16 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception

before_filter :initial_or_add_stay_time

def initial_or_add_stay_time
if current_user.present?

else
@stranger = Stranger.find_or_create_by(session_id: session['session_id'])
@stranger.stay_seconds += 1
@stranger.save
end
end
end
4 changes: 2 additions & 2 deletions app/controllers/home_controller.rb
Expand Up @@ -3,8 +3,8 @@ def index
if current_user.present?
@greeting = "#{current_user.name} 谢谢你登陆了我们网站!你已经登陆了#{current_user.sign_in_count}次了,总共登陆时间是?分钟了。"
else
@greeting = "你好,陌生人!你没有登陆,或者你还没有注册,但是你已经浏览这个页面?分钟了。"
@greeting = "你好,陌生人!你没有登陆,或者你还没有注册,但是你已经浏览这个页面#{(@stranger.stay_seconds/60).round(1)}分钟了。"
end
@stat = "现在总共有#{User.all.size}个注册用户在查看这个网站,有?个陌生人在查看这个网站。"
@stat = "现在总共有#{User.all.size}个注册用户在查看这个网站,有#{Stranger.where(updated_at: 5.minutes.ago).size}个陌生人在查看这个网站。"
end
end
3 changes: 3 additions & 0 deletions app/models/stranger.rb
@@ -0,0 +1,3 @@
class Stranger < ActiveRecord::Base
validates :session_id, presence: true, uniqueness: true, :length => { :minimum => 2 }
end
10 changes: 10 additions & 0 deletions db/migrate/20150330141141_create_strangers.rb
@@ -0,0 +1,10 @@
class CreateStrangers < ActiveRecord::Migration
def change
create_table :strangers do |t|
t.string :session_id
t.integer :stay_seconds, :null => false, :default => 0

t.timestamps
end
end
end
9 changes: 8 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,14 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20131121162928) do
ActiveRecord::Schema.define(version: 20150330141141) do

create_table "strangers", force: true do |t|
t.string "session_id"
t.integer "stay_seconds", default: 0, null: false
t.datetime "created_at"
t.datetime "updated_at"
end

create_table "users", force: true do |t|
t.string "name", default: "", null: false
Expand Down

0 comments on commit 31e154c

Please sign in to comment.