photon 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,104 @@
1
+ Introducing Photon - the Dead Simple Rails messenger service
2
+
3
+ Photon assumes very basic things about your schema. Right now, there is no config (it's coming, and the framework is there for it), and no migrations.
4
+
5
+ It assumes you have the following 4 tables:
6
+
7
+ Users
8
+
9
+ Messages, containing:
10
+ body
11
+ subject
12
+ delivery_id
13
+ sender_id
14
+ timestamps
15
+
16
+ MessageThreads, containing:
17
+ id, timestamps
18
+
19
+
20
+ Deliveries, containing:
21
+ user_id
22
+ message_id
23
+ read
24
+ message_thread_id
25
+ timestamps
26
+
27
+
28
+ To setup the proper associations, there are the following model methods:
29
+
30
+ User: acts_as_sender
31
+ Delivery: acts_as_deliverable
32
+ Message: acts_as_message
33
+ MessageThread: acts_as_threadable
34
+
35
+ This provides the following methods at this time:
36
+
37
+
38
+ user#send_message(:to => User<object>, :subject => string, :body => string)
39
+
40
+ Sends a message to a user. It starts a thread.
41
+
42
+
43
+
44
+ user#reply_to(Message<object>, :subject => string, :body => string)
45
+
46
+ Replies to one of the user's received messages
47
+
48
+
49
+
50
+ user#sent_messages
51
+
52
+ Array of sent messages
53
+
54
+
55
+ user#unread_messages
56
+
57
+ Array of unread messages
58
+
59
+
60
+ user#read_messages
61
+
62
+ Array of read messages
63
+
64
+
65
+ user#read(Message<Object>)
66
+
67
+ Marks the specified message object as read
68
+
69
+
70
+ user#unread(Message<Object>)
71
+
72
+ Marks the specified message object as unread
73
+
74
+
75
+ Message#find_thread(User<Object>, Message<Object>)
76
+
77
+ Finds the thread that the message and user belong to.
78
+
79
+
80
+
81
+ EXAMPLE USAGE:
82
+
83
+ user1 = User.create
84
+ user2 = User.create
85
+
86
+ user1.send_message(:to => user2, :subject => "Hello!", :body => "Hello world!")
87
+ user1.sent_messages # => [#<Message id: 30, subject: "Hello!", body: "Hello world!", sender_id: 1>]
88
+ unread_messages = user2.unread_messages # => [#<Message id: 30, subject: "Hello!", body: "Hello world!", sender_id: 1>]
89
+ user2.read(unread_messages[0])
90
+ user2.read_messages # => [#<Message id: 30, subject: "Hello!", body: "Hello world!", sender_id: 1>]
91
+ user2.reply_to(unread_messages[0], :subject => "Goodbye!", :body => "Hello again!")
92
+ user1.unread_messages # => [#<Message id: 31, subject: "Goodbye!", body: "Hello again!", sender_id: 2>]
93
+ Message.find_thread(user1, user1.unread_messages[0]) # => [#<Message id: 30, subject: "Hello!", body: "Hello world!", sender_id: 1>, #<Message id: 31, subject: "Goodbye!", body: "Hello again!", sender_id: 2>]
94
+
95
+
96
+ TODO:
97
+
98
+ Add configuration files
99
+ Implement attachment support
100
+ Make methods chainable - sending and replying should return the delivery object.
101
+ Support external ActiveRecord databases - involving creating a lookup table on an external database based on current user ids.
102
+
103
+
104
+ An extremely robust Riak version is coming soon.
@@ -0,0 +1,72 @@
1
+ module Photon
2
+ module Delivers
3
+ module Core
4
+ def self.included(base)
5
+ base.send :include, Photon::Delivers::Core::InstanceMethods
6
+ base.extend Photon::Delivers::Core::ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ end
11
+
12
+ module InstanceMethods
13
+ def send_message(opts={})
14
+ if opts[:to].nil? || opts[:subject].nil? || opts[:body].nil?
15
+ false
16
+ else
17
+ opts[:message_thread_id] = opts[:message_thread_id] || constantize_model(Photon.thread).create.id
18
+ message = constantize_model(Photon.using).create!(:sender => self,
19
+ :users => [opts[:to]].flatten,
20
+ :subject => opts[:subject],
21
+ :body => opts[:body]
22
+ )
23
+
24
+ message.deliveries.update_all({:message_thread_id => opts[:message_thread_id], :scope_id => opts[:scope_id], :scope_type => opts[:scope_type]})
25
+ end
26
+ end
27
+
28
+ def sent_messages
29
+ constantize_model(Photon.using).where(:sender_id => self.id)
30
+ end
31
+
32
+ def unread_messages
33
+ through_string = Photon.through.to_s
34
+ constantize_model(Photon.using).where("#{through_string}.#{self.class.to_s.downcase}_id = ? AND #{through_string}.read IS NOT ?", self.id, true).joins(Photon.through)
35
+ end
36
+
37
+ def read_messages
38
+ through_string = Photon.through.to_s
39
+ constantize_model(Photon.using).where("#{through_string}.#{self.class.to_s.downcase}_id = ? AND #{through_string}.read IS ?", self.id, true).joins(Photon.through)
40
+ end
41
+
42
+ def read(message)
43
+ change_read_status(message, true)
44
+ end
45
+
46
+ def unread(message)
47
+ change_read_status(message, false)
48
+ end
49
+
50
+ def reply_to(message, opts = {})
51
+ if message.nil? || opts[:body].nil? || opts[:subject].nil?
52
+ false
53
+ else
54
+ delivery_object = constantize_model(Photon.through).where("#{self.class.to_s.downcase}_id = ? AND #{Photon.using.to_s.singularize}_id = ?", self.id, message.id).first
55
+ self.send_message(:to => message.sender, :message_thread_id => delivery_object.message_thread_id, :subject => opts[:subject], :body => opts[:body])
56
+ end
57
+ end
58
+
59
+
60
+ private
61
+ def change_read_status(message, status)
62
+ constantize_model(Photon.through).where("#{self.class.to_s.downcase}_id = ? AND #{Photon.using.to_s.singularize}_id = ?", self.id, message.id).update_all({:read => status})
63
+ end
64
+
65
+ def constantize_model(model_string)
66
+ model_string.to_s.singularize.classify.constantize
67
+ end
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,20 @@
1
+ module Photon
2
+ module Delivers
3
+ module Message
4
+ def self.included(base)
5
+ base.send :include, Photon::Delivers::Message::InstanceMethods
6
+ base.extend Photon::Delivers::Message::ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def find_thread(user, message)
11
+ delivery_string = Photon.through.to_s
12
+ Photon.using.to_s.singularize.classify.constantize.joins(Photon.through).where("#{delivery_string}.#{Photon.thread.to_s.singularize}_id = (SELECT #{Photon.thread.to_s.singularize}_id FROM #{delivery_string} WHERE #{delivery_string}.#{Photon.to.to_s.singularize}_id = ? AND #{delivery_string}.#{Photon.using.to_s.singularize}_id = ?)", Photon.to.to_s.singularize.classify.constantize.first, Photon.using.to_s.singularize.classify.constantize.last)
13
+ end
14
+ end
15
+
16
+ module InstanceMethods
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ module Photon
2
+ module Delivers
3
+ def acts_as_sender
4
+ class_eval do
5
+
6
+ has_many Photon.through
7
+ has_many Photon.using, :through => Photon.through
8
+
9
+ include Photon::Delivers::Core
10
+ end
11
+ end
12
+
13
+ def acts_as_deliverable
14
+ class_eval do
15
+ belongs_to Photon.to.to_s.singularize.to_sym
16
+ belongs_to Photon.using.to_s.singularize.to_sym
17
+ belongs_to Photon.thread.to_s.singularize.to_sym
18
+
19
+ end
20
+ end
21
+
22
+ def acts_as_message
23
+ class_eval do
24
+ has_many Photon.through
25
+ has_many Photon.to, :through => Photon.through
26
+ belongs_to :sender, :class_name => Photon.to.to_s.singularize.classify
27
+
28
+ include Photon::Delivers::Message
29
+ end
30
+ end
31
+
32
+ def acts_as_threadable
33
+ class_eval do
34
+ has_many Photon.through
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,5 @@
1
+ module Photon
2
+ module DeliveryHelpers
3
+
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module Photon
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/photon.rb CHANGED
@@ -1,3 +1,31 @@
1
1
  module Photon
2
- # Your code goes here...
2
+ require "active_record"
3
+ require "action_view"
4
+
5
+ require 'photon/delivers'
6
+ require 'photon/delivery_helpers'
7
+ require 'photon/delivers/message'
8
+ require 'photon/delivers/core'
9
+
10
+ mattr_accessor :using
11
+ @@using = :messages
12
+
13
+ mattr_accessor :through
14
+ @@through = :deliveries
15
+
16
+ mattr_accessor :to
17
+ @@to = :users
18
+
19
+ mattr_accessor :thread
20
+ @@thread = :message_threads
21
+
22
+ if defined?(ActiveRecord::Base)
23
+ ActiveRecord::Base.extend Photon::Delivers
24
+ end
25
+
26
+ if defined?(ActionView::Base)
27
+ ActionView::Base.send :include, Photon::DeliveryHelpers
28
+ end
29
+
30
+
3
31
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: photon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gerred Dillon
@@ -31,8 +31,13 @@ extra_rdoc_files: []
31
31
  files:
32
32
  - .gitignore
33
33
  - Gemfile
34
+ - README
34
35
  - Rakefile
35
36
  - lib/photon.rb
37
+ - lib/photon/delivers.rb
38
+ - lib/photon/delivers/core.rb
39
+ - lib/photon/delivers/message.rb
40
+ - lib/photon/delivery_helpers.rb
36
41
  - lib/photon/version.rb
37
42
  - photon.gemspec
38
43
  has_rdoc: true