alarm_it 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,5 @@
1
+ *.tmproj
2
+ .DS_Store
3
+ Gemfile.lock
4
+ .bundle
5
+ spec/database.yml
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --backtrace
File without changes
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2007 Michael Bleigh and Intridea Inc.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,33 @@
1
+ = AlarmIt
2
+
3
+ This plugin is a work in progress.
4
+ it allows the attachment of alarms to any kind of models.
5
+
6
+ === Rails 3.0
7
+
8
+ To use it, add it to your Gemfile:
9
+
10
+ gem 'alarm_it'
11
+
12
+ ==== Post Installation
13
+
14
+ 1. rails generate alarm_it:migration
15
+ 2. rake db:migrate
16
+
17
+ == Testing
18
+
19
+ Acts As Alarmable On uses RSpec for its test coverage. Inside the plugin
20
+ directory, you can run the specs for RoR 3.0.0 with:
21
+
22
+ rake spec
23
+
24
+
25
+ == Usage
26
+
27
+ class User < ActiveRecord::Base
28
+ acts_as_alarmer
29
+ end
30
+
31
+ class AModel < ActiveRecord::Base
32
+ acts_as_alarmable
33
+ end
@@ -0,0 +1,18 @@
1
+ begin
2
+ require 'rspec/core/rake_task'
3
+
4
+ desc 'Default: run specs'
5
+ task :default => :spec
6
+ RSpec::Core::RakeTask.new do |t|
7
+ t.pattern = "spec/**/*_spec.rb"
8
+ end
9
+
10
+ RSpec::Core::RakeTask.new('rcov') do |t|
11
+ t.pattern = "spec/**/*_spec.rb"
12
+ t.rcov = true
13
+ t.rcov_opts = ['--exclude', 'spec']
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Rspec not available. Install it with: gem install rspec"
18
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "alarm_it"
3
+ s.version = '0.0.0'
4
+ # s.platform = Gem::Platform::RUBY
5
+
6
+ s.authors = ["Yannis Jaquet"]
7
+ s.date = %q{2011-05-24}
8
+ s.description = %q{Associate alarms to any model}
9
+ s.summary = %q{Associate alarms to any model}
10
+ s.email = %q{suvar3_7@mac.com}
11
+
12
+ s.files = `git ls-files`.split("\n")
13
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
14
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
15
+ s.require_paths = ["lib"]
16
+
17
+ s.add_dependency "rails", "~> 3.1.0.rc1"
18
+ s.add_dependency "mysql2", "~> 0.3.2"
19
+ s.add_dependency "will_paginate", "~>3.0.pre2"
20
+
21
+ s.add_development_dependency "rspec", "~> 2.6.0"
22
+ s.add_development_dependency 'shoulda-matchers'
23
+ s.add_development_dependency 'factory_girl'
24
+ end
25
+
File without changes
@@ -0,0 +1,7 @@
1
+ class AlarmItMigrationGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.migration_template 'migration.rb', 'db/migrate', :migration_file_name => "alarm_it_migration"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ class AlarmItMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :alarms do |t|
4
+ t.string :name
5
+ t.text :description
6
+ t.reference :alarmable, :polymorphic => true
7
+ t.datetime :set_at
8
+ t.datetime :repeat_at
9
+ t.datetime :inactivated_at
10
+ t.reference :alarmer, :polymorphic => true
11
+
12
+ t.timestamps
13
+ end
14
+ add_index :alarms, [:alarmable_type, :alarmable_id]
15
+ add_index :alarms, [:alarmer_type, :alarmer_id]
16
+ end
17
+
18
+ def self.down
19
+ drop_table :alarms
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ require "active_record"
2
+ require "action_view"
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+
6
+ require "alarm_it/alarmable"
7
+ require "alarm_it/alarmer"
8
+ require "alarm_it/alarm"
9
+ require "alarm_it/alarms_helper"
10
+
11
+ $LOAD_PATH.shift
12
+
13
+ if defined?(ActiveRecord::Base)
14
+ ActiveRecord::Base.send :include, AlarmIt::Alarmer
15
+ ActiveRecord::Base.send :include, AlarmIt::Alarmable
16
+ end
17
+
18
+ if defined?(ActionView::Base)
19
+ ActionView::Base.send :include, AlarmIt::AlarmsHelper
20
+ end
@@ -0,0 +1,62 @@
1
+ module AlarmIt
2
+ class Alarm < ::ActiveRecord::Base
3
+
4
+ attr_accessible :name, :set_at, :repeat_at, :alarmable, :alarmable_type, :alarmable_id, :alarmer, :alarmer_type, :alarmer_id, :inactivated_at
5
+
6
+ belongs_to :alarmable, :polymorphic => true
7
+ belongs_to :alarmer, :polymorphic => true
8
+
9
+ validates_presence_of :name, :alarmable_type, :alarmable_id, :alarmer_type, :alarmer_id, :set_at
10
+
11
+ validates_each :repeat_at do |alarmable, attr, value|
12
+ if !alarmable.set_at.blank? and !alarmable.repeat_at.blank? and alarmable.repeat_at < alarmable.set_at
13
+ alarmable.errors[:repeat_at] < ": anterior to set time!"
14
+ end
15
+ end
16
+
17
+ before_validation :set_set_at
18
+
19
+ default_scope order("alarms.set_at ASC")
20
+ scope :inactivated, order("set_at DESC, repeat_at DESC").where("inactivated_at IS NOT NULL")
21
+ scope :pending, order("set_at DESC, repeat_at DESC").where("inactivated_at IS NULL")
22
+ scope :ringing, order("set_at DESC, repeat_at DESC").where("inactivated_at IS NULL AND (set_at < UTC_TIMESTAMP() OR (repeat_at IS NOT NULL AND repeat_at < UTC_TIMESTAMP()))")
23
+
24
+ def symbols
25
+ "⚠" if ringing?
26
+ end
27
+
28
+
29
+ def inactivate!
30
+ update_attributes(:inactivated_at => Time.current)
31
+ end
32
+
33
+ def is_pending?
34
+ inactivated_at.blank? and (set_at > Time.current or (repeat_at.present? and repeat_at > Time.current))
35
+ end
36
+
37
+ def is_inactivated?
38
+ inactivated_at.present? and inactivated_at < Time.current
39
+ end
40
+
41
+ def is_ringing?
42
+ inactivated_at.blank? and (set_at < Time.current or (repeat_at.present? and repeat_at < Time.current))
43
+ end
44
+
45
+ def inactivated_at_bool=(inactivated_at_bool)
46
+ if inactivated_at_bool.nil?
47
+ self.inactivated_at = ''
48
+ elsif self.inactivated_at.blank?
49
+ self.inactivated_at = Time.current
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def set_set_at
56
+ if self.set_at.blank? and !self.repeat_at.blank?
57
+ self.set_at = self.repeat_at
58
+ self.repeat_at = nil
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ module AlarmIt
2
+ module Alarmable
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_as_alarmable
9
+ has_many :alarms, :as => :alarmable, :dependent => :destroy, :include => :alarmer, :class_name => "AlarmIt::Alarm"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,21 @@
1
+ module AlarmIt
2
+ module Alarmer
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_as_alarmer
9
+ has_many :alarms, :as => :alarmer, :dependent => :destroy, :include => :alarmable, :class_name => "AlarmIt::Alarm"
10
+
11
+ include AlarmIt::Alarmer::InstanceMethods
12
+ end
13
+ end
14
+
15
+ module InstanceMethods
16
+ def set_alarm(name, alarmable, datetime)
17
+ alarms.create!(:name => name, :alarmable => alarmable, :set_at => datetime)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,17 @@
1
+ module AlarmIt
2
+ module AlarmsHelper
3
+ # See the README for an example using tag_cloud.
4
+ # def tag_cloud(tags, classes)
5
+ # tags = tags.all if tags.respond_to?(:all)
6
+ #
7
+ # return [] if tags.empty?
8
+ #
9
+ # max_count = tags.sort_by(&:count).last.count.to_f
10
+ #
11
+ # tags.each do |tag|
12
+ # index = ((tag.count / max_count) * (classes.size - 1)).round
13
+ # yield tag, classes[index]
14
+ # end
15
+ # end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module AlarmIt
2
+ VERSION = "0.0.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module AlarmIt
4
+ class MigrationGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+
7
+ desc "Generates migration for Alarm"
8
+
9
+ def self.orm
10
+ Rails::Generators.options[:rails][:orm]
11
+ end
12
+
13
+ def self.source_root
14
+ File.join(File.dirname(__FILE__), 'templates', (orm.to_s unless orm.class.eql?(String)) )
15
+ end
16
+
17
+ def self.orm_has_migration?
18
+ [:active_record].include? orm
19
+ end
20
+
21
+ def self.next_migration_number(path)
22
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
23
+ end
24
+
25
+ def url(/assets/
26
+ if self.class.orm_has_migration?
27
+ migration_template 'migration.rb', 'db/migrate/alarm_it_migration'
28
+ end
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,21 @@
1
+ class AlarmItMigration < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :alarms do |t|
4
+ t.string :name
5
+ t.text :description
6
+ t.reference :alarmable, :polymorphic => true
7
+ t.datetime :set_at
8
+ t.datetime :repeat_at
9
+ t.datetime :inactivated_at
10
+ t.reference :alarmer, :polymorphic => true
11
+
12
+ t.timestamps
13
+ end
14
+ add_index :alarms, [:alarmable_type, :alarmable_id]
15
+ add_index :alarms, [:alarmer_type, :alarmer_id]
16
+ end
17
+
18
+ def self.down
19
+ drop_table :alarms
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require 'alarm_it'
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ describe AlarmIt::Alarm do
3
+ before(:each) do
4
+ clean_database!
5
+ @alarmer = Alarmer.create(:name => 'pablo')
6
+ @alarmable = Alarmable.create(:name => 'thiz_obzect')
7
+ end
8
+
9
+ it { should belong_to(:alarmable) }
10
+ it { should belong_to(:alarmer) }
11
+ it { should validate_presence_of :alarmable_type }
12
+ it { should validate_presence_of :alarmable_id }
13
+ it { should validate_presence_of :set_at }
14
+ it { should validate_presence_of :alarmer_id }
15
+ it { should validate_presence_of :alarmer_type }
16
+
17
+ describe "an alarm" do
18
+ let(:alarm){AlarmIt::Alarm.create(:name => 'alarm', :alarmer => @alarmer, :alarmable => @alarmable, :set_at => 2.days.from_now)}
19
+ specify {alarm.alarmer.should eql(@alarmer)}
20
+ specify {alarm.alarmable.should eql(@alarmable)}
21
+ specify {alarm.should(be_valid, alarm.errors.full_messages.to_sentence)}
22
+ specify {alarm.should be_an_instance_of(AlarmIt::Alarm)}
23
+ specify {alarm.should respond_to(:alarmer)}
24
+ specify {alarm.should respond_to(:alarmable)}
25
+ it "should be among the pending alarms" do
26
+ AlarmIt::Alarm.pending.include?(alarm)
27
+ end
28
+ it "should be pending" do
29
+ alarm.is_pending?.should be_true
30
+ end
31
+
32
+ it "should not be inactivated" do
33
+ alarm.is_inactivated?.should_not be_true
34
+ end
35
+
36
+ it "should not be ringing" do
37
+ alarm.is_ringing?.should_not be_true
38
+ end
39
+
40
+ describe "when it's inactivated" do
41
+ before :each do
42
+ alarm.inactivate!
43
+ end
44
+
45
+ it "should be among the inactivated alarms" do
46
+ AlarmIt::Alarm.inactivated.include?(alarm)
47
+ end
48
+
49
+ it "should be inactivated" do
50
+ alarm.is_inactivated?.should be_true
51
+ end
52
+
53
+ it "should not be pending" do
54
+ alarm.is_pending?.should_not be_true
55
+ end
56
+
57
+ it "should not be ringing" do
58
+ alarm.is_ringing?.should_not be_true
59
+ end
60
+ end
61
+
62
+ describe "with set_at time in the past" do
63
+ before :each do
64
+ alarm.update_attributes(:set_at => 2.hours.ago)
65
+ end
66
+
67
+ it "should be among the ringing alarms" do
68
+ AlarmIt::Alarm.ringing.include?(alarm)
69
+ end
70
+
71
+ it "should be ringing" do
72
+ alarm.is_ringing?.should be_true
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+ describe Alarmable do
3
+ before(:each) do
4
+ clean_database!
5
+ end
6
+
7
+ it { should have_many(:alarms) }
8
+ end