mack-data_mapper 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. data/README +6 -0
  2. data/lib/database.rb +94 -0
  3. data/lib/dm_patches/confirmation_validation.rb +19 -0
  4. data/lib/dm_patches/dm-cli.rb +1 -0
  5. data/lib/dm_patches/migrations.rb +23 -0
  6. data/lib/dm_patches/pooling.rb +229 -0
  7. data/lib/genosaurus_helpers.rb +40 -0
  8. data/lib/helpers/orm_helpers.rb +50 -21
  9. data/lib/mack-data_mapper.rb +43 -16
  10. data/lib/migration_generator/migration_generator.rb +28 -17
  11. data/lib/migration_generator/templates/db/migrations/%=@migration_name%.rb.template +6 -6
  12. data/lib/model_column.rb +42 -0
  13. data/lib/model_generator/manifest.yml +9 -2
  14. data/lib/model_generator/model_generator.rb +25 -20
  15. data/lib/model_generator/templates/model.rb.template +4 -4
  16. data/lib/model_generator/templates/rspec.rb.template +7 -0
  17. data/lib/model_generator/templates/{test.rb.template → test_case.rb.template} +0 -0
  18. data/lib/resource.rb +17 -0
  19. data/lib/runner.rb +17 -0
  20. data/lib/scaffold_generator/manifest.yml +14 -3
  21. data/lib/scaffold_generator/scaffold_generator.rb +4 -4
  22. data/lib/scaffold_generator/templates/app/controllers/controller.rb.template +10 -9
  23. data/lib/scaffold_generator/templates/app/helpers/controllers/helper.rb.template +7 -0
  24. data/lib/scaffold_generator/templates/app/views/edit.html.erb.template +1 -1
  25. data/lib/scaffold_generator/templates/app/views/new.html.erb.template +1 -1
  26. data/lib/scaffold_generator/templates/test/functional/rspec.rb.template +47 -0
  27. data/lib/scaffold_generator/templates/{test.rb.template → test/functional/test_case.rb.template} +0 -0
  28. data/lib/tasks/db_create_drop_tasks.rake +43 -62
  29. data/lib/tasks/db_migration_tasks.rake +25 -62
  30. data/lib/tasks/test_tasks.rake +12 -0
  31. data/lib/test_extensions.rb +90 -0
  32. metadata +28 -52
  33. data/lib/configuration.rb +0 -22
  34. data/lib/persistence.rb +0 -9
  35. data/test/database.yml +0 -3
  36. data/test/fixtures/add_users_migration.rb.fixture +0 -9
  37. data/test/fixtures/album.rb.fixture +0 -4
  38. data/test/fixtures/album_unit_test.rb.fixture +0 -9
  39. data/test/fixtures/album_with_cols.rb.fixture +0 -7
  40. data/test/fixtures/create_users_migration.rb.fixture +0 -12
  41. data/test/fixtures/routes.rb.fixture +0 -3
  42. data/test/fixtures/zoo_no_cols/edit.html.erb.fixture +0 -11
  43. data/test/fixtures/zoo_no_cols/index.html.erb.fixture +0 -20
  44. data/test/fixtures/zoo_no_cols/new.html.erb.fixture +0 -11
  45. data/test/fixtures/zoo_no_cols/show.html.erb.fixture +0 -6
  46. data/test/fixtures/zoo_with_cols/edit.html.erb.fixture +0 -19
  47. data/test/fixtures/zoo_with_cols/index.html.erb.fixture +0 -26
  48. data/test/fixtures/zoo_with_cols/new.html.erb.fixture +0 -19
  49. data/test/fixtures/zoo_with_cols/show.html.erb.fixture +0 -22
  50. data/test/fixtures/zoo_with_cols/zoo.rb.fixture +0 -4
  51. data/test/fixtures/zoo_with_cols/zoos_controller.rb.fixture +0 -50
  52. data/test/generators/migration_generator_test.rb +0 -71
  53. data/test/generators/model_generator_test.rb +0 -37
  54. data/test/generators/scaffold_generator_test.rb +0 -61
  55. data/test/lib/user.rb +0 -6
  56. data/test/tasks/db_migration_tasks_test.rb +0 -57
  57. data/test/test_helper.rb +0 -77
@@ -1,25 +1,52 @@
1
1
  require 'rubygems'
2
2
  require 'genosaurus'
3
- require 'erubis'
4
- begin
5
- require 'mack-orm_common'
6
- rescue Exception => e
7
- end
8
3
 
