From 45a45b9129204dafdd7767c78f2c75f86f129696 Mon Sep 17 00:00:00 2001 From: Diego Carrion Date: Mon, 31 Aug 2009 19:26:15 -0300 Subject: [PATCH] added a default generator for active model to be used in scaffolds, so libraries that implement active model dont worry about how the controller calls the model --- railties/lib/generators/active_model.rb | 34 +++++++++++++++++++++++++++ railties/lib/generators/resource_helpers.rb | 12 +++++---- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/railties/lib/generators/active_model.rb b/railties/lib/generators/active_model.rb index 1a849a0..bd67f24 100644 --- a/railties/lib/generators/active_model.rb +++ b/railties/lib/generators/active_model.rb @@ -70,5 +70,39 @@ module Rails raise NotImplementedError end end + + class ActiveModelDefault < ActiveModel + def self.all(klass) + "#{klass}.all" + end + + def self.find(klass, params=nil) + "#{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=nil) + "#{name}.update(#{params})" + end + + def errors + "#{name}.errors" + end + + def destroy + "#{name}.destroy" + end + end end end diff --git a/railties/lib/generators/resource_helpers.rb b/railties/lib/generators/resource_helpers.rb index ba14446..51cefed 100644 --- a/railties/lib/generators/resource_helpers.rb +++ b/railties/lib/generators/resource_helpers.rb @@ -55,12 +55,14 @@ module Rails klass = active_model.constantize rescue NameError require "generators/#{options[:orm]}" - end - # Try once again after loading the file with success. - klass ||= active_model.constantize - rescue Exception => e - raise Error, "Could not load #{active_model}, skipping controller. Error: #{e.message}." + begin + # Try once again after loading the file with success. + klass = active_model.constantize + rescue Exception + klass = Rails::Generators::ActiveModelDefault + end + end end end -- 1.6.0.4