facteur 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/.gitignore +23 -0
  2. data/CHANGELOG +4 -0
  3. data/Gemfile +4 -0
  4. data/Gemfile.lock +115 -0
  5. data/LICENSE +20 -20
  6. data/README.markdown +135 -137
  7. data/Rakefile +19 -41
  8. data/lib/facteur.rb +25 -14
  9. data/lib/facteur/active_record_addressee_model.rb +12 -0
  10. data/lib/facteur/active_record_mailbox_model.rb +15 -0
  11. data/lib/facteur/active_record_message_model.rb +24 -0
  12. data/lib/facteur/addressee_model.rb +29 -130
  13. data/lib/facteur/base_addressee_model.rb +163 -0
  14. data/lib/facteur/mailbox.rb +16 -0
  15. data/lib/facteur/mailbox_model.rb +9 -6
  16. data/lib/facteur/message.rb +16 -0
  17. data/lib/facteur/message_model.rb +16 -16
  18. data/lib/facteur/mongoid_addressee_model.rb +12 -0
  19. data/lib/facteur/mongoid_mailbox_model.rb +18 -0
  20. data/lib/facteur/mongoid_message_model.rb +19 -0
  21. data/lib/facteur/version.rb +3 -0
  22. data/lib/generators/facteur/install/install_generator.rb +25 -28
  23. data/lib/generators/facteur/install/templates/create_mailboxes.rb +14 -14
  24. data/lib/generators/facteur/install/templates/create_messages.rb +19 -19
  25. data/spec/activerecord/activerecord_spec_helper.rb +11 -0
  26. data/spec/activerecord/facteur_spec.rb +47 -0
  27. data/spec/mongoid/facteur_spec.rb +49 -0
  28. data/spec/mongoid/mongoid_spec_helper.rb +11 -0
  29. data/spec/spec_helper.rb +10 -67
  30. data/spec/support/activerecord/schema.rb +32 -0
  31. data/spec/{facteur_spec.rb → support/facteur_examples.rb} +38 -49
  32. data/spec/support/mongoid/database.rb +12 -0
  33. metadata +139 -20
  34. data/.document +0 -5
  35. data/VERSION +0 -1
  36. data/lib/facteur/rails/tasks/facteur.rake +0 -13
  37. data/lib/generators/facteur/install/templates/mailbox.rb +0 -3
  38. data/lib/generators/facteur/install/templates/message.rb +0 -3
@@ -0,0 +1,16 @@
1
+ module Facteur
2
+ if Object.const_defined? :ActiveRecord
3
+ class Mailbox < ActiveRecord::Base
4
+ include Facteur::MailboxModel
5
+ end
6
+
7
+ elsif Object.const_defined? :Mongoid
8
+ class Mailbox
9
+ include Mongoid::Document
10
+ include Facteur::MailboxModel
11
+ end
12
+
13
+ else
14
+ raise "Facteur only supports ActiveRecord and Mongoid"
15
+ end
16
+ end
@@ -1,11 +1,14 @@
1
1
  module Facteur
2
2
  module MailboxModel
3
- def self.included(receiver)
4
- receiver.class_exec do
5
- belongs_to :addressee, :polymorphic => true
6
- has_many :messages
7
- validates_presence_of :name
8
- validates_uniqueness_of :name, :scope => [:addressee_id, :addressee_type]
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if self.ancestors.map(&:to_s).include?("ActiveRecord::Base")
7
+ include Facteur::ActiveRecordMailboxModel
8
+ elsif self.ancestors.map(&:to_s).include?("Mongoid::Document")
9
+ include Facteur::MongoidMailboxModel
10
+ else
11
+ raise "Facteur only supports ActiveRecord and Mongoid"
9
12
  end
10
13
  end
11
14
  end
