From ff48e23a4eb9f4a5fd3a02addb398499fb3c1455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 27 Jun 2009 11:05:43 +0200 Subject: Make Observer generator agnostic. --- railties/lib/generators/active_record.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 railties/lib/generators/active_record.rb (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb new file mode 100644 index 0000000000..be5d033868 --- /dev/null +++ b/railties/lib/generators/active_record.rb @@ -0,0 +1,8 @@ +require 'generators/named_base' + +module ActiveRecord + module Generators + class Base < Rails::Generators::NamedBase + end + end +end -- cgit v1.2.3 From 496dde95fbedddad5d04afbe660ff1544229032c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 28 Jun 2009 11:56:44 +0200 Subject: Added migrations and make base generators be lazy loaded. --- railties/lib/generators/active_record.rb | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index be5d033868..2c4c3286d4 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -1,8 +1,58 @@ require 'generators/named_base' +require 'active_record' module ActiveRecord module Generators + module Migration + + # Creates a migration template at the given destination. The difference + # to the default template method is that the migration number is appended + # to the destination file name. + # + # The migration number, migration file name, migration class name are + # available as instance variables in the template to be rendered. + # + # ==== Examples + # + # migration_template "migrate.rb", "db/migrate/add_foo_to_bar" + # + def migration_template(source, destination=nil, log_status=true) + destination = File.expand_path(destination || source, self.destination_root) + + migration_dir = File.dirname(destination) + @migration_number = next_migration_number(migration_dir) + @migration_file_name = File.basename(destination).sub(/\.rb$/, '') + @migration_class_name = @migration_file_name.camelize + + if existing = migration_exists?(migration_dir, @migration_file_name) + raise Rails::Generators::Error, "Another migration is already named #{@migration_file_name}: #{existing}" + end + + destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb") + template(source, destination, log_status) + end + + protected + + def migration_exists?(dirname, file_name) #:nodoc: + Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first + end + + def current_migration_number(dirname) #:nodoc: + Dir.glob("#{dirname}/[0-9]*_*.rb").collect{ |f| f.split("_").first.to_i }.max + end + + def next_migration_number(dirname) #:nodoc: + if ActiveRecord::Base.timestamped_migrations + Time.now.utc.strftime("%Y%m%d%H%M%S") + else + "%.3d" % (current_migration_number(dirname) + 1) + end + end + end + class Base < Rails::Generators::NamedBase + include Migration end end end -- cgit v1.2.3 From 1ca51df0001a750fafb0da70e4560c1ec439236c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 28 Jun 2009 13:16:14 +0200 Subject: Added migration and session_migration generator. --- railties/lib/generators/active_record.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 2c4c3286d4..4e585a11e7 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -14,7 +14,7 @@ module ActiveRecord # # ==== Examples # - # migration_template "migrate.rb", "db/migrate/add_foo_to_bar" + # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" # def migration_template(source, destination=nil, log_status=true) destination = File.expand_path(destination || source, self.destination_root) -- cgit v1.2.3 From 10a9205c583a95ef35704aca93079b1700dd35f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 1 Jul 2009 11:50:06 +0200 Subject: Put migration methods into a separate folder and let just ActiveRecord dependency in ActiveRecord models. --- railties/lib/generators/active_record.rb | 44 ++++---------------------------- 1 file changed, 5 insertions(+), 39 deletions(-) (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 4e585a11e7..70f2421ac2 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -1,47 +1,16 @@ require 'generators/named_base' +require 'generators/migration' require 'active_record' module ActiveRecord module Generators - module Migration - - # Creates a migration template at the given destination. The difference - # to the default template method is that the migration number is appended - # to the destination file name. - # - # The migration number, migration file name, migration class name are - # available as instance variables in the template to be rendered. - # - # ==== Examples - # - # migration_template "migration.rb", "db/migrate/add_foo_to_bar.rb" - # - def migration_template(source, destination=nil, log_status=true) - destination = File.expand_path(destination || source, self.destination_root) - - migration_dir = File.dirname(destination) - @migration_number = next_migration_number(migration_dir) - @migration_file_name = File.basename(destination).sub(/\.rb$/, '') - @migration_class_name = @migration_file_name.camelize - - if existing = migration_exists?(migration_dir, @migration_file_name) - raise Rails::Generators::Error, "Another migration is already named #{@migration_file_name}: #{existing}" - end - - destination = File.join(migration_dir, "#{@migration_number}_#{@migration_file_name}.rb") - template(source, destination, log_status) - end + class Base < Rails::Generators::NamedBase + include Rails::Generators::Migration protected - def migration_exists?(dirname, file_name) #:nodoc: - Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first - end - - def current_migration_number(dirname) #:nodoc: - Dir.glob("#{dirname}/[0-9]*_*.rb").collect{ |f| f.split("_").first.to_i }.max - end - + # Implement the required interface for Rails::Generators::Migration. + # def next_migration_number(dirname) #:nodoc: if ActiveRecord::Base.timestamped_migrations Time.now.utc.strftime("%Y%m%d%H%M%S") @@ -49,10 +18,7 @@ module ActiveRecord "%.3d" % (current_migration_number(dirname) + 1) end end - end - class Base < Rails::Generators::NamedBase - include Migration end end end -- cgit v1.2.3 From bf672a12a3726a74a436746f4a82fefd9527686c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 1 Jul 2009 12:38:08 +0200 Subject: Added Rails::Generators::ActionORM to hold ORM methods necessary to build a generator. --- railties/lib/generators/active_record.rb | 36 ++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 70f2421ac2..c7316ed38b 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -4,11 +4,10 @@ require 'active_record' module ActiveRecord module Generators - class Base < Rails::Generators::NamedBase + class Base < Rails::Generators::NamedBase #:nodoc: include Rails::Generators::Migration protected - # Implement the required interface for Rails::Generators::Migration. # def next_migration_number(dirname) #:nodoc: @@ -18,7 +17,40 @@ module ActiveRecord "%.3d" % (current_migration_number(dirname) + 1) end end + end + + class ActionORM < Rails::Generators::ActionORM #:nodoc: + def self.all(klass) + "#{klass}.all" + end + + def self.find(klass, params) + "#{klass}.find(#{params})" + end + + def self.build(klass, params=nil) + if params + "#{klass}.new(#{params})" + else + "#{klass}.new" + end + end + + def save + "#{name}.save" + end + + def update_attributes(params) + "#{name}.update_attributes(#{params})" + end + + def errors + "#{name}.errors" + end + def destroy + "#{name}.destroy" + end end end end -- cgit v1.2.3 From 9541977e049d236564f34cf58660b61e154ccb32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Wed, 1 Jul 2009 13:30:07 +0200 Subject: Make scaffold controller generate agnostic code and mark some hooks as required. --- railties/lib/generators/active_record.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index c7316ed38b..6640988c40 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -1,5 +1,6 @@ require 'generators/named_base' require 'generators/migration' +require 'generators/action_orm' require 'active_record' module ActiveRecord -- cgit v1.2.3 From ae7a7852d56bd99304affa220dd4d91a2fff16a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Mon, 13 Jul 2009 15:08:45 +0200 Subject: Always allow params to be skipped on action orm in generators. --- railties/lib/generators/active_record.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'railties/lib/generators/active_record.rb') diff --git a/railties/lib/generators/active_record.rb b/railties/lib/generators/active_record.rb index 6640988c40..64bee3904e 100644 --- a/railties/lib/generators/active_record.rb +++ b/railties/lib/generators/active_record.rb @@ -25,7 +25,7 @@ module ActiveRecord "#{klass}.all" end - def self.find(klass, params) + def self.find(klass, params=nil) "#{klass}.find(#{params})" end @@ -41,7 +41,7 @@ module ActiveRecord "#{name}.save" end - def update_attributes(params) + def update_attributes(params=nil) "#{name}.update_attributes(#{params})" end -- cgit v1.2.3