rails-ext 0.2.17 → 0.2.18

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.
Files changed (68) hide show
  1. data/.git/index +0 -0
  2. data/.git/logs/HEAD +4 -0
  3. data/.git/logs/refs/heads/master +4 -0
  4. data/.git/logs/refs/remotes/origin/master +1 -0
  5. data/.git/objects/04/cd62e7f8158fe54d21e6f63617c43b593b8b59 +0 -0
  6. data/.git/objects/06/c2384aa5f8b5614038b48a726d1d4f98a00f3b +0 -0
  7. data/.git/objects/11/9db1e92a2d4c7ff2c6d9a58940aa25d8bd4bc3 +0 -0
  8. data/.git/objects/15/8eef048d128710bfc0cb60114f7405e45f9b6c +2 -0
  9. data/.git/objects/1b/d7d8887251e9bd43d88a5da5115ff4f1f5f10d +0 -0
  10. data/.git/objects/1c/14e28ef05892ccfe61da3dc28e16f87040fe43 +0 -0
  11. data/.git/objects/24/62bd996e6c1b78d669a9eb3303c74217260f4b +0 -0
  12. data/.git/objects/25/ad912bcedeb900c551388c8d4f062812848af2 +0 -0
  13. data/.git/objects/27/f8a63386b8acf5edba3ed9334644455045acd8 +0 -0
  14. data/.git/objects/3d/5b84167d35cb75647f38a6d475a452ba3c6178 +0 -0
  15. data/.git/objects/44/c96e2a778dcc62c10b3c329ab0211115a9f546 +0 -0
  16. data/.git/objects/46/3353505eed0c799d05f025788437decbabafec +0 -0
  17. data/.git/objects/47/afb623cc9949394fc403bc977671307c1a9eda +0 -0
  18. data/.git/objects/49/ca132451f70ee989ff7a7cf487b029fe706b28 +0 -0
  19. data/.git/objects/53/b8187421c09e55e44b8d6fb3c4cd7feb0a51b2 +0 -0
  20. data/.git/objects/65/e77ca17328dfeb3477287af40fb834284c8f61 +0 -0
  21. data/.git/objects/66/95d9ac4648cdadd9922d3b355a1d6f56547f4c +0 -0
  22. data/.git/objects/6b/779e60dfa44ba3daa26badf65b3422f30c7b9b +2 -0
  23. data/.git/objects/6c/e5f7ee8ecbaa20a264bb2f2fb2fd821b5ad478 +0 -0
  24. data/.git/objects/6f/72c276ac50a5731aa5f7e7e01dd2793113fe5b +0 -0
  25. data/.git/objects/74/4d46a654ae9e0867055184f2011668c7e1acd2 +0 -0
  26. data/.git/objects/75/c64796b5ffb6107b861cfa77fa28e8e43a1156 +1 -0
  27. data/.git/objects/82/0190b802ed213aab7a56a928a09db06e2a90a2 +0 -0
  28. data/.git/objects/86/23de615f686de621ca23cbbff6a07885101df9 +0 -0
  29. data/.git/objects/8a/d2c8f5ee0627cf48aa39d5f10578f929bfc2c5 +0 -0
  30. data/.git/objects/8f/5465ea782955ad019122d92a0f162945339f8e +0 -0
  31. data/.git/objects/94/afd196cc89d60f0a06d4cad619eaa79921a9ae +0 -0
  32. data/.git/objects/97/5b5b662b4c712668b3fe5dfe917000338e6b3e +0 -0
  33. data/.git/objects/99/a23df6dd9c448928b25f2e749871c6fc696a32 +0 -0
  34. data/.git/objects/9c/20b1616810e3d1d5951ea6251ff2d28322dc16 +0 -0
  35. data/.git/objects/9d/9f28ff50236f1e7f78cbd17b8aa25a0bf8b6fc +4 -0
  36. data/.git/objects/9f/91a2fdd59ecdfb4f8f5025fdbe590eb536e96f +0 -0
  37. data/.git/objects/a0/90ab2a9e739787b6090c187acf95fdd763afef +0 -0
  38. data/.git/objects/a2/d43a3a789e655cc306618d480193709d580c30 +2 -0
  39. data/.git/objects/a4/c75ead5dcfe97a67d302464584be205e8c48b1 +0 -0
  40. data/.git/objects/ad/66aac16165dd71456e4488069248d6f10ebb8f +0 -0
  41. data/.git/objects/b8/aaaf4949881600102a72a60f17fd58918dbd7b +0 -0
  42. data/.git/objects/b8/efcaff7f11cc584ac44303449d96592853c528 +2 -0
  43. data/.git/objects/b9/ecca04b12de69ffab2cc5d22cb52d5babef24f +2 -0
  44. data/.git/objects/be/29c116694d85d48cf177cf30816aa159189536 +0 -0
  45. data/.git/objects/df/f63029f32b25c2bf18d0f3f4e7907daa7c1bca +0 -0
  46. data/.git/objects/e9/46302310acac8a7a6b64a58b47062934765ede +0 -0
  47. data/.git/objects/f1/9154fc5093f2fd56f2660f386975b2a79be76f +0 -0
  48. data/.git/refs/heads/master +1 -1
  49. data/.git/refs/remotes/origin/master +1 -1
  50. data/Rakefile +1 -1
  51. data/lib/mongo_mapper_ext/db_config.rb +76 -0
  52. data/lib/mongo_mapper_ext/fixes.rb +9 -6
  53. data/lib/mongo_mapper_ext/migration.rb +33 -52
  54. data/lib/mongo_mapper_ext/mongo_mapper.rb +1 -150
  55. data/lib/mongo_mapper_ext/plugins/attributes_cache.rb +22 -0
  56. data/lib/mongo_mapper_ext/plugins/default_scope.rb +40 -0
  57. data/lib/mongo_mapper_ext/plugins/micelaneous.rb +17 -0
  58. data/lib/mongo_mapper_ext/spec_helper.rb +2 -6
  59. data/lib/mongo_mapper_ext/view_helpers.rb +2 -0
  60. data/lib/mongo_mapper_ext.rb +22 -2
  61. data/lib/rails_ext/config_files/safe_hash.rb +10 -2
  62. data/spec/mongo_mapper_ext/default_scope_spec.rb +62 -0
  63. data/spec/mongo_mapper_ext/mongo_mapper_migration_spec.rb +74 -0
  64. data/spec/{safe_hash_spec.rb → rails_ext/safe_hash_spec.rb} +2 -2
  65. metadata +53 -7
  66. data/lib/mongo_mapper_ext/multitenant.rb +0 -8
  67. data/lib/mongo_mapper_ext/object_id.rb +0 -12
  68. data/spec/mongo_mapper_migration_spec.rb +0 -137
