padrino-gen 0.6.3 → 0.6.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. data/Rakefile +1 -1
  2. data/VERSION +1 -1
  3. data/bin/padrino-gen +2 -2
  4. data/lib/padrino-gen/generators/actions.rb +50 -30
  5. data/lib/padrino-gen/generators/app/Gemfile +1 -0
  6. data/lib/padrino-gen/generators/app/config/boot.rb +1 -1
  7. data/lib/padrino-gen/generators/app.rb +7 -6
  8. data/lib/padrino-gen/generators/components/actions.rb +55 -56
  9. data/lib/padrino-gen/generators/components/orms/activerecord_gen.rb +48 -31
  10. data/lib/padrino-gen/generators/components/orms/couchrest_gen.rb +7 -9
  11. data/lib/padrino-gen/generators/components/orms/datamapper_gen.rb +16 -12
  12. data/lib/padrino-gen/generators/components/orms/mongomapper_gen.rb +6 -20
  13. data/lib/padrino-gen/generators/components/orms/sequel_gen.rb +6 -9
  14. data/lib/padrino-gen/generators/components/renderers/haml_gen.rb +1 -2
  15. data/lib/padrino-gen/generators/components/scripts/jquery_gen.rb +2 -2
  16. data/lib/padrino-gen/generators/components/scripts/prototype_gen.rb +3 -3
  17. data/lib/padrino-gen/generators/components/scripts/rightjs_gen.rb +2 -4
  18. data/lib/padrino-gen/generators/components/tests/bacon_test_gen.rb +3 -3
  19. data/lib/padrino-gen/generators/components/tests/riot_test_gen.rb +3 -3
  20. data/lib/padrino-gen/generators/components/tests/rspec_test_gen.rb +3 -3
  21. data/lib/padrino-gen/generators/components/tests/shoulda_test_gen.rb +3 -3
  22. data/lib/padrino-gen/generators/components/tests/testspec_test_gen.rb +3 -3
  23. data/lib/padrino-gen/generators/controller.rb +9 -8
  24. data/lib/padrino-gen/generators/mailer.rb +6 -5
  25. data/lib/padrino-gen/generators/migration.rb +5 -5
  26. data/lib/padrino-gen/generators/model.rb +5 -5
  27. data/lib/padrino-gen/generators.rb +12 -8
  28. data/lib/padrino-gen/padrino-tasks/activerecord.rb +33 -32
  29. data/lib/padrino-gen.rb +3 -0
  30. data/padrino-gen.gemspec +5 -5
  31. data/test/helper.rb +1 -1
  32. data/test/test_app_generator.rb +38 -39
  33. data/test/test_controller_generator.rb +11 -11
  34. data/test/test_mailer_generator.rb +4 -4
  35. data/test/test_migration_generator.rb +21 -21
  36. data/test/test_model_generator.rb +29 -31
  37. metadata +3 -3
data/Rakefile CHANGED
@@ -62,4 +62,4 @@ Rake::RDocTask.new do |rdoc|
62
62
  rdoc.title = "padrino-gen #{version}"
63
63
  rdoc.rdoc_files.include('README*')
64
64
  rdoc.rdoc_files.include('lib/**/*.rb')
65
- end
65
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.6.3
1
+ 0.6.7
data/bin/padrino-gen CHANGED
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env ruby
2
2
  %w[rubygems thor].each { |gem| require gem }
3
- $LOAD_PATH.unshift File.dirname(__FILE__) + "/../lib"
3
+ $:.unshift File.dirname(__FILE__) + "/../lib"
4
4
 
5
5
  # We try to load the vendored padrino-core if exist
6
6
  if File.exist?(File.dirname(__FILE__) + "/../../padrino-core/lib")
7
- $LOAD_PATH.unshift File.dirname(__FILE__) + "/../../padrino-core/lib"
7
+ $:.unshift File.dirname(__FILE__) + "/../../padrino-core/lib"
8
8
  end
9
9
 
10
10
  require 'padrino-gen'
@@ -15,16 +15,35 @@ module Padrino
15
15
  self.class.send(:include, generator_module_for(choice, component))
16
16
  send("setup_#{component}") if respond_to?("setup_#{component}")
17
17
  end
