mongo_mapper_ext 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (26) hide show
  1. data/Rakefile +2 -2
  2. data/lib/mongo_mapper_ext/carrier_wave.rb +4 -2
  3. data/lib/mongo_mapper_ext/carrier_wave/mongo_mapper/plugins/mount_uploader.rb +31 -0
  4. data/lib/mongo_mapper_ext/carrier_wave/mongo_mapper/plugins/mount_uploaders.rb +137 -0
  5. data/lib/mongo_mapper_ext/gems.rb +6 -7
  6. data/lib/mongo_mapper_ext/mongo_mapper.rb +12 -9
  7. data/lib/mongo_mapper_ext/mongo_mapper/fixes.rb +13 -21
  8. data/lib/mongo_mapper_ext/mongo_mapper/hacks.rb +59 -0
  9. data/lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_cache.rb +14 -18
  10. data/lib/mongo_mapper_ext/mongo_mapper/plugins/attribute_convertors.rb +69 -73
  11. data/lib/mongo_mapper_ext/mongo_mapper/plugins/belongs_to_with_counter_cache.rb +33 -37
  12. data/lib/mongo_mapper_ext/mongo_mapper/plugins/custom_scope.rb +67 -71
  13. data/lib/mongo_mapper_ext/mongo_mapper/plugins/db_config.rb +27 -24
  14. data/lib/mongo_mapper_ext/mongo_mapper/plugins/micelaneous.rb +46 -50
  15. data/lib/mongo_mapper_ext/rad.rb +3 -0
  16. data/lib/mongo_mapper_ext/rad/file_uploader.rb +21 -23
  17. data/readme.md +1 -1
  18. data/spec/carrier_wave/carrier_wave_spec.rb +82 -27
  19. data/spec/mongo_mapper/mongo_mapper_spec.rb +28 -27
  20. data/spec/rad/uploading_spec.rb +1 -1
  21. metadata +106 -112
  22. data/lib/mongo_mapper_ext/carrier_wave/mongo_mapper/plugins/carrier_wave.rb +0 -41
  23. data/lib/mongo_mapper_ext/mongo_mapper/locales/activemodel/ru.yml +0 -27
  24. data/lib/mongo_mapper_ext/mongo_mapper/locales/mongo_mapper/en.yml +0 -4
  25. data/lib/mongo_mapper_ext/mongo_mapper/locales/mongo_mapper/ru.yml +0 -4
  26. data/lib/mongo_mapper_ext/mongo_mapper/logging.rb +0 -75
@@ -1,45 +1,41 @@
1
- module MongoMapper
2
- module Plugins
3
- module BelongsToWithCounterCache
4
- extend ActiveSupport::Concern
1
+ module MongoMapper::Plugins::BelongsToWithCounterCache
2
+ extend ActiveSupport::Concern
5
3
 
6
- module ClassMethods
7
- #
8
- # CounterCache
9
- # belongs_to :item, counter_cashe: true
10
- #
11
- def belongs_to association_id, options={}, &extension
12
- # options.must_not.include :counter_cashe
13
- if options.delete(:counter_cache)
14
- association_id = association_id.to_s
15
- association_key = "#{association_id}_id"
16
- cache_attribute = "#{self.alias.pluralize.underscore}_count"
17
- cache_class = if class_name = options[:class_name]
18
- class_name.constantize
19
- else
20
- association_id.classify.constantize
21
- end
22
- raise "key :#{cache_attribute} not defined on :#{cache_class}!" unless cache_class.keys.include? cache_attribute
23
- increase_method_name = "increase_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter"
24
- decrease_method_name = "decrease_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter"
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
- define_method decrease_method_name do
32
- cache_class.upsert!({id: self.send(association_key)}, :$inc => {cache_attribute => -1})
33
- end
34
- protected decrease_method_name
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
- after_create increase_method_name
37
- after_destroy decrease_method_name
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
- module Plugins
3
- module CustomScope
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def query options = {}
8
- super default_scope_options.merge(options)
9
- end
10
-
11
- def find_one options = {}
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
- def find_many options = {}
16
- super default_scope_options.merge(options)
17
- end
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
- def count options={}
20
- super default_scope_options.merge(options)
21
- end
56
+ exclusive_options = Thread.current['mm_with_exclusive_scope']
57
+ return exclusive_options if exclusive_options
22
58
 
23
- def with_exclusive_scope options = {}, &block
24
- raise "exclusive scope already applied, can't apply it twice!" if Thread.current['mm_with_exclusive_scope']
25
-
26
- before = Thread.current['mm_with_exclusive_scope']
27
- begin
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
- def with_scope options = {}, &block
36
- raise "exclusive scope already applied, can't apply it twice!" if Thread.current['mm_with_exclusive_scope']
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
- protected
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
- end
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
- PhusionPassenger.on_event(:starting_worker_process) do |forked|
15
- connection.connect_to_master if forked
16
- end
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
- module Plugins
67
- module DbConfig
68
- extend ActiveSupport::Concern
69
-
70
- module ClassMethods
71
- #
72
- # Connect to database_alias specified in config
73
- #
74
- def use_database database_alias
75
- database_alias = database_alias.to_s
76
- raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias
77
-
78
- self.connection MongoMapper.connections[database_alias]
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
- module Plugins
3
- module Micelaneous
4
- extend ActiveSupport::Concern
5
-
6
- module InstanceMethods
7
- def upsert! *args
8
- self.class.upsert!({id: id}, *args)
9
- end
10
-
11
- def exist?
12
- self.class.find(id) != nil
13
- end
14
- end
15
-
16
- module ClassMethods
17
- #
18
- # model_name
19
- #
20
- def model_name *args
21
- if args.empty?
22
- @model_name ||= ::ActiveModel::Name.new self, self.alias
23
- else
24
- @model_name = ::ActiveModel::Name.new self, args.first
25
- end
26
- end
27
-
28
-
29
- #
30
- # Sequentiall :all for big collection
31
- #
32
- def all_sequentially &block
33
- page, per_page = 1, 5
34
- begin
35
- results = paginate(page: page, per_page: per_page, order: '_id asc')
36
- results.each{|o| block.call o}
37
- page += 1
38
- end until results.blank? or results.size < per_page
39
- end
40
-
41
-
42
- #
43
- # shortcut for upsert
44
- #
45
- def upsert! query, *args
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
@@ -3,4 +3,7 @@ require 'rad'
3
3
  require 'mongo_mapper_ext/mongo_mapper'
4
4
  require 'mongo_mapper_ext/carrier_wave'
5
5
 
6
+ module Models
7
+ end
8
+
6
9
  require 'mongo_mapper_ext/rad/file_uploader'
@@ -1,28 +1,26 @@
1
1
  require 'carrierwave/processing/mini_magick'
2
2
 
3
- module Models
4
- class FileUploader < CarrierWave::Uploader::Base
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
- # 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
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