crier 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb5415fa85424816676fe6ae83a4bc2eac4a52b4
4
+ data.tar.gz: 8f006ece5a90a38925788fbfb9a7db7f30fb0143
5
+ SHA512:
6
+ metadata.gz: 9f15a7d2b94be0ed17a1fad2a22747011b738c1f7edfbd250d9dc85a630982c67506b2d6732beb68922f6c4a677bc850fea07537304b09ce65cbe030b067c846
7
+ data.tar.gz: 3ee4e43692408be9e213ed14769d45c109d580c21c4bb0723214d633a32f66e9a1fb62ee0d6cbc519a85fa0b9f567a7d18f7c31ea02f090a5f248a0048d4441c
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ crier
2
+ =====
3
+
4
+ Simple notification system for the whole town
5
+
6
+ ## Installation
7
+
8
+ ### in your Gemfile
9
+
10
+ ```ruby
11
+ gem 'crier'
12
+ ```
13
+
14
+ ### at the command line
15
+
16
+ ```bash
17
+ rake crier:create_tables # not working yet
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ class User < ActiveRecord::Base
24
+ acts_as_crier
25
+ end
26
+ ```
27
+
28
+ ### Notification Attributes
29
+
30
+ * crier: Who did the crying
31
+ * subject: What they were crying about
32
+ * action: What action was being carried out
33
+ * scope: A way to group notifications for easy querying
34
+ * audience: An optional list of users who can see the notification
35
+ * metadata: A hash of metadata to go along with the notification (automatically includes :crier, :subject, :action)
36
+
37
+ ### Public Crying
38
+
39
+ ```ruby
40
+ # cry(message, metadata, audience)
41
+
42
+ someuser.cry("Hello, my name is.") # Shout about yourself
43
+ someuser.cry("The roof is on fire!", :subject => @house) # Shout about the house
44
+ someuser.cry("Bring out yer dead", :scope => :my_town) # Shout within a custom scope for finding
45
+ someuser.cry("Reticulating Splines", :action => :reticulated) # Shout with a specific action verb for use in the view
46
+ someuser.cry("This is why I'm hot!", :reasons => @reasons) # Shout with custom metadata
47
+ ```
48
+
49
+ ### Private Crying
50
+
51
+ Cries are public unless they have an audience.
52
+
53
+ ```ruby
54
+ someuser.cry("Lend me your ears", {}, @other_users) # Shout only to specific users
55
+ someuser.cry("Lend me your ears").to(@other_users) # Shout only to specific users, alternate syntax, non-transactional
56
+ someuser.cry("Lend me your ears").to_others(@other_users) # Shout only to specific users, alternate syntax, non-transactional, excludes crier from @other_users
57
+ ```
58
+
59
+ ### Listening
60
+
61
+ ```ruby
62
+ Crier::Notification.heard_by(some_user) # Get all the notifications the user heard
63
+ Crier::Notification.about(@record) # Get all the notifications with the given subject
64
+ Crier::Notification.in_scope(:my_town) # Get all the notifications within the given scope
65
+ ```
@@ -0,0 +1,35 @@
1
+ module Crier
2
+ module ActMethod
3
+ def acts_as_crier(options = {})
4
+ has_many :listenings, :class_name => 'Crier::Listening', :dependent => :delete_all
5
+ has_many :private_notifications, :class_name => 'Crier::Notification', :source => :notification, :through => :listenings
6
+
7
+ extend Crier::ClassMethods
8
+ include Crier::InstanceMethods
9
+ end
10
+ end
11
+
12
+ module ClassMethods
13
+ end
14
+
15
+ module InstanceMethods
16
+ def cry(message, metadata = {}, audience = nil)
17
+ Notification.create! do |n|
18
+ n.message = message
19
+ n.crier = metadata.delete(:crier) || self
20
+ n.subject = metadata.delete(:subject) || self
21
+ n.action = metadata.delete(:action)
22
+ n.metadata = metadata
23
+ n.audience = Array(audience)
24
+ n.scope = Crier::HelperMethods.scope_for(n.subject)
25
+ n.private = true if audience
26
+ end
27
+ end
28
+ end
29
+
30
+ module HelperMethods
31
+ def self.scope_for(record)
32
+ "#{record.class.name}##{record.id}"
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module Crier
2
+ # If a notification has listeners, the notification is considered private and only available to those listeners
3
+ class Listening < ActiveRecord::Base
4
+ self.table_name = 'crier_listenings'
5
+
6
+ belongs_to :notification
7
+ belongs_to :user
8
+
9
+ validates_presence_of :user_id, :notification_id
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module Crier
2
+ class Notification < ActiveRecord::Base
3
+ self.table_name = 'crier_notifications'
4
+
5
+ belongs_to :crier, :class_name => 'User'
6
+ belongs_to :subject, :polymorphic => true
7
+
8
+ has_many :listenings, :dependent => :delete_all
9
+ has_many :audience, :through => :listenings, :source => :user # If any, this constitutes a private audience for this notification
10
+
11
+ store :metadata
12
+
13
+ scope :heard_by, lambda {|user| joins("LEFT OUTER JOIN #{Listening.table_name} ON #{Listening.table_name}.notification_id = #{table_name}.id").where("#{Listening.table_name}.user_id = #{user.id} OR NOT private")}
14
+ scope :about, lambda {|subject| where(:subject_id => subject.id, :subject_type => subject.class) }
15
+ scope :in_scope, lambda {|scope| where(:scope => scope) }
16
+
17
+ # Shortcut for creating a private audience for this notification
18
+ def to(audience)
19
+ self.update_column(:private, true)
20
+ self.audience = (self.audience + Array(audience)).uniq
21
+
22
+ return self
23
+ end
24
+
25
+ def to_others(audience)
26
+ to(Array(audience) - [crier])
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Crier
2
+ VERSION = '0.0.1'
3
+ end
data/lib/crier.rb ADDED
@@ -0,0 +1,20 @@
1
+ # Load the crier files
2
+ require 'crier/acts_as_crier'
3
+ require 'crier/listening'
4
+ require 'crier/notification'
5
+
6
+ # FIXME: This doesn't seem to connect to the database correctly
7
+ # Load the rake tasks
8
+ # require 'rails'
9
+ # module Crier
10
+ # class Railtie < Rails::Railtie
11
+ # railtie_name :crier
12
+
13
+ # rake_tasks do
14
+ # load "tasks/crier_tasks.rake"
15
+ # end
16
+ # end
17
+ # end
18
+
19
+ # Load the act method
20
+ ActiveRecord::Base.send :extend, Crier::ActMethod
@@ -0,0 +1,32 @@
1
+ # desc "Explaining what the task does"
2
+
3
+ namespace :crier do
4
+ task :create_tables do
5
+ ActiveRecord::Migration.create_table Crier::Notification.table_name, :force => true do |t|
6
+ t.string :scope
7
+ t.text :message
8
+
9
+ t.integer :crier_id
10
+
11
+ t.string :subject_type
12
+ t.integer :subject_id
13
+
14
+ t.string :action
15
+
16
+ t.text :metadata
17
+
18
+ t.boolean :private, :null => false, :default => false
19
+ t.timestamps
20
+ end
21
+
22
+ ActiveRecord::Migration.add_index Crier::Notification.table_name, :scope
23
+
24
+ ActiveRecord::Migration.create_table Crier::Listening.table_name, :force => true do |t|
25
+ t.belongs_to :notification
26
+ t.belongs_to :user
27
+ end
28
+
29
+ ActiveRecord::Migration.add_index Crier::Listening.table_name, :notification_id
30
+ ActiveRecord::Migration.add_index Crier::Listening.table_name, :user_id
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crier
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Wallace
8
+ - Nicholas Jakobsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-01-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '3.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '3.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: sqlite3
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ description: Simple notification for the whole town
43
+ email:
44
+ - contact@culturecode.ca
45
+ executables: []
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - README.md
50
+ - lib/crier.rb
51
+ - lib/crier/acts_as_crier.rb
52
+ - lib/crier/listening.rb
53
+ - lib/crier/notification.rb
54
+ - lib/crier/version.rb
55
+ - lib/tasks/crier_tasks.rake
56
+ homepage:
57
+ licenses: []
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubyforge_project:
75
+ rubygems_version: 2.2.1
76
+ signing_key:
77
+ specification_version: 4
78
+ summary: Simple notification for the whole town
79
+ test_files: []