apartment 0.22.1 → 0.23.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -2
  3. data/.rspec +2 -1
  4. data/.travis.yml +3 -1
  5. data/Appraisals +7 -0
  6. data/Gemfile +0 -21
  7. data/HISTORY.md +6 -0
  8. data/README.md +13 -71
  9. data/Rakefile +4 -2
  10. data/TODO.md +0 -5
  11. data/apartment.gemspec +19 -0
  12. data/gemfiles/rails3.2.gemfile +7 -0
  13. data/gemfiles/rails4.0.gemfile +7 -0
  14. data/lib/apartment.rb +2 -28
  15. data/lib/apartment/adapters/abstract_adapter.rb +0 -2
  16. data/lib/apartment/adapters/abstract_jdbc_adapter.rb +2 -0
  17. data/lib/apartment/adapters/jdbc_mysql_adapter.rb +2 -0
  18. data/lib/apartment/adapters/jdbc_postgresql_adapter.rb +1 -1
  19. data/lib/apartment/adapters/mysql2_adapter.rb +2 -3
  20. data/lib/apartment/adapters/postgis_adapter.rb +0 -1
  21. data/lib/apartment/adapters/postgresql_adapter.rb +2 -3
  22. data/lib/apartment/adapters/sqlite3_adapter.rb +2 -0
  23. data/lib/apartment/database.rb +0 -4
  24. data/lib/apartment/elevators/domain.rb +2 -0
  25. data/lib/apartment/elevators/first_subdomain.rb +2 -0
  26. data/lib/apartment/elevators/generic.rb +3 -0
  27. data/lib/apartment/elevators/host_hash.rb +2 -0
  28. data/lib/apartment/elevators/subdomain.rb +19 -2
  29. data/lib/apartment/migrator.rb +2 -1
  30. data/lib/apartment/railtie.rb +2 -2
  31. data/lib/apartment/reloader.rb +0 -3
  32. data/lib/apartment/version.rb +2 -2
  33. data/lib/tasks/apartment.rake +2 -0
  34. data/spec/adapters/jdbc_mysql_adapter_spec.rb +1 -3
  35. data/spec/adapters/jdbc_postgresql_adapter_spec.rb +1 -3
  36. data/spec/adapters/mysql2_adapter_spec.rb +1 -2
  37. data/spec/adapters/postgresql_adapter_spec.rb +1 -2
  38. data/spec/adapters/sqlite3_adapter_spec.rb +1 -2
  39. data/spec/database_spec.rb +32 -50
  40. data/spec/dummy/app/models/user.rb +0 -2
  41. data/spec/dummy/config/application.rb +2 -0
  42. data/spec/dummy/config/environments/development.rb +2 -0
  43. data/spec/dummy/config/environments/production.rb +2 -0
  44. data/spec/dummy/config/environments/test.rb +1 -2
  45. data/spec/dummy/config/initializers/apartment.rb +1 -1
  46. data/spec/dummy/db/seeds.rb +1 -4
  47. data/spec/examples/elevator_examples.rb +4 -4
  48. data/spec/integration/apartment_rake_integration_spec.rb +15 -19
  49. data/spec/integration/middleware/domain_elevator_spec.rb +4 -3
  50. data/spec/integration/middleware/generic_elevator_spec.rb +4 -3
  51. data/spec/integration/middleware/subdomain_elevator_spec.rb +29 -3
  52. data/spec/integration/query_caching_spec.rb +8 -4
  53. data/spec/schemas/v1.rb +16 -0
  54. data/spec/schemas/v2.rb +43 -0
  55. data/spec/schemas/v3.rb +49 -0
  56. data/spec/spec_helper.rb +5 -11
  57. data/spec/support/apartment_helpers.rb +4 -2
  58. data/spec/support/contexts.rb +15 -19
  59. data/spec/support/requirements.rb +1 -17
  60. data/spec/support/setup.rb +47 -0
  61. data/spec/tasks/apartment_rake_spec.rb +6 -3
  62. data/spec/unit/config_spec.rb +3 -3
  63. data/spec/unit/middleware/domain_elevator_spec.rb +1 -2
  64. data/spec/{integration → unit}/middleware/first_subdomain_elevator_spec.rb +1 -0
  65. data/spec/unit/middleware/host_hash_elevator_spec.rb +1 -2
  66. data/spec/unit/middleware/subdomain_elevator_spec.rb +1 -2
  67. data/spec/unit/migrator_spec.rb +5 -4
  68. data/spec/unit/reloader_spec.rb +6 -4
  69. metadata +130 -14
  70. data/lib/apartment/delayed_job/enqueue.rb +0 -26
  71. data/lib/apartment/delayed_job/hooks.rb +0 -26
  72. data/lib/apartment/delayed_job/psych_ext.rb +0 -61
  73. data/lib/apartment/delayed_job/requirements.rb +0 -23
  74. data/lib/apartment/delayed_job/syck_ext.rb +0 -29
  75. data/spec/dummy/lib/fake_dj_class.rb +0 -6
  76. data/spec/integration/delayed_job_integration_spec.rb +0 -99
