active_mongoid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.coveralls.yml +1 -0
  4. data/.gitignore +22 -0
  5. data/.ruby_gemset +1 -0
  6. data/.ruby_version +1 -0
  7. data/.travis.yml +11 -0
  8. data/Appraisals +7 -0
  9. data/Gemfile +4 -0
  10. data/LICENSE.txt +22 -0
  11. data/README.md +198 -0
  12. data/Rakefile +7 -0
  13. data/active_mongoid.gemspec +36 -0
  14. data/gemfiles/mongoid_2.8.gemfile +7 -0
  15. data/gemfiles/mongoid_2.8.gemfile.lock +105 -0
  16. data/gemfiles/mongoid_3.1.gemfile +7 -0
  17. data/gemfiles/mongoid_3.1.gemfile.lock +108 -0
  18. data/lib/active_mongoid.rb +8 -0
  19. data/lib/active_mongoid/associations.rb +82 -0
  20. data/lib/active_mongoid/associations/binding.rb +77 -0
  21. data/lib/active_mongoid/associations/builder.rb +26 -0
  22. data/lib/active_mongoid/associations/builders/in.rb +17 -0
  23. data/lib/active_mongoid/associations/builders/many.rb +15 -0
  24. data/lib/active_mongoid/associations/builders/one.rb +15 -0
  25. data/lib/active_mongoid/associations/document_relation/accessors.rb +100 -0
  26. data/lib/active_mongoid/associations/document_relation/associations.rb +33 -0
  27. data/lib/active_mongoid/associations/document_relation/auto_save.rb +31 -0
  28. data/lib/active_mongoid/associations/document_relation/bindings/in.rb +48 -0
  29. data/lib/active_mongoid/associations/document_relation/bindings/many.rb +19 -0
  30. data/lib/active_mongoid/associations/document_relation/bindings/one.rb +19 -0
  31. data/lib/active_mongoid/associations/document_relation/builders.rb +31 -0
  32. data/lib/active_mongoid/associations/document_relation/dependent.rb +26 -0
  33. data/lib/active_mongoid/associations/document_relation/macros.rb +51 -0
  34. data/lib/active_mongoid/associations/document_relation/referenced/in.rb +72 -0
  35. data/lib/active_mongoid/associations/document_relation/referenced/many.rb +125 -0
  36. data/lib/active_mongoid/associations/document_relation/referenced/one.rb +75 -0
  37. data/lib/active_mongoid/associations/many.rb +211 -0
  38. data/lib/active_mongoid/associations/metadata.rb +229 -0
  39. data/lib/active_mongoid/associations/one.rb +21 -0
  40. data/lib/active_mongoid/associations/proxy.rb +38 -0
  41. data/lib/active_mongoid/associations/record_relation/accessors.rb +80 -0
  42. data/lib/active_mongoid/associations/record_relation/associations.rb +33 -0
  43. data/lib/active_mongoid/associations/record_relation/auto_save.rb +43 -0
  44. data/lib/active_mongoid/associations/record_relation/bindings/in.rb +48 -0
  45. data/lib/active_mongoid/associations/record_relation/bindings/many.rb +19 -0
  46. data/lib/active_mongoid/associations/record_relation/bindings/one.rb +19 -0
  47. data/lib/active_mongoid/associations/record_relation/builders.rb +31 -0
  48. data/lib/active_mongoid/associations/record_relation/dependent.rb +26 -0
  49. data/lib/active_mongoid/associations/record_relation/macros.rb +65 -0
  50. data/lib/active_mongoid/associations/record_relation/referenced/in.rb +72 -0
  51. data/lib/active_mongoid/associations/record_relation/referenced/many.rb +128 -0
  52. data/lib/active_mongoid/associations/record_relation/referenced/one.rb +75 -0
  53. data/lib/active_mongoid/associations/targets/enumerable.rb +161 -0
  54. data/lib/active_mongoid/bson_id.rb +44 -0
  55. data/lib/active_mongoid/finder_proxy.rb +55 -0
  56. data/lib/active_mongoid/finders.rb +60 -0
  57. data/lib/active_mongoid/version.rb +3 -0
  58. data/spec/lib/associations/document_relation/accessors_spec.rb +330 -0
  59. data/spec/lib/associations/document_relation/auto_save_spec.rb +157 -0
  60. data/spec/lib/associations/document_relation/bindings/in_spec.rb +39 -0
  61. data/spec/lib/associations/document_relation/bindings/many_spec.rb +36 -0
  62. data/spec/lib/associations/document_relation/bindings/one_spec.rb +39 -0
  63. data/spec/lib/associations/document_relation/builders_spec.rb +117 -0
  64. data/spec/lib/associations/document_relation/dependent_spec.rb +87 -0
  65. data/spec/lib/associations/document_relation/macros_spec.rb +68 -0
  66. data/spec/lib/associations/document_relation/referenced/in_spec.rb +27 -0
  67. data/spec/lib/associations/document_relation/referenced/many_spec.rb +32 -0
  68. data/spec/lib/associations/document_relation/referenced/one_spec.rb +28 -0
  69. data/spec/lib/associations/metadata_spec.rb +157 -0
  70. data/spec/lib/associations/record_relation/accessors_spec.rb +328 -0
  71. data/spec/lib/associations/record_relation/auto_save_spec.rb +157 -0
  72. data/spec/lib/associations/record_relation/bindings/in_spec.rb +39 -0
  73. data/spec/lib/associations/record_relation/bindings/many_spec.rb +39 -0
  74. data/spec/lib/associations/record_relation/bindings/one_spec.rb +57 -0
  75. data/spec/lib/associations/record_relation/builders_spec.rb +118 -0
  76. data/spec/lib/associations/record_relation/dependent_spec.rb +87 -0
  77. data/spec/lib/associations/record_relation/macros_spec.rb +73 -0
  78. data/spec/lib/associations/record_relation/referenced/in_spec.rb +27 -0
  79. data/spec/lib/associations/record_relation/referenced/many_spec.rb +32 -0
  80. data/spec/lib/associations/record_relation/referenced/one_spec.rb +27 -0
  81. data/spec/lib/bson_id_spec.rb +48 -0
  82. data/spec/lib/finders_spec.rb +105 -0
  83. data/spec/spec_helper.rb +89 -0
  84. data/spec/support/models/active_record/address.rb +6 -0
  85. data/spec/support/models/active_record/division.rb +16 -0
  86. data/spec/support/models/active_record/division_setting.rb +9 -0
  87. data/spec/support/models/active_record/player.rb +12 -0
  88. data/spec/support/models/mongoid/league.rb +10 -0
  89. data/spec/support/models/mongoid/person.rb +9 -0
  90. data/spec/support/models/mongoid/post.rb +9 -0
  91. data/spec/support/models/mongoid/stat.rb +8 -0
  92. data/spec/support/models/mongoid/team.rb +9 -0
  93. data/spec/support/shared_examples/shared_many_spec.rb +411 -0
  94. metadata +370 -0
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::RecordRelation::Macros do
4
+
5
+ class MongoidTestClass
6
+ include Mongoid::Document
7
+ include ActiveMongoid::Associations
8
+ end
9
+
10
+ let(:klass) do
11
+ MongoidTestClass
12
+ end
13
+
14
+ before do
15
+ klass.am_relations.clear
16
+ end
17
+
18
+ describe ".belongs_to_record" do
19
+
20
+ it "defines the macro" do
21
+ expect(klass).to respond_to(:belongs_to_record)
22
+ end
23
+
24
+ context "when defining the relation" do
25
+
26
+ before do
27
+ klass.belongs_to_record(:person)
28
+ end
29
+
30
+ let(:object) do
31
+ klass.new
32
+ end
33
+
34
+ it "adds the metadata to the klass" do
35
+ expect(klass.am_relations["person"]).to_not be_nil
36
+ end
37
+
38
+ it "defines the getter" do
39
+ expect(object).to respond_to(:person)
40
+ end
41
+
42
+ it "defines the setter" do
43
+ expect(object).to respond_to(:person=)
44
+ end
45
+
46
+ it "creates the correct relation" do
47
+ expect(klass.am_relations["person"].relation).to eq(
48
+ ActiveMongoid::Associations::RecordRelation::Referenced::In
49
+ )
50
+ end
51
+
52
+ it "creates the field for the foreign key" do
53
+ expect(object).to respond_to(:person_id)
54
+ end
55
+
56
+ context "metadata properties" do
57
+
58
+ let(:metadata) do
59
+ klass.am_relations["person"]
60
+ end
61
+
62
+ it "automatically adds the name" do
63
+ expect(metadata.name).to eq("person")
64
+ end
65
+
66
+ it "automatically adds the inverse class name" do
67
+ expect(metadata.inverse_class_name).to eq("MongoidTestClass")
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::RecordRelation::Referenced::In do
4
+
5
+ describe ".criteria" do
6
+
7
+
8
+ context "" do
9
+
10
+ let(:id) do
11
+ 1
12
+ end
13
+
14
+ let(:metadata) do
15
+ Person.am_relations["player"]
16
+ end
17
+
18
+ let(:criteria) do
19
+ described_class.criteria(metadata, id, Player)
20
+ end
21
+
22
+ it "does not include the type in the criteria" do
23
+ expect(criteria.where_values_hash).to eq({"id" => id})
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::DocumentRelation::Referenced::Many do
4
+
5
+ describe ".criteria" do
6
+
7
+ let(:id) do
8
+ 1
9
+ end
10
+
11
+ let(:metadata) do
12
+ Team.am_relations["players"]
13
+ end
14
+
15
+ let(:criteria) do
16
+ described_class.criteria(metadata, id, Player)
17
+ end
18
+
19
+ it "does not include the type in the criteria" do
20
+ expect(criteria.where_values_hash).to eq({"team_id" => id})
21
+ expect(criteria.klass).to eq(Player)
22
+ end
23
+
24
+ end
25
+
26
+ it_behaves_like "a has_many relation" do
27
+ let(:base_class) { Team }
28
+ let(:target_class) { Player }
29
+ let(:relation_name) { :players }
30
+ end
31
+
32
+ end
@@ -0,0 +1,27 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Associations::RecordRelation::Referenced::One do
4
+
5
+ describe ".criteria" do
6
+
7
+
8
+ context "" do
9
+
10
+ let(:id) do
11
+ 1
12
+ end
13
+
14
+ let(:metadata) do
15
+ League.am_relations["division"]
16
+ end
17
+
18
+ let(:criteria) do
19
+ described_class.criteria(metadata, id, Division)
20
+ end
21
+
22
+ it "does not include the type in the criteria" do
23
+ expect(criteria.where_values_hash).to eq({"league_id" => id})
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,48 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::BsonId do
4
+ let(:klass) { Division }
5
+ let(:division) { klass.new }
6
+
7
+ describe "bson attr getter" do
8
+
9
+ it "defines the getter" do
10
+ expect(division).to respond_to(:_id)
11
+ end
12
+
13
+ it "initializes with a bson_id when option is present" do
14
+ expect(division._id).to_not be_nil
15
+ end
16
+
17
+ it "does not initializes with a bson_id when option is not present" do
18
+ expect(division.sport_id).to be_nil
19
+ end
20
+
21
+ it "returns valid bson_id" do
22
+ expect(::ActiveMongoid::BSON::ObjectId.legal?(division._id))
23
+ end
24
+
25
+ end
26
+
27
+ describe "bson attr setter" do
28
+
29
+ let(:bson_id) { ::ActiveMongoid::BSON::ObjectId.new }
30
+
31
+ before do
32
+ division.sport_id = bson_id
33
+ end
34
+
35
+ it "defines the setter" do
36
+ expect(division).to respond_to(:_id=)
37
+ end
38
+
39
+ it "sets attr from bson object" do
40
+ expect(division.sport_id).to eq(bson_id)
41
+ end
42
+
43
+ it "sets attr as string" do
44
+ expect(division.read_attribute(:sport_id)).to eq(bson_id.to_s)
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,105 @@
1
+ require "spec_helper"
2
+
3
+ describe ActiveMongoid::Finders do
4
+ let(:klass) { Division }
5
+ let(:division) { klass.create(name: 'foo') }
6
+
7
+ describe ".find" do
8
+
9
+ describe "when not scoped" do
10
+
11
+ it "returns object when request with object fixnum id" do
12
+ expect(klass.find(division.id)).to eq(division)
13
+ end
14
+
15
+ it "returns object when request with object bson id" do
16
+ expect(klass.find(division._id)).to eq(division)
17
+ end
18
+
19
+ it "raises NotFound exception when request with fixnum id" do
20
+ expect{klass.find(-1)}.to raise_error(ActiveRecord::RecordNotFound)
21
+ end
22
+
23
+ it "raises NotFound exception when request with bson id" do
24
+ expect{klass.find(::ActiveMongoid::BSON::ObjectId.new)}.to raise_error(ActiveRecord::RecordNotFound)
25
+ end
26
+
27
+ end
28
+
29
+ describe "when scoped" do
30
+
31
+ let(:scoped_klass) { klass.by_name(division.name) }
32
+
33
+ it "returns object when request with object fixnum id" do
34
+ expect(scoped_klass.find(division.id)).to eq(division)
35
+ end
36
+
37
+ it "returns object when request with object bson id" do
38
+ expect(scoped_klass.find(division._id)).to eq(division)
39
+ end
40
+
41
+ it "raises NotFound exception when request with fixnum id" do
42
+ expect{scoped_klass.find(-1)}.to raise_error(ActiveRecord::RecordNotFound)
43
+ end
44
+
45
+ it "raises NotFound exception when request with bson id" do
46
+ expect{scoped_klass.find(::ActiveMongoid::BSON::ObjectId.new)}.to raise_error(ActiveRecord::RecordNotFound)
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ describe ".where" do
54
+
55
+ let(:bson_id) { ::ActiveMongoid::BSON::ObjectId.new }
56
+
57
+ before do
58
+ division.update_attributes(sport_id: bson_id)
59
+ end
60
+
61
+ describe "when not scoped" do
62
+
63
+ it "returns relation by bson_id" do
64
+ expect(klass.where(sport_id: bson_id)).to eq([division])
65
+ end
66
+
67
+ it "returns relation by bson_id string" do
68
+ expect(klass.where(sport_id: bson_id.to_s)).to eq([division])
69
+ end
70
+
71
+ it "return relation by primary key" do
72
+ expect(klass.where(id: division.id)).to eq([division])
73
+ end
74
+
75
+ it "return relation by bson primary key" do
76
+ expect(klass.where(id: division._id)).to eq([division])
77
+ end
78
+
79
+ end
80
+
81
+ describe "when scoped" do
82
+
83
+ let(:scoped_klass) { klass.by_name(division.name) }
84
+
85
+ it "returns relation by bson_id" do
86
+ expect(scoped_klass.where(sport_id: bson_id)).to eq([division])
87
+ end
88
+
89
+ it "returns relation by bson_id string" do
90
+ expect(scoped_klass.where(sport_id: bson_id.to_s)).to eq([division])
91
+ end
92
+
93
+ it "return relation by primary key" do
94
+ expect(scoped_klass.where(id: division.id)).to eq([division])
95
+ end
96
+
97
+ it "return relation by bson primary key" do
98
+ expect(scoped_klass.where(id: division._id)).to eq([division])
99
+ end
100
+
101
+ end
102
+
103
+ end
104
+
105
+ end
@@ -0,0 +1,89 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'simplecov'
5
+ require 'simplecov-gem-adapter'
6
+
7
+ require 'coveralls'
8
+
9
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
10
+ SimpleCov::Formatter::HTMLFormatter,
11
+ Coveralls::SimpleCov::Formatter
12
+ ]
13
+
14
+ SimpleCov.start 'gem'
15
+
16
+ require 'i18n'
17
+ I18n.enforce_available_locales = false
18
+
19
+ require 'mongoid'
20
+ require 'active_record'
21
+ require 'active_mongoid'
22
+ require 'database_cleaner'
23
+
24
+ require 'pry'
25
+
26
+ require 'rspec'
27
+
28
+ Mongoid.configure do |config|
29
+ if Mongoid::VERSION >= "3"
30
+ config.connect_to('active_mongoid_test')
31
+ else
32
+ config.master = Mongo::Connection.new.db('active_mongoid_test')
33
+ config.allow_dynamic_fields = false
34
+ end
35
+ config.identity_map_enabled = false
36
+ end
37
+
38
+ RSpec.configure do |config|
39
+ config.mock_with :rspec
40
+
41
+ config.before :suite do
42
+ DatabaseCleaner[:active_record].strategy = :transaction
43
+ DatabaseCleaner[:active_record].clean_with :truncation
44
+ DatabaseCleaner[:mongoid].clean_with :truncation
45
+ end
46
+
47
+ config.before :each do
48
+ DatabaseCleaner[:active_record].start
49
+ DatabaseCleaner[:mongoid].start
50
+ end
51
+
52
+ config.after :each do
53
+ DatabaseCleaner[:active_record].clean
54
+ DatabaseCleaner[:mongoid].clean
55
+ end
56
+ end
57
+
58
+
59
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
60
+ ActiveRecord::Schema.define do
61
+ create_table :players, :force => true do |t|
62
+ t.string :_id
63
+ t.string :name
64
+ t.string :title
65
+ t.string :team_id
66
+ end
67
+
68
+ create_table :divisions, :force => true do |t|
69
+ t.string :_id
70
+ t.string :name
71
+ t.string :league_id
72
+ t.string :pid
73
+ t.string :sport_id
74
+ end
75
+
76
+ create_table :division_settings, :force => true do |t|
77
+ t.string :_id
78
+ t.string :name
79
+ t.string :league_id
80
+ end
81
+
82
+ create_table :addresses, :force => true do |t|
83
+ t.string :_id
84
+ t.string :target_id
85
+ t.string :target_type
86
+ end
87
+ end
88
+
89
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
@@ -0,0 +1,6 @@
1
+ class Address < ActiveRecord::Base
2
+ include ActiveMongoid::Associations
3
+ include ActiveMongoid::Finders
4
+
5
+ belongs_to_document :target, polymorphic: true
6
+ end
@@ -0,0 +1,16 @@
1
+ class Division < ActiveRecord::Base
2
+ include ActiveMongoid::Associations
3
+ include ActiveMongoid::Finders
4
+ include ActiveMongoid::BsonId
5
+
6
+ has_many_documents :teams, order: [:name, :asc]
7
+ belongs_to_document :league
8
+ belongs_to_document :post, foreign_key: :pid
9
+ has_many_documents :stats, as: :target
10
+
11
+ scope :sport_id, ->(id){ where(sport_id: id) }
12
+ scope :by_name, ->(n){ where(name: n) }
13
+
14
+ bsonify_attr :_id, initialize: true
15
+ bsonify_attr :sport_id
16
+ end
@@ -0,0 +1,9 @@
1
+ module Settings
2
+ class DivisionSetting < ActiveRecord::Base
3
+ include ActiveMongoid::Associations
4
+ include ActiveMongoid::Finders
5
+
6
+ belongs_to_document :league
7
+ end
8
+ end
9
+
@@ -0,0 +1,12 @@
1
+ class Player < ActiveRecord::Base
2
+ include ActiveMongoid::Associations
3
+ include ActiveMongoid::Finders
4
+ include ActiveMongoid::BsonId
5
+
6
+ bsonify_attr :_id, initialize: true
7
+
8
+ belongs_to_document :team
9
+ has_one_document :person
10
+ has_one_document :post
11
+ has_one_document :stat, as: :target
12
+ end