18
-
18
+
19
+ # Returns the related module for a given component and option
20
+ # generator_module_for('rr', :mock)
21
+ def generator_module_for(choice, component)
22
+ "Padrino::Generators::Components::#{component.to_s.capitalize.pluralize}::#{choice.to_s.capitalize}Gen".constantize
23
+ end
24
+
19
25
  # Includes the component module for the given component and choice
20
26
  # Determines the choice using .components file
21
27
  # include_component_module_for(:mock)
22
28
  # include_component_module_for(:mock, 'rr')
23
- def include_component_module_for(component, root=nil, choice=nil)
24
- choice = fetch_component_choice(component, root) unless choice
29
+ def include_component_module_for(component, choice=nil)
30
+ choice = fetch_component_choice(component) unless choice
25
31
  self.class.send(:include, generator_module_for(choice, component))
26
32
  end
27
33
 
34
+
35
+ # Returns the component choice stored within the .component file of an application
36
+ # fetch_component_choice(:mock)
37
+ def fetch_component_choice(component)
38
+ retrieve_component_config(destination_root('.components'))[component]
39
+ end
40
+
41
+ # Loads the component config back into a hash
42
+ # i.e retrieve_component_config(...) => { :mock => 'rr', :test => 'riot', ... }
43
+ def retrieve_component_config(target)
44
+ YAML.load_file(target)
45
+ end
46
+
28
47
  # Prompts the user if necessary until a valid choice is returned for the component
29
48
  # resolve_valid_choice(:mock) => 'rr'
30
49
  def resolve_valid_choice(component)
@@ -43,12 +62,6 @@ module Padrino
43
62
  self.class.available_choices_for(component).include? choice.to_sym
44
63
  end
45
64
 
46
- # Returns the related module for a given component and option
47
- # generator_module_for('rr', :mock)
48
- def generator_module_for(choice, component)
49
- "Padrino::Generators::Components::#{component.to_s.capitalize.pluralize}::#{choice.to_s.capitalize}Gen".constantize
50
- end
51
-
52
65
  # Creates a component_config file at the destination containing all component options
53
66
  # Content is a yamlized version of a hash containing component name mapping to chosen value
54
67
  def store_component_config(destination)
@@ -59,38 +72,45 @@ module Padrino
59
72
  end
60
73
  end
61
74
 
62
- # Loads the component config back into a hash
63
- # i.e retrieve_component_config(...) => { :mock => 'rr', :test => 'riot', ... }
64
- def retrieve_component_config(target)
65
- YAML.load_file(target)
66
- end
67
-
68
- # Returns the component choice stored within the .component file of an application
69
- # fetch_component_choice(:mock)
70
- def fetch_component_choice(component, root=nil)
71
- comp_path = root ? File.join(root, '.components') : '.components'
72
- retrieve_component_config(comp_path)[component]
75
+ # Returns the root for this thor class (also aliased as destination root).
76
+ def destination_root(*paths)
77
+ File.join(@destination_stack.last, paths)
73
78
  end
74
-
79
+
75
80
  # Returns true if inside a Padrino application
76
- def in_app_root?(root=nil)
77
- root ? File.exist?(File.join(root, 'config/boot.rb')) : File.exist?('config/boot.rb')
81
+ def in_app_root?
82
+ File.exist?(destination_root('config/boot.rb'))
78
83
  end
79
-
84
+
80
85
  # Returns the app_name for the application at root
81
86
  def fetch_app_name(root=nil)
82
87
  app_path = root ? File.join(root, 'app/app.rb') : 'app/app.rb'
83
88
  @app_name ||= File.read(app_path).scan(/class\s(.*?)\s</).flatten[0]
84
89
  end