9
- gem("datamapper", "0.3.2")
4
+ # gem 'dm-core', '0.9.2'
5
+ # require 'dm-core'
6
+ # gem 'dm-validations', '0.9.2'
7
+ # require 'dm-validations'
8
+ # gem 'dm-migrations', '0.9.2'
9
+ # require 'dm-migrations'
10
+ $: << File.expand_path(File.join(File.dirname(__FILE__), "dm_patches"))
10
11
  require 'data_mapper'
11
12
 
12
13
  fl = File.dirname(__FILE__)
13
- require File.join(fl, "persistence")
14
- require File.join(fl, "configuration")
15
- Mack::Configuration.load_database_configurations
16
- class SchemaInfo # :nodoc:
17
- include DataMapper::Persistence
18
-
19
- property :version, :integer, :default => 0
20
- end
14
+ require File.join(fl, "database")
15
+ require File.join(fl, "helpers", "orm_helpers")
16
+ require File.join(fl, "resource")
17
+ require File.join(fl, "runner")
18
+ require File.join(fl, "test_extensions")
19
+
20
+ require File.join(fl, "model_column")
21
+ require File.join(fl, "genosaurus_helpers")
21
22
 
22
- [:helpers, :migration_generator, :model_generator, :scaffold_generator].each do |folder|
23
+
24
+ [:helpers, :migration_generator, :model_generator, :scaffold_generator, :dm_patches].each do |folder|
23
25
  Dir.glob(File.join(File.dirname(__FILE__), folder.to_s, "**/*.rb")).each {|f| require f}
24
26
  end
25
- # Dir.glob(File.join(File.dirname(__FILE__), "tasks", "**/*.rake")).each {|f| load f}
27
+
28
+ English::Inflect.word 'email_address'
29
+ English::Inflect.word 'address'
30
+
31
+ module DataMapper # :nodoc:
32
+ class Logger # :nodoc:
33
+
34
+ [:debug, :info, :warn, :error, :fatal].each do |m|
35
+ unless method_defined?("dm_#{m}")
36
+ eval %{
37
+ alias_method :dm_#{m}, :#{m}
38
+
39
+ def #{m}(message)
40
+ Mack.logger.#{m}(message)
41
+ dm_#{m}(message)
42
+ end
43
+ }
44
+ end
45
+ end
46
+
47
+ end
48
+ end
49
+
50
+ DataMapper.logger = DataMapper::Logger.new(StringIO.new, 0)
51
+
52
+ Mack::Database.establish_connection(Mack.env)
@@ -4,28 +4,30 @@
4
4
  # rake generate:migration name=create_users
5
5
  #
6
6
  # db/migrations/<number>_create_users.rb:
7
- # class CreateUsers < DataMapper::Migration
8
- # self.up
7
+ # migration <number>, :create_users do
8
+ # up do
9
9
  # end
10
- #
11
- # self.down
10
+ #
11
+ # down do
12
12
  # end
13
13
  # end
14
14
  #
15
15
  # Example with columns:
16
- # rake generate:migration name=create_users cols=username:string,email_address:string,created_at:datetime,updated_at:datetime
16
+ # rake generate:migration name=create_users cols=username:string,email_address:string,created_at:date_time,updated_at:date_time
17
17
  #
18
18
  # db/migrations/<number>_create_users.rb:
19
- # class CreateUsers < DataMapper::Migration
20
- # self.up
21
- # create_table :users do |t|
22
- # t.column :username, :string
23
- # t.column :email_address, :string
24
- # t.column :created_at, :datetime
25
- # t.column :updated_at, :datetime
19
+ # migration <number>, :create_users do
20
+ # up do
21
+ # create_table :users do
22
+ # column :id, Integer, :serial => true
23
+ # column :username, String, :size => 50
24
+ # column :email, String, :size => 50
25
+ # column :created_at, DateTime
26
+ # column :updated_at, DateTime
27
+ # end
26
28
  # end
27
- #
28
- # self.down
29
+ #
30
+ # down do
29
31
  # drop_table :users
30
32
  # end
31
33
  # end
@@ -33,9 +35,18 @@ class MigrationGenerator < Genosaurus
33
35
 
34
36
  require_param :name
35
37
 
36
- def setup
37
- @table_name = param(:name).underscore.plural.gsub("create_", "")
38
- @migration_name = "#{next_migration_number}_#{param(:name).underscore}"
38
+ def setup # :nodoc:
39
+ @table_name = param(:name).underscore.gsub("create_", "")
40
+ @current_migration_number = next_migration_number
41
+ @migration_name = "#{@current_migration_number}_#{param(:name).underscore}"
42
+ end
43
+
44
+ def migration_columns # :nodoc:
45
+ [Mack::Genosaurus::DataMapper::ModelColumn.new(param(:name), "id:serial"), columns].flatten
46
+ end
47
+
48
+ def get_column_type(column) # :nodoc:
49
+ column.column_type.camelcase
39
50
  end
