merb_datamapper 0.9.2 → 0.9.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. data/History.txt +1 -0
  2. data/Manifest.txt +33 -0
  3. data/README.txt +13 -0
  4. data/Rakefile +47 -35
  5. data/TODO +10 -4
  6. data/datamapper_generators/migration/USAGE +2 -3
  7. data/datamapper_generators/migration/migration_generator.rb +6 -6
  8. data/datamapper_generators/migration/templates/new_migration.erb +3 -3
  9. data/datamapper_generators/model/model_generator.rb +17 -10
  10. data/datamapper_generators/model/templates/app/models/%model_file_name%.rb +7 -5
  11. data/datamapper_generators/resource_controller/resource_controller_generator.rb +62 -27
  12. data/datamapper_generators/resource_controller/templates/app/controllers/%controller_file_name%.rb +6 -6
  13. data/datamapper_generators/resource_controller/templates/app/helpers/%controller_file_name%_helper.rb +1 -1
  14. data/datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/edit.html.erb +19 -1
  15. data/datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/index.html.erb +22 -1
  16. data/datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/new.html.erb +18 -1
  17. data/datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/show.html.erb +12 -1
  18. data/datamapper_generators/resource_migration/USAGE +4 -0
  19. data/datamapper_generators/resource_migration/resource_migration_generator.rb +122 -0
  20. data/datamapper_generators/resource_migration/templates/class_migration.erb +13 -0
  21. data/lib/merb/orms/data_mapper/connection.rb +61 -26
  22. data/lib/merb/orms/data_mapper/database.yml.sample +32 -12
  23. data/lib/merb/orms/data_mapper/resource.rb +12 -0
  24. data/lib/merb/session/data_mapper_session.rb +47 -40
  25. data/lib/merb_datamapper.rb +15 -11
  26. data/lib/merb_datamapper/merbtasks.rb +42 -13
  27. data/lib/merb_datamapper/version.rb +5 -0
  28. data/spec/connection_spec.rb +67 -0
  29. data/spec/spec.opts +2 -0
  30. data/spec/spec_helper.rb +15 -0
  31. metadata +58 -45
  32. data/README +0 -10
  33. data/lib/merb/orms/data_mapper/base.rb +0 -6
  34. data/specs/merb_sequel_spec.rb +0 -0
  35. data/specs/spec_helper.rb +0 -2
data/History.txt ADDED
@@ -0,0 +1 @@
1
+
data/Manifest.txt ADDED
@@ -0,0 +1,33 @@
1
+ History.txt
2
+ LICENSE
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ datamapper_generators/migration/USAGE
8
+ datamapper_generators/migration/migration_generator.rb
9
+ datamapper_generators/migration/templates/new_migration.erb
10
+ datamapper_generators/model/USAGE
11
+ datamapper_generators/model/model_generator.rb
12
+ datamapper_generators/model/templates/app/models/%model_file_name%.rb
13
+ datamapper_generators/resource_controller/USAGE
14
+ datamapper_generators/resource_controller/resource_controller_generator.rb
15
+ datamapper_generators/resource_controller/templates/app/controllers/%controller_file_name%.rb
16
+ datamapper_generators/resource_controller/templates/app/helpers/%controller_file_name%_helper.rb
17
+ datamapper_generators/resource_controller/templates/app/views/%controller_file_name%/edit.html.erb
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
24
+ lib/merb/orms/data_mapper/connection.rb
25
+ lib/merb/orms/data_mapper/database.yml.sample
26
+ lib/merb/orms/data_mapper/resource.rb
27
+ lib/merb/session/data_mapper_session.rb
28
+ lib/merb_datamapper.rb
29
+ lib/merb_datamapper/merbtasks.rb
30
+ lib/merb_datamapper/version.rb
31
+ spec/connection_spec.rb
32
+ spec/spec.opts
33
+ spec/spec_helper.rb
data/README.txt ADDED
@@ -0,0 +1,13 @@
1
+ = merb_datamapper
2
+
3
+ A plugin for the Merb framework that provides DataMapper access
4
+
5
+ To use sessions:
6
+
7
+ 1. set the session store to datamapper in init.rb:
8
+ Merb::Config.use { |c|
9
+ c[:session_store] = 'datamapper'
10
+ }
11
+
12
+ 2. add the dependency in init.rb:
13
+ use_orm :datamapper
data/Rakefile CHANGED
@@ -1,46 +1,58 @@
1
1
  require 'rubygems'
