lib/padrino-gen/generators/components/orms/mongoid.rb in padrino-gen-0.10.7 vs lib/padrino-gen/generators/components/orms/mongoid.rb in padrino-gen-0.11.0

- old
+ new

@@ -1,7 +1,6 @@ MONGOID = (<<-MONGO) unless defined?(MONGOID) - # Connection.new takes host, port host = 'localhost' port = Mongo::Connection::DEFAULT_PORT database_name = case Padrino.env @@ -21,17 +20,72 @@ # Mongo::Connection.new(host, @settings["slave_one"]["port"], :slave_ok => true).db(name), # Mongo::Connection.new(host, @settings["slave_two"]["port"], :slave_ok => true).db(name) # ] # end # -# More installation and setup notes are on http://mongoid.org/docs/ +# More installation and setup notes are on http://mongoid.org/ MONGO +MONGOID3 = (<<-MONGO) unless defined?(MONGOID3) +# Connection.new takes host, port + +host = 'localhost' +port = 27017 + +database_name = case Padrino.env + when :development then '!NAME!_development' + when :production then '!NAME!_production' + when :test then '!NAME!_test' +end + +# Use MONGO_URI if it's set as an environmental variable +Mongoid::Config.sessions = + if ENV['MONGO_URI'] + {default: {uri: ENV['MONGO_URI'] }} + else + {default: {hosts: ["#\{host\}:#\{port\}"], database: database_name}} + end + +# If you want to use a YML file for config, use this instead: +# +#  Mongoid.load!(File.join(Padrino.root, 'config', 'database.yml'), Padrino.env) +# +# And add a config/database.yml file like this: +# development: +# sessions: +# default: +# database: !NAME!_development +# hosts: +# - localhost:27017 +# production: +# sessions: +# default: +# database: !NAME!_production +# hosts: +# - localhost:27017 +# test: +# sessions: +# default: +# database: !NAME!_test +# hosts: +# - localhost:27017 +# +# +# More installation and setup notes are on http://mongoid.org/en/mongoid/docs/installation.html#configuration +MONGO + def setup_orm - require_dependencies 'bson_ext', :require => 'mongo' - require_dependencies 'mongoid' - require_dependencies('SystemTimer', :require => 'system_timer') if RUBY_VERSION =~ /1\.8/ && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby') - create_file("config/database.rb", MONGOID.gsub(/!NAME!/, @app_name.underscore)) + require_dependencies 'mongoid', :version => (RUBY_VERSION >= '1.9' ? '~>3.0.0' : '~>2.0') + + if RUBY_VERSION =~ /1\.8/ && (!defined?(RUBY_ENGINE) || RUBY_ENGINE == 'ruby') + require_dependencies('SystemTimer', :require => 'system_timer') + end + + if RUBY_VERSION >= '1.9' + create_file('config/database.rb', MONGOID3.gsub(/!NAME!/, @project_name.underscore)) + else + create_file('config/database.rb', MONGOID.gsub(/!NAME!/, @project_name.underscore)) + end end MONGOID_MODEL = (<<-MODEL) unless defined?(MONGOID_MODEL) class !NAME! include Mongoid::Document