40
51
 
41
52
  end
@@ -1,14 +1,14 @@
1
- class <%= param(:name).camelcase %> < DataMapper::Migration
1
+ migration <%= @current_migration_number.to_i %>, :<%= param(:name) %> do
2
2
 
3
- def self.up
3
+ up do
4
4
  <%
5
5
  unless columns.empty?
6
6
  -%>
7
- create_table :<%= @table_name %> do |t|
7
+ create_table :<%= @table_name %> do
8
8
  <%
9
- for column in columns
9
+ for column in migration_columns
10
10
  -%>
11
- t.column :<%= column.column_name %>, :<%= column.column_type %>
11
+ column :<%= column.column_name %>, <%= get_column_type(column) %>
12
12
  <%
13
13
  end
14
14
  -%>
@@ -18,7 +18,7 @@ class <%= param(:name).camelcase %> < DataMapper::Migration
18
18
  -%>
19
19
  end
20
20
 
21
- def self.down
21
+ down do
22
22
  <%
23
23
  unless columns.empty?
24
24
  -%>
@@ -0,0 +1,42 @@
1
+ module Mack
2
+ module Genosaurus
3
+ module DataMapper
4
+ # Used to represent a 'column' from the param cols or columns for generators.
5
+ class ModelColumn
6
+
7
+ # The name of the column.
8
+ attr_accessor :column_name
9
+ # The type of the column. Ie. string, integer, datetime, etc...
10
+ attr_accessor :column_type
11
+ # The name of the model associated with the column. Ie. user, post, etc...
12
+ attr_accessor :model_name
13
+
14
+ # Takes in the model_name (user, post, etc...) and the column (username:string, body:text, etc...)
15
+ def initialize(model_name, column_unsplit)
16
+ self.model_name = model_name.singular.underscore
17
+ cols = column_unsplit.split(":")
18
+ self.column_name = cols.first#.underscore
19
+ self.column_type = cols.last#.underscore
20
+ end
21
+
22
+ # Generates the appropriate HTML form field for the type of column represented.
23
+ #
24
+ # Examples:
25
+ # Mack::Generator::ColumnObject.new("user", "username:string").form_field
26
+ # => "<%= model_text_field(@user, :username) %>"
27
+ # Mack::Generator::ColumnObject.new("Post", "body:text").form_field
28
+ # => "<%= model_textarea(@user, :username) %>"
29
+ def form_field
30
+ case self.column_type
31
+ when "text"
32
+ %{<%= model_textarea(@#{self.model_name}, :#{self.column_name}) %>}
33
+ else
34
+ %{<%= model_text_field(@#{self.model_name}, :#{self.column_name}) %>}
35
+ end
36
+ end
37
+
38
+ end # ModelColumn
39
+ end # DataMapper
40
+
41
+ end # Generator
42
+ end # Mack
@@ -5,7 +5,14 @@ model_template:
5
5
  type: file
6
6
  template_path: <%= File.join(templates_directory_path, "model.rb.template") %>
7
7
  output_path: <%= File.join("app", "models", "#{param(:name).singular.underscore}.rb") %>
8
+ <% if app_config.mack.testing_framework == "test_case" -%>
8
9
  test_template:
9
10
  type: file
10
- template_path: <%= File.join(templates_directory_path, "test.rb.template") %>
11
- output_path: <%= File.join("test", "unit", "#{param(:name).singular.underscore}_test.rb") %>
11
+ template_path: <%= File.join(templates_directory_path, "test_case.rb.template") %>
12
+ output_path: <%= File.join("test", "unit", "#{param(:name).singular.underscore}_test.rb") %>
13
+ <% elsif app_config.mack.testing_framework == "rspec" -%>
14
+ test_template:
15
+ type: file
16
+ template_path: <%= File.join(templates_directory_path, "rspec.rb.template") %>
17
+ output_path: <%= File.join("test", "unit", "#{param(:name).singular.underscore}_spec.rb") %>
18
+ <% end -%>
@@ -5,15 +5,15 @@
5
5
  #
6
6
  # app/models/user.rb:
7
7
  # class User
8
- # include DataMapper::Persistence
8
+ # include DataMapper::Resource
9
9
  #
10
10
  # end
11
11
  # db/migrations/<number>_create_users.rb:
12
- # class CreateUsers < DataMapper::Migration
13
- # self.up
12
+ # migration <number>, :create_users do
13
+ # up do
14
14
  # end
15
- #
16
- # self.down
15
+ #
16
+ # down do
17
17
  # end
18
18
  # end
19
19
  #
@@ -22,24 +22,25 @@
22
22
  #
23
23
  # app/models/user.rb:
24
24
  # class User
25
- # include DataMapper::Persistence
25
+ # include DataMapper::Resource
26
26
  #
27
- # property :username, :string
28
- # property :email_address, :string
29
- # property :created_at, :datetime
30
- # property :updated_at, :datetime
27
+ # property :username, String
28
+ # property :email_address, String
29
+ # property :created_at, DateTime
30
+ # property :updated_at, DateTime
31
31
  # end
32
32
  # db/migrations/<number>_create_users.rb:
33
- # class CreateUsers < DataMapper::Migration
34
- # self.up
35
- # create_table :users do |t|
36
- # t.column :username, :string
37
- # t.column :email_address, :string
38
- # t.column :created_at, :datetime
39
- # t.column :updated_at, :datetime
33
+ # migration <number>, :create_users do
34
+ # up do
35
+ # create_table :users do
36
+ # column :username, String, :size => 50
37
+ # column :email, String, :size => 50
38
+ # column :created_at, DateTime
39
+ # column :updated_at, DateTime
40
+ # end
40
41
  # end
41
- #
42
- # self.down
42
+ #
43
+ # down do
43
44
  # drop_table :users
44
45
  # end
45
46
  # end
@@ -47,8 +48,12 @@ class ModelGenerator < Genosaurus
47
48
 
48
49
  require_param :name
49
50
 
50
- def after_generate
51
+ def after_generate # :nodoc:
51
52
  MigrationGenerator.run(@options.merge({"name" => "create_#{param(:name).plural}"}))
52
53
  end
53
54
 
55
+ def migration_columns # :nodoc:
56
+ [Mack::Genosaurus::DataMapper::ModelColumn.new(param(:name), "id:serial"), columns].flatten
57
+ end
58
+
54
59
  end
@@ -1,10 +1,10 @@
1
1
  class <%= param(:name).singular.camelcase %>
2
- include DataMapper::Persistence
3
-
2
+ include DataMapper::Resource
3
+
4
4
  <%
5
- for column in columns
5
+ for column in migration_columns
6
6
  -%>
7
- property :<%= column.column_name %>, :<%= column.column_type %>
7
+ property :<%= column.column_name %>, <%= column.column_type.camelcase %>
8
8
  <%
9
9
  end
10
10
  -%>
@@ -0,0 +1,7 @@
1
+ require File.join(File.dirname(__FILE__), "..", "spec_helper")
2
+
3
+ describe <%= param(:name).singular.camelcase %> do
4
+
5
+ it "should do something :)"
6
+
7
+ end
@@ -0,0 +1,17 @@
1
+ module DataMapper
2
+ module Resource
3
+
4
+ # Returns the key of the DataMapper::Resource object when passed into a url helper
5
+ #
6
+ # Example:
7
+ # class User
8
+ # include DataMapper::Resource
9
+ # property :name, String, :key => true
10
+ # end
11
+ # users_show_url(User.new(:username => "markbates")) # => /users/markbates
12
+ def to_param
13
+ self.key
14
+ end
15
+
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ #--
2
+ # module Mack
3
+ # class Runner
4
+ #
5
+ # alias_method :data_mapper_custom_dispatch_wrapper, :custom_dispatch_wrapper# if self.respond_to? :custom_dispatch_wrapper
6
+ #
7
+ # # Wrap all requests in a 'default' DataMapper repository block.
8
+ # def custom_dispatch_wrapper(&block)
9
+ # repo = (app_config.mack.default_respository_name || "default").to_sym
10
+ # repository(repo) do
11
+ # data_mapper_custom_dispatch_wrapper(&block)
12
+ # end
13
+ # end
14
+ #
15
+ # end # Runner
16
+ # end # Mack
17
+ #++
@@ -18,7 +18,18 @@ show_template:
18
18
  type: file
19
19
  template_path: <%= File.join(templates_directory_path, "app", "views", "show.html.erb.template") %>
20
20
  output_path: <%= File.join("app", "views", @name_plural, "show.html.erb") %>
21
- functional_teat_template:
21
+ helper_template:
22
22
  type: file
23
- template_path: <%= File.join(templates_directory_path, "test.rb.template") %>
24
- output_path: <%= File.join("test", "functional", "#{@name_plural}_controller_test.rb") %>
23
+ template_path: <%= File.join(templates_directory_path, "app", "helpers", "controllers", "helper.rb.template") %>
24
+ output_path: <%= File.join("app", "helpers", "controllers", "#{@name_plural}_controller_helper.rb") %>
25
+ <% if app_config.mack.testing_framework == "test_case" -%>
26
+ functional_test_template:
27
+ type: file
28
+ template_path: <%= File.join(templates_directory_path, "test", "functional", "test_case.rb.template") %>
29
+ output_path: <%= File.join("test", "functional", "#{@name_plural}_controller_test.rb") %>
30
+ <% elsif app_config.mack.testing_framework == "rspec" -%>
31
+ functional_test_template:
32
+ type: file
33
+ template_path: <%= File.join(templates_directory_path, "test", "functional", "rspec.rb.template") %>
34
+ output_path: <%= File.join("test", "functional", "#{@name_plural}_controller_spec.rb") %>
35
+ <% end -%>
@@ -6,21 +6,21 @@ class ScaffoldGenerator < Genosaurus
6
6
 
7
7
  require_param :name
8
8
 
9
- def setup
9
+ def setup # :nodoc:
10
10
  @name_singular = param(:name).singular.underscore
11
11
  @name_plural = param(:name).plural.underscore
12
12
  @name_singular_camel = @name_singular.camelcase
13
13
  @name_plural_camel = @name_plural.camelcase
14
14
  end
15
15
 
16
- def after_generate
16
+ def after_generate # :nodoc:
17
17
  ModelGenerator.run(@options)
18
18
  update_routes_file
19
19
  end
20
20
 
21
- def update_routes_file
21
+ def update_routes_file # :nodoc:
22
22
  # update routes.rb
23
- routes = File.join(Mack::Configuration.config_directory, "routes.rb")
23
+ routes = File.join(Mack.root, "config", "routes.rb")
24
24
  rf = File.open(routes).read
25
25
  unless rf.match(".resource :#{@name_plural}")
26
26
  puts "Updating routes.rb"
@@ -1,5 +1,6 @@
1
- class <%= @name_plural_camel %>Controller < Mack::Controller::Base
2
-
1
+ class <%= @name_plural_camel %>Controller
2
+ include Mack::Controller
3
+
3
4
  # GET /<%= @name_plural %>
4
5
  def index
5
6
  @<%= @name_plural %> = <%= @name_singular_camel %>.all
@@ -7,7 +8,7 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
7
8
 
8
9
  # GET /<%= @name_plural %>/1
9
10
  def show
10
- @<%= @name_singular %> = <%= @name_singular_camel %>.first(params(:id))
11
+ @<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
11
12
  end
12
13
 
13
14
  # GET /<%= @name_plural %>/new
@@ -17,12 +18,12 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
17
18
 
18
19
  # GET /<%= @name_plural %>/1/edit
19
20
  def edit
20
- @<%= @name_singular %> = <%= @name_singular_camel %>.first(params(:id))
21
+ @<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
21
22
  end
22
23
 
23
24
  # POST /<%= @name_plural %>
24
25
  def create
25
- @<%= @name_singular %> = <%= @name_singular_camel %>.new(params(:<%= @name_singular %>))
26
+ @<%= @name_singular %> = <%= @name_singular_camel %>.new(params[:<%= @name_singular %>])
26
27
  if @<%= @name_singular %>.save
27
28
  redirect_to(<%= @name_plural %>_show_url(:id => @<%= @name_singular %>))
28
29
  else
@@ -32,8 +33,8 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
32
33
 
33
34
  # PUT /<%= @name_plural %>/1
34
35
  def update
35
- @<%= @name_singular %> = <%= @name_singular_camel %>.first(params(:id))
36
- if @<%= @name_singular %>.update_attributes(params(:<%= @name_singular %>))
36
+ @<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
37
+ if @<%= @name_singular %>.update_attributes(params[:<%= @name_singular %>])
37
38
  redirect_to(<%= @name_plural %>_show_url(:id => @<%= @name_singular %>))
38
39
  else
39
40
  render(:action, "edit")
@@ -42,8 +43,8 @@ class <%= @name_plural_camel %>Controller < Mack::Controller::Base
42
43
 
43
44
  # DELETE /<%= @name_plural %>/1
44
45
  def delete
45
- @<%= @name_singular %> = <%= @name_singular_camel %>.first(params(:id))
46
- @<%= @name_singular %>.destroy!
46
+ @<%= @name_singular %> = <%= @name_singular_camel %>.get(params[:id])
47
+ @<%= @name_singular %>.destroy
47
48
  redirect_to(<%= @name_plural %>_index_url)
48
49
  end
49
50