lib/generators/components/orms/sequel_gen.rb in padrino-gen-0.2.0 vs lib/generators/components/orms/sequel_gen.rb in padrino-gen-0.2.1
- old
+ new
@@ -7,13 +7,13 @@
SEQUEL = (<<-SEQUEL).gsub(/^ {10}/, '')
module DatabaseSetup
def self.registered(app)
Sequel::Model.plugin(:schema)
- app.configure(:development) { Sequel.connect('your_dev_db_here') }
- app.configure(:production) { Sequel.connect('your_production_db_here') }
- app.configure(:test) { Sequel.connect('your_test_db_here') }
+ app.configure(:development) { Sequel.connect("sqlite://your_dev_db_here", :loggers => [logger]) }
+ app.configure(:production) { Sequel.connect("sqlite://your_production_db_here", :loggers => [logger]) }
+ app.configure(:test) { Sequel.connect("sqlite://your_test_db_here", :loggers => [logger]) }
end
end
SEQUEL
def setup_orm
@@ -34,33 +34,51 @@
model_contents = SQ_MODEL.gsub(/!NAME!/, name.to_s.downcase.camelize)
create_file(model_path, model_contents)
end
SQ_MIGRATION = (<<-MIGRATION).gsub(/^ {10}/, '')
- class !FILENAME! < Sequel::Migration
+ class !FILECLASS! < Sequel::Migration
def up
- create_table :!TABLE! do
- primary_key :id
- # <type> <name>
- !FIELDS!
- end
+ !UP!
end
def down
- drop_table :!TABLE!
+ !DOWN!
end
end
MIGRATION
- def create_migration_file(filename, name, fields)
- model_name = name.to_s.pluralize
- field_tuples = fields.collect { |value| value.split(":") }
- column_declarations = field_tuples.collect { |field, kind| "#{kind} :#{field}" }.join("\n ")
- migration_contents = SQ_MIGRATION.gsub(/!NAME!/, model_name.camelize).gsub(/!TABLE!/, model_name.underscore)
- migration_contents.gsub!(/!FILENAME!/, filename.camelize)
- migration_contents.gsub!(/!FIELDS!/, column_declarations)
- migration_filename = "#{Time.now.to_i}_#{filename}.rb"
- create_file(app_root_path('db/migrate/', migration_filename), migration_contents)
+
+ SQ_MODEL_UP_MG = (<<-MIGRATION).gsub(/^ {6}/, '')
+ create_table :!TABLE! do
+ primary_key :id
+ # <type> <name>
+ !FIELDS!
+ end
+ MIGRATION
+
+ SQ_MODEL_DOWN_MG = (<<-MIGRATION).gsub(/^ {10}/, '')
+ drop_table :!TABLE!
+ MIGRATION
+
+ def create_model_migration(migration_name, name, columns)
+ output_model_migration(migration_name, name, columns,
+ :column_format => lambda { |field, kind| "#{kind.camelize} :#{field}" },
+ :base => SQ_MIGRATION, :up => SQ_MODEL_UP_MG, :down => SQ_MODEL_DOWN_MG)
+ end
+
+ SQ_CHANGE_MG = (<<-MIGRATION).gsub(/^ {6}/, '')
+ alter_table :!TABLE! do
+ !COLUMNS!
+ end
+ MIGRATION
+
+ def create_migration_file(migration_name, name, columns)
+ output_migration_file(migration_name, name, columns,
+ :base => SQ_MIGRATION, :change_format => SQ_CHANGE_MG,
+ :add => lambda { |field, kind| "add_column :#{field}, #{kind.camelize}" },
+ :remove => lambda { |field, kind| "drop_column :#{field}" }
+ )
end
end
end
end