2
- require 'rake/gempackagetask'
2
+ require 'spec'
3
+ require 'spec/rake/spectask'
4
+ require 'pathname'
5
+
6
+ ROOT = Pathname(__FILE__).dirname.expand_path
7
+ require ROOT + 'lib/merb_datamapper/version'
3
8
 
4
- PLUGIN = "merb_datamapper"
5
- NAME = "merb_datamapper"
6
- VERSION = "0.9.2"
7
9
  AUTHOR = "Jason Toy"
8
- EMAIL = "jtoy@rubynow.com"
9
- HOMEPAGE = "http://merbivore.com"
10
- SUMMARY = "Merb plugin that provides DataMapper support for Merb"
11
-
12
- spec = Gem::Specification.new do |s|
13
- s.name = NAME
14
- s.version = VERSION
15
- s.platform = Gem::Platform::RUBY
16
- s.has_rdoc = true
17
- s.extra_rdoc_files = ["README", "LICENSE", 'TODO']
18
- s.summary = SUMMARY
19
- s.description = s.summary
20
- s.author = AUTHOR
21
- s.email = EMAIL
22
- s.homepage = HOMEPAGE
23
- s.add_dependency('merb-core', '>= 0.9.2')
24
- s.add_dependency('datamapper', '>= 0.2.3')
25
- s.require_path = 'lib'
26
- s.autorequire = PLUGIN
27
- s.files = %w(LICENSE README Rakefile TODO) + Dir.glob("{lib,specs,datamapper_generators}/**/*")
28
- end
10
+ EMAIL = "jtoy@rubynow.com"
11
+ GEM_NAME = "merb_datamapper"
12
+ GEM_VERSION = DataMapper::MerbDataMapper::VERSION
13
+ GEM_DEPENDENCIES = [["dm-core", GEM_VERSION], ["dm-migrations", GEM_VERSION], ["merb-core", ">=0.9.3"]]
14
+ GEM_CLEAN = ["log", "pkg", "coverage"]
15
+ GEM_EXTRAS = { :has_rdoc => true, :extra_rdoc_files => %w[ README.txt LICENSE TODO ] }
16
+
17
+ PROJECT_NAME = "datamapper"
18
+ PROJECT_URL = "http://github.com/sam/dm-more/tree/master/merb_datamapper"
19
+ PROJECT_DESCRIPTION = PROJECT_SUMMARY = "DataMapper plugin providing DataMapper support for Merb"
20
+
21
+ require ROOT.parent + 'tasks/hoe'
29
22
 
30
- windows = (PLATFORM =~ /win32|cygwin/) rescue nil
23
+ task :default => [ :spec ]
31
24
 
32
- SUDO = windows ? "" : "sudo"
25
+ WIN32 = (RUBY_PLATFORM =~ /win32|mingw|cygwin/) rescue nil
26
+ SUDO = WIN32 ? '' : ('sudo' unless ENV['SUDOLESS'])
33
27
 
34
- Rake::GemPackageTask.new(spec) do |pkg|
35
- pkg.gem_spec = spec
28
+ desc "Install #{GEM_NAME} #{GEM_VERSION} (default ruby)"
29
+ task :install => [ :package ] do
30
+ sh "#{SUDO} gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources", :verbose => false
36
31
  end
37
32
 