85
-
86
- # Constructs a path from the specified app root
87
- # app_root_path("app/mailers", "#{@mailer_basename}.rb")
88
- def app_root_path(*paths)
89
- settings = paths.extract_options!
90
- File.join(settings[:root] || options[:root] || '.', *paths)
90
+
91
+ # Adds all the specified gems into the Gemfile for bundler
92
+ # require_dependencies 'active_record'
93
+ # require_dependencies 'mocha', 'bacon', :only => :testing
94
+ def require_dependencies(*gem_names)
95
+ options = gem_names.extract_options!
96
+ gem_names.reverse.each { |lib| insert_into_gemfile(lib, options) }
97
+ end
98
+
99
+ # Inserts a required gem into the Gemfile to add the bundler dependency
100
+ # insert_into_gemfile(name)
101
+ # insert_into_gemfile(name, :only => :testing, :require_as => 'foo')
102
+ def insert_into_gemfile(name, options={})
103
+ after_pattern = options[:only] ? "#{options[:only].to_s.capitalize} requirements\n" : "Component requirements\n"
104
+ gem_options = options.slice(:only, :require_as).collect { |k, v| "#{k.inspect} => #{v.inspect}" }.join(", ")
105
+ include_text = "gem '#{name}'" << (gem_options.present? ? ", #{gem_options}" : "") << "\n"
106
+ options.merge!(:content => include_text, :after => after_pattern)
107
+ if behavior == :revoke || !File.read(destination_root('Gemfile')).include?(options[:content])
108
+ inject_into_file('Gemfile', options[:content], :after => options[:after])
109
+ end
91
110
  end
92
111
 
93
112
  module ClassMethods
113
+
94
114
  # Defines a class option to allow a component to be chosen and add to component type list
95
115
  # Also builds the available_choices hash of which component choices are supported
96
116
  # component_option :test, "Testing framework", :aliases => '-t', :choices => [:bacon, :shoulda]
@@ -1,5 +1,6 @@
1
1
  clear_sources
2
2
  source 'http://gemcutter.org'
3
+
3
4
  # Project requirements
4
5
  gem 'sinatra'
5
6
  gem 'padrino'
@@ -3,7 +3,7 @@ PADRINO_ROOT = File.dirname(__FILE__) + '/..' unless defined? PADRINO_ROOT
3
3
 
4
4
  # Try to load the vendorized padrino.
5
5
  Dir[PADRINO_ROOT + "/vendor/padrino/**/lib"].each do |gem|
6
- $LOAD_PATH.unshift gem
6
+ $:.unshift gem
7
7
  end
8
8
 
9
9
  require 'padrino'
@@ -9,7 +9,7 @@ module Padrino
9
9
 
10
10
  # Define the source template root
11
11
  def self.source_root; File.dirname(__FILE__); end
12
- def self.banner; "padrino-gen project [name] [path] [options]"; end
12
+ def self.banner; "padrino-gen project [name] [options]"; end
13
13
 
14
14
  # Include related modules
15
15
  include Thor::Actions
@@ -19,15 +19,16 @@ module Padrino
19
19
  desc "Description:\n\n\tpadrino-gen project generates a new Padrino project"
20
20
 
21
21
  argument :name, :desc => "The name of your padrino project"
22
- argument :path, :desc => "The path to create your padrino project", :default => "."
22
+
23
23
  class_option :run_bundler, :aliases => '-b', :default => false, :type => :boolean
24
+ class_option :root, :desc => "The root destination", :aliases => '-r', :default => ".", :type => :string
24
25
 
25
26
  # Definitions for the available customizable components
26
27
  component_option :orm, "database engine", :aliases => '-d', :choices => [:datamapper, :mongomapper, :activerecord, :sequel, :couchrest]
27
28
  component_option :test, "testing framework", :aliases => '-t', :choices => [:bacon, :shoulda, :rspec, :testspec, :riot]
28
29
  component_option :mock, "mocking library", :aliases => '-m', :choices => [:mocha, :rr]
29
30
  component_option :script, "javascript library", :aliases => '-s', :choices => [:jquery, :prototype, :rightjs]
30
- component_option :renderer, "template engine", :aliases => '-r', :choices => [:erb, :haml]
31
+ component_option :renderer, "template engine", :aliases => '-e', :choices => [:erb, :haml]
31
32
 
32
33
  # Show help if no argv given
33
34
  def self.start(given_args=ARGV, config={})
@@ -37,9 +38,9 @@ module Padrino
37
38
 
38
39
  # Copies over the Padrino base application App
39
40
  def setup_app
40
- self.destination_root = File.join(path, name)
41
- @class_name = name.classify
42
- directory("app/", self.destination_root)
41
+ @class_name = name.underscore.classify
42
+ self.destination_root = File.join(options[:root], name)
43
+ directory("app/", destination_root)
43
44
  store_component_config('.components')
