thorsson-mongo_mapper 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +27 -0
- data/bin/mmconsole +60 -0
- data/examples/keys.rb +40 -0
- data/examples/modifiers/set.rb +25 -0
- data/examples/plugins.rb +41 -0
- data/examples/querying.rb +35 -0
- data/examples/scopes.rb +52 -0
- data/lib/mongo_mapper.rb +79 -0
- data/lib/mongo_mapper/connection.rb +83 -0
- data/lib/mongo_mapper/document.rb +41 -0
- data/lib/mongo_mapper/embedded_document.rb +31 -0
- data/lib/mongo_mapper/exceptions.rb +27 -0
- data/lib/mongo_mapper/extensions/array.rb +19 -0
- data/lib/mongo_mapper/extensions/binary.rb +22 -0
- data/lib/mongo_mapper/extensions/boolean.rb +44 -0
- data/lib/mongo_mapper/extensions/date.rb +25 -0
- data/lib/mongo_mapper/extensions/float.rb +14 -0
- data/lib/mongo_mapper/extensions/hash.rb +14 -0
- data/lib/mongo_mapper/extensions/integer.rb +19 -0
- data/lib/mongo_mapper/extensions/kernel.rb +9 -0
- data/lib/mongo_mapper/extensions/nil_class.rb +18 -0
- data/lib/mongo_mapper/extensions/object.rb +27 -0
- data/lib/mongo_mapper/extensions/object_id.rb +30 -0
- data/lib/mongo_mapper/extensions/set.rb +20 -0
- data/lib/mongo_mapper/extensions/string.rb +18 -0
- data/lib/mongo_mapper/extensions/time.rb +29 -0
- data/lib/mongo_mapper/plugins.rb +16 -0
- data/lib/mongo_mapper/plugins/accessible.rb +44 -0
- data/lib/mongo_mapper/plugins/associations.rb +97 -0
- data/lib/mongo_mapper/plugins/associations/base.rb +124 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +29 -0
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/collection.rb +22 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +127 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +28 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +110 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +32 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +24 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +14 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +40 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +68 -0
- data/lib/mongo_mapper/plugins/associations/proxy.rb +126 -0
- data/lib/mongo_mapper/plugins/caching.rb +21 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +241 -0
- data/lib/mongo_mapper/plugins/clone.rb +19 -0
- data/lib/mongo_mapper/plugins/descendants.rb +17 -0
- data/lib/mongo_mapper/plugins/dirty.rb +120 -0
- data/lib/mongo_mapper/plugins/document.rb +41 -0
- data/lib/mongo_mapper/plugins/dynamic_querying.rb +43 -0
- data/lib/mongo_mapper/plugins/dynamic_querying/dynamic_finder.rb +44 -0
- data/lib/mongo_mapper/plugins/embedded_document.rb +49 -0
- data/lib/mongo_mapper/plugins/equality.rb +17 -0
- data/lib/mongo_mapper/plugins/identity_map.rb +128 -0
- data/lib/mongo_mapper/plugins/indexes.rb +12 -0
- data/lib/mongo_mapper/plugins/inspect.rb +15 -0
- data/lib/mongo_mapper/plugins/keys.rb +309 -0
- data/lib/mongo_mapper/plugins/keys/key.rb +55 -0
- data/lib/mongo_mapper/plugins/logger.rb +18 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +112 -0
- data/lib/mongo_mapper/plugins/pagination.rb +14 -0
- data/lib/mongo_mapper/plugins/persistence.rb +69 -0
- data/lib/mongo_mapper/plugins/protected.rb +53 -0
- data/lib/mongo_mapper/plugins/querying.rb +176 -0
- data/lib/mongo_mapper/plugins/querying/decorator.rb +46 -0
- data/lib/mongo_mapper/plugins/querying/plucky_methods.rb +15 -0
- data/lib/mongo_mapper/plugins/rails.rb +58 -0
- data/lib/mongo_mapper/plugins/safe.rb +28 -0
- data/lib/mongo_mapper/plugins/sci.rb +32 -0
- data/lib/mongo_mapper/plugins/scopes.rb +21 -0
- data/lib/mongo_mapper/plugins/serialization.rb +76 -0
- data/lib/mongo_mapper/plugins/timestamps.rb +22 -0
- data/lib/mongo_mapper/plugins/userstamps.rb +15 -0
- data/lib/mongo_mapper/plugins/validations.rb +50 -0
- data/lib/mongo_mapper/support/descendant_appends.rb +45 -0
- data/lib/mongo_mapper/version.rb +4 -0
- data/test/_NOTE_ON_TESTING +1 -0
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +63 -0
- data/test/functional/associations/test_belongs_to_proxy.rb +93 -0
- data/test/functional/associations/test_in_array_proxy.rb +319 -0
- data/test/functional/associations/test_many_documents_as_proxy.rb +229 -0
- data/test/functional/associations/test_many_documents_proxy.rb +575 -0
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +176 -0
- data/test/functional/associations/test_many_embedded_proxy.rb +256 -0
- data/test/functional/associations/test_many_polymorphic_proxy.rb +302 -0
- data/test/functional/associations/test_one_embedded_proxy.rb +67 -0
- data/test/functional/associations/test_one_proxy.rb +182 -0
- data/test/functional/test_accessible.rb +168 -0
- data/test/functional/test_associations.rb +44 -0
- data/test/functional/test_binary.rb +27 -0
- data/test/functional/test_caching.rb +76 -0
- data/test/functional/test_callbacks.rb +151 -0
- data/test/functional/test_dirty.rb +163 -0
- data/test/functional/test_document.rb +253 -0
- data/test/functional/test_dynamic_querying.rb +75 -0
- data/test/functional/test_embedded_document.rb +210 -0
- data/test/functional/test_identity_map.rb +506 -0
- data/test/functional/test_indexes.rb +42 -0
- data/test/functional/test_logger.rb +20 -0
- data/test/functional/test_modifiers.rb +416 -0
- data/test/functional/test_pagination.rb +91 -0
- data/test/functional/test_protected.rb +175 -0
- data/test/functional/test_querying.rb +873 -0
- data/test/functional/test_safe.rb +76 -0
- data/test/functional/test_sci.rb +230 -0
- data/test/functional/test_scopes.rb +171 -0
- data/test/functional/test_string_id_compatibility.rb +67 -0
- data/test/functional/test_timestamps.rb +62 -0
- data/test/functional/test_userstamps.rb +27 -0
- data/test/functional/test_validations.rb +342 -0
- data/test/models.rb +233 -0
- data/test/test_active_model_lint.rb +13 -0
- data/test/test_helper.rb +104 -0
- data/test/unit/associations/test_base.rb +212 -0
- data/test/unit/associations/test_proxy.rb +105 -0
- data/test/unit/serializers/test_json_serializer.rb +202 -0
- data/test/unit/test_clone.rb +69 -0
- data/test/unit/test_descendant_appends.rb +71 -0
- data/test/unit/test_document.rb +213 -0
- data/test/unit/test_dynamic_finder.rb +125 -0
- data/test/unit/test_embedded_document.rb +644 -0
- data/test/unit/test_extensions.rb +380 -0
- data/test/unit/test_key.rb +185 -0
- data/test/unit/test_keys.rb +55 -0
- data/test/unit/test_mongo_mapper.rb +110 -0
- data/test/unit/test_pagination.rb +11 -0
- data/test/unit/test_plugins.rb +50 -0
- data/test/unit/test_rails.rb +181 -0
- data/test/unit/test_rails_compatibility.rb +52 -0
- data/test/unit/test_serialization.rb +51 -0
- data/test/unit/test_time_zones.rb +39 -0
- data/test/unit/test_validations.rb +544 -0
- metadata +316 -0
data/test/models.rb
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
# custom type
|
2
|
+
class WindowSize
|
3
|
+
attr_reader :width, :height
|
4
|
+
|
5
|
+
def self.to_mongo(value)
|
6
|
+
value.to_a
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.from_mongo(value)
|
10
|
+
value.is_a?(self) ? value : WindowSize.new(value)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(*args)
|
14
|
+
@width, @height = args.flatten
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_a
|
18
|
+
[width, height]
|
19
|
+
end
|
20
|
+
|
21
|
+
def ==(other)
|
22
|
+
other.is_a?(self.class) && other.width == width && other.height == height
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module AccountsExtensions
|
27
|
+
def inactive
|
28
|
+
all(:last_logged_in => nil)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Post
|
33
|
+
include MongoMapper::Document
|
34
|
+
|
35
|
+
key :title, String
|
36
|
+
key :body, String
|
37
|
+
|
38
|
+
many :comments, :as => :commentable, :class_name => 'PostComment'
|
39
|
+
|
40
|
+
timestamps!
|
41
|
+
end
|
42
|
+
|
43
|
+
class PostComment
|
44
|
+
include MongoMapper::Document
|
45
|
+
|
46
|
+
key :username, String, :default => 'Anonymous'
|
47
|
+
key :body, String
|
48
|
+
|
49
|
+
key :commentable_id, ObjectId
|
50
|
+
key :commentable_type, String
|
51
|
+
belongs_to :commentable, :polymorphic => true
|
52
|
+
|
53
|
+
timestamps!
|
54
|
+
end
|
55
|
+
|
56
|
+
class Address
|
57
|
+
include MongoMapper::EmbeddedDocument
|
58
|
+
|
59
|
+
key :address, String
|
60
|
+
key :city, String
|
61
|
+
key :state, String
|
62
|
+
key :zip, Integer
|
63
|
+
end
|
64
|
+
|
65
|
+
class Message
|
66
|
+
include MongoMapper::Document
|
67
|
+
|
68
|
+
key :body, String
|
69
|
+
key :position, Integer
|
70
|
+
key :room_id, ObjectId
|
71
|
+
|
72
|
+
belongs_to :room
|
73
|
+
end
|
74
|
+
|
75
|
+
class Enter < Message; end
|
76
|
+
class Exit < Message; end
|
77
|
+
class Chat < Message; end
|
78
|
+
|
79
|
+
class Room
|
80
|
+
include MongoMapper::Document
|
81
|
+
|
82
|
+
key :name, String
|
83
|
+
many :messages, :polymorphic => true, :order => 'position' do
|
84
|
+
def older
|
85
|
+
all(:position => {'$gt' => 5})
|
86
|
+
end
|
87
|
+
end
|
88
|
+
many :latest_messages, :class_name => 'Message', :order => 'position desc', :limit => 2
|
89
|
+
|
90
|
+
many :accounts, :polymorphic => true, :extend => AccountsExtensions
|
91
|
+
end
|
92
|
+
|
93
|
+
class Account
|
94
|
+
include MongoMapper::Document
|
95
|
+
|
96
|
+
key :room_id, ObjectId
|
97
|
+
key :last_logged_in, Time
|
98
|
+
|
99
|
+
belongs_to :room
|
100
|
+
end
|
101
|
+
class AccountUser < Account; end
|
102
|
+
class Bot < Account; end
|
103
|
+
|
104
|
+
class Answer
|
105
|
+
include MongoMapper::Document
|
106
|
+
|
107
|
+
key :body, String
|
108
|
+
end
|
109
|
+
|
110
|
+
module CollaboratorsExtensions
|
111
|
+
def top
|
112
|
+
first
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
class Project
|
117
|
+
include MongoMapper::Document
|
118
|
+
|
119
|
+
key :name, String
|
120
|
+
|
121
|
+
many :collaborators, :extend => CollaboratorsExtensions
|
122
|
+
many :statuses, :order => 'position' do
|
123
|
+
def open
|
124
|
+
all(:name => %w(New Assigned))
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
many :addresses do
|
129
|
+
def find_all_by_state(state)
|
130
|
+
# can't use select here for some reason
|
131
|
+
find_all { |a| a.state == state }
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
class Collaborator
|
137
|
+
include MongoMapper::Document
|
138
|
+
key :project_id, ObjectId
|
139
|
+
key :name, String
|
140
|
+
belongs_to :project
|
141
|
+
end
|
142
|
+
|
143
|
+
class Status
|
144
|
+
include MongoMapper::Document
|
145
|
+
|
146
|
+
scope :complete, where(:name => 'Complete')
|
147
|
+
|
148
|
+
def self.by_position(position)
|
149
|
+
where(:position => position)
|
150
|
+
end
|
151
|
+
|
152
|
+
key :project_id, ObjectId
|
153
|
+
key :target_id, ObjectId
|
154
|
+
key :target_type, String
|
155
|
+
key :name, String, :required => true
|
156
|
+
key :position, Integer
|
157
|
+
|
158
|
+
belongs_to :project
|
159
|
+
belongs_to :target, :polymorphic => true
|
160
|
+
end
|
161
|
+
|
162
|
+
class Media
|
163
|
+
include MongoMapper::EmbeddedDocument
|
164
|
+
|
165
|
+
key :file, String
|
166
|
+
key :visible, Boolean
|
167
|
+
end
|
168
|
+
|
169
|
+
class Video < Media
|
170
|
+
key :length, Integer
|
171
|
+
end
|
172
|
+
|
173
|
+
class Image < Media
|
174
|
+
key :width, Integer
|
175
|
+
key :height, Integer
|
176
|
+
end
|
177
|
+
|
178
|
+
class Music < Media
|
179
|
+
key :bitrate, String
|
180
|
+
end
|
181
|
+
|
182
|
+
class Catalog
|
183
|
+
include MongoMapper::Document
|
184
|
+
|
185
|
+
many :medias, :polymorphic => true do
|
186
|
+
def visible
|
187
|
+
# for some reason we can't use select here
|
188
|
+
find_all { |m| m.visible? }
|
189
|
+
end
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
module TrModels
|
194
|
+
class Transport
|
195
|
+
include MongoMapper::EmbeddedDocument
|
196
|
+
|
197
|
+
key :license_plate, String
|
198
|
+
key :purchased_on, Date
|
199
|
+
end
|
200
|
+
|
201
|
+
class Car < TrModels::Transport
|
202
|
+
include MongoMapper::EmbeddedDocument
|
203
|
+
|
204
|
+
key :model, String
|
205
|
+
key :year, Integer
|
206
|
+
end
|
207
|
+
|
208
|
+
class Bus < TrModels::Transport
|
209
|
+
include MongoMapper::EmbeddedDocument
|
210
|
+
|
211
|
+
key :max_passengers, Integer
|
212
|
+
end
|
213
|
+
|
214
|
+
class Ambulance < TrModels::Transport
|
215
|
+
include MongoMapper::EmbeddedDocument
|
216
|
+
|
217
|
+
key :icu, Boolean
|
218
|
+
end
|
219
|
+
|
220
|
+
class Fleet
|
221
|
+
include MongoMapper::Document
|
222
|
+
|
223
|
+
module TransportsExtension
|
224
|
+
def to_be_replaced
|
225
|
+
# for some reason we can't use select
|
226
|
+
find_all { |t| t.purchased_on < 2.years.ago.to_date }
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
many :transports, :polymorphic => true, :class_name => "TrModels::Transport", :extend => TransportsExtension
|
231
|
+
key :name, String
|
232
|
+
end
|
233
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
# For testing against edge rails also.
|
3
|
+
# $:.unshift '/Users/jnunemaker/dev/ruby/rails/activemodel/lib'
|
4
|
+
require 'active_model'
|
5
|
+
require 'models'
|
6
|
+
|
7
|
+
class ActiveModelLintTest < ActiveModel::TestCase
|
8
|
+
include ActiveModel::Lint::Tests
|
9
|
+
|
10
|
+
def setup
|
11
|
+
@model = Post.new
|
12
|
+
end
|
13
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'activesupport', ENV['ACTIVE_SUPPORT_VERSION']
|
3
|
+
gem 'json', '~> 1.2.3'
|
4
|
+
gem 'jnunemaker-matchy', '~> 0.4.0'
|
5
|
+
gem 'shoulda', '~> 2.10.2'
|
6
|
+
gem 'timecop', '~> 0.3.1'
|
7
|
+
gem 'mocha', '~> 0.9.8'
|
8
|
+
|
9
|
+
$:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
|
10
|
+
require 'mongo_mapper'
|
11
|
+
require 'fileutils'
|
12
|
+
require 'ostruct'
|
13
|
+
require 'pp'
|
14
|
+
|
15
|
+
require 'active_support/version'
|
16
|
+
require 'json'
|
17
|
+
require 'matchy'
|
18
|
+
require 'shoulda'
|
19
|
+
require 'timecop'
|
20
|
+
require 'mocha'
|
21
|
+
|
22
|
+
class Test::Unit::TestCase
|
23
|
+
def Doc(name=nil, &block)
|
24
|
+
klass = Class.new do
|
25
|
+
include MongoMapper::Document
|
26
|
+
set_collection_name :test
|
27
|
+
|
28
|
+
if name
|
29
|
+
class_eval "def self.name; '#{name}' end"
|
30
|
+
class_eval "def self.to_s; '#{name}' end"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
klass.class_eval(&block) if block_given?
|
35
|
+
klass.collection.remove
|
36
|
+
klass
|
37
|
+
end
|
38
|
+
|
39
|
+
def EDoc(name=nil, &block)
|
40
|
+
klass = Class.new do
|
41
|
+
include MongoMapper::EmbeddedDocument
|
42
|
+
|
43
|
+
if name
|
44
|
+
class_eval "def self.name; '#{name}' end"
|
45
|
+
class_eval "def self.to_s; '#{name}' end"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
klass.class_eval(&block) if block_given?
|
50
|
+
klass
|
51
|
+
end
|
52
|
+
|
53
|
+
def drop_indexes(klass)
|
54
|
+
if klass.database.collection_names.include?(klass.collection.name)
|
55
|
+
klass.collection.drop_indexes
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
custom_matcher :be_true do |receiver, matcher, args|
|
60
|
+
matcher.positive_failure_message = "Expected #{receiver} to be true but it wasn't"
|
61
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be true but it was"
|
62
|
+
receiver.eql?(true)
|
63
|
+
end
|
64
|
+
|
65
|
+
custom_matcher :be_false do |receiver, matcher, args|
|
66
|
+
matcher.positive_failure_message = "Expected #{receiver} to be false but it wasn't"
|
67
|
+
matcher.negative_failure_message = "Expected #{receiver} not to be false but it was"
|
68
|
+
receiver.eql?(false)
|
69
|
+
end
|
70
|
+
|
71
|
+
custom_matcher :have_error_on do |receiver, matcher, args|
|
72
|
+
receiver.valid?
|
73
|
+
attribute = args[0]
|
74
|
+
expected_message = args[1]
|
75
|
+
|
76
|
+
if expected_message.nil?
|
77
|
+
matcher.positive_failure_message = "#{receiver} had no errors on #{attribute}"
|
78
|
+
matcher.negative_failure_message = "#{receiver} had errors on #{attribute} #{receiver.errors.inspect}"
|
79
|
+
!receiver.errors.on(attribute).blank?
|
80
|
+
else
|
81
|
+
actual = receiver.errors.on(attribute)
|
82
|
+
matcher.positive_failure_message = %Q(Expected error on #{attribute} to be "#{expected_message}" but was "#{actual}")
|
83
|
+
matcher.negative_failure_message = %Q(Expected error on #{attribute} not to be "#{expected_message}" but was "#{actual}")
|
84
|
+
actual == expected_message
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
custom_matcher :have_index do |receiver, matcher, args|
|
89
|
+
index_name = args[0]
|
90
|
+
matcher.positive_failure_message = "#{receiver} does not have index named #{index_name}, but should"
|
91
|
+
matcher.negative_failure_message = "#{receiver} does have index named #{index_name}, but should not"
|
92
|
+
!receiver.collection.index_information.detect { |index| index[0] == index_name }.nil?
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
log_dir = File.expand_path('../../log', __FILE__)
|
97
|
+
FileUtils.mkdir_p(log_dir) unless File.exist?(log_dir)
|
98
|
+
logger = Logger.new(log_dir + '/test.log')
|
99
|
+
|
100
|
+
MongoMapper.connection = Mongo::Connection.new('127.0.0.1', 27017, :logger => logger)
|
101
|
+
MongoMapper.database = "mm-test-#{RUBY_VERSION.gsub('.', '-')}"
|
102
|
+
MongoMapper.database.collections.each { |c| c.drop_indexes }
|
103
|
+
|
104
|
+
puts "\n--- Active Support Version: #{ActiveSupport::VERSION::STRING} ---\n"
|
@@ -0,0 +1,212 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'models'
|
3
|
+
|
4
|
+
class FooMonster; end
|
5
|
+
|
6
|
+
class AssociationBaseTest < Test::Unit::TestCase
|
7
|
+
include MongoMapper::Plugins::Associations
|
8
|
+
|
9
|
+
should "initialize with type and name" do
|
10
|
+
base = Base.new(:many, :foos)
|
11
|
+
base.type.should == :many
|
12
|
+
base.name.should == :foos
|
13
|
+
end
|
14
|
+
|
15
|
+
should "also allow options when initializing" do
|
16
|
+
base = Base.new(:many, :foos, :polymorphic => true)
|
17
|
+
base.options[:polymorphic].should be_true
|
18
|
+
end
|
19
|
+
|
20
|
+
context "class_name" do
|
21
|
+
should "work for belongs_to" do
|
22
|
+
Base.new(:belongs_to, :user).class_name.should == 'User'
|
23
|
+
end
|
24
|
+
|
25
|
+
should "work for many" do
|
26
|
+
Base.new(:many, :smart_people).class_name.should == 'SmartPerson'
|
27
|
+
end
|
28
|
+
|
29
|
+
should "be changeable using class_name option" do
|
30
|
+
base = Base.new(:many, :smart_people, :class_name => 'IntelligentPerson')
|
31
|
+
base.class_name.should == 'IntelligentPerson'
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "klass" do
|
36
|
+
should "default to class_name constantized" do
|
37
|
+
Base.new(:belongs_to, :foo_monster).klass.should == FooMonster
|
38
|
+
end
|
39
|
+
|
40
|
+
should "be the specified class" do
|
41
|
+
anonnymous_class = Class.new
|
42
|
+
Base.new(:belongs_to, :foo_monster, :class => anonnymous_class).klass.should == anonnymous_class
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context "many?" do
|
47
|
+
should "be true if many" do
|
48
|
+
Base.new(:many, :foos).many?.should be_true
|
49
|
+
end
|
50
|
+
|
51
|
+
should "be false if not many" do
|
52
|
+
Base.new(:belongs_to, :foo).many?.should be_false
|
53
|
+
Base.new(:one, :foo).many?.should be_false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "one?" do
|
58
|
+
should "be true if one" do
|
59
|
+
Base.new(:one, :foo).one?.should be_true
|
60
|
+
end
|
61
|
+
|
62
|
+
should "be false if not one" do
|
63
|
+
Base.new(:many, :foo).one?.should be_false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context "belongs_to?" do
|
68
|
+
should "be true if belongs_to" do
|
69
|
+
Base.new(:belongs_to, :foo).belongs_to?.should be_true
|
70
|
+
end
|
71
|
+
|
72
|
+
should "be false if not belongs_to" do
|
73
|
+
Base.new(:many, :foos).belongs_to?.should be_false
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
context "polymorphic?" do
|
78
|
+
should "be true if polymorphic" do
|
79
|
+
Base.new(:many, :foos, :polymorphic => true).polymorphic?.should be_true
|
80
|
+
end
|
81
|
+
|
82
|
+
should "be false if not polymorphic" do
|
83
|
+
Base.new(:many, :bars).polymorphic?.should be_false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "as?" do
|
88
|
+
should "be true if one" do
|
89
|
+
Base.new(:one, :foo, :as => :commentable).as?.should be_true
|
90
|
+
end
|
91
|
+
|
92
|
+
should "be false if not one" do
|
93
|
+
Base.new(:many, :foo).as?.should be_false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context "in_array?" do
|
98
|
+
should "be true if one" do
|
99
|
+
Base.new(:one, :foo, :in => :list_ids).in_array?.should be_true
|
100
|
+
end
|
101
|
+
|
102
|
+
should "be false if not one" do
|
103
|
+
Base.new(:many, :foo).in_array?.should be_false
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context "query_options" do
|
108
|
+
should "default to empty hash" do
|
109
|
+
base = Base.new(:many, :foos)
|
110
|
+
base.query_options.should == {}
|
111
|
+
end
|
112
|
+
|
113
|
+
should "work with order" do
|
114
|
+
base = Base.new(:many, :foos, :order => 'position')
|
115
|
+
base.query_options.should == {:order => 'position'}
|
116
|
+
end
|
117
|
+
|
118
|
+
should "correctly parse from options" do
|
119
|
+
base = Base.new(:many, :foos, :order => 'position', :somekey => 'somevalue')
|
120
|
+
base.query_options.should == {:order => 'position', :somekey => 'somevalue'}
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "type_key_name" do
|
125
|
+
should "be _type for many" do
|
126
|
+
Base.new(:many, :foos).type_key_name.should == '_type'
|
127
|
+
end
|
128
|
+
|
129
|
+
should "be association name _ type for belongs_to" do
|
130
|
+
Base.new(:belongs_to, :foo).type_key_name.should == 'foo_type'
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
context "foreign_key" do
|
135
|
+
should "default to assocation name _id for belongs to" do
|
136
|
+
base = Base.new(:belongs_to, :foo)
|
137
|
+
base.foreign_key.should == 'foo_id'
|
138
|
+
end
|
139
|
+
|
140
|
+
should "be overridable with :foreign_key option" do
|
141
|
+
base = Base.new(:belongs_to, :foo, :foreign_key => 'foobar_id')
|
142
|
+
base.foreign_key.should == 'foobar_id'
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
should "have ivar that is association name" do
|
147
|
+
Base.new(:belongs_to, :foo).ivar.should == '@_foo'
|
148
|
+
end
|
149
|
+
|
150
|
+
context "embeddable?" do
|
151
|
+
should "be true if class is embeddable" do
|
152
|
+
base = Base.new(:many, :medias)
|
153
|
+
base.embeddable?.should be_true
|
154
|
+
end
|
155
|
+
|
156
|
+
should "be false if class is not embeddable" do
|
157
|
+
base = Base.new(:many, :statuses)
|
158
|
+
base.embeddable?.should be_false
|
159
|
+
|
160
|
+
base = Base.new(:belongs_to, :project)
|
161
|
+
base.embeddable?.should be_false
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "proxy_class" do
|
166
|
+
should "be ManyDocumentsProxy for many" do
|
167
|
+
base = Base.new(:many, :statuses)
|
168
|
+
base.proxy_class.should == ManyDocumentsProxy
|
169
|
+
end
|
170
|
+
|
171
|
+
should "be ManyPolymorphicProxy for polymorphic many" do
|
172
|
+
base = Base.new(:many, :messages, :polymorphic => true)
|
173
|
+
base.proxy_class.should == ManyPolymorphicProxy
|
174
|
+
end
|
175
|
+
|
176
|
+
should "be ManyEmbeddedProxy for many embedded" do
|
177
|
+
base = Base.new(:many, :medias)
|
178
|
+
base.proxy_class.should == ManyEmbeddedProxy
|
179
|
+
end
|
180
|
+
|
181
|
+
should "be ManyEmbeddedPolymorphicProxy for polymorphic many embedded" do
|
182
|
+
base = Base.new(:many, :medias, :polymorphic => true)
|
183
|
+
base.proxy_class.should == ManyEmbeddedPolymorphicProxy
|
184
|
+
end
|
185
|
+
|
186
|
+
should "be BelongsToProxy for belongs_to" do
|
187
|
+
base = Base.new(:belongs_to, :project)
|
188
|
+
base.proxy_class.should == BelongsToProxy
|
189
|
+
end
|
190
|
+
|
191
|
+
should "be BelongsToPolymorphicProxy for polymorphic belongs_to" do
|
192
|
+
base = Base.new(:belongs_to, :target, :polymorphic => true)
|
193
|
+
base.proxy_class.should == BelongsToPolymorphicProxy
|
194
|
+
end
|
195
|
+
|
196
|
+
should "be OneProxy for one" do
|
197
|
+
base = Base.new(:one, :status, :polymorphic => true)
|
198
|
+
base.proxy_class.should == OneProxy
|
199
|
+
end
|
200
|
+
|
201
|
+
should "be OneEmbeddedProxy for one embedded" do
|
202
|
+
base = Base.new(:one, :media)
|
203
|
+
base.proxy_class.should == OneEmbeddedProxy
|
204
|
+
end
|
205
|
+
|
206
|
+
should "be InArrayProxy for many with :in option" do
|
207
|
+
base = Base.new(:many, :messages, :in => :message_ids)
|
208
|
+
base.proxy_class.should == InArrayProxy
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
end
|