data/.git/index CHANGED
Binary file
data/.git/logs/HEAD CHANGED
@@ -32,3 +32,7 @@ a59472e8ecb5d06647adeec11ae931b956508191 08f5dafa9123b7f8d1f22a796c4cba4a4063ad0
32
32
  08f5dafa9123b7f8d1f22a796c4cba4a4063ad0d b533c1c1bca9eae5b7e75c639fff8ec2c2f9fea1 alex <alex@amac.local> 1267048597 +0300 commit: refactor mongo connections and add multitenant migration with specs
33
33
  b533c1c1bca9eae5b7e75c639fff8ec2c2f9fea1 288e20f19c6662b1425e0a0797ae443f7eddcc4a alex <alex@amac.local> 1267051247 +0300 commit: upd
34
34
  288e20f19c6662b1425e0a0797ae443f7eddcc4a 480602886685e84f2a257eb8e95acfc8af1166b6 alex <alex@amac.local> 1268137298 +0300 commit: upd
35
+ 480602886685e84f2a257eb8e95acfc8af1166b6 158eef048d128710bfc0cb60114f7405e45f9b6c alex <alex@amac.local> 1268680881 +0300 commit: add default_scope to MM
36
+ 158eef048d128710bfc0cb60114f7405e45f9b6c b8efcaff7f11cc584ac44303449d96592853c528 alex <alex@amac.local> 1268692589 +0300 commit: refactor MM and switch to single DB for accounts
37
+ b8efcaff7f11cc584ac44303449d96592853c528 1c14e28ef05892ccfe61da3dc28e16f87040fe43 alex <alex@amac.local> 1268699329 +0300 commit: update mongo-mapper migrations
38
+ 1c14e28ef05892ccfe61da3dc28e16f87040fe43 94afd196cc89d60f0a06d4cad619eaa79921a9ae alex <alex@amac.local> 1268706470 +0300 commit: upd
@@ -32,3 +32,7 @@ a59472e8ecb5d06647adeec11ae931b956508191 08f5dafa9123b7f8d1f22a796c4cba4a4063ad0
32
32
  08f5dafa9123b7f8d1f22a796c4cba4a4063ad0d b533c1c1bca9eae5b7e75c639fff8ec2c2f9fea1 alex <alex@amac.local> 1267048597 +0300 commit: refactor mongo connections and add multitenant migration with specs