44
45
  end
45
46
 
@@ -3,46 +3,31 @@ module Padrino
3
3
  module Components
4
4
  module Actions
5
5
  BASE_TEST_HELPER = (<<-TEST).gsub(/^ {8}/, '')
6
- RACK_ENV = 'test' unless defined?(RACK_ENV)
6
+ PADRINO_ENV = 'test' unless defined?(PADRINO_ENV)
7
7
  require File.dirname(__FILE__) + "/../config/boot"
8
- Bundler.require_env(:testing)
9
8
  TEST
10
9
 
11
- # Adds all the specified gems into the Gemfile for bundler
12
- # require_dependencies 'active_record'
13
- # require_dependencies 'mocha', 'bacon', :only => :testing
14
- def require_dependencies(*gem_names)
15
- options = gem_names.extract_options!
16
- gem_names.reverse.each { |lib| insert_into_gemfile(lib, options) }
17
- end
18
-
19
- # Inserts a required gem into the Gemfile to add the bundler dependency
20
- # insert_into_gemfile(name)
21
- # insert_into_gemfile(name, :only => :testing, :require_as => 'foo')
22
- def insert_into_gemfile(name, options={})
23
- after_pattern = options[:only] ? "#{options[:only].to_s.capitalize} requirements\n" : "Component requirements\n"
24
- gem_options = options.slice(:only, :require_as).collect { |k, v| "#{k.inspect} => #{v.inspect}" }.join(", ")
25
- include_text = "gem '#{name}'" << (gem_options.present? ? ", #{gem_options}" : "") << "\n"
26
- options.merge!(:content => include_text, :after => after_pattern)
27
- inject_into_file('Gemfile', options[:content], :after => options[:after])
28
- end
29
-
30
10
  # For orm database components
31
11
  # Generates the model migration file created when generating a new model
32
12
  # options => { :base => "....text...", :up => "..text...",
33
13
  # :down => "..text...", column_format => "t.column :#{field}, :#{kind}" }
34
14
  def output_model_migration(filename, name, columns, options={})
35
- model_name = name.to_s.pluralize
36
- field_tuples = fields.collect { |value| value.split(":") }
37
- field_tuples.collect! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
38
- column_declarations = field_tuples.collect(&options[:column_format]).join("\n ")
39
- contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, options[:up]).gsub(/!DOWN!\n/m, options[:down])
40
- contents = contents.gsub(/!NAME!/, model_name.camelize).gsub(/!TABLE!/, model_name.underscore)
41
- contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.camelize)
42
- current_migration_number = return_last_migration_number
43
- contents = contents.gsub(/!FIELDS!/, column_declarations).gsub(/!VERSION!/, (current_migration_number + 1).to_s)
44
- migration_filename = "#{format("%03d", current_migration_number+1)}_#{filename.underscore}.rb"
45
- create_file(app_root_path('db/migrate/', migration_filename), contents, :skip => true)
15
+ if behavior == :revoke
16
+ remove_migration(name)
17
+ else
18
+ return if migration_exist?(filename)
19
+ model_name = name.to_s.pluralize
20
+ field_tuples = fields.collect { |value| value.split(":") }
21
+ field_tuples.collect! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
22
+ column_declarations = field_tuples.collect(&options[:column_format]).join("\n ")
23
+ contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, options[:up]).gsub(/!DOWN!\n/m, options[:down])
24
+ contents = contents.gsub(/!NAME!/, model_name.camelize).gsub(/!TABLE!/, model_name.underscore)
25
+ contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.camelize)
26
+ current_migration_number = return_last_migration_number
27
+ contents = contents.gsub(/!FIELDS!/, column_declarations).gsub(/!VERSION!/, (current_migration_number + 1).to_s)
28
+ migration_filename = "#{format("%03d", current_migration_number+1)}_#{filename.underscore}.rb"
29
+ create_file(destination_root('db/migrate/', migration_filename), contents, :skip => true)
30
+ end
46
31
  end
47
32
 
48
33
  # For orm database components
@@ -51,33 +36,42 @@ module Padrino
51
36
  # :add => lambda { |field, kind| "add_column :#{table_name}, :#{field}, :#{kind}" },
52
37
  # :remove => lambda { |field, kind| "remove_column :#{table_name}, :#{field}" }
53
38
  def output_migration_file(filename, name, columns, options={})
54
- change_format = options[:change_format]
55
- migration_scan = filename.camelize.scan(/(Add|Remove)(?:.*?)(?:To|From)(.*?)$/).flatten
56
- direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any?
57
- tuples = direction ? columns.collect { |value| value.split(":") } : []
58
- tuples.collect! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
59
- add_columns = tuples.collect(&options[:add]).join("\n ")
60
- remove_columns = tuples.collect(&options[:remove]).join("\n ")
61
- forward_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, add_columns) if tuples.any?
62
- back_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, remove_columns) if tuples.any?
63
- contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, (direction == 'add' ? forward_text.to_s : back_text.to_s))
64
- contents.gsub!(/\s{4}!DOWN!\n/m, (direction == 'add' ? back_text.to_s : forward_text.to_s))
65
- contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.camelize)
66
- current_migration_number = return_last_migration_number
67
- contents.gsub!(/!VERSION!/, (current_migration_number + 1).to_s)
68
- migration_filename = "#{format("%03d", current_migration_number+1)}_#{filename.underscore}.rb"
69
- # migration_filename = "#{Time.now.strftime("%Y%m%d%H%M%S")}_#{filename.underscore}.rb"
70
- create_file(app_root_path('db/migrate/', migration_filename), contents)
39
+ if behavior == :revoke
40
+ remove_migration(name)
41
+ else
42
+ return if migration_exist?(filename)
43
+ change_format = options[:change_format]
44
+ migration_scan = filename.camelize.scan(/(Add|Remove)(?:.*?)(?:To|From)(.*?)$/).flatten
45
+ direction, table_name = migration_scan[0].downcase, migration_scan[1].downcase.pluralize if migration_scan.any?
46
+ tuples = direction ? columns.collect { |value| value.split(":") } : []
47
+ tuples.collect! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
48
+ add_columns = tuples.collect(&options[:add]).join("\n ")
49
+ remove_columns = tuples.collect(&options[:remove]).join("\n ")
50
+ forward_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, add_columns) if tuples.any?
51
+ back_text = change_format.gsub(/!TABLE!/, table_name).gsub(/!COLUMNS!/, remove_columns) if tuples.any?
52
+ contents = options[:base].dup.gsub(/\s{4}!UP!\n/m, (direction == 'add' ? forward_text.to_s : back_text.to_s))
53
+ contents.gsub!(/\s{4}!DOWN!\n/m, (direction == 'add' ? back_text.to_s : forward_text.to_s))
54
+ contents = contents.gsub(/!FILENAME!/, filename.underscore).gsub(/!FILECLASS!/, filename.camelize)
55
+ current_migration_number = return_last_migration_number
56
+ contents.gsub!(/!VERSION!/, (current_migration_number + 1).to_s)
57
+ migration_filename = "#{format("%03d", current_migration_number+1)}_#{filename.underscore}.rb"
58
+ create_file(destination_root('db/migrate/', migration_filename), contents, :skip => true)
59
+ end
71
60
  end
72
61
 
73
62
  # For migration files
74
63
  # returns the number of the latest(most current) migration file
75
64
  def return_last_migration_number
76
- Dir[app_root_path('db/migrate/*.rb')].map do |f|
65
+ Dir[destination_root('db/migrate/*.rb')].map do |f|
77
66
  File.basename(f).match(/^(\d+)/)[0].to_i
78
67
  end.max.to_i || 0
79
68
  end
80
69
 
70
+ # Return true if the migration already exist
71
+ def migration_exist?(filename)
72
+ Dir[destination_root("db/migrate/*_#{filename.underscore}.rb")].size > 0
73
+ end
74
+
81
75
  # For model destroy option
82
76
  # removes the initial migration file of model
83
77
  def remove_model_migration(name)
@@ -87,10 +81,15 @@ module Padrino
87
81
  # For the removal of migration files
88
82
  # removes the migration file based on the migration name
89
83
  def remove_migration(name)
