Last active
August 29, 2015 14:07
-
-
Save jacoyutorius/8158c1b046629b128f1a to your computer and use it in GitHub Desktop.
ユーザーの行動履歴をMongoDBに保存する ref: http://qiita.com/jacoyutorius/items/c31305f9f577edd3ffa1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Application < Rails::Application | |
| config.generators do |g| | |
| g.orm :active_record | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails g active_record:migration AddHoge |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| bundle install --path .bundle |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails g mongoid:config | |
| =>create config/mongoid.yml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails g mongoid:model action_log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails g mongo:model action_log |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| gem "mongoid" | |
| gem "bson_ext" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class UsersController < ApplicationController | |
| include LogMonger | |
| # -(省略)- | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment