From 1a2946a6d9b1dbcf3a4c77654693d73f9b11bde1 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Wed, 15 Jul 2009 14:16:30 -0700 Subject: Add some missing dependencies --- activemodel/lib/active_model/validations.rb | 2 ++ 1 file changed, 2 insertions(+) (limited to 'activemodel') 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 -- cgit v1.2.3 From 45d41d8012c605f21de51e7018fa31e1d07776eb Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Sun, 19 Jul 2009 23:24:19 +0900 Subject: Add some simple examples for unconventional AMo and AP use --- activemodel/examples/amo_ap_example.rb | 36 ++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 activemodel/examples/amo_ap_example.rb (limited to 'activemodel') 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 -- cgit v1.2.3 From 5ffaaa71d149c9807260c950c9a61d01fe734827 Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Mon, 20 Jul 2009 00:27:04 +0900 Subject: Define ActiveModel API Compliance - Define to_model on AR - Define to_model on ActiveModel::APICompliant - Update test fixtures to be API Compliant - Start using to_model in AP --- activemodel/lib/active_model.rb | 1 + activemodel/lib/active_model/api_compliant.rb | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 activemodel/lib/active_model/api_compliant.rb (limited to 'activemodel') 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 -- cgit v1.2.3