@@ -1,61 +0,0 @@
1
- if defined?(ActiveRecord)
2
- class ActiveRecord::Base
3
- # @override
4
- # serialize to YAML
5
- def encode_with(coder)
6
- coder["attributes"] = @attributes
7
- coder["database"] = @database unless @database.nil?
8
- coder.tag = ['!ruby/ActiveRecord', self.class.name].join(':')
9
- end
10
- end
11
- end
12
-
13
- module Psych
14
- module Visitors
15
- class ToRuby
16
- # @override
17
- #
18
- # NOTE I don't have a great idea what's going on here...
19
- # The only change is the `Apartment::Database.process`
20
- #
21
- def visit_Psych_Nodes_Mapping_with_class_and_db(object)
22
- return revive(Psych.load_tags[object.tag], object) if Psych.load_tags[object.tag]
23
-
24
- case object.tag
25
- when /^!ruby\/ActiveRecord:(.+)$/
26
- klass = resolve_class($1)
27
- payload = Hash[*object.children.map { |c| accept c }]
28
- id = payload["attributes"][klass.primary_key]
29
- begin
30
- Apartment::Database.process(payload['database']) do
31
- klass.unscoped.find(id)
32
- end
33
- rescue ActiveRecord::RecordNotFound
34
- raise Delayed::DeserializationError
35
- end
36
- when /^!ruby\/Mongoid:(.+)$/
37
- klass = resolve_class($1)
38
- payload = Hash[*object.children.map { |c| accept c }]
39
- begin
40
- klass.find(payload["attributes"]["_id"])
41
- rescue Mongoid::Errors::DocumentNotFound
42
- raise Delayed::DeserializationError
43
- end
44
- when /^!ruby\/DataMapper:(.+)$/
45
- klass = resolve_class($1)
46
- payload = Hash[*object.children.map { |c| accept c }]
47
- begin
48
- primary_keys = klass.properties.select { |p| p.key? }
49
- key_names = primary_keys.map { |p| p.name.to_s }
50
- klass.get!(*key_names.map { |k| payload["attributes"][k] })
51
- rescue DataMapper::ObjectNotFoundError
52
- raise Delayed::DeserializationError
53
- end
54
- else
55
- visit_Psych_Nodes_Mapping_without_class_and_db(object)
56
- end
57
- end
58
- alias_method_chain :visit_Psych_Nodes_Mapping, :class_and_db
59
- end
60
- end
61
- end
@@ -1,23 +0,0 @@
1
- require 'apartment/delayed_job/enqueue'
2
-
3
- module Apartment
4
- module Delayed
5
-
6
- # Mix this module into any ActiveRecord model that gets serialized by DJ
7
- module Requirements
8
- attr_accessor :database
9
-
10
- def self.included(klass)
11
- klass.after_find :set_database # set db when records are pulled so they deserialize properly
12
- klass.before_save :set_database # set db before records are saved so that they also get deserialized properly
13
- end
14
-
15
- private
16
-
17
- def set_database
18
- @database = Apartment::Database.current_database
19
- end
20
-
21
- end
22
- end
23
- end
@@ -1,29 +0,0 @@
1
- module ActiveRecord
2
- class Base
3
-
4
- # Overriding Delayed Job's monkey_patch of ActiveRecord so that it works with Apartment
5
- yaml_as "tag:ruby.yaml.org,2002:ActiveRecord"
6
-
7
- def self.yaml_new(klass, tag, val)
8
- Apartment::Database.process(val['database']) do
9
- klass.find(val['attributes']['id'])
10
- end
11
- rescue ActiveRecord::RecordNotFound => e
12
- raise Delayed::DeserializationError, e.message
13
- end
14
-
15
- # Rails > 3.0 now uses encode_with to determine what to encode with yaml
16
- # @override to include database attribute
17
- def encode_with_with_database(coder)
18
- coder['database'] = @database if @database.present?
19
- encode_with_without_database(coder)
20
- end
21
- alias_method_chain :encode_with, :database
22
-
23
- # Remain backwards compatible with old yaml serialization
24
- def to_yaml_properties
25
- ['@attributes', '@database'] # add in database attribute for serialization
26
- end
27
-
28
- end
29
- end
@@ -1,6 +0,0 @@
1
- class FakeDjClass
2
-
3
- def perform
4
- end
5
-
6
- end
@@ -1,99 +0,0 @@
1
- require 'spec_helper'
2
- require 'delayed_job'
3
- require 'delayed_job_active_record'
4
-
5
- describe Apartment::Delayed do
6
- unless defined?(JRUBY_VERSION)
7
-
8
- # See apartment.yml file in dummy app config
9
-
10
- let(:config) { Apartment::Test.config['connections']['postgresql'].symbolize_keys }
11
- let(:database) { Apartment::Test.next_db }
12
- let(:database2) { Apartment::Test.next_db }
13
-
14
- before do
15
- ActiveRecord::Base.establish_connection config
16
- Apartment::Test.load_schema # load the Rails schema in the public db schema
17
- Apartment::Database.stub(:config).and_return config # Use postgresql database config for this test
18
-
19
- Apartment.configure do |config|
20
- config.use_schemas = true
21
- end
22
-
23
- Apartment::Database.create database
24
- Apartment::Database.create database2
25
- end
26
-
27
- after do
28
- Apartment::Test.drop_schema database
29
- Apartment::Test.drop_schema database2
30
- Apartment.reset
31
- end
32
-
33
- describe Apartment::Delayed::Requirements do
34
-
35
- before do
36
- Apartment::Database.switch database
37
- User.send(:include, Apartment::Delayed::Requirements)
38
- User.create
39
- end
40
-
41
- it "should initialize a database attribute on a class" do
42
- user = User.first
43
- user.database.should == database
44
- end
45
-
46
- context 'when there are defined callbacks' do
47
- before do
48
- User.class_eval do
49
- after_find :set_name
50
-
51
- def set_name
52
- self.name = "Some Name"
53
- end
54
- end
55
- end
56
-
57
- after do
58
- User.class_eval do
59
- reset_callbacks :find
60
- end
61
- end
62
-
63
- it "should not overwrite any previous after_initialize declarations" do
64
- user = User.first
65
- user.database.should == database
66
- user.name.should == "Some Name"
67
- end
68
- end
69
-
70
- it "should set the db on a new record before it saves" do
71
- user = User.create
72
- user.database.should == database
73
- end
74
-
75
- context "serialization" do
76
- it "should serialize the proper database attribute" do
77
- user_yaml = User.first.to_yaml
78
- Apartment::Database.switch database2
79
- user = YAML.load user_yaml
80
- user.database.should == database
81
- end
82
- end
83
- end
84
-
85
- describe Apartment::Delayed::Job::Hooks do
86
-
87
- let(:worker) { Delayed::Worker.new }
88
- let(:job) { Delayed::Job.enqueue User.new }
89
-
90
- it "should switch to previous db" do
91
- Apartment::Database.switch database
92
- worker.run(job)
93
-
94
- Apartment::Database.current_database.should == database
95
- end
96
- end
97
-
98
- end
99
- end