90
- migration_path = Dir[app_root_path('db/migrate/*.rb')].select do |f|
91
- File.basename(f).match(/#{name.to_s.underscore}/)
92
- end.first
93
- remove_file migration_path if migration_path && File.exist?(migration_path)
84
+ migration_path = Dir[destination_root('db/migrate/*.rb')].find do |f|
85
+ File.basename(f) =~ /#{name.to_s.underscore}/
86
+ end
87
+ return unless migration_path
88
+ if behavior == :revoke # we need to reverse the operation for revoke
89
+ create_file migration_path
90
+ else
91
+ remove_file migration_path
92
+ end
94
93
  end
95
94
 
96
95
  # For testing components
@@ -109,8 +108,8 @@ module Padrino
109
108
  # => inject_into_file("test/test_config.rb", " include Mocha::API\n", :after => /class.*?\n/)
110
109
  def insert_mocking_include(library_name, options={})
111
110
  options.reverse_merge!(:indent => 2, :after => /class.*?\n/, :path => "test/test_config.rb")
112
- return unless File.exist?(File.join(self.destination_root, options[:path]))
113
- include_text = indent_spaces(options[:indent]) + "include #{library_name}\n"
111
+ return unless File.exist?(destination_root(options[:path]))
112
+ include_text = indent_spaces(2) + "include #{library_name}\n"
114
113
  inject_into_file(options[:path], include_text, :after => options[:after])
115
114
  end
116
115
 
@@ -6,31 +6,50 @@ module Padrino
6
6
  module ActiverecordGen
7
7
 
8
8
  AR = (<<-AR).gsub(/^ {10}/, '')
9
- module DatabaseSetup
10
- def self.registered(app)
11
- app.configure { ActiveRecord::Base.logger = logger }
12
- app.configure :development do
13
- ActiveRecord::Base.establish_connection(
14
- :adapter => 'sqlite3',
15
- :database => Padrino.root('db', "development.db")
16
- )
17
- end
18
-
19
- app.configure :production do
20
- ActiveRecord::Base.establish_connection(
21
- :adapter => 'sqlite3',
22
- :database => Padrino.root('db', "production.db")
23
- )
24
- end
25
-
26
- app.configure :test do
27
- ActiveRecord::Base.establish_connection(
28
- :adapter => 'sqlite3',
29
- :database => Padrino.root('db', "test.db")
30
- )
31
- end
32
- end
33
- end
9
+ ##
10
+ # You can use other adapters like:
11
+ #
12
+ # ActiveRecord::Base.configurations[:development] = {
13
+ # :adapter => "mysql",
14
+ # :host => "localhost",
15
+ # :username => "myuser",
16
+ # :password => "mypass",
17
+ # :database => "somedatabase"
18
+ # )
19
+ #
20
+ ActiveRecord::Base.configurations[:development] = {
21
+ :adapter => 'sqlite3',
22
+ :database => Padrino.root('db', "development.db")
23
+ }
24
+
25
+ ActiveRecord::Base.configurations[:production] = {
26
+ :adapter => 'sqlite3',
27
+ :database => Padrino.root('db', "production.db")
28
+ }
29
+
30
+ ActiveRecord::Base.configurations[:test] = {
31
+ :adapter => 'sqlite3',
32
+ :database => Padrino.root('db', "test.db")
33
+ }
34
+
35
+ # Setup our logger
36
+ ActiveRecord::Base.logger = logger
37
+
38
+ # Include Active Record class name as root for JSON serialized output.
39
+ ActiveRecord::Base.include_root_in_json = true
40
+
41
+ # Store the full class name (including module namespace) in STI type column.
42
+ ActiveRecord::Base.store_full_sti_class = true
43
+
44
+ # Use ISO 8601 format for JSON serialized times and dates.
45
+ ActiveSupport.use_standard_json_time_format = true
46
+
47
+ # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
48
+ # if you're including raw json in an HTML page.
49
+ ActiveSupport.escape_html_entities_in_json = false
50
+
51
+ # Now we can estabilish connection with our db
52
+ ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Padrino.env])
34
53
  AR
35
54
 
36
55
  RAKE = (<<-RAKE).gsub(/^ {10}/, '')
@@ -51,7 +70,7 @@ module Padrino
51
70
 
52
71
 
53
72
  def setup_orm