33
33
  b533c1c1bca9eae5b7e75c639fff8ec2c2f9fea1 288e20f19c6662b1425e0a0797ae443f7eddcc4a alex <alex@amac.local> 1267051247 +0300 commit: upd
34
34
  288e20f19c6662b1425e0a0797ae443f7eddcc4a 480602886685e84f2a257eb8e95acfc8af1166b6 alex <alex@amac.local> 1268137298 +0300 commit: upd
35
+ 480602886685e84f2a257eb8e95acfc8af1166b6 158eef048d128710bfc0cb60114f7405e45f9b6c alex <alex@amac.local> 1268680881 +0300 commit: add default_scope to MM
36
+ 158eef048d128710bfc0cb60114f7405e45f9b6c b8efcaff7f11cc584ac44303449d96592853c528 alex <alex@amac.local> 1268692589 +0300 commit: refactor MM and switch to single DB for accounts
37
+ b8efcaff7f11cc584ac44303449d96592853c528 1c14e28ef05892ccfe61da3dc28e16f87040fe43 alex <alex@amac.local> 1268699329 +0300 commit: update mongo-mapper migrations
38
+ 1c14e28ef05892ccfe61da3dc28e16f87040fe43 94afd196cc89d60f0a06d4cad619eaa79921a9ae alex <alex@amac.local> 1268706470 +0300 commit: upd
@@ -30,3 +30,4 @@ a59472e8ecb5d06647adeec11ae931b956508191 08f5dafa9123b7f8d1f22a796c4cba4a4063ad0
30
30
  08f5dafa9123b7f8d1f22a796c4cba4a4063ad0d b533c1c1bca9eae5b7e75c639fff8ec2c2f9fea1 alex <alex@amac.local> 1267048616 +0300 update by push
31
31
  b533c1c1bca9eae5b7e75c639fff8ec2c2f9fea1 288e20f19c6662b1425e0a0797ae443f7eddcc4a alex <alex@amac.local> 1267051254 +0300 update by push
32
32
  288e20f19c6662b1425e0a0797ae443f7eddcc4a 480602886685e84f2a257eb8e95acfc8af1166b6 alex <alex@amac.local> 1268137307 +0300 update by push
