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
File without changes
|
@@ -1,45 +0,0 @@
|
|
1
|
-
class ModelGenerator < Merb::GeneratorBase
|
2
|
-
attr_reader :model_attributes, :model_class_name, :model_file_name
|
3
|
-
|
4
|
-
def initialize(args, runtime_args = {})
|
5
|
-
@base = File.dirname(__FILE__)
|
6
|
-
super
|
7
|
-
@model_file_name = args.shift.snake_case
|
8
|
-
@model_class_name = @model_file_name.to_const_string
|
9
|
-
@model_attributes = normalize_attributes(args)
|
10
|
-
@model_file_name = "#{@model_class_name.snake_case}"
|
11
|
-
|
12
|
-
end
|
13
|
-
|
14
|
-
def manifest
|
15
|
-
record do |m|
|
16
|
-
@m = m
|
17
|
-
|
18
|
-
@assigns = { :model_file_name => model_file_name,
|
19
|
-
:model_attributes => model_attributes,
|
20
|
-
:model_class_name => model_class_name
|
21
|
-
}
|
22
|
-
copy_dirs
|
23
|
-
copy_files
|
24
|
-
|
25
|
-
m.dependency "merb_model_test", [model_file_name], @assigns
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
protected
|
30
|
-
def banner
|
31
|
-
<<-EOS.split("\n").map{|x| x.strip}.join("\n")
|
32
|
-
Creates a Datamapper Model stub..
|
33
|
-
|
34
|
-
USAGE: #{spec.name}"
|
35
|
-
EOS
|
36
|
-
end
|
37
|
-
|
38
|
-
def normalize_attributes(attributes_input)
|
39
|
-
attributes_input.map do |a|
|
40
|
-
name, type = a.split(":")
|
41
|
-
[name.snake_case, type.to_const_string]
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
File without changes
|
@@ -1,109 +0,0 @@
|
|
1
|
-
class ResourceControllerGenerator < Merb::GeneratorBase
|
2
|
-
|
3
|
-
attr_reader :controller_class_name,
|
4
|
-
:controller_file_name,
|
5
|
-
:controller_base_path,
|
6
|
-
:controller_modules,
|
7
|
-
:model_class_name,
|
8
|
-
:full_controller_const,
|
9
|
-
:singular_model,
|
10
|
-
:plural_model,
|
11
|
-
:properties
|
12
|
-
|
13
|
-
def initialize(args, runtime_args = {})
|
14
|
-
@base = File.dirname(__FILE__)
|
15
|
-
|
16
|
-
super
|
17
|
-
|
18
|
-
name = args.shift
|
19
|
-
nfp = name.snake_case.gsub("::", "/").split("/")
|
20
|
-
|
21
|
-
@model_class_name = nfp.pop.singularize.to_const_string
|
22
|
-
@model_class_name = runtime_args[:model_class_name] if runtime_args[:model_class_name]
|
23
|
-
@singular_model = @model_class_name.snake_case
|
24
|
-
@plural_model = @singular_model.pluralize
|
25
|
-
|
26
|
-
nfp << @plural_model
|
27
|
-
|
28
|
-
@controller_file_name = nfp.join("/")
|
29
|
-
|
30
|
-
# Need to setup the directories
|
31
|
-
unless @controller_file_name == File.basename(@controller_file_name)
|
32
|
-
@controller_base_path = controller_file_name.split("/")[0..-2].join("/")
|
33
|
-
end
|
34
|
-
|
35
|
-
@controller_modules = @controller_file_name.to_const_string.split("::")[0..-2]
|
36
|
-
@controller_class_name = @controller_file_name.to_const_string.split("::").last
|
37
|
-
|
38
|
-
@full_controller_const = ((@controller_modules.dup || []) << @controller_class_name).join("::")
|
39
|
-
|
40
|
-
# Gets the properties of the model
|
41
|
-
# FIXME: the model file needs to be required
|
42
|
-
@properties = if Object.const_defined?(@model_class_name)
|
43
|
-
model_class = Object.const_get(@model_class_name)
|
44
|
-
model_class.properties if model_class.respond_to?(:properties)
|
45
|
-
else
|
46
|
-
[]
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def manifest
|
51
|
-
record do |m|
|
52
|
-
@m = m
|
53
|
-
|
54
|
-
# Create the controller directory
|
55
|
-
m.directory File.join("app/controllers", controller_base_path) if controller_base_path
|
56
|
-
|
57
|
-
# Create the helpers directory
|
58
|
-
m.directory File.join("app/helpers", controller_base_path) if controller_base_path
|
59
|
-
|
60
|
-
@assigns = {
|
61
|
-
:controller_modules => controller_modules,
|
62
|
-
:controller_class_name => controller_class_name,
|
63
|
-
:controller_file_name => controller_file_name,
|
64
|
-
:controller_base_path => controller_base_path,
|
65
|
-
:full_controller_const => full_controller_const,
|
66
|
-
:model_class_name => model_class_name,
|
67
|
-
:singular_model => singular_model,
|
68
|
-
:plural_model => plural_model,
|
69
|
-
:properties => properties
|
70
|
-
}
|
71
|
-
copy_dirs
|
72
|
-
copy_files
|
73
|
-
|
74
|
-
m.dependency "merb_resource_controller_test", [@controller_class_name], @assigns
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
# methods used by the templates
|
79
|
-
|
80
|
-
def field_from_type(type)
|
81
|
-
case type.name.to_sym
|
82
|
-
when :String
|
83
|
-
"text_control"
|
84
|
-
when :TrueClass
|
85
|
-
"checkbox_control"
|
86
|
-
when :"DataMapper::Types::Text"
|
87
|
-
"text_area_control"
|
88
|
-
else
|
89
|
-
"text_control"
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
#returns the params needed for getting the object.
|
94
|
-
# uses params[:id] as a default if no keys has been set in the model.
|
95
|
-
def params_for_get
|
96
|
-
params = properties.select{|p| p.key?}.map{|p| "params[:#{p.field}]"}.join(', ')
|
97
|
-
@params_for_get ||= (params == "" ? "params[:id]" : params)
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
|
-
protected
|
102
|
-
def banner
|
103
|
-
<<-EOS.split("\n").map{|x| x.strip}.join("\n")
|
104
|
-
Creates a basic Merb resource controller.
|
105
|
-
|
106
|
-
USAGE: #{spec.name} my_resource
|
107
|
-
EOS
|
108
|
-
end
|
109
|
-
end
|
data/datamapper_generators/resource_controller/templates/app/controllers/%controller_file_name%.rb
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
<% counter = 0 -%>
|
2
|
-
<% controller_modules.each_with_index do |mod, i| -%>
|
3
|
-
<%= " " * i %>module <%= mod %>
|
4
|
-
<% counter = i -%>
|
5
|
-
<% end -%>
|
6
|
-
<% counter = counter == 0 ? 0 : (counter + 1) -%>
|
7
|
-
<%= " " * counter %>class <%= controller_class_name %> < Application
|
8
|
-
<%= " " * counter %> # provides :xml, :yaml, :js
|
9
|
-
|
10
|
-
<%= " " * counter %> def index
|
11
|
-
<%= " " * counter %> @<%= plural_model %> = <%= model_class_name %>.all
|
12
|
-
<%= " " * counter %> display @<%= plural_model %>
|
13
|
-
<%= " " * counter %> end
|
14
|
-
|
15
|
-
<%= " " * counter %> def show
|
16
|
-
<%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
17
|
-
<%= " " * counter %> raise NotFound unless @<%= singular_model %>
|
18
|
-
<%= " " * counter %> display @<%= singular_model %>
|
19
|
-
<%= " " * counter %> end
|
20
|
-
|
21
|
-
<%= " " * counter %> def new
|
22
|
-
<%= " " * counter %> only_provides :html
|
23
|
-
<%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.new
|
24
|
-
<%= " " * counter %> render
|
25
|
-
<%= " " * counter %> end
|
26
|
-
|
27
|
-
<%= " " * counter %> def edit
|
28
|
-
<%= " " * counter %> only_provides :html
|
29
|
-
<%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
30
|
-
<%= " " * counter %> raise NotFound unless @<%= singular_model %>
|
31
|
-
<%= " " * counter %> render
|
32
|
-
<%= " " * counter %> end
|
33
|
-
|
34
|
-
<%= " " * counter %> def create
|
35
|
-
<%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.new(params[:<%= singular_model %>])
|
36
|
-
<%= " " * counter %> if @<%= singular_model %>.save
|
37
|
-
<%= " " * counter %> redirect url(:<%= (controller_modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
38
|
-
<%= " " * counter %> else
|
39
|
-
<%= " " * counter %> render :new
|
40
|
-
<%= " " * counter %> end
|
41
|
-
<%= " " * counter %> end
|
42
|
-
|
43
|
-
<%= " " * counter %> def update
|
44
|
-
<%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
45
|
-
<%= " " * counter %> raise NotFound unless @<%= singular_model %>
|
46
|
-
<%= " " * counter %> if @<%= singular_model %>.update_attributes(params[:<%= singular_model %>]) || !@<%= singular_model %>.dirty?
|
47
|
-
<%= " " * counter %> redirect url(:<%= (controller_modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
|
48
|
-
<%= " " * counter %> else
|
49
|
-
<%= " " * counter %> raise BadRequest
|
50
|
-
<%= " " * counter %> end
|
51
|
-
<%= " " * counter %> end
|
52
|
-
|
53
|
-
<%= " " * counter %> def destroy
|
54
|
-
<%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
|
55
|
-
<%= " " * counter %> raise NotFound unless @<%= singular_model %>
|
56
|
-
<%= " " * counter %> if @<%= singular_model %>.destroy
|
57
|
-
<%= " " * counter %> redirect url(:<%= (controller_modules.collect{|m| m.downcase} << singular_model).join("_") %>)
|
58
|
-
<%= " " * counter %> else
|
59
|
-
<%= " " * counter %> raise BadRequest
|
60
|
-
<%= " " * counter %> end
|
61
|
-
<%= " " * counter %> end
|
62
|
-
|
63
|
-
<%= " " * counter %>end
|
64
|
-
<% counter = counter == 0 ? 0 : (counter - 1) -%>
|
65
|
-
<% controller_modules.reverse.each_with_index do |mod, i| -%>
|
66
|
-
<%= " " * counter %>end # <%= mod %>
|
67
|
-
<% counter = counter - 1 -%>
|
68
|
-
<% end -%>
|
@@ -1,16 +0,0 @@
|
|
1
|
-
<% counter = 1 -%>
|
2
|
-
module Merb
|
3
|
-
<% controller_modules.each_with_index do |mod, i| -%>
|
4
|
-
<%= " " * i %>module <%= mod %>
|
5
|
-
<% counter = i -%>
|
6
|
-
<% end -%>
|
7
|
-
<% counter = counter == 0 ? 0 : (counter + 1) -%>
|
8
|
-
<%= " " * counter %>module <%= controller_class_name %>Helper
|
9
|
-
|
10
|
-
<%= " " * counter %>end
|
11
|
-
<% counter = counter == 0 ? 0 : (counter - 1) -%>
|
12
|
-
<% controller_modules.reverse.each_with_index do |mod, i| -%>
|
13
|
-
<%= " " * counter %>end # <%= mod %>
|
14
|
-
<% counter = counter - 1 -%>
|
15
|
-
<% end -%>
|
16
|
-
end
|
@@ -1,122 +0,0 @@
|
|
1
|
-
# require 'merb'
|
2
|
-
class ResourceMigrationGenerator < RubiGen::Base
|
3
|
-
|
4
|
-
# these define datamapper specific properties, not general database concepts
|
5
|
-
UNWANTED_PROPERTIES = [:public, :protected, :private, :accessor,
|
6
|
-
:reader, :writer, :lazy, :lock, :field, :ordinal,
|
7
|
-
:track, :auto_validation, :validates]
|
8
|
-
|
9
|
-
default_options :author => nil
|
10
|
-
|
11
|
-
attr_reader :name, :klass
|
12
|
-
|
13
|
-
def initialize(runtime_args, runtime_options = {})
|
14
|
-
super
|
15
|
-
usage if args.empty?
|
16
|
-
@name = args.shift
|
17
|
-
extract_options
|
18
|
-
end
|
19
|
-
|
20
|
-
def manifest
|
21
|
-
unless @name
|
22
|
-
puts banner
|
23
|
-
exit 1
|
24
|
-
end
|
25
|
-
|
26
|
-
# first see if we can find our model file where it should be!
|
27
|
-
model_file = File.join(Dir.pwd, "app/models","#{@name.snake_case}.rb")
|
28
|
-
unless File.exist?(model_file)
|
29
|
-
puts "Couldn't find the model file for #{@name}"
|
30
|
-
exit 1
|
31
|
-
end
|
32
|
-
|
33
|
-
# next, check we actually have a class!
|
34
|
-
require model_file
|
35
|
-
@klass = Module.const_get(@name.camel_case)
|
36
|
-
if @klass.nil?
|
37
|
-
puts "File didn't seem to include the model #{@name.camel_case}"
|
38
|
-
exit 1
|
39
|
-
end
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
record do |m|
|
45
|
-
# Ensure appropriate folders exists
|
46
|
-
m.directory 'schema/migrations'
|
47
|
-
|
48
|
-
@migration_name = "create_%s_table" % @name.snake_case.pluralize
|
49
|
-
|
50
|
-
filename = format("%03d_%s", (highest_migration+1), @migration_name)
|
51
|
-
|
52
|
-
m.template "class_migration.erb", "schema/migrations/#{filename}.rb",
|
53
|
-
:assigns => {
|
54
|
-
:migration_name => @migration_name ,
|
55
|
-
:number => (highest_migration+1),
|
56
|
-
:klass_name => klass.storage_name,
|
57
|
-
:properties => properties_as_strings
|
58
|
-
}
|
59
|
-
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
protected
|
64
|
-
def banner
|
65
|
-
<<-EOS
|
66
|
-
Creates a new migration for merb using DataMapper
|
67
|
-
|
68
|
-
USAGE: #{$0} #{spec.name} ResourceClass
|
69
|
-
|
70
|
-
Example:
|
71
|
-
#{$0} #{spec.name} Post
|
72
|
-
|
73
|
-
If you already have 3 migrations, this will create the Post migration in
|
74
|
-
schema/migration/004_create_posts_table.rb
|
75
|
-
|
76
|
-
NB: Currently the generator doesn't make any columns for 'belongs_to'-type
|
77
|
-
associations.
|
78
|
-
EOS
|
79
|
-
end
|
80
|
-
|
81
|
-
def add_options!(opts)
|
82
|
-
# opts.separator ''
|
83
|
-
# opts.separator 'Options:'
|
84
|
-
# For each option below, place the default
|
85
|
-
# at the top of the file next to "default_options"
|
86
|
-
# opts.on("-a", "--author=\"Your Name\"", String,
|
87
|
-
# "Some comment about this option",
|
88
|
-
# "Default: none") { |options[:author]| }
|
89
|
-
# opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
|
90
|
-
end
|
91
|
-
|
92
|
-
def extract_options
|
93
|
-
# for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
|
94
|
-
# Templates can access these value via the attr_reader-generated methods, but not the
|
95
|
-
# raw instance variable value.
|
96
|
-
# @author = options[:author]
|
97
|
-
end
|
98
|
-
|
99
|
-
def highest_migration
|
100
|
-
@highest_migration ||= Dir[Dir.pwd+'/schema/migrations/*'].map{ |f|
|
101
|
-
File.basename(f) =~ /^(\d+)/
|
102
|
-
$1}.max.to_i
|
103
|
-
end
|
104
|
-
|
105
|
-
def property_as_string(p)
|
106
|
-
name = p.field
|
107
|
-
type = p.type.to_s
|
108
|
-
# clear out non-db related properties
|
109
|
-
options = p.options.reject { |key, value| UNWANTED_PROPERTIES.include? key }
|
110
|
-
options_as_array = []
|
111
|
-
options.each do |key, value|
|
112
|
-
options_as_array << ":#{key} => #{value}"
|
113
|
-
end
|
114
|
-
|
115
|
-
(options_as_array.empty?) ? ":#{name}, #{type}" :
|
116
|
-
":#{name}, #{type}, #{options_as_array.join(', ')}"
|
117
|
-
end
|
118
|
-
|
119
|
-
def properties_as_strings
|
120
|
-
@properties_as_strings ||= klass.properties.map {|p| property_as_string(p) }
|
121
|
-
end
|
122
|
-
end
|