54
- require_dependencies 'activerecord'
73
+ require_dependencies 'active_record'
55
74
  create_file("config/database.rb", AR)
56
75
  create_file("Rakefile", RAKE.gsub(/APP_CLASS/, @class_name))
57
76
  empty_directory('app/models')
@@ -64,7 +83,7 @@ module Padrino
64
83
  MODEL
65
84
 
66
85
  def create_model_file(name, fields)
67
- model_path = app_root_path('app/models/', "#{name.to_s.underscore}.rb")
86
+ model_path = destination_root('app/models/', "#{name.to_s.underscore}.rb")
68
87
  model_contents = AR_MODEL.gsub(/!NAME!/, name.to_s.downcase.camelize)
69
88
  create_file(model_path, model_contents,:skip => true)
70
89
  end
@@ -83,8 +102,6 @@ module Padrino
83
102
 
84
103
  AR_MODEL_UP_MG = (<<-MIGRATION).gsub(/^ {6}/, '')
85
104
  create_table :!TABLE! do |t|
86
- # t.column <name>, <type>
87
- # t.column :age, :integer
88
105
  !FIELDS!
89
106
  end
90
107
  MIGRATION
@@ -96,7 +113,7 @@ module Padrino
96
113
  def create_model_migration(migration_name, name, columns)
97
114
  output_model_migration(migration_name, name, columns,
98
115
  :base => AR_MIGRATION,
99
- :column_format => lambda { |field, kind| "t.column :#{field}, :#{kind.underscore.gsub(/_/, '')}" },
116
+ :column_format => lambda { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
100
117
  :up => AR_MODEL_UP_MG, :down => AR_MODEL_DOWN_MG)
101
118
  end
102
119
 
@@ -109,7 +126,7 @@ module Padrino
109
126
  def create_migration_file(migration_name, name, columns)
110
127
  output_migration_file(migration_name, name, columns,
111
128
  :base => AR_MIGRATION, :change_format => AR_CHANGE_MG,
112
- :add => lambda { |field, kind| "t.column :#{field}, :#{kind.underscore.gsub(/_/, '')}" },
129
+ :add => lambda { |field, kind| "t.#{kind.underscore.gsub(/_/, '')} :#{field}" },
113
130
  :remove => lambda { |field, kind| "t.remove :#{field}" })
114
131
  end
115
132
 
@@ -6,13 +6,12 @@ module Padrino
6
6
  module CouchrestGen
7
7
 
8
8
  COUCHREST = (<<-COUCHREST).gsub(/^ {10}/, '')
9
- module DatabaseSetup
10
- def self.registered(app)
11
- app.configure(:development) { set :couchdb, CouchRest.database!("your_dev_db_here") }
12
- app.configure(:production) { set :couchdb, CouchRest.database!("your_production_db_here") }
13
- app.configure(:test) { set :couchdb, CouchRest.database!("your_test_db_here") }
14
- end
9
+ case Padrino.env
10
+ when :development then COUCHDB = "your_db_name_development"
11
+ when :production then COUCHDB = "your_db_name_production"
12
+ when :test then COUCHDB = "your_db_name_test"
15
13
  end
14
+ CouchRest.database!(COUCHDB)
16
15
  COUCHREST
17
16
 
18
17
  def setup_orm
@@ -25,7 +24,7 @@ module Padrino
25
24
  class !NAME! < CouchRest::ExtendedDocument
26
25
  include CouchRest::Validation
27
26
 
28
- use_database app { couchdb }
27
+ use_database COUCHDB
29
28
 
30
29
  unique_id :id
31
30
  # property <name>
@@ -34,8 +33,7 @@ module Padrino
34
33
  MODEL
35
34
 
36
35
  def create_model_file(name, fields)
37
- model_path = app_root_path('app/models/', "#{name.to_s.underscore}.rb")
38
- return false if File.exist?(model_path)
36
+ model_path = destination_root('app/models/', "#{name.to_s.underscore}.rb")
39
37
  field_tuples = fields.collect { |value| value.split(":") }
40
38
  column_declarations = field_tuples.collect { |field, kind| "property :#{field}" }.join("\n ")
41
39
  model_contents = CR_MODEL.gsub(/!NAME!/, name.to_s.camelize)
@@ -5,20 +5,25 @@ module Padrino
5
5
 
6
6
  module DatamapperGen
7
7
  DM = (<<-DM).gsub(/^ {10}/, '')
8
- module DatabaseSetup
9
- def self.registered(app)
10
- app.configure { DataMapper.logger = logger }
11
- app.configure(:development) { DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "development.db")) }
12
- app.configure(:production) { DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "production.db")) }
13
- app.configure(:test) { DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "test.db")) }
14
- rescue ArgumentError => e
15
- logger.error "Database options need to be configured within 'config/database.rb'!" if app.logging?
16
- end
8
+ ##
9
+ # A MySQL connection:
10
+ # DataMapper.setup(:default, 'mysql://user:password@localhost/the_database_name')
11
+ #
12
+ # # A Postgres connection:
13
+ # DataMapper.setup(:default, 'postgres://user:password@localhost/the_database_name')
14
+ #
15
+
16
+ DataMapper.logger = logger
17
+
18
+ case Padrino.env
19
+ when :development then DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "development.db"))
20
+ when :production then DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "production.db"))
21
+ when :test then DataMapper.setup(:default, "sqlite3://" + Padrino.root('db', "test.db"))
17
22
  end
