mongo_mapper_ext 0.2.4 → 0.2.5
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.
- data/Rakefile +2 -2
- data/lib/mongo_mapper_ext/carrier_wave.rb +4 -2
- data/lib/mongo_mapper_ext/carrier_wave/mongo_mapper/plugins/mount_uploader.rb +31 -0
- data/lib/mongo_mapper_ext/carrier_wave/mongo_mapper/plugins/mount_uploaders.rb +137 -0
- data/lib/mongo_mapper_ext/gems.rb +6 -7
- data/lib/mongo_mapper_ext/mongo_mapper.rb +12 -9
- data/lib/mongo_mapper_ext/mongo_mapper/fixes.rb +13 -21
- data/lib/mongo_mapper_ext/mongo_mapper/hacks.rb +59 -0
- data/lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_cache.rb +14 -18
- data/lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_convertors.rb +69 -73
- data/lib/mongo_mapper_ext/mongo_mapper/plugins/belongs_to_with_counter_cache.rb +33 -37
- data/lib/mongo_mapper_ext/mongo_mapper/plugins/custom_scope.rb +67 -71
- data/lib/mongo_mapper_ext/mongo_mapper/plugins/db_config.rb +27 -24
- data/lib/mongo_mapper_ext/mongo_mapper/plugins/micelaneous.rb +46 -50
- data/lib/mongo_mapper_ext/rad.rb +3 -0
- data/lib/mongo_mapper_ext/rad/file_uploader.rb +21 -23
- data/readme.md +1 -1
- data/spec/carrier_wave/carrier_wave_spec.rb +82 -27
- data/spec/mongo_mapper/mongo_mapper_spec.rb +28 -27
- data/spec/rad/uploading_spec.rb +1 -1
- metadata +106 -112
- data/lib/mongo_mapper_ext/carrier_wave/mongo_mapper/plugins/carrier_wave.rb +0 -41
- data/lib/mongo_mapper_ext/mongo_mapper/locales/activemodel/ru.yml +0 -27
- data/lib/mongo_mapper_ext/mongo_mapper/locales/mongo_mapper/en.yml +0 -4
- data/lib/mongo_mapper_ext/mongo_mapper/locales/mongo_mapper/ru.yml +0 -4
- data/lib/mongo_mapper_ext/mongo_mapper/logging.rb +0 -75
@@ -1,45 +1,41 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
|
3
|
-
module BelongsToWithCounterCache
|
4
|
-
extend ActiveSupport::Concern
|
1
|
+
module MongoMapper::Plugins::BelongsToWithCounterCache
|
2
|
+
extend ActiveSupport::Concern
|
5
3
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
define_method increase_method_name do
|
27
|
-
cache_class.upsert!({id: self.send(association_key)}, :$inc => {cache_attribute => 1})
|
28
|
-
end
|
29
|
-
protected increase_method_name
|
4
|
+
module ClassMethods
|
5
|
+
#
|
6
|
+
# CounterCache
|
7
|
+
# belongs_to :item, counter_cashe: true
|
8
|
+
#
|
9
|
+
def belongs_to association_id, options={}, &extension
|
10
|
+
# options.must_not.include :counter_cashe
|
11
|
+
if options.delete(:counter_cache)
|
12
|
+
association_id = association_id.to_s
|
13
|
+
association_key = "#{association_id}_id"
|
14
|
+
cache_attribute = "#{self.alias.pluralize.underscore}_count"
|
15
|
+
cache_class = if class_name = options[:class_name]
|
16
|
+
class_name.constantize
|
17
|
+
else
|
18
|
+
association_id.classify.constantize
|
19
|
+
end
|
20
|
+
raise "key :#{cache_attribute} not defined on :#{cache_class}!" unless cache_class.keys.include? cache_attribute
|
21
|
+
increase_method_name = "increase_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter"
|
22
|
+
decrease_method_name = "decrease_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter"
|
30
23
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
24
|
+
define_method increase_method_name do
|
25
|
+
cache_class.upsert!({id: self.send(association_key)}, :$inc => {cache_attribute => 1})
|
26
|
+
end
|
27
|
+
protected increase_method_name
|
35
28
|
|
36
|
-
|
37
|
-
|
38
|
-
end
|
39
|
-
super association_id, options, &extension
|
29
|
+
define_method decrease_method_name do
|
30
|
+
cache_class.upsert!({id: self.send(association_key)}, :$inc => {cache_attribute => -1})
|
40
31
|
end
|
32
|
+
protected decrease_method_name
|
33
|
+
|
34
|
+
after_create increase_method_name
|
35
|
+
after_destroy decrease_method_name
|
41
36
|
end
|
42
|
-
|
37
|
+
super association_id, options, &extension
|
43
38
|
end
|
44
39
|
end
|
40
|
+
|
45
41
|
end
|
@@ -1,81 +1,77 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
super default_scope_options.merge(options)
|
13
|
-
end
|
1
|
+
module MongoMapper::Plugins::CustomScope
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def query options = {}
|
6
|
+
super default_scope_options.merge(options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def find_one options = {}
|
10
|
+
super default_scope_options.merge(options)
|
11
|
+
end
|
14
12
|
|
15
|
-
|
16
|
-
|
17
|
-
|
13
|
+
def find_many options = {}
|
14
|
+
super default_scope_options.merge(options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def count options={}
|
18
|
+
super default_scope_options.merge(options)
|
19
|
+
end
|
20
|
+
|
21
|
+
def with_exclusive_scope options = {}, &block
|
22
|
+
raise "exclusive scope already applied, can't apply it twice!" if Thread.current['mm_with_exclusive_scope']
|
23
|
+
|
24
|
+
before = Thread.current['mm_with_exclusive_scope']
|
25
|
+
begin
|
26
|
+
Thread.current['mm_with_exclusive_scope'] = options
|
27
|
+
block.call if block
|
28
|
+
ensure
|
29
|
+
Thread.current['mm_with_exclusive_scope'] = before
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def with_scope options = {}, &block
|
34
|
+
raise "exclusive scope already applied, can't apply it twice!" if Thread.current['mm_with_exclusive_scope']
|
35
|
+
|
36
|
+
before = Thread.current['mm_with_scope']
|
37
|
+
begin
|
38
|
+
options = before.merge options if before
|
39
|
+
Thread.current['mm_with_scope'] = options
|
40
|
+
block.call if block
|
41
|
+
ensure
|
42
|
+
Thread.current['mm_with_scope'] = before
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
def default_scope options = nil, &block
|
48
|
+
raise "invalid option (#{options.inspect})!" if options and !options.is_a?(Hash)
|
49
|
+
self.write_inheritable_attribute(:default_scope, (options || block))
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
def default_scope_options
|
54
|
+
default_scope = self.read_inheritable_attribute(:default_scope)
|
18
55
|
|
19
|
-
|
20
|
-
|
21
|
-
end
|
56
|
+
exclusive_options = Thread.current['mm_with_exclusive_scope']
|
57
|
+
return exclusive_options if exclusive_options
|
22
58
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
Thread.current['mm_with_exclusive_scope'] = options
|
29
|
-
block.call if block
|
30
|
-
ensure
|
31
|
-
Thread.current['mm_with_exclusive_scope'] = before
|
59
|
+
options = if default_scope
|
60
|
+
if default_scope.respond_to? :call
|
61
|
+
default_scope.call self
|
62
|
+
else
|
63
|
+
default_scope
|
32
64
|
end
|
65
|
+
else
|
66
|
+
{}
|
33
67
|
end
|
34
68
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
before = Thread.current['mm_with_scope']
|
39
|
-
begin
|
40
|
-
options = before.merge options if before
|
41
|
-
Thread.current['mm_with_scope'] = options
|
42
|
-
block.call if block
|
43
|
-
ensure
|
44
|
-
Thread.current['mm_with_scope'] = before
|
45
|
-
end
|
69
|
+
if scope_options = Thread.current['mm_with_scope']
|
70
|
+
options = options.merge scope_options
|
46
71
|
end
|
47
72
|
|
48
|
-
|
49
|
-
def default_scope options = nil, &block
|
50
|
-
raise "invalid option (#{options.inspect})!" if options and !options.is_a?(Hash)
|
51
|
-
self.write_inheritable_attribute(:default_scope, (options || block))
|
52
|
-
end
|
53
|
-
|
54
|
-
private
|
55
|
-
def default_scope_options
|
56
|
-
default_scope = self.read_inheritable_attribute(:default_scope)
|
57
|
-
|
58
|
-
exclusive_options = Thread.current['mm_with_exclusive_scope']
|
59
|
-
return exclusive_options if exclusive_options
|
60
|
-
|
61
|
-
options = if default_scope
|
62
|
-
if default_scope.respond_to? :call
|
63
|
-
default_scope.call self
|
64
|
-
else
|
65
|
-
default_scope
|
66
|
-
end
|
67
|
-
else
|
68
|
-
{}
|
69
|
-
end
|
70
|
-
|
71
|
-
if scope_options = Thread.current['mm_with_scope']
|
72
|
-
options = options.merge scope_options
|
73
|
-
end
|
74
|
-
|
75
|
-
options
|
76
|
-
end
|
73
|
+
options
|
77
74
|
end
|
78
|
-
|
79
|
-
end
|
80
75
|
end
|
81
|
-
|
76
|
+
|
77
|
+
end
|
@@ -10,11 +10,11 @@ module MongoMapper
|
|
10
10
|
db_options = MongoMapper.db_config[database_alias]
|
11
11
|
connection = Mongo::Connection.new(db_options['host'], db_options['port'], logger: MongoMapper.logger)
|
12
12
|
|
13
|
-
if defined?(PhusionPassenger)
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
end
|
13
|
+
# if defined?(PhusionPassenger)
|
14
|
+
# PhusionPassenger.on_event(:starting_worker_process) do |forked|
|
15
|
+
# connection.connect_to_master if forked
|
16
|
+
# end
|
17
|
+
# end
|
18
18
|
|
19
19
|
self[database_alias] = connection
|
20
20
|
end
|
@@ -55,6 +55,13 @@ module MongoMapper
|
|
55
55
|
def databases
|
56
56
|
@databases ||= DatabasesPool.new
|
57
57
|
end
|
58
|
+
|
59
|
+
def use_database database_alias
|
60
|
+
database_alias = database_alias.to_s
|
61
|
+
raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias
|
62
|
+
MongoMapper.connection = MongoMapper.connections[database_alias]
|
63
|
+
MongoMapper.database = MongoMapper.db_config[database_alias]['name']
|
64
|
+
end
|
58
65
|
end
|
59
66
|
end
|
60
67
|
|
@@ -62,24 +69,20 @@ end
|
|
62
69
|
#
|
63
70
|
# Plugin
|
64
71
|
#
|
65
|
-
module MongoMapper
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
set_database_name MongoMapper.db_config[database_alias]['name']
|
80
|
-
end
|
81
|
-
end
|
82
|
-
|
83
|
-
end
|
72
|
+
module MongoMapper::Plugins::DbConfig
|
73
|
+
extend ActiveSupport::Concern
|
74
|
+
|
75
|
+
module ClassMethods
|
76
|
+
#
|
77
|
+
# Connect to database_alias specified in config
|
78
|
+
#
|
79
|
+
def use_database database_alias
|
80
|
+
database_alias = database_alias.to_s
|
81
|
+
raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias
|
82
|
+
|
83
|
+
self.connection MongoMapper.connections[database_alias]
|
84
|
+
set_database_name MongoMapper.db_config[database_alias]['name']
|
85
|
+
end
|
84
86
|
end
|
87
|
+
|
85
88
|
end
|
@@ -1,53 +1,49 @@
|
|
1
|
-
module MongoMapper
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
query[:_id] = query.delete :id if query.include? :id
|
47
|
-
collection.upsert! query, *args
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
1
|
+
module MongoMapper::Plugins::Micelaneous
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module InstanceMethods
|
5
|
+
def upsert! *args
|
6
|
+
self.class.upsert!({id: id}, *args)
|
7
|
+
end
|
8
|
+
|
9
|
+
def exist?
|
10
|
+
self.class.find(id) != nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ClassMethods
|
15
|
+
#
|
16
|
+
# model_name
|
17
|
+
#
|
18
|
+
def model_name *args
|
19
|
+
if args.empty?
|
20
|
+
@model_name ||= ::ActiveModel::Name.new self, self.alias
|
21
|
+
else
|
22
|
+
@model_name = ::ActiveModel::Name.new self, args.first
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
#
|
28
|
+
# Sequentiall :all for big collection
|
29
|
+
#
|
30
|
+
def all_sequentially &block
|
31
|
+
page, per_page = 1, 5
|
32
|
+
begin
|
33
|
+
results = paginate(page: page, per_page: per_page, order: '_id asc')
|
34
|
+
results.each{|o| block.call o}
|
35
|
+
page += 1
|
36
|
+
end until results.blank? or results.size < per_page
|
37
|
+
end
|
38
|
+
|
39
|
+
|
40
|
+
#
|
41
|
+
# shortcut for upsert
|
42
|
+
#
|
43
|
+
def upsert! query, *args
|
44
|
+
query[:_id] = query.delete :id if query.include? :id
|
45
|
+
collection.upsert! query, *args
|
51
46
|
end
|
52
47
|
end
|
48
|
+
|
53
49
|
end
|
data/lib/mongo_mapper_ext/rad.rb
CHANGED
@@ -1,28 +1,26 @@
|
|
1
1
|
require 'carrierwave/processing/mini_magick'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
include CarrierWave::MiniMagick
|
6
|
-
|
7
|
-
storage rad.config.fs.type(:file)
|
3
|
+
class Models::FileUploader < CarrierWave::Uploader::Base
|
4
|
+
include CarrierWave::MiniMagick
|
8
5
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
6
|
+
storage rad.config.fs.type(:file)
|
7
|
+
|
8
|
+
# def sanitize_regexp
|
9
|
+
# /[^[:word:]\.\-\+\s_]/i
|
10
|
+
# end
|
11
|
+
|
12
|
+
def file_path
|
13
|
+
"#{rad.config.fs.prefix('/fs')}/system/#{model.class.model_name.underscore}/#{model.id}"
|
27
14
|
end
|
15
|
+
|
16
|
+
def store_dir
|
17
|
+
"#{root}#{file_path}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def extension_white_list
|
21
|
+
[/.*/]
|
22
|
+
end
|
23
|
+
|
24
|
+
def cache_dir; rad.config.fs.cache_path! end
|
25
|
+
def root; rad.config.fs.path! end
|
28
26
|
end
|