Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* added datamapper migration to the model

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

0 notes on commit 6c4e37b

Please sign in to comment.
Something went wrong with that request. Please try again.