18
23
  DM
19
24
 
20
25
  def setup_orm
21
- require_dependencies 'dm-core', 'dm-validations'
26
+ require_dependencies 'dm-core', 'dm-validations', 'dm-aggregates', 'dm-timestamps'
22
27
  create_file("config/database.rb", DM)
23
28
  empty_directory('app/models')
24
29
  end
@@ -34,8 +39,7 @@ module Padrino
34
39
  MODEL
35
40
 
36
41
  def create_model_file(name, fields)
37
- model_path = app_root_path('app/models/', "#{name.to_s.underscore}.rb")
38
- return false if File.exist?(model_path)
42
+ model_path = destination_root('app/models/', "#{name.to_s.underscore}.rb")
39
43
  model_contents = DM_MODEL.gsub(/!NAME!/, name.to_s.downcase.camelize)
40
44
  field_tuples = fields.collect { |value| value.split(":") }
41
45
  field_tuples.collect! { |field, kind| kind =~ /datetime/i ? [field, 'DateTime'] : [field, kind] } # fix datetime
@@ -6,25 +6,12 @@ module Padrino
6
6
  module MongomapperGen
7
7
 
8
8
  MONGO = (<<-MONGO).gsub(/^ {10}/, '')
9
- class MongoDBConnectionFailure < RuntimeError; end
9
+ MongoMapper.connection = Mongo::Connection.new('localhost', nil, :logger => logger)
10
10
 
11
- module DatabaseSetup
12
- def self.registered(app)
13
- app.configure :development do
14
- MongoMapper.connection = Mongo::Connection.new('localhost', nil, :logger => logger)
15
- MongoMapper.database = 'your_dev_db_here'
16
- end
17
-
18
- app.configure :production do
19
- MongoMapper.connection = Mongo::Connection.new('localhost', nil, :logger => logger)
20
- MongoMapper.database = 'your_production_db_here'
21
- end
22
-
23
- app.configure :test do
24
- MongoMapper.connection = Mongo::Connection.new('localhost', nil, :logger => logger)
25
- MongoMapper.database = 'your_test_db_here'
26
- end
27
- end
11
+ case Padrino.env
12
+ when :development then MongoMapper.database = 'your_db_development'
13
+ when :production then MongoMapper.database = 'your_db_production'
14
+ when :test then MongoMapper.database = 'your_db_test'
28
15
  end
29
16
  MONGO
30
17
 
@@ -44,8 +31,7 @@ module Padrino
44
31
  MODEL
45
32
 
46
33
  def create_model_file(name, fields)
47
- model_path = app_root_path('app/models/', "#{name.to_s.underscore}.rb")
48
- return false if File.exist?(model_path)
34
+ model_path = destination_root('app/models/', "#{name.to_s.underscore}.rb")
49
35
  field_tuples = fields.collect { |value| value.split(":") }
50
36
  column_declarations = field_tuples.collect { |field, kind| "key :#{field}, #{kind.camelize}" }.join("\n ")
51
37
  model_contents = MM_MODEL.gsub(/!NAME!/, name.to_s.camelize)