lib/padrino-gen/generators/components/orms/mongoid.rb in padrino-gen-0.9.10 vs lib/padrino-gen/generators/components/orms/mongoid.rb in padrino-gen-0.9.11
- old
+ new
@@ -1,6 +1,6 @@
-MONGOID = (<<-MONGO).gsub(/^ {10}/, '') unless defined?(MONGOID)
+MONGOID = (<<-MONGO) unless defined?(MONGOID)
# Connection.new takes host, port
host = 'localhost'
port = Mongo::Connection::DEFAULT_PORT
@@ -27,15 +27,15 @@
MONGO
def setup_orm
require_dependencies 'bson_ext', :require => 'mongo'
require_dependencies 'mongoid'
- create_file("config/database.rb", MONGOID.gsub(/!NAME!/, name.underscore))
+ create_file("config/database.rb", MONGOID.gsub(/!NAME!/, name.underscore.gsub('.','_')))
empty_directory('app/models')
end
-MONGOID_MODEL = (<<-MODEL).gsub(/^ {10}/, '') unless defined?(MONGOID_MODEL)
+MONGOID_MODEL = (<<-MODEL) unless defined?(MONGOID_MODEL)
class !NAME!
include Mongoid::Document
include Mongoid::Timestamps # adds created_at and updated_at fields
# field <name>, :type => <type>, :default => <value>
@@ -48,12 +48,13 @@
# key :field <, :another_field, :one_more ....>
end
MODEL
-def create_model_file(name, fields)
- model_path = destination_root('app/models/', "#{name.to_s.underscore}.rb")
- field_tuples = fields.collect { |value| value.split(":") }
+# options => { :fields => ["title:string", "body:string"], :app => 'app' }
+def create_model_file(name, options={})
+ model_path = destination_root(options[:app], 'models', "#{name.to_s.underscore}.rb")
+ field_tuples = options[:fields].collect { |value| value.split(":") }
column_declarations = field_tuples.collect { |field, kind| "field :#{field}, :type => #{kind.camelize}" }.join("\n ")
model_contents = MONGOID_MODEL.gsub(/!NAME!/, name.to_s.camelize)
model_contents.gsub!(/!FIELDS!/, column_declarations)
create_file(model_path, model_contents)
end
\ No newline at end of file