33
+ 480602886685e84f2a257eb8e95acfc8af1166b6 94afd196cc89d60f0a06d4cad619eaa79921a9ae alex <alex@amac.local> 1268706480 +0300 update by push
@@ -0,0 +1,2 @@
1
+ x��K
2
+ �0@]��$��D�@� �d�BjJM���9���y<x���C��U$�$$���>�z�lit�3�#�Ȫ��c�c&����c�TKA$�dd뷶�T}��ˋ̒�%�g@GLl���h�I����Fr��E�گ����`��nLB�
@@ -0,0 +1,2 @@
1
+ xU��
2
+ �0 �=�9n =���Qd�m6�)�v����
@@ -0,0 +1 @@
1
+ xmM=�0u�W��v!F�B$88JLgS� M���~,�xK%���޽ww�^�L��~�*��M!��l�(�ܝ^B�548R��@��:(��d�,=�V��b3��>��fZ*��ۤ�6g�@
@@ -0,0 +1,4 @@
1
+ x�TK��0�9�b�H���^Z )ͩ�V���c�B��wLmHZE���!��� b�y}��x�����_���+P�Y��e���(�-�X|<=)�,Q6�{�8~���k�C��G+Z����wJ��j�d�B�S�3��l���V ��O�~#!�<���Y�$f�JX��T:��9��P�Aj,��
2
+ �s�^ �u�v�r<B�a�"A�e|üC�qi+�rї�\� �Qo�r���_
3
+ �֪/ ��f��� q���,�-���B�0G�5��A��ވ��5%m���\�����9��V�J'ۈT_�5�|��ф�w��`B%�6�]W�xr� &�Q��px&A�J'|o���:��Q'$�A;ڙ��r��5V�ˁI�q�ѡf�O�(�����x��ݜ3O�fy��X��%pX��eP)̡r��K4:PD=R�H^&k��`t��9.�����{��S�^��
4
+ t{ƴc�.�g45��.m��u�m$éE��ŅF(
@@ -0,0 +1,2 @@
1
+ x�P�j�0��x�k�R��24�j�d+���9H:c�����+;Ť����Aw��{w����~��ɱ�mww��H��"v&@�}zR�5
2
+ !�m���V���e9�{�W/���4���Z�?p��i(�Gא֤��FG>ųQzEŎu�M��?yo���'�� �矘뚎�&<�M-x0㥭�.a4��-�f���<��)���2�d۳�X�f+�#1z�NN3��Bqi'"�aV�<���� ]�����Ğ'�e�0p-�K�?қ/.1��s�/Q�
@@ -0,0 +1,2 @@
1
+ x��Aj�0E��)f(�X�5J)���Ѩ1�V�'��o�t�V���u�
2
+ �Π"����~x_K�
@@ -0,0 +1,2 @@
1
+ xU��j�0E��W %�FYJ�.�
2
+
@@ -1 +1 @@
1
- 480602886685e84f2a257eb8e95acfc8af1166b6
1
+ 94afd196cc89d60f0a06d4cad619eaa79921a9ae
@@ -1 +1 @@
1
- 480602886685e84f2a257eb8e95acfc8af1166b6
1
+ 94afd196cc89d60f0a06d4cad619eaa79921a9ae
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ require 'fileutils'
18
18
 
19
19
  spec = Gem::Specification.new do |s|
20
20
  s.name = "rails-ext"
21
- s.version = "0.2.17"
21
+ s.version = "0.2.18"
22
22
  s.summary = "Set of common Ruby on Rails extensions"
23
23
  s.description = "Set of common Ruby on Rails extensions"
24
24
  s.author = "Alexey Petrushin"
@@ -0,0 +1,76 @@
1
+ module MongoMapper
2
+ class ConnectionsPool < Hash
3
+ attr_accessor :mongo_mapper
4
+ def initialize mongo_mapper
5
+ self.mongo_mapper = mongo_mapper
6
+ end
7
+
8
+ def [](database_alias)
9
+ database_alias = database_alias.to_s
10
+ unless connection = super(database_alias)
11
+ mongo_mapper.db_config.should! :include, database_alias
12
+ db_options = mongo_mapper.db_config[database_alias]
13
+ connection = Mongo::Connection.new(db_options['host'], db_options['port'], :logger => mongo_mapper.logger)
14
+
15
+ if defined?(PhusionPassenger)
16
+ PhusionPassenger.on_event(:starting_worker_process) do |forked|
17
+ connection.connect_to_master if forked
18
+ end
19
+ end
20
+
21
+ self[database_alias] = connection
22
+ end
23
+ return connection
24
+ end
25
+ end
26
+
27
+ class DatabasesPool < Hash
28
+ attr_accessor :mongo_mapper
29
+ def initialize mongo_mapper
30
+ self.mongo_mapper = mongo_mapper
31
+ end
32
+
33
+ def [](database_alias)
34
+ database_alias = database_alias.to_s
35
+ unless db = super(database_alias)
36
+ mongo_mapper.db_config.should! :include, database_alias
37
+ db_options = mongo_mapper.db_config[database_alias]
38
+ db = mongo_mapper.connections[database_alias].db db_options['name'].should!(:be_a, String)
39
+ self[database_alias] = db
40
+ end
41
+ return db
42
+ end
43
+ end
44
+
45
+ class << self
46
+ attr_accessor :db_config
47
+
48
+ def db_config
49
+ unless @db_config
50
+ if defined?(::Rails) and defined?(DATABASE)
51
+ @db_config = {}
52
+ DATABASE["#{::Rails.env}!"].to_h.each do |db_alias, options|
53
+ @db_config[db_alias.to_s] = {
54
+ 'name' => options.name!,
55
+ 'host' => options.host(nil),
56
+ 'port' => options.port(nil)
57
+ }
58
+ end
59
+ else
60
+ @db_config = {}
61
+ # raise "If you don't using Rails you must override this method and provide your own config!"
62
+ end
63
+ end
64
+ @db_config
65
+ end
66
+
67
+ def connections
68
+ @connections ||= ConnectionsPool.new(self)
69
+ end
70
+
71
+ def databases
72
+ @databases ||= DatabasesPool.new(self)
73
+ end
74
+ end
75
+
76
+ end
@@ -1,9 +1,12 @@
1
- MongoMapper::Plugins::Keys::Key.class_eval do
2
- def get(value)
3
- if value.nil? && !default_value.nil?
4
- return default_value.respond_to?(:clone) ? default_value.clone : default_value
5
- end
1
+ #
2
+ # ObjectID
3
+ #
4
+ Mongo::ObjectID.class_eval do
5
+ def == other
6
+ self.to_s == other.to_s
7
+ end
6
8
 
7
- type.from_mongo(value)
9
+ def to_yaml *args
10
+ to_s.to_yaml *args
8
11
  end
9
12
  end
@@ -20,90 +20,71 @@ module MongoMapper::Migration
20
20
  end
21
21
 
22
22
  class << self
23
- def define_migration version, &block
24
- version.should! :be_a, Integer
25
- definition = MigrationDefinition.new
26
- block.call definition
27
- migration_definitions[version] = definition
28
- end
29
-
30
- def define_global_migration version, &block
23
+ def define database_alias, version, &block
24
+ database_alias = database_alias.to_s
25
+
31
26
  version.should! :be_a, Integer
32
27
  definition = MigrationDefinition.new
33
28
  block.call definition
34
- global_migration_definitions[version] = definition
35
- end
36
-
37
- def update_global_database_to version
38
- puts "Migration for Global Database:"
39
- update_database MongoMapper.global_database, global_migration_definitions, version
29
+ definitions[database_alias][version] = definition
40
30
  end
41
-
42
- def update_database_to version
43
- puts "Migration for Accounts Databases:"
44
- results = {}
45
- MongoMapper.accounts_names.each do |account_name|
46
- MongoMapper.account_database account_name do |db|
47
- result = update_database db, migration_definitions, version
48
- results[db.name] = result
49
- end
31
+
32
+ def update database_alias, version
33
+ database_alias = database_alias.to_s
34
+ db = MongoMapper.databases[database_alias]
35
+
36
+ if metadata(db).version == version
37
+ MongoMapper.logger.info "Database #{database_alias} already is of #{version} version, no migration needed."
38
+ return false
39
+ else
40
+ MongoMapper.logger.info "Migration for '#{database_alias}' Database:"
50
41
  end
51
- results
42
+
43
+ increase_db_version database_alias, db while metadata(db).version < version
44
+ decrease_db_version database_alias, db while metadata(db).version > version
45
+ true
52
46
  end
53
-
47
+
54
48
  def metadata db
55
49
  col = db.collection 'db_metadata'
56
50
  metadata = (col.find_one || {:version => 0}).to_openobject
57
51
  end
58
52
 
59
- def global_migration_definitions; @global_migration_definitions ||= [] end
60
- def migration_definitions; @migration_definitions ||= [] end
53
+ def definitions
54
+ @definitions ||= Hash.new{|h, k| h[k] = []}
55
+ end
61
56
 
62
57
  protected
63
- def increase_db_version migration_definitions, db
58
+ def increase_db_version database_alias, db
64
59
  m = metadata(db)
65
- migration = migration_definitions[m.version + 1]
66
- raise "No upgrade for version #{m.version + 1}" unless migration and migration.up
60
+ migration = definitions[database_alias][m.version + 1]
61
+ raise "No upgrade for version #{m.version + 1} of #{database_alias} Database!" unless migration and migration.up
67
62
 
68
- migration.up.call
63
+ migration.up.call db
69
64
 
70
- m.version += 1
65
+ m.version += 1
71
66
  update_metadata db, m
72
67
 
73
- puts "Database #{db.name} upgraded to version #{m.version}"
68
+ MongoMapper.logger.info "Database #{database_alias} upgraded to version #{m.version}."
74
69
  end
75
70
 
76
- def decrease_db_version migration_definitions, db
71
+ def decrease_db_version database_alias, db
77
72
  m = metadata(db)
78
- migration = migration_definitions[m.version]
79
- raise "No downgrade for version #{m.version}" unless migration and migration.down
73
+ migration = definitions[database_alias][m.version]
74
+ raise "No downgrade for version #{m.version} of #{database_alias} Database!" unless migration and migration.down
80
75
 
81
- migration.down.call
76
+ migration.down.call db
82
77
 
83
78
  m.version -= 1
84
79
  update_metadata db, m
85
80
 
86
- puts "Database #{db.name} downgraded to version #{m.version}"
81
+ MongoMapper.logger.info "Database #{database_alias} downgraded to version #{m.version}."
87
82
  end
88
-
89
- def update_database db, migrations, version
90
- if metadata(db).version == version
91
- puts "Database #{db.name} already is of #{version} version, no migration needed"
92
- return false
93
- end
94
83
 
95
- increase_db_version migrations, db while metadata(db).version < version
96
- decrease_db_version migrations, db while metadata(db).version > version
97
- true
98
- end
99
84
 
100
85
  def update_metadata db, metadata
101
86
  col = db.collection 'db_metadata'
102
87
  col.save metadata.to_hash
103
88
  end
104
-
105
- def reset
106
- [global_migration_definitions, migration_definitions].every.clear
107
- end
108
89
  end
109
90
  end
@@ -1,160 +1,11 @@
1
1
  MongoMapper.class_eval do
2
- const_set :ACCOUNT_TOKEN, ':account'
3
2
  class << self
4
- #
5
- # Config
6
- #
7
- attr_accessor :custom_config
8
-
9
- def config
10
- unless @config
11
- @config = if defined?(Rails) and defined?(DATABASE)
12
- global_database = DATABASE["#{Rails.env}!"].global_database!
13
- accounts_database = DATABASE["#{Rails.env}!"].accounts_database!
14
- config = {
15
- :global_database => {
16
- :name => global_database.name!,
17
- :host => global_database.host(nil),
18
- :port => global_database.port(nil)
19
- }.to_openobject,
20
- :accounts_database => {
21
- :name => accounts_database.name!,
22
- :host => accounts_database.host(nil),
23
- :port => accounts_database.port(nil)
24
- }.to_openobject
25
- }.to_openobject
26
- else
27
- raise "If you don't using Rails you must override this method and provide your own custom_config!" if custom_config.blank?
28
- custom_config
29
- end
30
-
31
- # Global database should't looks the same as Account database. We need to distinguish it by names!
32
- db_pattern = accounts_database.name
33
- re = Regexp.new(db_pattern.gsub(MongoMapper::ACCOUNT_TOKEN, '.+'))
34
- raise "Global Database Name shouldn't be similar to Account Database Name!" if global_database.name =~ re
35
- end
36
- @config
37
- end
38
-
39
- #
40
- # Global Database
41
- #
42
- def global_connection
43
- unless @global_connection
44
- @global_connection = Mongo::Connection.new(config.global_database.host, config.global_database.port, :logger => logger)
45
-
46
- if defined?(PhusionPassenger)
47
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
48
- @global_connection.connect_to_master if forked
49
- end
50
- end
51
- end
52
- @global_connection
53
- end
54
-
55
- def global_database
56
- @global_database ||= global_connection.db config.global_database.name
57
- end
58
-
59
- def current_global_database
60
- global_database
61
- end
62
-
63
-
64
- #
65
- # Accounts Database
66
- #
67
- def account_connection
68
- unless @account_connection
69
- @account_connection = Mongo::Connection.new(config.accounts_database.host, config.accounts_database.port, :logger => logger)
70
-
71
- if defined?(PhusionPassenger)
72
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
73
- @account_connection.connect_to_master if forked
74
- end
75
- end
76
- end
77
- @account_connection
78
- end
79
-
80
- attr_accessor :multitenant_mode
81
- def multitenant_mode?; !!@multitenant_mode end
82
-
83
- def account_database_name account_name
84
- db_name_template = config.accounts_database.name
85
- db_name_template.should! :include, MongoMapper::ACCOUNT_TOKEN
86
- db_name = db_name_template.gsub(MongoMapper::ACCOUNT_TOKEN, account_name)
87
- end
88
-
89
- def account_database account_name, &block
90
- self.logger.warn "Previous database #{self.database.name} has note be closed!" if multitenant_mode?
91
-
92
- db_name = account_database_name account_name
93
-
94
- begin
95
- self.connection = account_connection
96
- self.database = db_name
97
- self.multitenant_mode = true
98
- block.call self.database if block
99
- ensure
100
- if block
101
- self.multitenant_mode = false
102
- self.database = nil
103
- self.connection = nil
104
- else
105
- self.logger.warn "Account Database #{self.database.name} will not be closed automatically!"
106
- end
107
- end
108
- end
109
-
110
- def current_account_database
111
- multitenant_mode? ? database : nil
112
- end
113
3
 
114
4
  def logger
115
5
  return @logger ||= Logger.new(nil) if defined?(Spec)
116
-
6
+
117
7
  @logger ||= ("irb" == $0) ? Logger.new(STDOUT) : (defined?(Rails) ? Rails.logger : Logger.new(STDOUT))
118
8
  end
119
9
 
120
- def accounts_names
121
- accounts_names = []
122
- db_pattern = MongoMapper.config.accounts_database.name
123
- re = Regexp.new(db_pattern.gsub(MongoMapper::ACCOUNT_TOKEN, '.+'))
124
-
125
- token_start = db_pattern.index MongoMapper::ACCOUNT_TOKEN
126
- token_size = MongoMapper::ACCOUNT_TOKEN.size
127
-
128
- prefix_size = token_start
129
- postfix_size = db_pattern.size - prefix_size - token_size
130
-
131
- MongoMapper.account_connection.database_names.each do |name|
132
- if name =~ re
133
- account_name = name[prefix_size..-(postfix_size + 1)]
134
- accounts_names << account_name
135
- end
136
- end
137
- accounts_names
138
- end
139
- end
140
- end
141
-
142
-
143
- #
144
- # cache
145
- #
146
- MongoMapper::Document::InstanceMethods.class_eval do
147
- def cache
148
- @cache ||= {}
149
- end
150
-
151
- def clear_cache
152
- @cache = {}
153
- end
154
-
155
- def reload_with_cache
156
- @cache.clear if @cache
157
- reload_without_cache
158
10
  end
159
- alias_method_chain :reload, :cache
160
11
  end
@@ -0,0 +1,22 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module AttributesCache
4
+
5
+ module InstanceMethods
6
+ def cache
7
+ @cache ||= {}
8
+ end
9
+
10
+ def clear_cache
11
+ @cache = {}
12
+ end
13
+
14
+ def reload
15
+ @cache.clear if @cache
16
+ super
17
+ end
18
+ end
19
+
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module DefaultScope
4
+
5
+ module ClassMethods
6
+ def default_scope options = nil, &block
7
+ options.should! :be_a, [NilClass, Hash]
8
+ self.write_inheritable_attribute(:default_scope, (options || block))
9
+ end
10
+
11
+ def find_one options = {}
12
+ super default_scope_options.merge(options)
13
+ end
14
+
15
+ def find_many options = {}
16
+ super default_scope_options.merge(options)
17
+ end
18
+
19
+ def count options={}
20
+ super default_scope_options.merge(options)
21
+ end
22
+
23
+ protected
24
+ def default_scope_options
25
+ default_scope = self.read_inheritable_attribute(:default_scope)
26
+ if default_scope
27
+ if default_scope.respond_to? :call
28
+ default_scope.call self
29
+ else
30
+ default_scope
31
+ end
32
+ else
33
+ {}
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ module MongoMapper
2
+ module Plugins
3
+ module Micelaneous
4
+
5
+ module ClassMethods
6
+ def use_database database_alias
7
+ database_alias = database_alias.to_s
8
+ MongoMapper.db_config.should! :include, database_alias
9
+
10
+ self.connection MongoMapper.connections[database_alias]
11
+ set_database_name MongoMapper.db_config[database_alias]['name']
12
+ end
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -1,11 +1,7 @@
1
- MongoMapper.account_database 'development'
2
-
3
1
  Spec::Runner.configure do |config|
4
2
  config.before(:each) do
5
- dbs = [MongoMapper.current_global_database]
6
- dbs << MongoMapper.current_account_database unless MongoMapper.current_account_database.blank?
7
-
8
- dbs.each do |db|
3
+ MongoMapper.db_config.each do |db_alias, opt|
4
+ db = MongoMapper.databases[db_alias]
9
5
  db.collection_names.each do |name|
10
6
  db.collection(name).drop
11
7
  end