aboutsummaryrefslogtreecommitdiffstats
path: root/activemodel
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2009-07-20 16:57:23 -0300
committerEmilio Tagua <miloops@gmail.com>2009-07-20 16:57:23 -0300
commit9a28bd787660b08aae36155066e61d3608d0b4dd (patch)
tree2d10dd32ad28cc070c3d142c45d6a35fcd5ae43e /activemodel
parentb326faef0936e5a845d1f6eb9ed2200babfd05f8 (diff)
parent37658f15bb88e054635a496327a4a82bb50fd5d5 (diff)
downloadrails-9a28bd787660b08aae36155066e61d3608d0b4dd.tar.gz
rails-9a28bd787660b08aae36155066e61d3608d0b4dd.tar.bz2
rails-9a28bd787660b08aae36155066e61d3608d0b4dd.zip
Merge commit 'rails/master'
Diffstat (limited to 'activemodel')
-rw-r--r--activemodel/examples/amo_ap_example.rb36
-rw-r--r--activemodel/lib/active_model.rb1
-rw-r--r--activemodel/lib/active_model/api_compliant.rb25
-rw-r--r--activemodel/lib/active_model/validations.rb2
4 files changed, 64 insertions, 0 deletions
diff --git a/activemodel/examples/amo_ap_example.rb b/activemodel/examples/amo_ap_example.rb
new file mode 100644
index 0000000000..cef718d0d4
--- /dev/null
+++ b/activemodel/examples/amo_ap_example.rb
@@ -0,0 +1,36 @@
+$:.push "activesupport/lib"
+$:.push "activemodel/lib"
+
+require "active_model/validations"
+require "active_model/deprecated_error_methods"
+require "active_model/errors"
+require "active_model/naming"
+
+class Person
+ include ActiveModel::Validations
+ extend ActiveModel::Naming
+
+ validates_presence_of :name
+
+ attr_accessor :name
+ def initialize(attributes = {})
+ @name = attributes[:name]
+ end
+
+ def persist
+ @persisted = true
+ end
+
+ def new_record?
+ @persisted
+ end
+
+ def to_model() self end
+end
+
+person1 = Person.new
+p person1.valid?
+person1.errors
+
+person2 = Person.new(:name => "matz")
+p person2.valid? \ No newline at end of file
diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb
index f988cd71b8..c6f63d2fdc 100644
--- a/activemodel/lib/active_model.rb
+++ b/activemodel/lib/active_model.rb
@@ -26,6 +26,7 @@ $:.unshift(activesupport_path) if File.directory?(activesupport_path)
require 'active_support'
module ActiveModel
+ autoload :APICompliant, 'active_model/api_compliant'
autoload :Attributes, 'active_model/attributes'
autoload :Base, 'active_model/base'
autoload :DeprecatedErrorMethods, 'active_model/deprecated_error_methods'
diff --git a/activemodel/lib/active_model/api_compliant.rb b/activemodel/lib/active_model/api_compliant.rb
new file mode 100644
index 0000000000..26f83feb6b
--- /dev/null
+++ b/activemodel/lib/active_model/api_compliant.rb
@@ -0,0 +1,25 @@
+module ActiveModel
+ module APICompliant
+ include Naming
+
+ def self.extended(klass)
+ klass.class_eval do
+ include Validations
+ include InstanceMethods
+ end
+ end
+
+ module InstanceMethods
+ def to_model
+ if respond_to?(:new_record?)
+ self.class.class_eval { def to_model() self end }
+ to_model
+ else
+ raise "In order to be ActiveModel API compliant, you need to define " \
+ "a new_record? method, which should return true if it has not " \
+ "yet been persisted."
+ end
+ end
+ end
+ end
+end \ No newline at end of file
diff --git a/activemodel/lib/active_model/validations.rb b/activemodel/lib/active_model/validations.rb
index 5223cea135..54a869396d 100644
--- a/activemodel/lib/active_model/validations.rb
+++ b/activemodel/lib/active_model/validations.rb
@@ -1,5 +1,7 @@
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/hash/keys'
+require 'active_support/concern'
+require 'active_support/callbacks'
module ActiveModel
module Validations