mongo_mapper_ext 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +5 -5
- data/lib/mongo_db_ext/micelaneous.rb +12 -0
- data/lib/mongo_mapper_ext/gems.rb +17 -2
- data/lib/mongo_mapper_ext/hacks/active_model.rb +16 -0
- data/lib/mongo_mapper_ext/hacks/fixes.rb +37 -0
- data/lib/mongo_mapper_ext/{micelaneous.rb → logging.rb} +30 -8
- data/lib/mongo_mapper_ext/migration.rb +1 -1
- data/lib/mongo_mapper_ext/mongo_mapper.rb +0 -25
- data/lib/mongo_mapper_ext/plugins/{attributes_cache.rb → attribute_cache.rb} +2 -1
- data/lib/mongo_mapper_ext/plugins/attribute_convertors.rb +79 -0
- data/lib/mongo_mapper_ext/plugins/belongs_to_with_counter_cache.rb +45 -0
- data/lib/mongo_mapper_ext/plugins/carrierwave.rb +60 -0
- data/lib/mongo_mapper_ext/plugins/{default_scope.rb → custom_scope.rb} +3 -2
- data/lib/mongo_mapper_ext/plugins/db_config.rb +84 -46
- data/lib/mongo_mapper_ext/plugins/micelaneous.rb +24 -68
- data/lib/mongo_mapper_ext/rad/file_uploader.rb +28 -0
- data/lib/mongo_mapper_ext/rad.rb +2 -0
- data/lib/mongo_mapper_ext/spec.rb +103 -0
- data/lib/mongo_mapper_ext/{migrate.rake → tasks.rb} +3 -15
- data/lib/mongo_mapper_ext.rb +41 -24
- data/readme.md +74 -2
- data/spec/attribute_convertors_spec.rb +71 -0
- data/spec/carrierwave_spec/plane.jpg +0 -0
- data/spec/carrierwave_spec.rb +42 -0
- data/spec/{scope_spec.rb → custom_scope_spec.rb} +26 -32
- data/spec/micelaneous_plugin_spec.rb +34 -20
- data/spec/micelaneous_spec.rb +105 -24
- data/spec/migration_spec.rb +8 -17
- data/spec/{mm_spec.rb → mongo_mapper_spec.rb} +0 -0
- data/spec/spec_helper.rb +2 -5
- data/spec/uploading_spec/ship.jpg +0 -0
- data/spec/uploading_spec//321/204/320/260/320/270/314/206/320/273 /321/201 /320/277/321/200/320/276/320/261/320/265/320/273/320/260/320/274/320/270.txt" +1 -0
- data/spec/uploading_spec.rb +49 -0
- metadata +164 -72
- data/lib/mongo_mapper_ext/db_config.rb +0 -67
- data/lib/mongo_mapper_ext/hacks/time_measuring.rb +0 -44
- data/lib/mongo_mapper_ext/spec/helper.rb +0 -61
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'carrierwave/processing/mini_magick'
|
2
|
+
|
3
|
+
module Models
|
4
|
+
class FileUploader < CarrierWave::Uploader::Base
|
5
|
+
include CarrierWave::MiniMagick
|
6
|
+
|
7
|
+
storage rad.config.fs.type(:file)
|
8
|
+
|
9
|
+
# def sanitize_regexp
|
10
|
+
# /[^[:word:]\.\-\+\s_]/i
|
11
|
+
# end
|
12
|
+
|
13
|
+
def file_path
|
14
|
+
"#{rad.config.fs.prefix('/fs')}/system/#{model.class.model_name.underscore}/#{model.id}"
|
15
|
+
end
|
16
|
+
|
17
|
+
def store_dir
|
18
|
+
"#{root}#{file_path}"
|
19
|
+
end
|
20
|
+
|
21
|
+
def extension_white_list
|
22
|
+
[/.*/]
|
23
|
+
end
|
24
|
+
|
25
|
+
def cache_dir; rad.config.fs.cache_path! end
|
26
|
+
def root; rad.config.fs.path! end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'mongo_mapper_ext'
|
2
|
+
|
3
|
+
#
|
4
|
+
# disabling :use_database, all tests will use the same :test database.
|
5
|
+
#
|
6
|
+
module MongoMapper::Plugins::DbConfig::ClassMethods
|
7
|
+
alias_method :_use_database, :use_database
|
8
|
+
def use_database database_alias; end
|
9
|
+
end
|
10
|
+
|
11
|
+
|
12
|
+
#
|
13
|
+
# Disabling indexes in testing environment
|
14
|
+
#
|
15
|
+
if Object.const_defined?(:Rad)
|
16
|
+
MongoMapper::Plugins::Indexes::ClassMethods.class_eval do
|
17
|
+
alias_method :original_ensure_index, :ensure_index
|
18
|
+
def ensure_index_stub *args; end
|
19
|
+
end
|
20
|
+
|
21
|
+
rad.after :mode, bang: false do
|
22
|
+
if rad.production?
|
23
|
+
MongoMapper::Plugins::Indexes::ClassMethods.send :alias_method, :ensure_index, :original_ensure_index
|
24
|
+
else
|
25
|
+
MongoMapper::Plugins::Indexes::ClassMethods.send :alias_method, :ensure_index, :ensure_index_stub
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
#
|
32
|
+
# Clear db before each test
|
33
|
+
#
|
34
|
+
rspec do
|
35
|
+
def self.with_mongo_mapper
|
36
|
+
before :all do
|
37
|
+
MongoMapper.db_config = {
|
38
|
+
'default' => {'name' => "test"}
|
39
|
+
}
|
40
|
+
|
41
|
+
MongoMapper.database = 'test'
|
42
|
+
end
|
43
|
+
|
44
|
+
before do
|
45
|
+
MongoMapper.db_config.each do |db_alias, opt|
|
46
|
+
db = MongoMapper.databases[db_alias]
|
47
|
+
db.collection_names.each do |name|
|
48
|
+
next if name =~ /^system\./
|
49
|
+
db.collection(name).drop
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
#
|
58
|
+
# Silence logger
|
59
|
+
#
|
60
|
+
MongoMapper.logger = Logger.new(nil)
|
61
|
+
if defined? Paperclip
|
62
|
+
Paperclip.class_eval do
|
63
|
+
def self.logger
|
64
|
+
@logger ||= Logger.new(nil)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
#
|
71
|
+
# Files
|
72
|
+
#
|
73
|
+
rspec do
|
74
|
+
class << self
|
75
|
+
def with_files
|
76
|
+
path, cache_path = '/tmp/spec_fs', '/tmp/spec_fs_cache'
|
77
|
+
|
78
|
+
before do
|
79
|
+
rad.config.merge!({fs: {path: path, cache_path: cache_path}}, override: true)
|
80
|
+
|
81
|
+
Models::FileUploader.storage :file
|
82
|
+
|
83
|
+
CarrierWave.configure do |config|
|
84
|
+
config.storage = :file
|
85
|
+
config.enable_processing = false
|
86
|
+
|
87
|
+
config.cache_dir = rad.config.fs.cache_path!
|
88
|
+
config.root = rad.config.fs.path!
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
before do
|
93
|
+
path.to_dir.destroy
|
94
|
+
cache_path.to_dir.destroy
|
95
|
+
end
|
96
|
+
|
97
|
+
after do
|
98
|
+
path.to_dir.destroy
|
99
|
+
cache_path.to_dir.destroy
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -1,23 +1,11 @@
|
|
1
|
-
Rake::TaskManager.class_eval do
|
2
|
-
def remove_task(task_name)
|
3
|
-
@tasks.delete(task_name.to_s)
|
4
|
-
end
|
5
|
-
end
|
6
|
-
|
7
|
-
def remove_task(task_name)
|
8
|
-
Rake.application.remove_task(task_name)
|
9
|
-
end
|
10
|
-
|
11
1
|
namespace :db do
|
12
|
-
|
13
|
-
remove_task 'db:migrate'
|
14
2
|
desc "Migrate Database"
|
15
|
-
task :
|
3
|
+
task migrate: :environment do
|
16
4
|
::Migration = MongoMapper::Migration
|
17
|
-
Dir["#{
|
5
|
+
Dir["#{rad.config.runtime_dir!}/db/**/*.rb"].each{|f| require f.sub(/\.rb$/, '')}
|
18
6
|
|
19
7
|
database_alias = ENV['d'] || ENV['database']
|
20
|
-
database_alias = '
|
8
|
+
database_alias = 'default' if database_alias.blank?
|
21
9
|
|
22
10
|
version = ENV['v'] || ENV['version']
|
23
11
|
if version.blank?
|
data/lib/mongo_mapper_ext.rb
CHANGED
@@ -1,32 +1,49 @@
|
|
1
1
|
require 'mongo_mapper_ext/gems'
|
2
2
|
|
3
3
|
require 'mongo_mapper'
|
4
|
+
require 'carrierwave'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
'micelaneous',
|
6
|
+
%w(
|
7
|
+
micelaneous
|
8
|
+
).each{|file| require "mongo_db_ext/#{file}"}
|
9
|
+
|
10
|
+
%w(
|
11
|
+
hacks/fixes
|
12
|
+
hacks/active_model
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
14
|
+
migration
|
15
|
+
mongo_mapper
|
16
|
+
view_helpers
|
17
|
+
logging
|
18
|
+
|
19
|
+
plugins/db_config
|
20
|
+
plugins/custom_scope
|
21
|
+
plugins/attribute_cache
|
22
|
+
plugins/carrierwave
|
23
|
+
plugins/attribute_convertors
|
24
|
+
plugins/micelaneous
|
25
|
+
plugins/belongs_to_with_counter_cache
|
26
|
+
).each{|file| require "mongo_mapper_ext/#{file}"}
|
21
27
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
model.attr_protected :id, :_id, :_type, :created_at, :updated_at
|
28
|
+
#
|
29
|
+
# Default plugins and settings
|
30
|
+
#
|
31
|
+
module MongoMapper::Plugins
|
32
|
+
[CustomScope, AttributeCache, AttributeConvertors, BelongsToWithCounterCache, Micelaneous, CarrierWave, DbConfig].each do |plugin|
|
33
|
+
::MongoMapper::Document.send :include, plugin
|
30
34
|
end
|
31
35
|
end
|
32
|
-
|
36
|
+
|
37
|
+
#
|
38
|
+
# Attribute protection
|
39
|
+
#
|
40
|
+
MongoMapper::Document.included do
|
41
|
+
attr_protected :id, :_id, :_type, :created_at, :updated_at
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
#
|
46
|
+
# Locales
|
47
|
+
#
|
48
|
+
dir = File.expand_path("#{__FILE__}/../..")
|
49
|
+
I18n.load_path += Dir["#{dir}/locales/**/*.{rb,yml}"]
|
data/readme.md
CHANGED
@@ -1,3 +1,75 @@
|
|
1
|
-
# Extensions for MongoMapper
|
1
|
+
# Extensions for MongoMapper
|
2
2
|
|
3
|
-
|
3
|
+
## Simultaneous use of multiple databases
|
4
|
+
|
5
|
+
MongoMapper.db_config = {
|
6
|
+
'default' => {'name' => "default_test"}
|
7
|
+
'global' => {'name' => 'global_test'},
|
8
|
+
}
|
9
|
+
|
10
|
+
# Comment will be connected to :default database
|
11
|
+
module Comment
|
12
|
+
belongs_to :user
|
13
|
+
end
|
14
|
+
|
15
|
+
# User will be connected to :global database
|
16
|
+
module User
|
17
|
+
use_database :global
|
18
|
+
|
19
|
+
has_many :comments
|
20
|
+
end
|
21
|
+
|
22
|
+
## Migrations
|
23
|
+
|
24
|
+
# Works with multiple databases, support versions
|
25
|
+
Migration.define :default, 1 do |m|
|
26
|
+
m.up{Sample.create name: 'name'}
|
27
|
+
m.down{Sample.destroy_all}
|
28
|
+
end
|
29
|
+
|
30
|
+
# Tell it the version of database You need, and it's smart enough to figure out all needed :up or :down
|
31
|
+
Migration.update(:global, 1)
|
32
|
+
|
33
|
+
|
34
|
+
## Custom Scope
|
35
|
+
:with_scope, :default_scope, :with_exclusive_scope, see spec for details.
|
36
|
+
|
37
|
+
## Counter Cache
|
38
|
+
|
39
|
+
class Comment
|
40
|
+
belongs_to :post, counter_cache: true
|
41
|
+
end
|
42
|
+
|
43
|
+
Post.comments_count
|
44
|
+
|
45
|
+
## Attribute Converters
|
46
|
+
|
47
|
+
For editing complex objects in forms:
|
48
|
+
|
49
|
+
class Post
|
50
|
+
key :tags, Array, as_string: :line
|
51
|
+
end
|
52
|
+
|
53
|
+
@post.tags_as_string = "personal, article"
|
54
|
+
@post.tags # => ['personal', 'article']
|
55
|
+
@post.tags_as_string # => "personal, article"
|
56
|
+
|
57
|
+
## Handy upserts
|
58
|
+
|
59
|
+
@post.upsert! :$inc => {comments_count: 1}
|
60
|
+
|
61
|
+
## CarrierWave integration
|
62
|
+
|
63
|
+
File attachments (stored on File System, S3, MongoDB-GridFS)
|
64
|
+
|
65
|
+
class User
|
66
|
+
mount_uploader :avatar, AvatarUploader
|
67
|
+
end
|
68
|
+
|
69
|
+
## more
|
70
|
+
|
71
|
+
Attribute Cache, Spec Helpers (auto-clean db before each test).
|
72
|
+
|
73
|
+
# License
|
74
|
+
|
75
|
+
Copyright (c) Alexey Petrushin [http://4ire.net](http://4ire.net), released under the MIT license.
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Attribute Convertors" do
|
4
|
+
with_mongo_mapper
|
5
|
+
|
6
|
+
after(:all){remove_constants %w(AsStringSample)}
|
7
|
+
|
8
|
+
before do
|
9
|
+
@convertors = MongoMapper::Plugins::AttributeConvertors::ClassMethods::ATTRIBUTE_CONVERTORS
|
10
|
+
@convertors[:test_convertor] = {
|
11
|
+
from_string: -> s {"from_string: #{s}"},
|
12
|
+
to_string: -> v {"to_string: #{v}"}
|
13
|
+
}
|
14
|
+
end
|
15
|
+
|
16
|
+
it ":line convertor" do
|
17
|
+
v = ['a', 'b']
|
18
|
+
str_v = 'a, b'
|
19
|
+
@convertors[:line][:from_string].call(str_v).should == v
|
20
|
+
@convertors[:line][:to_string].call(v).should == str_v
|
21
|
+
end
|
22
|
+
|
23
|
+
it ":yaml convertor" do
|
24
|
+
v = {'a' => 'b'}
|
25
|
+
str_v = v.to_yaml.strip
|
26
|
+
|
27
|
+
@convertors[:yaml][:from_string].call(str_v).should == v
|
28
|
+
@convertors[:yaml][:to_string].call(v).should == str_v
|
29
|
+
end
|
30
|
+
|
31
|
+
it ":json convertor" do
|
32
|
+
v = {'a' => 'b'}
|
33
|
+
str_v = v.to_json.strip
|
34
|
+
@convertors[:json][:from_string].call(str_v).should == v
|
35
|
+
@convertors[:json][:to_string].call(v).should == str_v
|
36
|
+
end
|
37
|
+
|
38
|
+
it ":key extension" do
|
39
|
+
class ::AsStringSample
|
40
|
+
include MongoMapper::Document
|
41
|
+
include MongoMapper::Plugins::Micelaneous
|
42
|
+
|
43
|
+
key :key, String, as_string: :test_convertor
|
44
|
+
key :protected_key, String, as_string: :test_convertor, protected: true
|
45
|
+
end
|
46
|
+
|
47
|
+
o = AsStringSample.new
|
48
|
+
|
49
|
+
# get
|
50
|
+
o.key_as_string.should == 'to_string: '
|
51
|
+
o.key = 'value'
|
52
|
+
o.clear_cache
|
53
|
+
o.key_as_string.should == 'to_string: value'
|
54
|
+
|
55
|
+
# set
|
56
|
+
o.key_as_string = ''
|
57
|
+
o.key.should == 'from_string: '
|
58
|
+
o.key_as_string = 'value'
|
59
|
+
o.key.should == 'from_string: value'
|
60
|
+
|
61
|
+
# mass assignment
|
62
|
+
o.key = ''
|
63
|
+
o.update_attributes key_as_string: 'value'
|
64
|
+
o.key.should == 'from_string: value'
|
65
|
+
|
66
|
+
# protection
|
67
|
+
o.protected_key = ''
|
68
|
+
o.update_attributes protected_key_as_string: 'value'
|
69
|
+
o.protected_key.should == ''
|
70
|
+
end
|
71
|
+
end
|
Binary file
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "MongoMapper & CarrierWave" do
|
4
|
+
with_mongo_mapper
|
5
|
+
with_tmp_spec_dir
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
class PlaneUploader < CarrierWave::Uploader::Base
|
9
|
+
storage :file
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :store_dir
|
13
|
+
end
|
14
|
+
|
15
|
+
def store_dir
|
16
|
+
self.class.store_dir
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class Plane
|
21
|
+
include MongoMapper::Document
|
22
|
+
|
23
|
+
mount_uploader :image, PlaneUploader
|
24
|
+
end
|
25
|
+
end
|
26
|
+
after(:all){remove_constants :Plane, :PlaneUploader}
|
27
|
+
|
28
|
+
before do
|
29
|
+
PlaneUploader.store_dir = "#{spec_dir}/data"
|
30
|
+
end
|
31
|
+
|
32
|
+
it "basic" do
|
33
|
+
plane = Plane.new
|
34
|
+
File.open "#{spec_dir}/plane.jpg" do |f|
|
35
|
+
plane.image = f
|
36
|
+
plane.save!
|
37
|
+
end
|
38
|
+
|
39
|
+
plane.image.current_path.should == '/tmp/carrierwave_spec/data/plane.jpg'
|
40
|
+
File.should exist("#{spec_dir}/data/plane.jpg")
|
41
|
+
end
|
42
|
+
end
|
@@ -1,29 +1,23 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
require "mongo_mapper_ext/plugins/default_scope"
|
4
|
-
|
5
3
|
describe "MongoMapper Default Scope" do
|
6
|
-
|
7
|
-
MongoMapper.database = 'test'
|
8
|
-
end
|
4
|
+
with_mongo_mapper
|
9
5
|
|
10
|
-
before
|
6
|
+
before do
|
11
7
|
class ::ScopeSample
|
12
8
|
include MongoMapper::Document
|
13
|
-
|
9
|
+
include MongoMapper::Plugins::CustomScope
|
14
10
|
|
15
11
|
key :name, String
|
16
12
|
key :_type, String
|
17
13
|
end
|
18
14
|
|
19
15
|
ScopeSample.delete_all
|
20
|
-
ScopeSample.create! :
|
21
|
-
ScopeSample.create! :
|
16
|
+
ScopeSample.create! name: 'a'
|
17
|
+
ScopeSample.create! name: 'b'
|
22
18
|
end
|
23
19
|
|
24
|
-
after :
|
25
|
-
Object.send :remove_const, :ScopeSample if Object.const_defined? :ScopeSample
|
26
|
-
end
|
20
|
+
after(:all){remove_constants :ScopeSample}
|
27
21
|
|
28
22
|
it "should not affect objects without default_scope" do
|
29
23
|
ScopeSample.count.should == 2
|
@@ -31,7 +25,7 @@ describe "MongoMapper Default Scope" do
|
|
31
25
|
end
|
32
26
|
|
33
27
|
it "default_scope" do
|
34
|
-
ScopeSample.send :default_scope, :
|
28
|
+
ScopeSample.send :default_scope, name: 'a'
|
35
29
|
ScopeSample.count.should == 1
|
36
30
|
ScopeSample.all.count.should == 1
|
37
31
|
ScopeSample.first.name.should == 'a'
|
@@ -39,7 +33,7 @@ describe "MongoMapper Default Scope" do
|
|
39
33
|
|
40
34
|
it "default_scope as block" do
|
41
35
|
ScopeSample.send :default_scope do
|
42
|
-
{:
|
36
|
+
{name: 'a'}
|
43
37
|
end
|
44
38
|
ScopeSample.count.should == 1
|
45
39
|
ScopeSample.all.count.should == 1
|
@@ -47,12 +41,12 @@ describe "MongoMapper Default Scope" do
|
|
47
41
|
end
|
48
42
|
|
49
43
|
it "default_scope should be inherited" do
|
50
|
-
ScopeSample.send :default_scope, :
|
44
|
+
ScopeSample.send :default_scope, name: 'a'
|
51
45
|
class ScopeSampleAncestor < ScopeSample
|
52
46
|
end
|
53
47
|
|
54
|
-
ScopeSampleAncestor.create! :
|
55
|
-
ScopeSampleAncestor.create! :
|
48
|
+
ScopeSampleAncestor.create! name: 'a'
|
49
|
+
ScopeSampleAncestor.create! name: 'b'
|
56
50
|
|
57
51
|
ScopeSampleAncestor.count.should == 1
|
58
52
|
ScopeSampleAncestor.all.count.should == 1
|
@@ -60,13 +54,13 @@ describe "MongoMapper Default Scope" do
|
|
60
54
|
end
|
61
55
|
|
62
56
|
it "ancestors should be able to override default_scope" do
|
63
|
-
ScopeSample.send :default_scope, :
|
57
|
+
ScopeSample.send :default_scope, name: 'a'
|
64
58
|
class ScopeSampleAncestor2 < ScopeSample
|
65
|
-
default_scope :
|
59
|
+
default_scope name: 'b'
|
66
60
|
end
|
67
61
|
|
68
|
-
ScopeSampleAncestor2.create! :
|
69
|
-
ScopeSampleAncestor2.create! :
|
62
|
+
ScopeSampleAncestor2.create! name: 'a'
|
63
|
+
ScopeSampleAncestor2.create! name: 'b'
|
70
64
|
|
71
65
|
ScopeSampleAncestor2.count.should == 1
|
72
66
|
ScopeSampleAncestor2.all.count.should == 1
|
@@ -74,13 +68,13 @@ describe "MongoMapper Default Scope" do
|
|
74
68
|
end
|
75
69
|
|
76
70
|
it "shouldn't allow to nest with_exclusive_scope" do
|
77
|
-
|
71
|
+
-> {
|
78
72
|
ScopeSample.with_exclusive_scope do
|
79
73
|
ScopeSample.with_exclusive_scope{}
|
80
74
|
end
|
81
75
|
}.should raise_error(AssertionError)
|
82
76
|
|
83
|
-
|
77
|
+
-> {
|
84
78
|
ScopeSample.with_exclusive_scope do
|
85
79
|
ScopeSample.with_scope{}
|
86
80
|
end
|
@@ -88,10 +82,10 @@ describe "MongoMapper Default Scope" do
|
|
88
82
|
end
|
89
83
|
|
90
84
|
it "with_exclusive_scope should clear other scopes" do
|
91
|
-
ScopeSample.send :default_scope, :
|
85
|
+
ScopeSample.send :default_scope, name: 'a'
|
92
86
|
|
93
|
-
ScopeSample.with_scope :
|
94
|
-
ScopeSample.with_exclusive_scope :
|
87
|
+
ScopeSample.with_scope name: 'a' do
|
88
|
+
ScopeSample.with_exclusive_scope name: 'b' do
|
95
89
|
ScopeSample.count.should == 1
|
96
90
|
ScopeSample.first.name.should == 'b'
|
97
91
|
end
|
@@ -99,7 +93,7 @@ describe "MongoMapper Default Scope" do
|
|
99
93
|
end
|
100
94
|
|
101
95
|
it "with_scope" do
|
102
|
-
ScopeSample.with_scope :
|
96
|
+
ScopeSample.with_scope name: 'a' do
|
103
97
|
ScopeSample.count.should == 1
|
104
98
|
ScopeSample.first.name.should == 'a'
|
105
99
|
end
|
@@ -109,13 +103,13 @@ describe "MongoMapper Default Scope" do
|
|
109
103
|
ScopeSample.class_eval do
|
110
104
|
key :name2, String
|
111
105
|
key :name3, String
|
112
|
-
default_scope :
|
106
|
+
default_scope name: 'a'
|
113
107
|
end
|
114
|
-
ScopeSample.create! :
|
115
|
-
ScopeSample.create! :
|
108
|
+
ScopeSample.create! name: 'a', name2: 'a2', name3: 'a3'
|
109
|
+
ScopeSample.create! name: 'b', name2: 'b2', name3: 'b3'
|
116
110
|
|
117
|
-
ScopeSample.with_scope :
|
118
|
-
ScopeSample.with_scope :
|
111
|
+
ScopeSample.with_scope name2: 'a2' do
|
112
|
+
ScopeSample.with_scope name3: 'a3' do
|
119
113
|
ScopeSample.count.should == 1
|
120
114
|
ScopeSample.first.name2.should == 'a2'
|
121
115
|
end
|
@@ -1,40 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
require "mongo_mapper_ext/micelaneous"
|
4
|
-
require "mongo_mapper_ext/plugins/micelaneous"
|
5
|
-
|
6
3
|
describe "MongoMapper Default Scope" do
|
4
|
+
with_mongo_mapper
|
7
5
|
|
8
6
|
before :all do
|
9
7
|
class ::Post
|
10
8
|
include MongoMapper::Document
|
11
|
-
|
9
|
+
include MongoMapper::Plugins::Micelaneous
|
12
10
|
|
13
|
-
key :comments_count, Integer, :
|
11
|
+
key :comments_count, Integer, default: 0
|
14
12
|
has_many :comments
|
15
13
|
end
|
16
14
|
|
17
15
|
class ::Comment
|
18
16
|
include MongoMapper::Document
|
19
|
-
|
17
|
+
include MongoMapper::Plugins::Micelaneous
|
20
18
|
|
21
19
|
key :post_id
|
22
|
-
belongs_to :post, :
|
23
|
-
end
|
24
|
-
|
25
|
-
MongoMapper.database = 'test'
|
26
|
-
end
|
27
|
-
|
28
|
-
after :all do
|
29
|
-
%w{Post Comment}.each do |obj_name|
|
30
|
-
Object.send :remove_const, obj_name if Object.const_defined? obj_name
|
20
|
+
belongs_to :post, counter_cache: true
|
31
21
|
end
|
32
|
-
end
|
22
|
+
end
|
23
|
+
after(:all){remove_constants %w{Post Comment Post2 Namespace}}
|
33
24
|
|
34
|
-
before :each do
|
35
|
-
[Post, Comment].every.delete_all
|
36
|
-
end
|
37
|
-
|
38
25
|
it "should increase count of comments" do
|
39
26
|
post = Post.create!
|
40
27
|
comment = post.comments.create!
|
@@ -53,4 +40,31 @@ describe "MongoMapper Default Scope" do
|
|
53
40
|
post.reload
|
54
41
|
post.comments_count.should == 0
|
55
42
|
end
|
43
|
+
|
44
|
+
describe "model_name" do
|
45
|
+
it "basic" do
|
46
|
+
Post.model_name.should == "Post"
|
47
|
+
Post.model_name "SuperPost"
|
48
|
+
Post.model_name.should == "SuperPost"
|
49
|
+
end
|
50
|
+
|
51
|
+
it "by default should be initialized from class alias" do
|
52
|
+
class ::Post2
|
53
|
+
include MongoMapper::Document
|
54
|
+
include MongoMapper::Plugins::Micelaneous
|
55
|
+
|
56
|
+
self.alias 'PostAlias'
|
57
|
+
end
|
58
|
+
|
59
|
+
module ::Namespace
|
60
|
+
class Post
|
61
|
+
include MongoMapper::Document
|
62
|
+
include MongoMapper::Plugins::Micelaneous
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
Post2.model_name.should == 'PostAlias'
|
67
|
+
Namespace::Post.model_name.should == 'Post' # not the Namespace::Post
|
68
|
+
end
|
69
|
+
end
|
56
70
|
end
|