Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit 7f6ee23387b0862fcccb05a6bbaeeebfe86db081 @shenoudab committed Jun 13, 2010
@@ -0,0 +1,4 @@
+.bundle/
+log/*.log
+pkg/
+test/
@@ -0,0 +1,4 @@
+source "http://gemcutter.org"
+
+gem 'rails', "3.0.0.beta4"
+gem 'mysql'
@@ -0,0 +1,20 @@
+Copyright 2010 mobiThought
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,34 @@
+= devise_traceable
+
+It adds support to devise[http://github.com/plataformatec/devise] for traceing.
+
+== Installation
+
+All gems are on gemcutter, so you need to add gemcutter to your sources if you haven’t yet:
+
+ sudo gem sources -a http://gemcutter.org/
+
+Install devise_traceable gem, it should install dependencies (such as devise and warden):
+
+ sudo gem install devise_traceable
+
+Configure devise_traceable inside your app (and warden and devise if you weren't using them):
+
+ config.gem 'warden'
+ config.gem 'devise'
+ config.gem 'devise_traceable'
+
+== Note on Patches/Pull Requests
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but
+ bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+== Copyright
+
+Copyright (c) 2010 mobiThought. See LICENSE for details.
@@ -0,0 +1,42 @@
+# encoding: UTF-8
+require 'rake'
+require 'rake/rdoctask'
+require 'rake/gempackagetask'
+require 'rake/testtask'
+require File.join(File.dirname(__FILE__), 'lib', 'devise_traceable', 'version')
+
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib'
+ t.libs << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = false
+end
+
+task :default => :test
+
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'DeviseTraceable'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README.rdoc')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+spec = Gem::Specification.new do |s|
+ s.name = "devise_traceable"
+ s.summary = "Devise Traceable"
+ s.description = "Devise Traceable"
+ s.files = FileList["[A-Z]*", "{app,config,lib}/**/*"]
+ s.version = DeviseTraceable::VERSION.dup
+ s.email = "sbertel@mobithought.com"
+ s.homepage = "http://github.com/shenoudab/devise_traceable"
+ s.author = 'Shenouda Bertel'
+end
+
+Rake::GemPackageTask.new(spec) do |pkg|
+end
+
+desc "Install the gem #{spec.name}-#{spec.version}.gem"
+task :install do
+ system("gem install pkg/#{spec.name}-#{spec.version}.gem --no-ri --no-rdoc")
+end
@@ -0,0 +1,6 @@
+unless defined?(Devise)
+ require 'devise'
+end
+
+require 'devise_traceable/model'
+require 'devise_traceable/rails'
@@ -0,0 +1,16 @@
+# After each sign in, sign out.
+# This is only triggered when the user is explicitly set (with set_user)
+# and on authentication. Retrieving the user from session (:fetch) does
+# not trigger it.
+
+Warden::Manager.after_set_user :except => :fetch do |record, warden, options|
+ if record.respond_to?(:insert_login!) #&& warden.authenticated?(options[:scope])
+ record.insert_login!(warden.request)
+ end
+end
+
+Warden::Manager.before_logout do |record, auth, opts|
+ if record.respond_to?(:update_logout!) #&& warden.authenticated?(options[:scope])
+ record.update_logout!(warden.request)
+ end
+end
@@ -0,0 +1,29 @@
+require 'devise_traceable/hooks/traceable'
+
+module Devise
+ module Models
+ # Trace information about your user sign in. It tracks the following columns:
+
+ # * resource_id
+ # * sign_in_at
+ # * sign_out_at
+ # * time
+ #
+
+ module Traceable
+ def insert_login!(request)
+ new_current = Time.now
+ self.sign_in_at = new_current
+ save(:validate => false)
+ end
+
+ def update_logout!(request)
+ new_current = Time.now
+ self.sign_out_at = new_current
+ time = self.sign_out_at - self.sign_in_at
+ self.time = time
+ save(:validate => false)
+ end
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module DeviseTraceable
+ class Engine < ::Rails::Engine
+
+ end
+end
@@ -0,0 +1,3 @@
+module DeviseTraceable
+ VERSION = "0.0.1".freeze
+end
@@ -0,0 +1,31 @@
+require 'rails/generators/migration'
+
+class DeviseTraceableGenerator < Rails::Generators::NamedBase
+ include Rails::Generators::Migration
+
+ desc "Generates a model with the given NAME (if one does not exist) with devise " <<
+ "configuration plus a migration file and devise routes."
+
+ def self.source_root
+ @_devise_source_root ||= File.expand_path("../templates", __FILE__)
+ end
+
+ def self.orm_has_migration?
+ Rails::Generators.options[:rails][:orm] == :active_record
+ end
+
+ def self.next_migration_number(dirname)
+ if ActiveRecord::Base.timestamped_migrations
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
+ else
+ "%.3d" % (current_migration_number(dirname) + 1)
+ end
+ end
+
+ class_option :orm
+ class_option :migration, :type => :boolean, :default => orm_has_migration?
+
+ def create_migration_file
+ migration_template 'migration.rb', "db/migrate/devise_create_#{name.tableize}_tracing.rb"
+ end
+end
@@ -0,0 +1,18 @@
+class DeviseCreate<%= table_name.camelize %>Tracing < ActiveRecord::Migration
+ def self.up
+ create_table :<%= table_name %>_tracing do |t|
+ t.integer :<%= table_name.classify.foreign_key %>
+ t.datetime :sign_in_at
+ t.datetime :sign_out_at
+ #Any additional fields here
+
+ t.timestamps
+end
+
+add_index :<%= table_name %>_tracing, :<%= table_name.classify.foreign_key %>
+end
+
+def self.down
+ drop_table :<%= table_name %>
+end
+end

0 comments on commit 7f6ee23

Please sign in to comment.