merb_datamapper 0.9.3 → 0.9.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest.txt +15 -17
- data/Rakefile +2 -2
- data/TODO +3 -3
- data/lib/generators/data_mapper_migration.rb +4 -0
- data/lib/generators/data_mapper_model.rb +15 -0
- data/lib/generators/data_mapper_resource_controller.rb +25 -0
- data/lib/generators/templates/migration.rb +7 -0
- data/lib/generators/templates/model.rb +9 -0
- data/lib/generators/templates/model_migration.rb +15 -0
- data/lib/generators/templates/resource_controller.rb +59 -0
- data/{datamapper_generators/resource_controller/templates/app/views/%controller_file_name% → lib/generators/templates/views}/edit.html.erb +3 -4
- data/{datamapper_generators/resource_controller/templates/app/views/%controller_file_name% → lib/generators/templates/views}/index.html.erb +2 -2
- data/{datamapper_generators/resource_controller/templates/app/views/%controller_file_name% → lib/generators/templates/views}/new.html.erb +3 -4
- data/{datamapper_generators/resource_controller/templates/app/views/%controller_file_name% → lib/generators/templates/views}/show.html.erb +2 -2
- data/lib/merb/session/data_mapper_session.rb +1 -1
- data/lib/merb_datamapper.rb +4 -0
- data/lib/merb_datamapper/merbtasks.rb +31 -0
- data/lib/merb_datamapper/version.rb +2 -2
- data/spec/generators/data_mapper_migration_spec.rb +31 -0
- data/spec/generators/data_mapper_model_spec.rb +38 -0
- data/spec/generators/data_mapper_resource_controller_spec.rb +24 -0
- data/spec/generators/spec_helper.rb +279 -0
- data/spec/spec_helper.rb +1 -1
- metadata +33 -25
- data/datamapper_generators/migration/USAGE +0 -4
- data/datamapper_generators/migration/migration_generator.rb +0 -64
- data/datamapper_generators/migration/templates/new_migration.erb +0 -7
- data/datamapper_generators/model/USAGE +0 -0
- data/datamapper_generators/model/model_generator.rb +0 -45
- data/datamapper_generators/model/templates/app/models/%model_file_name%.rb +0 -7
- data/datamapper_generators/resource_controller/USAGE +0 -0
- data/datamapper_generators/resource_controller/resource_controller_generator.rb +0 -109
- data/datamapper_generators/resource_controller/templates/app/controllers/%controller_file_name%.rb +0 -68
- data/datamapper_generators/resource_controller/templates/app/helpers/%controller_file_name%_helper.rb +0 -16
- data/datamapper_generators/resource_migration/USAGE +0 -4
- data/datamapper_generators/resource_migration/resource_migration_generator.rb +0 -122
- data/datamapper_generators/resource_migration/templates/class_migration.erb +0 -13
data/Manifest.txt
CHANGED
@@ -4,23 +4,17 @@ Manifest.txt
|
|
4
4
|
README.txt
|
5
5
|
Rakefile
|
6
6
|
TODO
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/index.html.erb
|
19
|
-
datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/new.html.erb
|
20
|
-
datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/show.html.erb
|
21
|
-
datamapper_generators/resource_migration/USAGE
|
22
|
-
datamapper_generators/resource_migration/resource_migration_generator.rb
|
23
|
-
datamapper_generators/resource_migration/templates/class_migration.erb
|
7
|
+
lib/generators/data_mapper_migration.rb
|
8
|
+
lib/generators/data_mapper_model.rb
|
9
|
+
lib/generators/data_mapper_resource_controller.rb
|
10
|
+
lib/generators/templates/migration.rb
|
11
|
+
lib/generators/templates/model.rb
|
12
|
+
lib/generators/templates/model_migration.rb
|
13
|
+
lib/generators/templates/resource_controller.rb
|
14
|
+
lib/generators/templates/views/edit.html.erb
|
15
|
+
lib/generators/templates/views/index.html.erb
|
16
|
+
lib/generators/templates/views/new.html.erb
|
17
|
+
lib/generators/templates/views/show.html.erb
|
24
18
|
lib/merb/orms/data_mapper/connection.rb
|
25
19
|
lib/merb/orms/data_mapper/database.yml.sample
|
26
20
|
lib/merb/orms/data_mapper/resource.rb
|
@@ -29,5 +23,9 @@ lib/merb_datamapper.rb
|
|
29
23
|
lib/merb_datamapper/merbtasks.rb
|
30
24
|
lib/merb_datamapper/version.rb
|
31
25
|
spec/connection_spec.rb
|
26
|
+
spec/generators/data_mapper_migration_spec.rb
|
27
|
+
spec/generators/data_mapper_model_spec.rb
|
28
|
+
spec/generators/data_mapper_resource_controller_spec.rb
|
29
|
+
spec/generators/spec_helper.rb
|
32
30
|
spec/spec.opts
|
33
31
|
spec/spec_helper.rb
|
data/Rakefile
CHANGED
@@ -10,11 +10,11 @@ AUTHOR = "Jason Toy"
|
|
10
10
|
EMAIL = "jtoy@rubynow.com"
|
11
11
|
GEM_NAME = "merb_datamapper"
|
12
12
|
GEM_VERSION = DataMapper::MerbDataMapper::VERSION
|
13
|
-
GEM_DEPENDENCIES = [["dm-core", GEM_VERSION], ["dm-migrations", GEM_VERSION], ["merb-core", ">=0.9.
|
13
|
+
GEM_DEPENDENCIES = [["dm-core", GEM_VERSION], ["dm-migrations", GEM_VERSION], ["merb-core", ">=0.9.4"], ['templater', ">=0.1.5"]]
|
14
14
|
GEM_CLEAN = ["log", "pkg", "coverage"]
|
15
15
|
GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
|
16
16
|
|
17
|
-
PROJECT_NAME = "
|
17
|
+
PROJECT_NAME = "merb"
|
18
18
|
PROJECT_URL = "http://github.com/sam/dm-more/tree/master/merb_datamapper"
|
19
19
|
PROJECT_DESCRIPTION = PROJECT_SUMMARY = "DataMapper plugin providing DataMapper support for Merb"
|
20
20
|
|
data/TODO
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
TODO
|
2
2
|
====
|
3
3
|
|
4
|
-
* Add proper migration support as soon as its released
|
5
4
|
* Fix session support
|
6
|
-
*
|
7
|
-
|
5
|
+
* Add in model migration generation
|
6
|
+
* Tidy up Resource Controller generator
|
7
|
+
* Add property interpolation for views
|
8
8
|
---
|
9
9
|
TODO tickets may also be found in the DataMapper Issue Tracker:
|
10
10
|
http://wm.lighthouseapp.com/projects/4819-datamapper/overview
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Merb::Generators::ModelGenerator.option :migration, :as => :boolean, :desc => 'Also generate a dm-migration for the model'
|
2
|
+
|
3
|
+
class Merb::Generators::ModelGenerator
|
4
|
+
##
|
5
|
+
# Corrects case of common datamapper arguments.
|
6
|
+
def datamapper_type(type)
|
7
|
+
return 'DateTime' if type == 'datetime'
|
8
|
+
return type.camel_case
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
Merb::Generators::ModelGenerator.template :model_datamapper, :orm => :datamapper do
|
13
|
+
source(File.dirname(__FILE__), "templates", "model.rb")
|
14
|
+
destination("app/models", base_path, "#{file_name}.rb")
|
15
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class Merb::Generators::ResourceControllerGenerator
|
2
|
+
# TODO: fix this for Datamapper, so that it returns the primary keys for the model
|
3
|
+
def params_for_get
|
4
|
+
"params[:id]"
|
5
|
+
end
|
6
|
+
|
7
|
+
# TODO: implement this for Datamapper so that we get the model properties
|
8
|
+
def properties
|
9
|
+
[]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
Merb::Generators::ResourceControllerGenerator.template :controller_datamapper, :orm => :datamapper do
|
15
|
+
source(File.dirname(__FILE__), "templates/resource_controller.rb")
|
16
|
+
destination("app/controllers", base_path, "#{file_name}.rb")
|
17
|
+
end
|
18
|
+
|
19
|
+
[:index, :show, :edit, :new].each do |view|
|
20
|
+
Merb::Generators::ResourceControllerGenerator.template "view_#{view}_datamapper".to_sym,
|
21
|
+
:orm => :datamapper, :template_engine => :erb do
|
22
|
+
source(File.dirname(__FILE__), "templates/views/#{view}.html.erb")
|
23
|
+
destination("app/views", base_path, "#{file_name}/#{view}.html.erb")
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
migration <%= current_migration_nr + 1 %>, :<%= migration_name %> do
|
2
|
+
up do
|
3
|
+
<% unless properties.empty? -%>
|
4
|
+
create_table :<%= table_name %> do
|
5
|
+
<% properties.each do |p| -%>
|
6
|
+
column :<%= p.name -%>, <%= p.type %>
|
7
|
+
<% end -%>
|
8
|
+
end
|
9
|
+
<% end -%>
|
10
|
+
end
|
11
|
+
|
12
|
+
down do
|
13
|
+
drop_table :<%= table_name %>
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
<% with_modules(modules) do -%>
|
2
|
+
class <%= class_name %> < Application
|
3
|
+
# provides :xml, :yaml, :js
|
4
|
+
|
5
|
+
def index
|
6
|
+
@<%= plural_model %> = <%= model_class_name %>.all
|
7
|
+
display @<%= plural_model %>
|
8
|
+
end
|
9
|
+
|
10
|
+
def show
|
11
|
+
@<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
12
|
+
raise NotFound unless @<%= singular_model %>
|
13
|
+
display @<%= singular_model %>
|
14
|
+
end
|
15
|
+
|
16
|
+
def new
|
17
|
+
only_provides :html
|
18
|
+
@<%= singular_model %> = <%= model_class_name %>.new
|
19
|
+
render
|
20
|
+
end
|
21
|
+
|
22
|
+
def edit
|
23
|
+
only_provides :html
|
24
|
+
@<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
25
|
+
raise NotFound unless @<%= singular_model %>
|
26
|
+
render
|
27
|
+
end
|
28
|
+
|
29
|
+
def create
|
30
|
+
@<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
|
31
|
+
if @<%= singular_model %>.save
|
32
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
33
|
+
else
|
34
|
+
render :new
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def update
|
39
|
+
@<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
40
|
+
raise NotFound unless @<%= singular_model %>
|
41
|
+
if @<%= singular_model %>.update_attributes(params[:<%= singular_model %>]) || !@<%= singular_model %>.dirty?
|
42
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
43
|
+
else
|
44
|
+
raise BadRequest
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def destroy
|
49
|
+
@<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
50
|
+
raise NotFound unless @<%= singular_model %>
|
51
|
+
if @<%= singular_model %>.destroy
|
52
|
+
redirect url(:<%= (modules.collect{|m| m.downcase} << singular_model).join("_") %>)
|
53
|
+
else
|
54
|
+
raise BadRequest
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
end # <%= class_name %>
|
59
|
+
<% end -%>
|
@@ -1,14 +1,13 @@
|
|
1
|
-
<h1><%=
|
1
|
+
<h1><%= class_name %> controller, edit action</h1>
|
2
2
|
|
3
|
-
<p>Edit this file in <tt>app/views/<%=
|
3
|
+
<p>Edit this file in <tt>app/views/<%= file_name %>/edit.html.erb</tt></p>
|
4
4
|
|
5
5
|
<%%= error_messages_for :<%= singular_model %> %>
|
6
6
|
|
7
7
|
<%% form_for(@<%= singular_model %>, :action => url(:<%= singular_model %>, @<%= singular_model %>)) do %>
|
8
8
|
<% for property in properties.select{|p| !p.key?} -%>
|
9
9
|
<p>
|
10
|
-
|
11
|
-
<%%= <%= field_from_type(property.type) %> :<%= property.getter %> %>
|
10
|
+
<%%= text_control :<%= property.getter %>, :label => "<%= Extlib::Inflection.humanize(property.field) %>" %>
|
12
11
|
</p>
|
13
12
|
|
14
13
|
<% end -%>
|
@@ -1,6 +1,6 @@
|
|
1
|
-
<h1><%=
|
1
|
+
<h1><%= class_name %> controller, index action</h1>
|
2
2
|
|
3
|
-
<p>Edit this file in <tt>app/views/<%=
|
3
|
+
<p>Edit this file in <tt>app/views/<%= file_name %>/index.html.erb</tt></p>
|
4
4
|
|
5
5
|
<table>
|
6
6
|
<tr>
|
@@ -1,14 +1,13 @@
|
|
1
|
-
<h1><%=
|
1
|
+
<h1><%= class_name %> controller, new action</h1>
|
2
2
|
|
3
|
-
<p>Edit this file in <tt>app/views/<%=
|
3
|
+
<p>Edit this file in <tt>app/views/<%= file_name %>/new.html.erb</tt></p>
|
4
4
|
|
5
5
|
<%%= error_messages_for :<%= singular_model %> %>
|
6
6
|
|
7
7
|
<%% form_for(@<%= singular_model %>, :action => url(:<%= plural_model %>) ) do |f| %>
|
8
8
|
<% for property in properties.select{|p| !p.key?} -%>
|
9
9
|
<p>
|
10
|
-
|
11
|
-
<%%= <%= field_from_type(property.type) %> :<%= property.getter %> %>
|
10
|
+
<%%= text_control :<%= property.getter %>, :label => "<%= Extlib::Inflection.humanize(property.field) %>" %>
|
12
11
|
</p>
|
13
12
|
|
14
13
|
<% end -%>
|
@@ -1,6 +1,6 @@
|
|
1
|
-
<h1><%=
|
1
|
+
<h1><%= class_name %> controller, show action</h1>
|
2
2
|
|
3
|
-
<p>Edit this file in <tt>app/views/<%=
|
3
|
+
<p>Edit this file in <tt>app/views/<%= file_name %>/show.html.erb</tt></p>
|
4
4
|
|
5
5
|
<% for property in properties -%>
|
6
6
|
<p>
|
data/lib/merb_datamapper.rb
CHANGED
@@ -17,4 +17,8 @@ if defined?(Merb::Plugins)
|
|
17
17
|
require File.join(File.dirname(__FILE__) / "merb" / "orms" / "data_mapper" / "resource")
|
18
18
|
|
19
19
|
Merb::Plugins.add_rakefiles "merb_datamapper" / "merbtasks"
|
20
|
+
|
21
|
+
Merb.add_generators File.join(File.dirname(__FILE__), 'generators', 'data_mapper_model')
|
22
|
+
Merb.add_generators File.join(File.dirname(__FILE__), 'generators', 'data_mapper_resource_controller')
|
23
|
+
Merb.add_generators File.join(File.dirname(__FILE__), 'generators', 'data_mapper_migration')
|
20
24
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'fileutils'
|
2
|
+
require 'merb/orms/data_mapper/connection'
|
2
3
|
|
3
4
|
namespace :dm do
|
4
5
|
|
@@ -43,6 +44,36 @@ namespace :dm do
|
|
43
44
|
migrate_down!(version)
|
44
45
|
end
|
45
46
|
end
|
47
|
+
|
48
|
+
desc "Migrate the database to the latest version"
|
49
|
+
task :migrate => 'dm:db:migrate:up'
|
50
|
+
|
51
|
+
desc "Create the database (postgres only)"
|
52
|
+
task :create do
|
53
|
+
config = Merb::Orms::DataMapper.config
|
54
|
+
puts "Creating database '#{config[:database]}'"
|
55
|
+
case config[:adapter]
|
56
|
+
when 'postgres'
|
57
|
+
`createdb -U #{config[:username]} #{config[:database]}`
|
58
|
+
else
|
59
|
+
raise "Adapter #{config[:adapter]} not supported yet."
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
desc "Drop the database (postgres only)"
|
64
|
+
task :drop do
|
65
|
+
config = Merb::Orms::DataMapper.config
|
66
|
+
puts "Droping database '#{config[:database]}'"
|
67
|
+
case config[:adapter]
|
68
|
+
when 'postgres'
|
69
|
+
`dropdb -U #{config[:username]} #{config[:database]}`
|
70
|
+
else
|
71
|
+
raise "Adapter #{config[:adapter]} not supported yet."
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
desc "Drop the database, and migrate from scratch"
|
76
|
+
task :reset => [:drop, :create, :migrate]
|
46
77
|
end
|
47
78
|
|
48
79
|
namespace :sessions do
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
# many specs copied from merb-gen master branch
|
3
|
+
describe "Merb::Generators::MigrationGenerator for DataMapper" do
|
4
|
+
it "should complain if no name is specified" do
|
5
|
+
lambda {
|
6
|
+
@generator = Merb::Generators::MigrationGenerator.new('/tmp', {:orm => :datamapper})
|
7
|
+
}.should raise_error(::Templater::TooFewArgumentsError)
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
describe "with no options" do
|
12
|
+
before(:each) do
|
13
|
+
@base_dir = "/tmp/migrations"
|
14
|
+
FileUtils.mkdir_p @base_dir
|
15
|
+
@generator = Merb::Generators::MigrationGenerator.new(@base_dir, {:orm => :datamapper}, 'SomeMoreStuff')
|
16
|
+
end
|
17
|
+
|
18
|
+
after(:each) do
|
19
|
+
FileUtils.rm_r @base_dir
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should render successfully" do
|
23
|
+
lambda { @generator.render! }.should_not raise_error
|
24
|
+
end
|
25
|
+
|
26
|
+
it "creates the file correctly" do
|
27
|
+
@generator.should create('/tmp/migrations/schema/migrations/001_some_more_stuff_migration.rb')
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
describe "Merb::Generators::ModelGenerator for DataMapper" do
|
3
|
+
it "complains if no name is specified" do
|
4
|
+
lambda {
|
5
|
+
@generator = Merb::Generators::ModelGenerator.new('/tmp', {:orm => :datamapper})
|
6
|
+
}.should raise_error(::Templater::TooFewArgumentsError)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
before do
|
11
|
+
@generator = Merb::Generators::ModelGenerator.new('/tmp',{:orm => :datamapper}, 'Stuff')
|
12
|
+
end
|
13
|
+
|
14
|
+
it_should_behave_like "namespaced generator"
|
15
|
+
|
16
|
+
it "should create a model" do
|
17
|
+
@generator.should create('/tmp/app/models/stuff.rb')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should render successfully" do
|
21
|
+
lambda { @generator.render! }.should_not raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
it "generates a resource" do
|
25
|
+
model_file = @generator.render!.detect { |file| file =~ /class/ }
|
26
|
+
model_file.should match(/include DataMapper::Resource/)
|
27
|
+
end
|
28
|
+
it "generates a resource with appropriate properties" do
|
29
|
+
@generator = Merb::Generators::ModelGenerator.new('/tmp',{:orm => :datamapper}, 'Stuff', 'id' => 'serial')
|
30
|
+
model_file = @generator.template(:model_datamapper).render
|
31
|
+
model_file.should match(/property :id, Serial/)
|
32
|
+
end
|
33
|
+
it "generates a resource with DateTime properties in the correct case when called with the common argument of datetime" do
|
34
|
+
@generator = Merb::Generators::ModelGenerator.new('/tmp',{:orm => :datamapper}, 'Stuff', 'created_at' => 'datetime')
|
35
|
+
model_file = @generator.template(:model_datamapper).render
|
36
|
+
model_file.should match(/property :created_at, DateTime/)
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
describe "Merb::Generators::ResourceControllerGenerator for DataMapper" do
|
3
|
+
it "complains if no name is specified" do
|
4
|
+
lambda {
|
5
|
+
@generator = Merb::Generators::ResourceControllerGenerator.new('/tmp', {:orm => :datamapper })
|
6
|
+
}.should raise_error(::Templater::TooFewArgumentsError)
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
before do
|
11
|
+
@generator = Merb::Generators::ResourceControllerGenerator.new('/tmp', { :orm => :datamapper }, 'Stuff')
|
12
|
+
end
|
13
|
+
|
14
|
+
it_should_behave_like "namespaced generator"
|
15
|
+
|
16
|
+
it "should create a model" do
|
17
|
+
@generator.should create('/tmp/app/controllers/stuff.rb')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should render successfully" do
|
21
|
+
lambda { @generator.render! }.should_not raise_error
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|