saw 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 201ccb92480175264bc7cb5955186328323c21a4
4
+ data.tar.gz: 2b4b6f7485a01ccd81e536e9d524246c688465dc
5
+ SHA512:
6
+ metadata.gz: 50b0a2956314584084cd0d4f2d1861dcb4c68701717f23243aed65b5fb565e5d96e32ba830287e973fbaaef7243dafe461aae2f256e87ec439dace92fe98c709
7
+ data.tar.gz: bf81963f5228ba8ef6d6c0cff3861b84dbcdd2d6474a9887b50ab848af3015a010affe2f5331184b6eb6f52f2a59241946801ed084aa0496f28991df9d1f0695
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.log
19
+ *.sqlite3
20
+ .rvmrc
21
+
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ require File.expand_path('test/support/detect_rails_version', File.dirname(__FILE__))
6
+
7
+ rails_version = detect_rails_version
8
+ gem 'rails', rails_version
9
+
10
+ case rails_version
11
+ when /^3\.0/
12
+ # Do nothing, bundler should figure it out
13
+ else
14
+ raise "Rails #{rails_version} is not supported yet"
15
+ end
16
+
17
+ group :test do |variable|
18
+ gem 'sqlite3-ruby'
19
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Amol Pujari
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Saw
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'saw'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install saw
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,23 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rake/rdoctask'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the saw plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the saw plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Saw'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README', 'CHANGELOG', 'LICENSE')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,48 @@
1
+ ActiveAdmin.register Visit do
2
+ menu :parent => "Users"
3
+ belongs_to :user, :optional => true
4
+
5
+ actions :all, :except => [:edit, :update, :create]
6
+
7
+ index do
8
+ selectable_column
9
+
10
+ column "" do |resource|
11
+ links = ''
12
+ links << link_to("#{resource.hits.count} hits", admin_visit_hits_path(resource))
13
+ links << " by "
14
+ links << link_to("#{resource.user.username}", admin_user_path(resource.user))
15
+ links.html_safe
16
+ end
17
+
18
+ column :user_agent
19
+ column :remote_host
20
+
21
+ column :starting, :sortable => :created_at do |resource|
22
+ resource.created_at.strftime('%b %-d %Y, %l %P')
23
+ end
24
+ column :lasts do |resource|
25
+ resource.lasts
26
+ end
27
+
28
+ default_actions
29
+ end
30
+ end
31
+
32
+ ActiveAdmin.register Hit do
33
+ menu :parent => "Users"
34
+ belongs_to :visit, :optional => true
35
+
36
+ actions :all, :except => [:edit, :update, :create]
37
+
38
+ index do
39
+ selectable_column
40
+ column :note
41
+
42
+ column :action, :sortable => :action do |resource|
43
+ "#{resource.http_method} #{resource.action}"
44
+ end
45
+
46
+ default_actions
47
+ end
48
+ end
@@ -0,0 +1,10 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ rails generate saw Thing
6
+
7
+ This will create
8
+ db/migrate/XXXXXXXXXXXXXXXX_add_visits_and_hits.rb
9
+ route post "visits" => "visits#create", :defaults => { :format => "json"}
10
+
@@ -0,0 +1,22 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ class SawGenerator < ActiveRecord::Generators::Base
4
+ include Rails::Generators::ResourceHelpers
5
+
6
+ desc "Create a migration to add saw-specific data. " +
7
+ "Add tables visits and hits." +
8
+ "Adds a route for recording visits by POST ajax" +
9
+ "Adds POST /visits => visits#create"
10
+
11
+ def self.source_root
12
+ @source_root ||= File.expand_path('../templates', __FILE__)
13
+ end
14
+
15
+ def generate_migration
16
+ migration_template "saw_migration.rb.erb", "db/migrate/add_visits_and_hits"
17
+ end
18
+
19
+ def generate_routes
20
+ route 'post "visits" => "visits#create", :defaults => { :format => "json"}'
21
+ end
22
+ end
@@ -0,0 +1,27 @@
1
+ class AddVisitsAndHits < ActiveRecord::Migration
2
+ def change
3
+ create_table :visits do |t|
4
+ t.integer :user_id
5
+ t.string :session_id
6
+ t.string :user_agent
7
+ t.string :remote_host
8
+ t.datetime :created_at
9
+ end
10
+
11
+ add_index :visits, [:user_id, :session_id]
12
+ add_index :visits, :user_id
13
+
14
+ create_table :hits do |t|
15
+ t.integer :visit_id
16
+ t.string :url
17
+ t.string :http_method
18
+ t.string :action
19
+ t.text :params
20
+ t.string :note
21
+ t.text :json_data
22
+ t.datetime :created_at
23
+ end
24
+
25
+ add_index :hits, :visit_id
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ require "saw/version"
2
+ require "saw/visit.rb"
3
+ require "saw/hit.rb"
4
+ require "saw/controller.rb"
5
+
6
+ module Saw
7
+ class Railtie < ::Rails::Railtie
8
+ config.after_initialize do
9
+ require "saw/visits_controller"
10
+ require "saw/user_addition"
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,42 @@
1
+ module Saw
2
+ module Controller
3
+ def saw doing=nil, json_data=nil
4
+ return unless current_user
5
+
6
+ return if request.fullpath.include? "/admin/"
7
+
8
+ user_id = current_user.id
9
+ session_id = request.session_options[:id]
10
+ remote_host = request.remote_ip
11
+ remote_host = request.env["HTTP_X_FORWARDED_FOR"] if remote_host.blank?
12
+ user_agent = request.env["HTTP_USER_AGENT"]
13
+ url = request.fullpath
14
+ http_method = request.method
15
+ action = "#{controller_name}##{action_name}"
16
+ doing = doing.to_s.strip
17
+ doing = action if doing.blank?
18
+
19
+ visit = Visit.where('user_id = ? and session_id = ? ', user_id, session_id).first
20
+
21
+ visit ||= Visit.create :user_id => user_id,
22
+ :session_id => session_id,
23
+ :remote_host => remote_host
24
+
25
+ hit = visit.hits.build :url => url,
26
+ :http_method => http_method,
27
+ :action => action,
28
+ :params => params
29
+
30
+ hit.note = doing
31
+ hit.json_data = json_data
32
+ hit.save!
33
+
34
+ end
35
+ end
36
+ end
37
+
38
+ if defined? ActionController::Base
39
+ ActionController::Base.class_eval do
40
+ include Saw::Controller
41
+ end
42
+ end
@@ -0,0 +1,6 @@
1
+ class Hit < ActiveRecord::Base
2
+ default_scope order('id')
3
+ serialize :json_data, JSON
4
+ attr_accessible :url, :http_method, :action, :params
5
+ belongs_to :visit
6
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :visits
3
+ has_many :hits, :through => :visits
4
+ end
@@ -0,0 +1,3 @@
1
+ module Saw
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,45 @@
1
+ class Visit < ActiveRecord::Base
2
+ default_scope order('id')
3
+
4
+ attr_accessible :user_id, :session_id, :remote_host
5
+
6
+ has_many :hits
7
+ belongs_to :user
8
+
9
+ def title
10
+ "#{lasts} on #{created_at.strftime('%b %-d %Y, %l %P')}"
11
+ end
12
+
13
+ def lasts
14
+ str = ''
15
+
16
+ last_hit = hits.last
17
+ diff = last_hit ? last_hit.created_at - created_at : 1
18
+
19
+ hours_diff = (diff/1.hour).round
20
+ if hours_diff > 0
21
+ str << "#{format('%02d', hours_diff)}"
22
+ diff = diff - hours_diff.hours
23
+ else
24
+ str << "00"
25
+ end
26
+
27
+ minutes_diff = (diff/1.minute).round
28
+ if minutes_diff > 0
29
+ str << ":#{format('%02d', minutes_diff)}"
30
+ diff = diff - minutes_diff.minutes
31
+ else
32
+ str << ":00"
33
+ end
34
+
35
+ seconds_diff = (diff/1.second).round
36
+ if seconds_diff > 0
37
+ str << ":#{format('%02d', seconds_diff)}"
38
+ #diff = diff - seconds_diff.seconds
39
+ else
40
+ str << ":00"
41
+ end
42
+
43
+ str
44
+ end
45
+ end
@@ -0,0 +1,6 @@
1
+ class VisitsController < ApplicationController
2
+ def create
3
+ saw params[:doing]
4
+ head :ok
5
+ end
6
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'saw/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "saw"
8
+ gem.version = Saw::VERSION
9
+ gem.authors = ["Amol Pujari"]
10
+ gem.email = ["amolpujari@gmail.com"]
11
+ gem.description = %q{I saw user clicking on that button there.}
12
+ gem.summary = %q{User visits, hits tracking for Ruby on Rails}
13
+ gem.homepage = %q{https://github.com/amolpujari/saw}
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_dependency("rails", ">= 3.0.0")
21
+ end
@@ -0,0 +1,6 @@
1
+ class VisitsController < ApplicationController
2
+ def create
3
+ saw params[:doing]
4
+ head :ok
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: saw.sqlite3
4
+ timeout: 5000
@@ -0,0 +1,10 @@
1
+ # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
+ amol:
3
+ id: 1
4
+ name: Amol
5
+ Hridhaan:
6
+ id: 2
7
+ name: Hridhaan
8
+ Renuka:
9
+ id: 3
10
+ name: Renuka
@@ -0,0 +1,39 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../..')
2
+ $:.unshift(File.dirname(__FILE__) + '/../../lib')
3
+ schema_file = File.join(File.dirname(__FILE__), '..', 'schema.rb')
4
+
5
+ ENV["RAILS_ENV"] = "test"
6
+
7
+ require 'rubygems'
8
+ require 'test/unit'
9
+ require 'active_record'
10
+ require 'active_record/fixtures'
11
+ require 'active_support'
12
+ require 'active_support/test_case'
13
+ require 'action_controller'
14
+ require 'action_controller/test_case'
15
+ #require 'action_controller/test_process'
16
+ #require 'action_controller/integration'
17
+ #require 'init'
18
+
19
+ config = YAML::load(IO.read(File.join(File.dirname(__FILE__), '..', 'database.yml')))[ENV['DB'] || 'test']
20
+ ActiveRecord::Base.configurations = config
21
+ ActiveRecord::Base.establish_connection(config)
22
+
23
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/models.log")
24
+ ActionController::Base.logger = Logger.new(File.dirname(__FILE__) + "/controllers.log")
25
+
26
+ load(schema_file) if File.exist?(schema_file)
27
+
28
+ if ActiveSupport::TestCase.method_defined?(:fixture_path)
29
+ Test::Unit::TestCase.fixture_path = File.join(File.dirname(__FILE__), '..', 'fixtures')
30
+ $:.unshift(Test::Unit::TestCase.fixture_path)
31
+ end
32
+
33
+ class Test::Unit::TestCase #:nodoc:
34
+ # Turn off transactional fixtures if you're working with MyISAM tables in MySQL
35
+ # self.use_transactional_fixtures = true
36
+
37
+ # Instantiated fixtures are slow
38
+ # self.use_instantiated_fixtures = true
39
+ end
@@ -0,0 +1,6 @@
1
+ class Hit < ActiveRecord::Base
2
+ default_scope order('id')
3
+ serialize :json_data, JSON
4
+ attr_accessible :url, :http_method, :action, :params
5
+ belongs_to :visit
6
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :visits
3
+ has_many :hits, :through => :visits
4
+ end
@@ -0,0 +1,45 @@
1
+ class Visit < ActiveRecord::Base
2
+ default_scope order('id')
3
+
4
+ attr_accessible :user_id, :session_id, :remote_host
5
+
6
+ has_many :hits
7
+ belongs_to :user
8
+
9
+ def title
10
+ "#{lasts} on #{created_at.strftime('%b %-d %Y, %l %P')}"
11
+ end
12
+
13
+ def lasts
14
+ str = ''
15
+
16
+ last_hit = hits.last
17
+ diff = last_hit ? last_hit.created_at - created_at : 1
18
+
19
+ hours_diff = (diff/1.hour).round
20
+ if hours_diff > 0
21
+ str << "#{format('%02d', hours_diff)}"
22
+ diff = diff - hours_diff.hours
23
+ else
24
+ str << "00"
25
+ end
26
+
27
+ minutes_diff = (diff/1.minute).round
28
+ if minutes_diff > 0
29
+ str << ":#{format('%02d', minutes_diff)}"
30
+ diff = diff - minutes_diff.minutes
31
+ else
32
+ str << ":00"
33
+ end
34
+
35
+ seconds_diff = (diff/1.second).round
36
+ if seconds_diff > 0
37
+ str << ":#{format('%02d', seconds_diff)}"
38
+ #diff = diff - seconds_diff.seconds
39
+ else
40
+ str << ":00"
41
+ end
42
+
43
+ str
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'helpers/unit_test_helper'
4
+ require 'models/user'
5
+ require 'models/visit'
6
+ require 'models/hit'
7
+
8
+ # ActionController::Routing::Routes.draw do |map|
9
+ # post "visits" => "visits#create"#, :defaults => { :format => "json"}
10
+ # end
11
+
12
+ class SawTests < Test::Unit::TestCase # :nodoc:
13
+ fixtures :users, :visits, :hits
14
+
15
+ def setup
16
+ end
17
+
18
+ end
@@ -0,0 +1,24 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table "users", :force => true do |t|
3
+ t.string "name"
4
+ end
5
+
6
+ create_table "visits", :force => true do |t|
7
+ t.integer "user_id"
8
+ t.string "session_id"
9
+ t.string "user_agent"
10
+ t.string "remote_host"
11
+ t.datetime "created_at"
12
+ end
13
+
14
+ create_table "hits", :force => true do |t|
15
+ t.integer "visit_id"
16
+ t.string "url"
17
+ t.string "http_method"
18
+ t.string "action"
19
+ t.text "params"
20
+ t.string "note"
21
+ t.text "json_data"
22
+ t.datetime "created_at"
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ # Detects the current version of Rails that is being used
2
+ #
3
+ #
4
+ unless defined? RAILS_VERSION_FILE
5
+ RAILS_VERSION_FILE = File.expand_path("../../../.rails-version", __FILE__)
6
+ end
7
+
8
+ unless defined? TRAVIS_CONFIG
9
+ require 'psych'
10
+ filename = File.expand_path("../../../.travis.yml", __FILE__)
11
+ TRAVIS_CONFIG = Psych.load File.read filename
12
+ TRAVIS_RAILS_VERSIONS = TRAVIS_CONFIG['env'].grep(/RAILS=(.*)/){ $1 }.sort
13
+ end
14
+
15
+ unless defined? DEFAULT_RAILS_VERSION
16
+ DEFAULT_RAILS_VERSION = TRAVIS_RAILS_VERSIONS.last
17
+ end
18
+
19
+ def detect_rails_version
20
+ version = version_from_file || version_from_env || DEFAULT_RAILS_VERSION
21
+
22
+ puts "Detected Rails: #{version}" if ENV['DEBUG']
23
+
24
+ version
25
+ end
26
+
27
+ def version_from_file
28
+ if File.exists?(RAILS_VERSION_FILE)
29
+ version = File.read(RAILS_VERSION_FILE).chomp.strip
30
+ version = nil if version == ""
31
+
32
+ version
33
+ end
34
+ end
35
+
36
+ def version_from_env
37
+ ENV['RAILS']
38
+ end
39
+
40
+ def write_rails_version(version)
41
+ File.open(RAILS_VERSION_FILE, "w+"){|f| f << version }
42
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saw
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Amol Pujari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ! '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ! '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ description: I saw user clicking on that button there.
28
+ email:
29
+ - amolpujari@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - LICENSE.txt
37
+ - README.md
38
+ - Rakefile
39
+ - examples/active_admin/saw.rb
40
+ - lib/generators/saw/USAGE
41
+ - lib/generators/saw/saw_generator.rb
42
+ - lib/generators/saw/templates/saw_migration.rb.erb
43
+ - lib/saw.rb
44
+ - lib/saw/controller.rb
45
+ - lib/saw/hit.rb
46
+ - lib/saw/user_addition.rb
47
+ - lib/saw/version.rb
48
+ - lib/saw/visit.rb
49
+ - lib/saw/visits_controller.rb
50
+ - saw.gemspec
51
+ - test/controllers/visits_controller.rb
52
+ - test/database.yml
53
+ - test/fixtures/users.yml
54
+ - test/helpers/unit_test_helper.rb
55
+ - test/models/hit.rb
56
+ - test/models/user.rb
57
+ - test/models/visit.rb
58
+ - test/saw_test.rb
59
+ - test/schema.rb
60
+ - test/support/detect_rails_version.rb
61
+ homepage: https://github.com/amolpujari/saw
62
+ licenses: []
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.3
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: User visits, hits tracking for Ruby on Rails
84
+ test_files:
85
+ - test/controllers/visits_controller.rb
86
+ - test/database.yml
87
+ - test/fixtures/users.yml
88
+ - test/helpers/unit_test_helper.rb
89
+ - test/models/hit.rb
90
+ - test/models/user.rb
91
+ - test/models/visit.rb
92
+ - test/saw_test.rb
93
+ - test/schema.rb
94
+ - test/support/detect_rails_version.rb