@@ -0,0 +1,16 @@
1
+ module Facteur
2
+ if Object.const_defined? :ActiveRecord
3
+ class Message < ActiveRecord::Base
4
+ include Facteur::MessageModel
5
+ end
6
+
7
+ elsif Object.const_defined? :Mongoid
8
+ class Message
9
+ include Mongoid::Document
10
+ include Facteur::MessageModel
11
+ end
12
+
13
+ else
14
+ raise "Facteur only supports ActiveRecord and Mongoid"
15
+ end
16
+ end
@@ -1,16 +1,16 @@
1
- module Facteur
2
- module MessageModel
3
- def self.included(receiver)
4
- receiver.class_exec do
5
- validates_presence_of :author_id, :mailbox_id, :body
6
-
7
- belongs_to :mailbox
8
- belongs_to :author, :class_name => "User", :foreign_key => "author_id"
9
-
10
- delegate :addressee, :to => :mailbox
11
- delegate :addressee_type, :to => :mailbox
12
- end
13
- end
14
- end
15
- end
16
-
1
+ module Facteur
2
+ module MessageModel
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ if self.ancestors.map(&:to_s).include?("ActiveRecord::Base")
7
+ include Facteur::ActiveRecordMessageModel
8
+ elsif self.ancestors.map(&:to_s).include?("Mongoid::Document")
9
+ include Facteur::MongoidMessageModel
10
+ else
11
+ raise "Facteur only supports ActiveRecord and Mongoid"
12
+ end
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,12 @@
1
+ require 'mongoid'
2
+
3
+ module Facteur
4
+ module MongoidAddresseeModel
5
+ include Facteur::BaseAddresseeModel
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ embeds_many :mailboxes, :class_name => "Facteur::Mailbox"
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,18 @@
1
+ require 'mongoid'
2
+
3
+ module Facteur
4
+ module MongoidMailboxModel
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ field :name
9
+ field :default, :default => false
10
+
11
+ embedded_in :addressee, :class_name => Facteur.addressee_model, :inverse_of => :mailboxes
12
+ embeds_many :messages, :class_name => "Facteur::Message"
13
+
14
+ validates_uniqueness_of :name
15
+ validates_presence_of :name
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,19 @@
1
+ require 'mongoid'
2
+
3
+ module Facteur
4
+ module MongoidMessageModel
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ field :body
9
+ field :subject
10
+
11
+ embedded_in :mailbox, :class_name => "Facteur::Mailbox"
12
+ referenced_in :author, :class_name => Facteur.addressee_model
13
+ references_and_referenced_in_many :addressees, :class_name => Facteur.addressee_model
14
+
15
+ validates_presence_of :body, :author_id
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,3 @@
1
+ module Facteur
2
+ VERSION = "1.3.0"
3
+ end
@@ -1,28 +1,25 @@
1
- require 'rails/generators'
2
- require 'rails/generators/migration'
3
-
4
- module Facteur
5
- module Generators
6
- class InstallGenerator < Rails::Generators::Base
7
- include Rails::Generators::Migration
8
-
9
- def self.source_root
10
- @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
11
- end
12
-
13
- def self.next_migration_number
14
- Time.now.utc.strftime("%Y%m%d%H%M%S")
15
- end
16
-
17
- desc "Generate the facteur's message and mailbox models"
18
- def create_facteur_files
19
- template 'message.rb', 'app/models/message.rb'
20
- template 'create_messages.rb', "db/migrate/#{self.class.next_migration_number}_create_messages.rb"
21
-
22
- sleep(1)
23
- template 'mailbox.rb', 'app/models/mailbox.rb'
24
- template 'create_mailboxes.rb', "db/migrate/#{self.class.next_migration_number}_create_mailboxes.rb"
25
- end
26
- end
27
- end
28
- end
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Facteur
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ include Rails::Generators::Migration
8
+
9
+ def self.source_root
10
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
11
+ end
12
+
13
+ def self.next_migration_number
14
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
15
+ end
16
+
17
+ desc "Generate the facteur's message and mailbox models"
18
+ def create_facteur_files
19
+ template 'create_messages.rb', "db/migrate/#{self.class.next_migration_number}_create_messages.rb"
20
+ sleep(1)
21
+ template 'create_mailboxes.rb', "db/migrate/#{self.class.next_migration_number}_create_mailboxes.rb"
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,15 +1,15 @@
1
- class CreateMailboxes < ActiveRecord::Migration
2
- def self.up
3
- create_table :mailboxes do |t|
4
- t.string :name
5
- t.text :contents
6
- t.integer :addressee_id
7
- t.string :addressee_type
8
- t.boolean :default, :default => false
9
- end
10
- end
11
-
12
- def self.down
13
- drop_table :mailboxes
14
- end
1
+ class CreateMailboxes < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :mailboxes do |t|
4
+ t.string :name
5
+ t.text :contents
6
+ t.integer :addressee_id
7
+ t.string :addressee_type
8
+ t.boolean :default, :default => false
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :mailboxes
14
+ end
15
15
  end
