Skip to content

Instantly share code, notes, and snippets.

@jacoyutorius
Last active August 29, 2015 14:07
Show Gist options
  • Select an option

  • Save jacoyutorius/8158c1b046629b128f1a to your computer and use it in GitHub Desktop.

Select an option

Save jacoyutorius/8158c1b046629b128f1a to your computer and use it in GitHub Desktop.
ユーザーの行動履歴をMongoDBに保存する ref: http://qiita.com/jacoyutorius/items/c31305f9f577edd3ffa1
class ActionLog
include Mongoid::Document
field :datetime, type: DateTime
field :user_id, type: String
field :user_name, type: String
field :controller, type: String
field :method, type: String
field :body, type: String
field :delete, type: Boolean
end
class Application < Rails::Application
config.generators do |g|
g.orm :active_record
end
end
brew install mongodb
# インストール完了後のメッセージに従って、以下のコマンドを実行
ln -sfv /usr/local/opt/mongodb/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mongodb.plist
mongo --version
=> MongoDB shell version: 2.6.4
rails g active_record:migration AddHoge
bundle install --path .bundle
rails g mongoid:config
=>create config/mongoid.yml
rails g mongoid:model action_log
rails g mongo:model action_log
irb(main):002:0> ActionLog.create!(datetime:Time.now, user_id:0, user_name:"jacoyutorius", body:"test")
=> #<ActionLog _id: 543df2987975746196000000, datetime: 2014-10-15 04:05:43 UTC, user_id: "0", user_name: "jacoyutorius", controller: nil, method: nil, body: "test", delete: nil>
irb(main):002:0> ActionLog.create!(datetime:Time.now, user_id:0, user_name:"jacoyutorius", body:"test")
=> #<ActionLog _id: 543df2987975746196000000, datetime: 2014-10-15 04:05:43 UTC, user_id: "0", user_name: "jacoyutorius", controller: nil, method: nil, body: "test", delete: nil>
irb(main):005:0> ActionLog.first
=> #<ActionLog _id: 543deed77975745d73000000, datetime: 2014-10-15 03:49:43 UTC, user_id: "1", user_name: "jacoyutorius", controller: "admin/top", method: nil, body: "{\"controller\"=>\"top\", \"action\"=>\"index\"}", delete: false>
gem "mongoid"
gem "bson_ext"
module LogMonger
extend ActiveSupport::Concern
included do
before_action :save_log
def save_log
ActionLog.create!(
datetime: Time.now,
user_id: 1,
user_name: "jacoyutorius",
method: params[:method],
controller: params[:controller],
body: params,
delete: false
)
end
end
end
class UsersController < ApplicationController
include LogMonger
# -(省略)-
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment