Skip to content

Commit

Permalink
major refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
al6x committed May 19, 2011
1 parent 32e97d4 commit 5ecfb87
Show file tree
Hide file tree
Showing 49 changed files with 393 additions and 284 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -2,7 +2,7 @@ require 'rake_ext'

project(
name: "mongo_mapper_ext",
version: "0.2.3",
version: "0.2.4",
summary: "Extensions for MongoMapper",

author: "Alexey Petrushin",
Expand Down
Binary file added icon_plane.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 0 additions & 49 deletions lib/mongo_mapper_ext.rb

This file was deleted.

12 changes: 12 additions & 0 deletions lib/mongo_mapper_ext/carrier_wave.rb
@@ -0,0 +1,12 @@
require 'carrierwave'
require 'carrierwave/validations/active_model'

require 'mongo_mapper_ext/mongo_mapper'

%w(
fixes
micelaneous
mongo_mapper/plugins/carrier_wave
).each{|f| require "mongo_mapper_ext/carrier_wave/#{f}"}

MongoMapper::Document.send :include, MongoMapper::Plugins::CarrierWave
12 changes: 12 additions & 0 deletions lib/mongo_mapper_ext/carrier_wave/fixes.rb
@@ -0,0 +1,12 @@
CarrierWave::SanitizedFile.class_eval do
def sanitize_regexp
/[^[:word:]\.\-\+\s_]/i
end
end

CarrierWave::Uploader::Cache.class_eval do
def original_filename=(filename)
raise CarrierWave::InvalidParameter, "invalid filename" unless filename =~ /\A[[:word:]\.\-\+\s_]+\z/i
@original_filename = filename
end
end
26 changes: 26 additions & 0 deletions lib/mongo_mapper_ext/carrier_wave/micelaneous.rb
@@ -0,0 +1,26 @@
#
# Changing filename format from <version>_<filename_with_extension> to <name>.<version>.<extension>
#
CarrierWave::Uploader::Versions.class_eval do
def full_filename(for_file)
name = super
if version_name
ext = File.extname name
base = File.basename name, ext
"#{base}.#{version_name}#{ext}"
else
name
end
end

def full_original_filename
name = super
if version_name
ext = File.extname name
base = File.basename name, ext
"#{base}.#{version_name}#{ext}"
else
name
end
end
end
@@ -1,22 +1,3 @@
require 'carrierwave/validations/active_model'

#
# Hacks
#
CarrierWave::SanitizedFile.class_eval do
def sanitize_regexp
/[^[:word:]\.\-\+\s_]/i
end
end

CarrierWave::Uploader::Cache.class_eval do
def original_filename=(filename)
raise CarrierWave::InvalidParameter, "invalid filename" unless filename =~ /\A[[:word:]\.\-\+\s_]+\z/i
@original_filename = filename
end
end


#
# mount_uploader
#
Expand Down
1 change: 1 addition & 0 deletions lib/mongo_mapper_ext/mongo_db.rb
@@ -0,0 +1 @@
require 'mongo_mapper_ext/mongo_db/upsert'
Expand Up @@ -3,9 +3,9 @@
#
Mongo::Collection.class_eval do
def upsert! query, opt
opt.size.must == 1
opt.must_be.a Hash
opt.values.first.must_be.a Hash
# opt.size.must == 1
# opt.must_be.a Hash
# opt.values.first.must_be.a Hash

update(query, opt, {upsert: true, safe: true})
end
Expand Down
46 changes: 46 additions & 0 deletions lib/mongo_mapper_ext/mongo_mapper.rb
@@ -0,0 +1,46 @@
require 'mongo_mapper_ext/gems'
require 'mongo_mapper'

require 'mongo_mapper_ext/mongo_db'

%w(
fixes
hacks
support

migration
view_helpers
logging

plugins/attribute_cache
plugins/attribute_convertors
plugins/belongs_to_with_counter_cache
plugins/custom_scope
plugins/db_config
plugins/micelaneous
).each{|file| require "mongo_mapper_ext/mongo_mapper/#{file}"}


#
# Default plugins
#
module MongoMapper::Plugins
[CustomScope, AttributeCache, AttributeConvertors, BelongsToWithCounterCache, Micelaneous, DbConfig].each do |plugin|
::MongoMapper::Document.send :include, plugin
end
end


#
# Attribute protection
#
MongoMapper::Document.included do
attr_protected :id, :_id, :_type, :created_at, :updated_at
end


#
# Locales
#
dir = __FILE__.dirname
I18n.load_path += Dir["#{dir}/mongo_mapper/locales/**/*.{rb,yml}"]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Expand Up @@ -54,7 +54,7 @@ def self.temporary_silince_logger &block

def self.use_database database_alias
database_alias = database_alias.to_s
MongoMapper.db_config.must.include database_alias
raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias
MongoMapper.connection = MongoMapper.connections['default']
MongoMapper.database = MongoMapper.db_config['default']['name']
end
Expand Down
Expand Up @@ -41,7 +41,7 @@ def logger; @logger ||= Logger.new end
def define database_alias, version, &block
database_alias = database_alias.to_s

version.must_be.a Integer
raise "version should be an Integer! (but you provided '#{version}' instad)!" unless version.is_a? Integer
definition = MigrationDefinition.new
block.call definition
definitions[database_alias][version] = definition
Expand All @@ -51,21 +51,21 @@ def update database_alias, version
database_alias = database_alias.to_s
db = MongoMapper.databases[database_alias]

if metadata(db).version == version
if metadata(db)['version'] == version
logger.info "Database '#{database_alias}' already is of #{version} version, no migration needed."
return false
else
logger.info "Migration for '#{database_alias}' Database:"
end

increase_db_version database_alias, db while metadata(db).version < version
decrease_db_version database_alias, db while metadata(db).version > version
increase_db_version database_alias, db while metadata(db)['version'] < version
decrease_db_version database_alias, db while metadata(db)['version'] > version
true
end

def metadata db
col = db.collection 'db_metadata'
metadata = (col.find_one || {version: 0}).to_openobject
col.find_one || {'version' => 0}
end

def definitions
Expand All @@ -75,28 +75,28 @@ def definitions
protected
def increase_db_version database_alias, db
m = metadata(db)
migration = definitions[database_alias][m.version + 1]
raise "No upgrade for version #{m.version + 1} of '#{database_alias}' Database!" unless migration and migration.up
migration = definitions[database_alias][m['version'] + 1]
raise "No upgrade for version #{m['version'] + 1} of '#{database_alias}' Database!" unless migration and migration.up

migration.up.call db

m.version += 1
m['version'] += 1
update_metadata db, m

logger.info "Database '#{database_alias}' upgraded to version #{m.version}."
logger.info "Database '#{database_alias}' upgraded to version #{m['version']}."
end

def decrease_db_version database_alias, db
m = metadata(db)
migration = definitions[database_alias][m.version]
raise "No downgrade for version #{m.version} of '#{database_alias}' Database!" unless migration and migration.down
migration = definitions[database_alias][m['version']]
raise "No downgrade for version #{m['version']} of '#{database_alias}' Database!" unless migration and migration.down

migration.down.call db

m.version -= 1
m['version'] -= 1
update_metadata db, m

logger.info "Database '#{database_alias}' downgraded to version #{m.version}."
logger.info "Database '#{database_alias}' downgraded to version #{m['version']}."
end


Expand Down
Expand Up @@ -9,8 +9,8 @@ module ClassMethods
# belongs_to :item, counter_cashe: true
#
def belongs_to association_id, options={}, &extension
options.must_not.include :counter_cashe
if options.delete(:counter_cache) || options.delete('counter_cache')
# options.must_not.include :counter_cashe
if options.delete(:counter_cache)
association_id = association_id.to_s
association_key = "#{association_id}_id"
cache_attribute = "#{self.alias.pluralize.underscore}_count"
Expand All @@ -19,7 +19,7 @@ def belongs_to association_id, options={}, &extension
else
association_id.classify.constantize
end
cache_class.keys.must.include cache_attribute
raise "key :#{cache_attribute} not defined on :#{cache_class}!" unless cache_class.keys.include? cache_attribute
increase_method_name = "increase_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter"
decrease_method_name = "decrease_#{cache_class.alias.underscore}_#{self.alias.pluralize.underscore}_counter"

Expand Down
Expand Up @@ -21,7 +21,7 @@ def count options={}
end

def with_exclusive_scope options = {}, &block
Thread.current['mm_with_exclusive_scope'].must_be.nil
raise "exclusive scope already applied, can't apply it twice!" if Thread.current['mm_with_exclusive_scope']

before = Thread.current['mm_with_exclusive_scope']
begin
Expand All @@ -33,7 +33,7 @@ def with_exclusive_scope options = {}, &block
end

def with_scope options = {}, &block
Thread.current['mm_with_exclusive_scope'].must_be.nil
raise "exclusive scope already applied, can't apply it twice!" if Thread.current['mm_with_exclusive_scope']

before = Thread.current['mm_with_scope']
begin
Expand All @@ -47,7 +47,7 @@ def with_scope options = {}, &block

protected
def default_scope options = nil, &block
options.must_be.a NilClass, Hash
raise "invalid option (#{options.inspect})!" if options and !options.is_a?(Hash)
self.write_inheritable_attribute(:default_scope, (options || block))
end

Expand Down
Expand Up @@ -6,7 +6,7 @@ class ConnectionsPool < Hash
def [](database_alias)
database_alias = database_alias.to_s
unless connection = super(database_alias)
MongoMapper.db_config.must.include database_alias
raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias
db_options = MongoMapper.db_config[database_alias]
connection = Mongo::Connection.new(db_options['host'], db_options['port'], logger: MongoMapper.logger)

Expand All @@ -30,9 +30,11 @@ class DatabasesPool < Hash
def [](database_alias)
database_alias = database_alias.to_s
unless db = super(database_alias)
MongoMapper.db_config.must.include database_alias
raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias
db_options = MongoMapper.db_config[database_alias]
db = MongoMapper.connections[database_alias].db db_options['name'].must_be.a(String)
name = db_options['name']
raise "database name should be a string (#{name})!" unless name.is_a?(String)
db = MongoMapper.connections[database_alias].db name
self[database_alias] = db
end
return db
Expand Down Expand Up @@ -70,13 +72,11 @@ module ClassMethods
# Connect to database_alias specified in config
#
def use_database database_alias
# defer do
database_alias = database_alias.to_s
MongoMapper.db_config.must.include database_alias
raise "unknown database alias :#{database_alias}" unless MongoMapper.db_config.include? database_alias

self.connection MongoMapper.connections[database_alias]
set_database_name MongoMapper.db_config[database_alias]['name']
# end
end
end

Expand Down
File renamed without changes.

0 comments on commit 5ecfb87

Please sign in to comment.