Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* added datamapper migration to the model

* "stole" rake from dm-more/rails_datamapper and adjusted it
  • Loading branch information...
commit 6c4e37b9ce0cb653973ab31dc3245056c6b67c58 1 parent 27e855f
@mkristian authored
View
4 Manifest.txt
@@ -2,10 +2,12 @@ History.txt
Manifest.txt
README.txt
Rakefile
+datamapper_rails_templates.rb
generate.sh
generators/datamapper_install/datamapper_install_generator.rb
generators/datamapper_install/templates/datamapper.rake
generators/datamapper_model/datamapper_model_generator.rb
+generators/datamapper_model/templates/migration.rb
generators/datamapper_model/templates/model.rb
generators/datamapper_rspec_model/datamapper_rspec_model_generator.rb
generators/datamapper_rspec_model/templates/model_spec.rb
@@ -21,7 +23,6 @@ generators/datamapper_scaffold/templates/view_show.html.erb
lib/datamapper4rails.rb
lib/datamapper4rails/adapters/base_adapter.rb
lib/datamapper4rails/adapters/restful_adapter.rb
-lib/datamapper4rails/database_config.rb-
lib/datamapper4rails/datamapper_store.rb
lib/datamapper4rails/identity_maps.rb
lib/datamapper4rails/overlay.rb
@@ -29,7 +30,6 @@ lib/datamapper4rails/preload_models.rb
lib/datamapper4rails/restful_transactions.rb
lib/datamapper4rails/rspec_default_values.rb
lib/datamapper4rails/version.rb
-lib/datamapper_rails_templates.rb
spec/datamapper_store_spec.rb
spec/restful_adapter_spec.rb
spec/spec.opts
View
63 datamapper_rails_templates.rb
@@ -0,0 +1,63 @@
+# inspired by http://www.rowtheboat.com/archives/32
+###################################################
+
+# get all datamapper related gems
+gem "addressable", :lib => "addressable/uri"
+
+# assume sqlite3 to be database
+gem "do_sqlite3"
+
+# assume you want to have validations and timestamps in your models
+gem 'dm-validations'
+gem 'dm-timestamps'
+gem 'dm-migrations'
+
+# assume you prefer rspec over unit tests
+gem "rspec", :lib => false
+gem "rspec-rails", :lib => false
+
+# this pulls in rails_datamapper and rack_datamapper
+gem "datamapper4rails"
+
+# install all gems
+rake "gems:install"
+
+# install specs rake tasks
+generate("rspec")
+
+# install datamapper rake tasks
+generate("datamapper_install")
+
+# fix config files to work with datamapper instead of active_record
+environment ""
+environment "config.frameworks -= [ :active_record ]"
+environment "# deactive active_record"
+gsub_file 'spec/spec_helper.rb', /^\s*config[.]/, ' #\0'
+gsub_file 'test/test_helper.rb', /^[^#]*fixtures/, ' #\0'
+
+# add middleware
+def middleware(name)
+ log "middleware", name
+ environment "config.middleware.use '#{name}'"
+end
+
+environment ""
+middleware "DataMapper::RestfulTransactions"
+middleware "DataMapper::IdentityMaps"
+middleware "Rack::Deflater"
+environment "# add middleware"
+
+
+# init a session store
+initializer 'datamapper_store.rb', <<-CODE
+# init a session store which uses a memory cache and a persistent store
+# cleanup can be a problem. jruby uses soft-references for the cache so
+# memory cleanup with jruby is not a problem.
+require 'ixtlan/session'
+ActionController::Base.session_store = :datamapper_store
+ActionController::Base.session = {
+ :cache => true,
+}
+CODE
+
+rake 'db:sessions:create'
View
48 generators/datamapper_install/templates/datamapper.rake
@@ -1,8 +1,36 @@
-namespace :dm do
+# this file is from dm-more/rails_datamapper
+# add adjusted to use the rack_datamapper session store
+
+# Monkey patch to allow overriding defined rake tasks (instead of
+# adding to them, which is the default behaviour when specifying tasks
+# >1 times).
+
+Rake::TaskManager.class_eval do
+ def remove_task(task_name)
+ @tasks.delete(task_name.to_s)
+ end
+end
+
+def remove_task(task_name)
+ returning Rake.application do |app|
+ app.remove_task(app[task_name].name)
+ end
+end
+
+# Until AR/DM co-existence becomes practically possible, presume
+# mutual exclusivity between the two. Thus we wipe all pre-existing
+# db tasks which we assume to be ActiveRecord-based and thus won't
+# work).
+
+Rake.application.tasks.select do |t|
+ t.class == Rake::Task && t.name.starts_with?("db")
+end.each { |t| remove_task(t.name) }
+
+namespace :db do
desc 'Perform automigration'
task :automigrate => :environment do
- FileList['app/models/**/*.rb'].each do |model|
+ FileList["app/models/**/*.rb"].each do |model|
load model
end
::DataMapper.auto_migrate!
@@ -10,7 +38,7 @@ namespace :dm do
desc 'Perform non destructive automigration'
task :autoupgrade => :environment do
- FileList['app/models/**/*.rb'].each do |model|
+ FileList["app/models/**/*.rb"].each do |model|
load model
end
::DataMapper.auto_upgrade!
@@ -38,5 +66,17 @@ namespace :dm do
end
desc 'Migrate the database to the latest version'
- task :migrate => 'dm:migrate:up'
+ task :migrate => 'db:migrate:up'
+
+ namespace :sessions do
+ desc "Creates the sessions table for rack DataMapperStore (works only with the default session class)"
+ task :create => :environment do
+ ::DataMapper::Session::Abstract::Session.auto_migrate!
+ end
+
+ desc "Clear the sessions table for rack DataMapperStore (works only with the default session class)"
+ task :clear => :environment do
+ ::DataMapper::Session::Abstract::Session.all.destroy!
+ end
+ end
end
View
17 generators/datamapper_model/templates/migration.rb
@@ -0,0 +1,17 @@
+migration <%= Time.now.utc.strftime("%Y%m%d%H%M%S") %>, :<%= migration_name.underscore %> do
+ up do
+ create_table :<%= table_name %> do
+<% Array(attributes).each do |attribute| -%>
+ column :<%= attribute.name if attribute %>, <%= attribute.type.to_s.capitalize %>, :nullable? => false
+<% end -%>
+<% unless options[:skip_timestamps] -%>
+ column :created_at, DateTime, :nullable? => false
+ column :updated_at, DateTime, :nullable? => false
+<% end -%>
+ end
+ end
+
+ down do
+ drop_table :<%= table_name %>
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.