@@ -1,20 +1,20 @@
1
- class CreateMessages < ActiveRecord::Migration
2
- def self.up
3
- create_table :messages do |t|
4
- t.string :subject
5
- t.text :body
6
- t.integer :mailbox_id
7
- t.integer :author_id
8
- t.boolean :deleted_by_author, :default => false
9
- t.datetime :deleted_by_author_at
10
- t.boolean :deleted_by_addressee, :default => false
11
- t.datetime :deleted_by_addressee_at
12
- t.boolean :read, :default => false
13
- t.timestamps
14
- end
15
- end
16
-
17
- def self.down
18
- drop_table :messages
19
- end
1
+ class CreateMessages < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :messages do |t|
4
+ t.string :subject
5
+ t.text :body
6
+ t.integer :mailbox_id
7
+ t.integer :author_id
8
+ t.boolean :deleted_by_author, :default => false
9
+ t.datetime :deleted_by_author_at
10
+ t.boolean :deleted_by_addressee, :default => false
11
+ t.datetime :deleted_by_addressee_at
12
+ t.boolean :read, :default => false
13
+ t.timestamps
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :messages
19
+ end
20
20
  end
@@ -0,0 +1,11 @@
1
+ require 'active_record'
2
+ require 'spec_helper'
3
+
4
+ Dir[File.expand_path('../../support/activerecord/*.rb', __FILE__)].each{|f| require f }
5
+
6
+ RSpec.configure do |c|
7
+ c.before(:suite) do
8
+ CreateSchema.suppress_messages{ CreateSchema.migrate(:up) }
9
+ end
10
+ end
11
+
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/activerecord_spec_helper')
2
+
3
+ describe Facteur::AddresseeModel do
4
+ context "When users are created after activating facteur" do
5
+ before(:all) do
6
+ # create a user model with facteur activated
7
+ Object.send(:remove_const, :Member) if Object.const_defined?(:Member)
8
+ Member = Class.new(ActiveRecord::Base)
9
+ Member.class_exec do
10
+ validates_uniqueness_of :name
11
+ include Facteur::AddresseeModel
12
+ mailbox :private_mailbox, :default => true
13
+ mailbox :public_mailbox
14
+ end
15
+
16
+ # create the users
17
+ Member.delete_all
18
+ create_users
19
+ end
20
+
21
+ it_should_behave_like "an addressee model"
22
+ end
23
+
24
+ context "When users are created before activating facteur" do
25
+ before(:all) do
26
+ # create a user model without amistad activated
27
+ Object.send(:remove_const, :Member) if Object.const_defined?(:Member)
28
+ Member = Class.new(ActiveRecord::Base)
29
+ Member.class_exec do
30
+ validates_uniqueness_of :name
31
+ end
32
+
33
+ # create the users
34
+ Member.delete_all
35
+ create_users
36
+
37
+ # activate facteur
38
+ Member.class_exec do
39
+ include Facteur::AddresseeModel
40
+ mailbox :private_mailbox, :default => true
41
+ mailbox :public_mailbox
42
+ end
43
+ end
44
+
45
+ it_should_behave_like "an addressee model"
46
+ end
47
+ end
@@ -0,0 +1,49 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/mongoid_spec_helper')
2
+
3
+ describe Facteur::AddresseeModel do
4
+ context "When users are created after activating facteur" do
5
+ before(:all) do
6
+ # create a user model with facteur activated
7
+ Object.send(:remove_const, :Member) if Object.const_defined?(:Member)
8
+ Member = Class.new
9
+ Member.class_exec do
10
+ include Mongoid::Document
11
+ include Facteur::AddresseeModel
12
+ field :name, :require => true
13
+ mailbox :private_mailbox, :default => true
14
+ mailbox :public_mailbox
15
+ end
16
+
17
+ # create the users
18
+ Member.delete_all
19
+ create_users
20
+ end
21
+
22
+ it_should_behave_like "an addressee model"
23
+ end
24
+
25
+ context "When users are created before activating facteur" do
26
+ before(:all) do
27
+ # create a user model without amistad activated
28
+ Object.send(:remove_const, :Member) if Object.const_defined?(:Member)
29
+ Member = Class.new
30
+ Member.class_exec do
31
+ include Mongoid::Document
32
+ field :name, :require => true
33
+ end
34
+
35
+ # create the users
36
+ Member.delete_all
37
+ create_users
38
+
39
+ # activate facteur
40
+ Member.class_exec do
41
+ include Facteur::AddresseeModel
42
+ mailbox :private_mailbox, :default => true
43
+ mailbox :public_mailbox
44
+ end
45
+ end
46
+
47
+ it_should_behave_like "an addressee model"
48
+ end
49
+ end
@@ -0,0 +1,11 @@
1
+ require 'mongoid'
2
+ require 'spec_helper'
3
+
4
+ Dir[File.expand_path('../../support/mongoid/*.rb', __FILE__)].each{|f| require f }
5
+
6
+ RSpec.configure do |c|
7
+ c.before(:suite) do
8
+ Database.connect
9
+ end
10
+ end
11
+
@@ -1,67 +1,10 @@
1
- $LOAD_PATH.unshift(File.dirname(__FILE__))
2
- $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'rspec'
4
- require 'active_record'
5
- require 'logger'
6
- require 'facteur'
7
-
8
- SPEC_ROOT = File.dirname(__FILE__)
9
-
10
- ActiveRecord::Base.establish_connection(
11
- :adapter => "sqlite3",
12
- :database => ":memory:"
13
- )
14
-
15
- ActiveRecord::Migration.verbose = false
16
-
17
- ActiveRecord::Schema.define do
18
- create_table :users, :force => true do |t|
19
- t.string :name, :null => false
20
- end
21
-
22
- create_table :mailboxes, :force => true do |t|
23
- t.string :name
24
- t.text :contents
25
- t.integer :addressee_id
26
- t.string :addressee_type
27
- t.boolean :default
28
- end
29
-
30
- create_table :messages, :force => true do |t|
31
- t.string :subject
32
- t.text :body
33
- t.integer :mailbox_id
34
- t.integer :author_id
35
- t.boolean :deleted_by_author, :default => false
36
- t.datetime :deleted_by_author_at
37
- t.boolean :deleted_by_addressee, :default => false
38
- t.datetime :deleted_by_addressee_at
39
- t.boolean :read, :default => false
40
- end
41
- end
42
-
43
- #ActiveRecord::Base.logger = Logger.new(File.open("#{SPEC_ROOT}/db/database.log", 'a'))
44
-
45
- class User < ActiveRecord::Base
46
- include Facteur::AddresseeModel
47
-
48
- mailbox :private_mailbox, :default => true
49
- mailbox :public_mailbox
50
- end
51
-
52
- class Mailbox < ActiveRecord::Base
53
- include Facteur::MailboxModel
54
- end
55
-
56
- class Message < ActiveRecord::Base
57
- include Facteur::MessageModel
58
- end
59
-
60
- def create_users
61
- User.delete_all
62
- Mailbox.delete_all
63
- %w(John Peter James Mary Jane Victoria).each do |name|
64
- eval "@#{name.downcase} = User.create(:name => '#{name}')"
65
- end
66
- end
67
-
1
+ require 'rspec'
2
+ require 'awesome_print'
3
+ require 'facteur'
4
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].each {|f| require f}
5
+
6
+ def create_users
7
+ %w(John Peter James Mary Jane Victoria).each do |name|
8
+ instance_variable_set("@#{name.downcase}".to_sym, Member.create(:name => name))
9
+ end
10
+ end