-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Florent Ruard-Dumaine
committed
Mar 28, 2010
0 parents
commit 3a33fa6
Showing
19 changed files
with
462 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Copyright (c) 2009 [name of plugin creator] | ||
|
||
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. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Signals | ||
======= | ||
|
||
BE CAREFUL in Beta yet!! | ||
|
||
in application layout add, after javascripts include tag : | ||
|
||
<%= activeSignalsJavascript %> | ||
|
||
or if you don't want default timeout | ||
|
||
<%= activeSignalsJavascript {:timeActive => XX, :timeInactive => XXXX} %> | ||
|
||
After you can send event in your code with : | ||
|
||
SignalEvent.emit(target, paramsevent); | ||
|
||
|
||
MORE DOCUMENTATION must be provided later!!! | ||
|
||
|
||
|
||
|
||
Copyright (c) 2009-2010 Florent Ruard-Dumaine, released under the MIT license |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'rake' | ||
require 'rake/testtask' | ||
require 'rake/rdoctask' | ||
|
||
desc 'Default: run unit tests.' | ||
task :default => :test | ||
|
||
desc 'Test the signals plugin.' | ||
Rake::TestTask.new(:test) do |t| | ||
t.libs << 'lib' | ||
t.libs << 'test' | ||
t.pattern = 'test/**/*_test.rb' | ||
t.verbose = true | ||
end | ||
|
||
desc 'Generate documentation for the signals plugin.' | ||
Rake::RDocTask.new(:rdoc) do |rdoc| | ||
rdoc.rdoc_dir = 'rdoc' | ||
rdoc.title = 'Signals' | ||
rdoc.options << '--line-numbers' << '--inline-source' | ||
rdoc.rdoc_files.include('README') | ||
rdoc.rdoc_files.include('lib/**/*.rb') | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class SignalsController < ApplicationController | ||
|
||
|
||
def check | ||
#integrate base check | ||
sigs = {:u => []} | ||
begin | ||
unless current_user.nil? | ||
begin | ||
SignalEvent.check(current_user).each do |signal| | ||
sigs[:u].push signal.to_struct | ||
end | ||
rescue => e | ||
RAILS_DEFAULT_LOGGER.debug('Plugin Signals Error: ' + e.message) | ||
sigs[:e] = ['error '+e.message] | ||
end | ||
|
||
SignalsLib.singleton_methods.each do |meth| | ||
begin | ||
SignalsLib.send(meth, current_user).each do |signal| | ||
sigs[:u].push signal.to_struct | ||
end | ||
rescue => e | ||
RAILS_DEFAULT_LOGGER.debug('Plugin Signals Error SignalsLib : ' + e.message) | ||
sigs[:e] = ['error '+e.message] | ||
end | ||
end | ||
end | ||
rescue => e | ||
end | ||
|
||
sigs[:t] = Time.new.utc.to_i | ||
render :json => {:signals => sigs}.to_json | ||
end | ||
|
||
|
||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module SignalsHelper | ||
def activeSignalsJavascript(options = nil) | ||
options = options || {} | ||
javascript_include_tag('signals', :plugin => 'signals')+ | ||
'<script type="text/javascript">Event.observe(window, \'load\', function(e){Signals.init(\''+controller.relative_url_root+'\', {'+options.collect {|key, value| "#{key.to_s}: #{value}"}.join(', ')+'});});</script>' | ||
end | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
class SignalEvent < ActiveRecord::Base | ||
set_table_name 'signals' | ||
belongs_to :signalable, :polymorphic => true | ||
serialize :params | ||
@@end_in = 5.minutes.from_now | ||
|
||
def self.check(instance) | ||
signalhistories = SignalHistory.find(:all, :conditions => ['user_id = ? AND signalable_type = ? AND signalable_id = ? AND end_at < ?', current_user.id, instance.class.name, instance.id, Time.new]) | ||
msids = '' | ||
msids = ' AND id NOT IN ('+signalhistories.collect{|sh|sh.signal_id}.join(',')+') ' if signalhistories.size > 0 ##don't take already taked multiple signals | ||
|
||
signals = SignalEvent.find(:all, {:conditions => ['signalable_type = ? AND signalable_id = ? AND end_at < ? '+msids, instance.class.name, instance.id, Time.new]}) | ||
sids = signals.collect{|s| (s.multiple ? nil : s.id)}.delete_if{|tid|tid.nil?}.join(',') | ||
sids = ' OR id IN ('+sids+') ' unless sids.empty? | ||
signals.each do |ms| | ||
next if !ms.multiple | ||
begin | ||
sh = SignalHistory.new | ||
sh.signalable = ms.signalable | ||
sh.signal_id = ms.id | ||
sh.user_id = current_user.id | ||
sh.end_at = ms.end_at | ||
sh.save | ||
rescue => e | ||
RAILS_DEFAULT_LOGGER.debug('SignalHistory add error : '+e.message) | ||
end | ||
end | ||
|
||
SignalEvent.connection.execute( "DELETE FROM signals WHERE end_at < '#{Time.new.utc}' #{sids};") # clean all old and all finded | ||
SignalHistory.connection.execute("DELETE FROM signal_histories WHERE end_at < '#{Time.new.utc}';") # clean all old histories | ||
signals | ||
end | ||
|
||
# def after_find | ||
|
||
# (self.destroy) if( self.multiple == false || self.end_date < Time.new) | ||
|
||
# end | ||
|
||
def self.emit(instance, options) | ||
s = SignalEvent.new | ||
options = options ||{} | ||
s.signalable = instance | ||
s.end_at = (options[:end_in] || @@end_in) | ||
s.multiple = options[:multiple] unless options[:multiple].nil? | ||
s.params = options[:params] unless options[:params].nil? | ||
s.crud = (options[:crud].nil? ? 'update' : options[:crud].to_s) | ||
s.qname = options[:qName] | ||
|
||
extracond = ( s.params == nil ? ' params IS NULL ' : ' params = ? ') | ||
conditions = ['signalable_type = ? AND signalable_id = ? AND end_at < ? AND qname = ? AND crud = ? AND multiple = ? AND '+extracond, instance.class.name, instance.id, Time.new, s.qname, s.crud, s.multiple] | ||
conditions.push s.params unless s.params.nil? | ||
|
||
sn = SignalEvent.find(:all, :conditions => conditions)#['signalable_type = ? AND signalable_id = ? AND end_at < ? AND qname = ? AND crud = ? AND multiple = ? AND '+extracond, instance.class.name, instance.id, Time.new, s.qname, s.crud, s.multiple, s.params]) | ||
if sn.size > 0 | ||
# update existants in this case | ||
# push end_at later | ||
sn.first.end_at = s.end_at | ||
sn.first.save | ||
else | ||
s.save | ||
end | ||
|
||
end | ||
|
||
def to_struct | ||
{:type => self.qname, :crud => self.crud, :params => self.params} | ||
end | ||
|
||
private | ||
def delete_now | ||
self.connection.execute("DELETE FROM signals WHERE id =#{self.id};") | ||
self.connection.execute("DELETE FROM signal_histories WHERE signal_id =#{self.id};") | ||
end | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class SignalHistory < ActiveRecord::Base | ||
belongs_to :signalable, :polymorphic => true | ||
|
||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
var Signals = {timeActive: 10, timeInactive: 120, delayInactive: 100 };//en seconde | ||
Signals.check = function(pe) { | ||
pe.stop(); | ||
new Ajax.Request(Signals.prefix+'/signals/check', { | ||
method:'get' | ||
}); | ||
}; | ||
|
||
Signals.inactive = function() { | ||
window.clearTimeout(Signals.checkMouseId); | ||
Signals.time = Signals.timeInactive; | ||
console.info("User became Inactive"); | ||
}; | ||
Signals.active = function() { | ||
window.clearTimeout(Signals.checkMouseId); | ||
if(Signals.time != Signals.timeActive) { | ||
Signals.time = Signals.timeActive; | ||
console.info("User became Active"); | ||
Signals.checkStart(); | ||
} | ||
Signals.checkMouse(); | ||
}; | ||
Signals.checkStart = function() { | ||
if (Signals.pe) | ||
Signals.pe.stop(); | ||
Signals.pe = new PeriodicalExecuter(Signals.check, Signals.time); | ||
}; | ||
Signals.checkMouse = function() { | ||
Signals.checkMouseId = Signals.inactive.delay(Signals.delayInactive); | ||
} | ||
|
||
Signals.init = function(pref, options) { | ||
options = options || {}; | ||
if(options['timeActive']) | ||
Signals.timeActive = options['timeActive']; | ||
if(options['timeInactive']) | ||
Signals.timeInactive = options['timeInactive']; | ||
|
||
Signals.time = Signals.timeActive; | ||
Signals.prefix = pref || ''; | ||
|
||
Ajax.Responders.register({ | ||
onCreate: function() { | ||
Signals.pe.stop(); | ||
}, | ||
onComplete: function(request ,response) { | ||
if (response.responseJSON) { | ||
if (response.responseJSON.signals) { | ||
Signals.signals(response.responseJSON.signals); | ||
delete response.responseJSON.signals; | ||
} | ||
|
||
} | ||
Signals.checkStart(); | ||
} | ||
}); | ||
Signals.checkStart(); | ||
Signals.checkMouse(); | ||
Event.observe(document, 'keydown', Signals.active); | ||
Event.observe(document, 'mousemove', Signals.active); | ||
Event.observe(document, 'click', Signals.active); | ||
|
||
}; | ||
|
||
Signals.signals = function(jsond) { | ||
var signals = jsond; | ||
console.log(signals); | ||
|
||
if(signals['e']) { | ||
console.log('error ==> ' +signals['e']); | ||
} | ||
if(signals['u']) { | ||
signals['u'].each(function(signal) { | ||
if(signal['type']) { | ||
console.log('fireEvent signals:'+signal.type); | ||
document.fire('signals:'+signal.type, {params:signal.params, timeserver: signals['t'], crud: signal.crud}); | ||
} | ||
}); | ||
} | ||
|
||
Signals.checkStart(); | ||
}; | ||
Signals.registre = []; | ||
Signals.regEvent = function(ev, callback) { | ||
console.log('Document observe signals:'+ev); | ||
document.observe('signals:'+ev, callback); | ||
Signals.registre.push([ev, callback]); | ||
}; | ||
Signals.delEvent = function(ev, callback) { | ||
console.log('Document delete observe signals:'+ev); | ||
Event.stopObserve(document, 'signals:'+ev, callback); | ||
Signals.registre.without([ev, callback]); | ||
}; | ||
//clean event and callback | ||
Signals.clean = function() { | ||
Signals.registre.each(function(dev) { | ||
Event.stopObserve(document, 'signals:'+dev[0], dev[1]); | ||
}); | ||
Signals.registre = []; | ||
}; | ||
/* | ||
Signals.init(pref); | ||
OR | ||
Signals.init(pref, {timeActive:XX, timeInactive:XXXX}); | ||
*/ | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class SignalsGenerator < Rails::Generator::Base | ||
def manifest | ||
@migration_name = "Create SignalsMigrations" | ||
@migration_action = "add" | ||
record do |m| | ||
m.migration_template '2009051821120000_create_signals.rb',"db/migrate", :migration_file_name => "create_signals" | ||
end | ||
end | ||
end | ||
|
44 changes: 44 additions & 0 deletions
44
generators/signals/templates/2009051821120000_create_signals.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class CreateSignals < ActiveRecord::Migration | ||
|
||
def self.up | ||
create_table :signals, :force => true do |t| | ||
t.references :signalable, :polymorphic => true, :null_allowed => false | ||
t.string :crud, :limit => 10, :null_allowed => false | ||
t.string :qname, :null_allowed => false | ||
t.boolean :multiple, :default => false, :null_allowed => false | ||
t.string :params, :default => nil, :null_allowed => true | ||
t.timestamp :end_at, :null_allowed => false | ||
t.timestamps | ||
end | ||
|
||
add_index :signals, [:signalable_type, :signalable_id] | ||
add_index :signals, :end_at | ||
add_index :signals, :created_at | ||
add_index :signals, :qname | ||
|
||
|
||
create_table :signal_histories, :force => true do |t| | ||
t.references :signalable, :polymorphic => true, :null_allowed => false | ||
t.integer :signal_id | ||
t.integer :user_id | ||
t.timestamp :end_at , :null_allowed => false | ||
end | ||
|
||
add_index :signal_histories, [:user_id, :signalable_type, :signalable_id, :signal_id], :uniq => true | ||
add_index :signal_histories, :end_at | ||
end | ||
|
||
def self.down | ||
remove_index :signal_histories, [:user_id, :signalable_type, :signalable_id, :signal_id] | ||
remove_index :signal_histories, :end_at | ||
drop_table :signal_histories | ||
|
||
remove_index :signals, [:signalable_type, :signalable_id] | ||
remove_index :signals, :end_at | ||
remove_index :signals, :created_at | ||
remove_index :signals, :qname | ||
drop_table :signals | ||
end | ||
|
||
end | ||
|
21 changes: 21 additions & 0 deletions
21
generators/signals/templates/2009051913390000_create_signal_histories.rb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class CreateSignalHistories < ActiveRecord::Migration | ||
|
||
def self.up | ||
create_table :signal_histories, :force => true do |t| | ||
t.references :signalable, :polymorphic => true, :null_allowed => false | ||
t.integer :signal_id | ||
t.integer :user_id | ||
t.timestamp :end_at , :null_allowed => false | ||
end | ||
|
||
add_index :signal_histories, [:user_id, :signalable_type, :signalable_id, :signal_id], :uniq => true | ||
add_index :signal_histories, :end_at | ||
end | ||
|
||
def self.down | ||
remove_index :signal_histories, [:user_id, :signalable_type, :signalable_id, :signal_id] | ||
remove_index :signal_histories, :end_at | ||
drop_table :signal_histories | ||
end | ||
end | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Include hook code here | ||
require File.dirname(__FILE__)+'/lib/signals' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Install hook code here |
Oops, something went wrong.