38
- desc "Install merb_datamapper"
39
- task :install => :package do
40
- sh %{#{SUDO} gem install pkg/#{NAME}-#{VERSION} --no-rdoc --no-ri --no-update-sources}
33
+ desc "Uninstall #{GEM_NAME} #{GEM_VERSION} (default ruby)"
34
+ task :uninstall => [ :clobber ] do
35
+ sh "#{SUDO} gem uninstall #{GEM_NAME} -v#{GEM_VERSION} -I -x", :verbose => false
41
36
  end
42
37
 
43
- desc 'Release the current version on rubyforge'
44
- task :release => :package do
45
- sh %{rubyforge add_release merb #{PLUGIN} #{VERSION} pkg/#{NAME}-#{VERSION}.gem}
38
+ namespace :jruby do
39
+ desc "Install #{GEM_NAME} #{GEM_VERSION} with JRuby"
40
+ task :install => [ :package ] do
41
+ sh %{#{SUDO} jruby -S gem install --local pkg/#{GEM_NAME}-#{GEM_VERSION} --no-update-sources}, :verbose => false
42
+ end
43
+ end
44
+
45
+ desc 'Run specifications'
46
+ Spec::Rake::SpecTask.new(:spec) do |t|
47
+ t.spec_opts << '--options' << 'spec/spec.opts' if File.exists?('spec/spec.opts')
48
+ t.spec_files = Pathname.glob(Pathname.new(__FILE__).dirname + 'spec/**/*_spec.rb')
49
+
50
+ begin
51
+ t.rcov = ENV.has_key?('NO_RCOV') ? ENV['NO_RCOV'] != 'true' : true
52
+ t.rcov_opts << '--exclude' << 'spec'
53
+ t.rcov_opts << '--text-summary'
54
+ t.rcov_opts << '--sort' << 'coverage' << '--sort-reverse'
55
+ rescue Exception
56
+ # rcov not installed
57
+ end
46
58
  end
data/TODO CHANGED
@@ -1,4 +1,10 @@
1
- TODO:
2
- Add proper migration support as soon as its released
3
- Fix session support
4
- finish rake tasks
1
+ TODO
2
+ ====
3
+
4
+ * Add proper migration support as soon as its released
5
+ * Fix session support
6
+ * Finish rake tasks
7
+
8
+ ---
9
+ TODO tickets may also be found in the DataMapper Issue Tracker:
10
+ http://wm.lighthouseapp.com/projects/4819-datamapper/overview
@@ -1,5 +1,4 @@
1
1
  Description:
2
-
3
-
2
+
3
+
4
4
  Usage:
5
-
@@ -1,10 +1,10 @@
1
1
  # require 'merb'
2
2
  class MigrationGenerator < RubiGen::Base
3
-
3
+
4
4
  default_options :author => nil
5
-
5
+
6
6
  attr_reader :name
7
-
7
+
8
8
  def initialize(runtime_args, runtime_options = {})
9
9
  super
10
10
  usage if args.empty?
@@ -24,7 +24,7 @@ class MigrationGenerator < RubiGen::Base
24
24
  # Create stubs
25
25
  highest_migration = Dir[Dir.pwd+'/schema/migrations/*'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max
26
26
  filename = format("%03d_%s", (highest_migration.to_i+1), @name.snake_case)
27
- m.template "new_migration.erb", "schema/migrations/#{filename}.rb", :assigns => { :class_name => @name }
27
+ m.template "new_migration.erb", "schema/migrations/#{filename}.rb", :assigns => { :class_name => @name , :number => (highest_migration.to_i+1) }
28
28
 
29
29
  end
30
30
  end
@@ -54,11 +54,11 @@ EOS
54
54
  # "Default: none") { |options[:author]| }
55
55
  # opts.on("-v", "--version", "Show the #{File.basename($0)} version number and quit.")
56
56
  end
57
-
57
+
58
58
  def extract_options
59
59
  # for each option, extract it into a local variable (and create an "attr_reader :author" at the top)
60
60
  # Templates can access these value via the attr_reader-generated methods, but not the
61
61
  # raw instance variable value.
62
62
  # @author = options[:author]
63
63
  end
64
- end
64
+ end
@@ -1,7 +1,7 @@
1
- class <%= class_name.snake_case.camel_case %> < DataMapper::Migration
2
- def self.up
1
+ migration(<%= number %>, :<%= class_name.snake_case %>) do
2
+ up do
3
3
  end
4
4
 
5
- def self.down
5
+ down do
6
6
  end
7
7
  end
@@ -1,31 +1,31 @@
1
1
  class ModelGenerator < Merb::GeneratorBase
2
2
  attr_reader :model_attributes, :model_class_name, :model_file_name
3
-
3
+
4
4
  def initialize(args, runtime_args = {})
5
5
  @base = File.dirname(__FILE__)
6
6
  super
7
7
  @model_file_name = args.shift.snake_case
8
8
  @model_class_name = @model_file_name.to_const_string
9
- @model_attributes = Hash[*(args.map{|a| a.split(":") }.flatten)]
9
+ @model_attributes = normalize_attributes(args)
10
10
  @model_file_name = "#{@model_class_name.snake_case}"
11
-
11
+
12
12
  end
13
-
13
+
14
14
  def manifest
15
15
  record do |m|
16
16
  @m = m
17
-
18
- @assigns = { :model_file_name => model_file_name,
17
+
18
+ @assigns = { :model_file_name => model_file_name,
19
19
  :model_attributes => model_attributes,
20
20
  :model_class_name => model_class_name
21
21
  }
22
22
  copy_dirs
23
23
  copy_files
24
-
24
+
25
25
  m.dependency "merb_model_test", [model_file_name], @assigns
26
26
  end
27
27
  end
28
-
28
+
29
29
  protected
30
30
  def banner
31
31
  <<-EOS.split("\n").map{|x| x.strip}.join("\n")
@@ -34,5 +34,12 @@ class ModelGenerator < Merb::GeneratorBase
34
34
  USAGE: #{spec.name}"
35
35
  EOS
36
36
  end
37
-
38
- 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
@@ -1,5 +1,7 @@
1
- class <%= model_class_name %> < DataMapper::Base
2
- <% model_attributes.each do |attr| -%>
3
- <%= "property :#{attr.first}, :#{attr.last}" %>
4
- <% end -%>
5
- end
1
+ class <%= model_class_name %>
2
+ include DataMapper::Resource
3
+
4
+ <% model_attributes.each do |name,type| -%>
5
+ property :<%=name-%>, <%=type%>
6
+ <% end %>
7
+ end
@@ -1,51 +1,62 @@
1
1
  class ResourceControllerGenerator < Merb::GeneratorBase
2
-
3
- attr_reader :controller_class_name,
4
- :controller_file_name,
2
+
3
+ attr_reader :controller_class_name,
4
+ :controller_file_name,
5
5
  :controller_base_path,
6
6
  :controller_modules,
7
7
  :model_class_name,
8
8
  :full_controller_const,
9
9
  :singular_model,
10
- :plural_model
11
-
10
+ :plural_model,
11
+ :properties
12
+
12
13
  def initialize(args, runtime_args = {})
13
- @base = File.dirname(__FILE__)
14
-
14
+ @base = File.dirname(__FILE__)
15
+
15
16
  super
17
+
16
18
  name = args.shift
17
- nfp = name.snake_case.gsub("::", "/")
18
- nfp = nfp.split("/")
19
+ nfp = name.snake_case.gsub("::", "/").split("/")
20
+
19
21
  @model_class_name = nfp.pop.singularize.to_const_string
20
22
  @model_class_name = runtime_args[:model_class_name] if runtime_args[:model_class_name]
21
- @singular_model = @model_class_name.snake_case
22
- @plural_model = @singular_model.pluralize
23
-
23
+ @singular_model = @model_class_name.snake_case
24
+ @plural_model = @singular_model.pluralize
25
+
24
26
  nfp << @plural_model
25
-
27
+
26
28
  @controller_file_name = nfp.join("/")
27
-
29
+
28
30
  # Need to setup the directories
29
31
  unless @controller_file_name == File.basename(@controller_file_name)
30
- @controller_base_path = controller_file_name.split("/")[0..-2].join("/")
32
+ @controller_base_path = controller_file_name.split("/")[0..-2].join("/")
31
33
  end
32
-
33
- @controller_modules = @controller_file_name.to_const_string.split("::")[0..-2]
34
- @controller_class_name = @controller_file_name.to_const_string.split("::").last
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
35
37
 
36
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
37
48
  end
38
-
49
+
39
50
  def manifest
40
51
  record do |m|
41
52
  @m = m
42
-
53
+
43
54
  # Create the controller directory
44
55
  m.directory File.join("app/controllers", controller_base_path) if controller_base_path
45
-
56
+
46
57
  # Create the helpers directory
47
58
  m.directory File.join("app/helpers", controller_base_path) if controller_base_path
48
-
59
+
49
60
  @assigns = {
50
61
  :controller_modules => controller_modules,
51
62
  :controller_class_name => controller_class_name,
@@ -54,15 +65,39 @@ class ResourceControllerGenerator < Merb::GeneratorBase
54
65
  :full_controller_const => full_controller_const,
55
66
  :model_class_name => model_class_name,
56
67
  :singular_model => singular_model,
57
- :plural_model => plural_model
58
- }
68
+ :plural_model => plural_model,
69
+ :properties => properties
70
+ }
59
71
  copy_dirs
60
72
  copy_files
61
-
73
+
62
74
  m.dependency "merb_resource_controller_test", [@controller_class_name], @assigns
63
75
  end
64
76
  end
65
-
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
+
66
101
  protected
67
102
  def banner
68
103
  <<-EOS.split("\n").map{|x| x.strip}.join("\n")
@@ -71,4 +106,4 @@ class ResourceControllerGenerator < Merb::GeneratorBase
71
106
  USAGE: #{spec.name} my_resource
72
107
  EOS
73
108
  end
74
- end
109
+ end
@@ -13,7 +13,7 @@
13
13
  <%= " " * counter %> end
14
14
 
15
15
  <%= " " * counter %> def show
16
- <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.first(params[:id])
16
+ <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
17
17
  <%= " " * counter %> raise NotFound unless @<%= singular_model %>
18
18
  <%= " " * counter %> display @<%= singular_model %>
19
19
  <%= " " * counter %> end
@@ -26,7 +26,7 @@
26
26
 
27
27
  <%= " " * counter %> def edit
28
28
  <%= " " * counter %> only_provides :html
29
- <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.first(params[:id])
29
+ <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
30
30
  <%= " " * counter %> raise NotFound unless @<%= singular_model %>
31
31
  <%= " " * counter %> render
32
32
  <%= " " * counter %> end
@@ -41,9 +41,9 @@
41
41
  <%= " " * counter %> end
42
42
 
43
43
  <%= " " * counter %> def update
44
- <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.first(params[:id])
44
+ <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
45
45
  <%= " " * counter %> raise NotFound unless @<%= singular_model %>
46
- <%= " " * counter %> if @<%= singular_model %>.update_attributes(params[:<%= singular_model %>])
46
+ <%= " " * counter %> if @<%= singular_model %>.update_attributes(params[:<%= singular_model %>]) || !@<%= singular_model %>.dirty?
47
47
  <%= " " * counter %> redirect url(:<%= (controller_modules.collect{|m| m.downcase} << singular_model).join("_") %>, @<%= singular_model %>)
48
48
  <%= " " * counter %> else
49
49
  <%= " " * counter %> raise BadRequest
@@ -51,9 +51,9 @@
51
51
  <%= " " * counter %> end
52
52
 
53
53
  <%= " " * counter %> def destroy
54
- <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.first(params[:id])
54
+ <%= " " * counter %> @<%= singular_model %> = <%= model_class_name %>.get(<%= params_for_get %>)
55
55
  <%= " " * counter %> raise NotFound unless @<%= singular_model %>
56
- <%= " " * counter %> if @<%= singular_model %>.destroy!
56
+ <%= " " * counter %> if @<%= singular_model %>.destroy
57
57
  <%= " " * counter %> redirect url(:<%= (controller_modules.collect{|m| m.downcase} << singular_model).join("_") %>)
58
58
  <%= " " * counter %> else
59
59
  <%= " " * counter %> raise BadRequest