Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 2b738e92a01abacc831a1b30b7ab14cf36e6e631 Mon Sep 17 00:00:00 2001
- From: Taryn East <git@taryneast.org>
- Date: Wed, 14 Oct 2009 22:10:09 +0100
- Subject: [PATCH] making active support work to make active resource tests pass again
- ---
- activesupport/lib/active_support/callbacks.rb | 1 +
- activesupport/lib/active_support/test_case.rb | 1 +
- 2 files changed, 2 insertions(+), 0 deletions(-)
- diff --git a/activesupport/lib/active_support/callbacks.rb b/activesupport/lib/active_support/callbacks.rb
- index 67e9b01..0856e4f 100644
- --- a/activesupport/lib/active_support/callbacks.rb
- +++ b/activesupport/lib/active_support/callbacks.rb
- @@ -1,6 +1,7 @@
- require 'active_support/core_ext/array/wrap'
- require 'active_support/core_ext/class/inheritable_attributes'
- require 'active_support/core_ext/kernel/reporting'
- +require 'active_support/concern'
- module ActiveSupport
- # Callbacks are hooks into the lifecycle of an object that allow you to trigger logic
- diff --git a/activesupport/lib/active_support/test_case.rb b/activesupport/lib/active_support/test_case.rb
- index 1646891..bad155f 100644
- --- a/activesupport/lib/active_support/test_case.rb
- +++ b/activesupport/lib/active_support/test_case.rb
- @@ -1,4 +1,5 @@
- require 'test/unit/testcase'
- +require 'active_support/callbacks'
- require 'active_support/testing/setup_and_teardown'
- require 'active_support/testing/assertions'
- require 'active_support/testing/deprecation'
- --
- 1.5.6.3
- From af9e31e696f34709daba5cdbcba139d0044579b9 Mon Sep 17 00:00:00 2001
- From: Taryn East <git@taryneast.org>
- Date: Wed, 2 Dec 2009 21:50:55 +0000
- Subject: [PATCH] Bare essentials of the schema - an array on ARes
- So, we will be embarking on the grand adventure of schema-dom for Active
- Resource. Begun by a very simple array being stored on the Active Resource
- object.
- This is clearly not useful for anything at this stage, but sets up the
- foundatin for all the rest of the funtionality.
- From here we can 'complicate' things by making the schema migration-like and
- actually using it to do useful stuff... but for now, this is the beginning.
- ---
- activeresource/lib/active_resource/base.rb | 16 ++++
- activeresource/test/cases/base/schema_test.rb | 115 +++++++++++++++++++++++++
- 2 files changed, 131 insertions(+), 0 deletions(-)
- create mode 100644 activeresource/test/cases/base/schema_test.rb
- diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
- index 18105e8..b8aa028 100644
- --- a/activeresource/lib/active_resource/base.rb
- +++ b/activeresource/lib/active_resource/base.rb
- @@ -241,6 +241,15 @@ module ActiveResource
- cattr_accessor :logger
- class << self
- + # Schema defines the known attributes of the current model.
- + #
- + # TODO: under construction - will add more doco as I go.
- + def schema=(the_schema)
- + @schema = the_schema
- + end
- + def schema
- + @schema ||= nil
- + end
- # Gets the URI of the REST resources to map for this class. The site variable is required for
- # Active Resource's mapping to work.
- def site
- @@ -776,6 +785,13 @@ module ActiveResource
- attr_accessor :attributes #:nodoc:
- attr_accessor :prefix_options #:nodoc:
- + # If no schema has been defined for the class (see
- + # <tt>ActiveResource::schema=</tt>), the default automatic schema is
- + # generated from the current instance's attributes
- + def schema
- + self.class.schema || self.attributes.keys
- + end
- +
- # Constructor method for \new resources; the optional +attributes+ parameter takes a \hash
- # of attributes for the \new resource.
- #
- diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb
- new file mode 100644
- index 0000000..2fb2707
- --- /dev/null
- +++ b/activeresource/test/cases/base/schema_test.rb
- @@ -0,0 +1,115 @@
- +require 'abstract_unit'
- +require "fixtures/person"
- +require "fixtures/street_address"
- +
- +########################################################################
- +# Testing the schema of your Active Resource models
- +########################################################################
- +class SchemaTest < ActiveModel::TestCase
- + def setup
- + @matz = { :id => 1, :name => 'Matz' }.to_xml(:root => 'person')
- + @david = { :id => 2, :name => 'David' }.to_xml(:root => 'person')
- + @greg = { :id => 3, :name => 'Greg' }.to_xml(:root => 'person')
- + @addy = { :id => 1, :street => '12345 Street', :country => 'Australia' }.to_xml(:root => 'address')
- + @default_request_headers = { 'Content-Type' => 'application/xml' }
- + @rick = { :name => "Rick", :age => 25 }.to_xml(:root => "person")
- + @people = [{ :id => 1, :name => 'Matz' }, { :id => 2, :name => 'David' }].to_xml(:root => 'people')
- + @people_david = [{ :id => 2, :name => 'David' }].to_xml(:root => 'people')
- + @addresses = [{ :id => 1, :street => '12345 Street', :country => 'Australia' }].to_xml(:root => 'addresses')
- +
- + ActiveResource::HttpMock.respond_to do |mock|
- + mock.get "/people/1.xml", {}, @matz
- + mock.get "/people/2.xml", {}, @david
- + mock.get "/people/Greg.xml", {}, @greg
- + mock.get "/people/4.xml", {'key' => 'value'}, nil, 404
- + mock.get "/people/5.xml", {}, @rick
- + mock.put "/people/1.xml", {}, nil, 204
- + mock.delete "/people/1.xml", {}, nil, 200
- + mock.delete "/people/2.xml", {}, nil, 400
- + mock.get "/people/99.xml", {}, nil, 404
- + mock.post "/people.xml", {}, @rick, 201, 'Location' => '/people/5.xml'
- + mock.get "/people.xml", {}, @people
- + mock.get "/people/1/addresses.xml", {}, @addresses
- + mock.get "/people/1/addresses/1.xml", {}, @addy
- + mock.get "/people/1/addresses/2.xml", {}, nil, 404
- + mock.get "/people/2/addresses/1.xml", {}, nil, 404
- + mock.get "/people/Greg/addresses/1.xml", {}, @addy
- + mock.put "/people/1/addresses/1.xml", {}, nil, 204
- + mock.delete "/people/1/addresses/1.xml", {}, nil, 200
- + mock.post "/people/1/addresses.xml", {}, nil, 201, 'Location' => '/people/1/addresses/5'
- + mock.get "/people//addresses.xml", {}, nil, 404
- + mock.get "/people//addresses/1.xml", {}, nil, 404
- + mock.put "/people//addressaddresseses/1.xml", {}, nil, 404
- + mock.delete "/people//addresses/1.xml", {}, nil, 404
- + mock.post "/people//addresses.xml", {}, nil, 404
- + mock.head "/people/1.xml", {}, nil, 200
- + mock.head "/people/Greg.xml", {}, nil, 200
- + mock.head "/people/99.xml", {}, nil, 404
- + mock.head "/people/1/addresses/1.xml", {}, nil, 200
- + mock.head "/people/1/addresses/2.xml", {}, nil, 404
- + mock.head "/people/2/addresses/1.xml", {}, nil, 404
- + mock.head "/people/Greg/addresses/1.xml", {}, nil, 200
- + end
- +
- + Person.user = nil
- + Person.password = nil
- + end
- +
- + test "schema on a new model should be empty" do
- + assert Person.schema.blank?, "should have a blank class schema"
- + assert Person.new.schema.blank?, "should have a blank instance schema"
- + end
- +
- + test "schema on a found model should return all the attributes of that model instance" do
- + p = Person.find(1)
- + s = p.schema
- +
- + assert s.present?, "should have found a non-empty schema!"
- +
- + p.attributes.each do |the_attr, val|
- + assert s.include?(the_attr), "should have found attr: #{the_attr} in schema, but only had: #{s.inspect}"
- + end
- + end
- +
- + test "with two instances, default schema should match the attributes of the individual instances - even if they differ" do
- + matz = Person.find(1)
- + rick = Person.find(5)
- +
- + m_attrs = matz.attributes.keys.sort
- + r_attrs = rick.attributes.keys.sort
- +
- + assert_not_equal m_attrs, r_attrs, "should have different attributes on each model"
- +
- + assert_not_equal matz.schema, rick.schema, "should have had different schemas too"
- + end
- +
- + test "defining a schema should return it when asked" do
- + assert Person.schema.blank?, "should have a blank class schema"
- + new_schema = [:name, :age, :height, :weight]
- + assert_nothing_raised {
- + Person.schema = new_schema
- + assert_equal new_schema, Person.schema, "should have saved the schema on the class"
- + assert_equal new_schema, Person.new.schema, "should have mde the schema available to every instance"
- + }
- +
- + Person.schema = nil # hack to stop test bleedthrough...
- + end
- +
- + test "defining a schema, then fetching a model should still match the defined schema" do
- + # sanity checks
- + assert Person.schema.blank?, "should have a blank class schema"
- + new_schema = [:name, :age, :height, :weight]
- +
- + matz = Person.find(1)
- + assert !matz.schema.blank?, "should have some sort of schema on an instance variable"
- + assert_not_equal new_schema, matz.schema, "should not have the class-level schema until it's been added to the class!"
- +
- + assert_nothing_raised {
- + Person.schema = new_schema
- + assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema"
- + }
- +
- + Person.schema = nil # hack to stop test bleedthrough...
- + end
- +
- +end
- --
- 1.5.6.3
- From 0806fdf0231651801281133425f1da599499ca2e Mon Sep 17 00:00:00 2001
- From: Taryn East <git@taryneast.org>
- Date: Wed, 2 Dec 2009 21:53:13 +0000
- Subject: [PATCH] use schema to respond_to known 'columns' with true
- First actual functionality of a schema - respond_to? will recognise any
- schema attributes as valid methods.
- ---
- activeresource/lib/active_resource/base.rb | 2 ++
- activeresource/test/cases/base/schema_test.rb | 16 ++++++++++++++++
- 2 files changed, 18 insertions(+), 0 deletions(-)
- diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
- index b8aa028..294acba 100644
- --- a/activeresource/lib/active_resource/base.rb
- +++ b/activeresource/lib/active_resource/base.rb
- @@ -1175,6 +1175,8 @@ module ActiveResource
- super
- elsif attributes.has_key?(method_name)
- true
- + elsif schema && schema.include?(method_name.to_sym)
- + true
- elsif method_name =~ /(?:=|\?)$/ && attributes.include?($`)
- true
- else
- diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb
- index 2fb2707..edd32e1 100644
- --- a/activeresource/test/cases/base/schema_test.rb
- +++ b/activeresource/test/cases/base/schema_test.rb
- @@ -112,4 +112,20 @@ class SchemaTest < ActiveModel::TestCase
- Person.schema = nil # hack to stop test bleedthrough...
- end
- + test "should respond to all attributes in a schema" do
- + assert Person.schema.blank?, "should have a blank class schema"
- + new_schema = [:name, :age, :height, :weight, :my_new_schema_attribute ]
- + p = Person.new
- + #sanity check - should not respond to the brand-new one
- + assert !p.respond_do?(:my_new_schema_attribute)
- +
- + assert_nothing_raised {
- + Person.schema = new_schema
- + new_schema.each do |the_attr|
- + assert Person.new.respond_to?(the_attr), "shoud respond to the schema's methods, but failed on: #{the_attr}"
- + end
- + }
- + Person.schema = nil # hack to stop test bleedthrough...
- + end
- +
- end
- --
- 1.5.6.3
- From af3481cd3e9e45490d8cffab52f0fed36b23c9b6 Mon Sep 17 00:00:00 2001
- From: Taryn East <git@taryneast.org>
- Date: Wed, 2 Dec 2009 22:06:43 +0000
- Subject: [PATCH] More documentation for schema to explain.
- Just a bit more commenting on what schema is and what it will do.
- ---
- activeresource/lib/active_resource/base.rb | 47 +++++++++++++++++++++++++--
- 1 files changed, 43 insertions(+), 4 deletions(-)
- diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
- index 294acba..f02e595 100644
- --- a/activeresource/lib/active_resource/base.rb
- +++ b/activeresource/lib/active_resource/base.rb
- @@ -243,13 +243,52 @@ module ActiveResource
- class << self
- # Schema defines the known attributes of the current model.
- #
- - # TODO: under construction - will add more doco as I go.
- - def schema=(the_schema)
- - @schema = the_schema
- - end
- + # The schema we pass into an Active Resource is similar to an Active
- + # Record database schema definition. It defines the known attributes
- + # of a certain Active Record - even if there are currently no values
- + # for those attributes. It is like telling Active Resource that you
- + # know there will be attributes there someday.
- + #
- + # There is no need to specify a schema for your Active Resource. If
- + # you do not, the schema will be automatically generated on an
- + # instance from the attributes found there. That kind of schema is
- + # transient and it is possible that each instance will have a
- + # different 'schema' of this sort.
- + #
- + # The benefits of knowing the schema in advance, is that your instance
- + # method will know that they can respond to the known attributes and
- + # will return 'nil' when questioned (instead of MethodNotFound). This
- + # is helpful if you wish to have validations on those attributes.
- + #
- + #
- + # This section is very much under construction. More documentaion will
- + # be added as the functionality is expanded.
- + #
- + #
- + # For the moment, all you can do is pass an array of known
- + # attribute-names and it will be saved on the Resource. See
- + # <tt>schema=</tt> for an example of how this works.
- + #
- + # In future, the Resource will be far more responsive eg will return
- + # 'true' for a known attribute or cast the value saved to the
- + # appropriate type - just as Active Record currently does.
- def schema
- @schema ||= nil
- end
- + # Saves a schema to this resource - telling it what attributes are
- + # already known prior to fetching an object of this type form the
- + # remote system.
- + #
- + # example:
- + # class Person < ActiveResource::Base
- + # schema = [:name, :age, :height, :weight]
- + # end
- + # p = Person.new
- + # p.respond_to? :name # > true
- + #
- + def schema=(the_schema)
- + @schema = the_schema
- + end
- # Gets the URI of the REST resources to map for this class. The site variable is required for
- # Active Resource's mapping to work.
- def site
- --
- 1.5.6.3
- From a6877071c7f1d1c2fb95250fd9f48083c208f833 Mon Sep 17 00:00:00 2001
- From: Taryn East <git@taryneast.org>
- Date: Wed, 2 Dec 2009 22:22:42 +0000
- Subject: [PATCH] return nil instead of MethodNotFound if in schema
- So this is why I wanted the schema all along - so that instead of exploding
- on a known attribute (simply because it hadn't been set yet) it should
- return nil - just like Active Record does.
- ---
- activeresource/lib/active_resource/base.rb | 4 ++-
- activeresource/test/cases/base/schema_test.rb | 33 +++++++++++++++++-------
- 2 files changed, 26 insertions(+), 11 deletions(-)
- diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
- index f02e595..b8134cd 100644
- --- a/activeresource/lib/active_resource/base.rb
- +++ b/activeresource/lib/active_resource/base.rb
- @@ -1319,7 +1319,9 @@ module ActiveResource
- attributes[$`]
- end
- else
- - attributes.include?(method_name) ? attributes[method_name] : super
- + return attributes[method_name] if attributes.include?(method_name)
- + return nil if self.class.schema.include?(method_symbol.to_sym)
- + super
- end
- end
- end
- diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb
- index edd32e1..a3517d4 100644
- --- a/activeresource/test/cases/base/schema_test.rb
- +++ b/activeresource/test/cases/base/schema_test.rb
- @@ -112,20 +112,33 @@ class SchemaTest < ActiveModel::TestCase
- Person.schema = nil # hack to stop test bleedthrough...
- end
- - test "should respond to all attributes in a schema" do
- - assert Person.schema.blank?, "should have a blank class schema"
- - new_schema = [:name, :age, :height, :weight, :my_new_schema_attribute ]
- - p = Person.new
- - #sanity check - should not respond to the brand-new one
- - assert !p.respond_do?(:my_new_schema_attribute)
- + test "should respond positively to attributes only in a schema" do
- + new_attr_name = :my_new_schema_attribute
- + assert Person.schema.blank?, "sanity check - should have a blank class schema"
- +
- + assert !Person.new.respond_do?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet"
- assert_nothing_raised {
- - Person.schema = new_schema
- - new_schema.each do |the_attr|
- - assert Person.new.respond_to?(the_attr), "shoud respond to the schema's methods, but failed on: #{the_attr}"
- - end
- + Person.schema = [new_attr_name]
- + assert Person.new.respond_to?(new_attr_name), "should respond to the schema's methods, but failed on: #{new_attr_name}"
- }
- Person.schema = nil # hack to stop test bleedthrough...
- end
- + test "should not give method_missing for attribute only in schema" do
- + new_attr_name = :another_new_schema_attribute
- +
- + assert Person.schema.blank?, "sanity check - should have a blank class schema"
- +
- + assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name} as a method") do
- + Person.new.send(new_attr_name)
- + end
- +
- + Person.schema = [new_attr_name]
- + assert_nothing_raised do
- + Person.new.send(new_attr_name)
- + end
- + Person.schema = nil # hack to stop test bleedthrough...
- + end
- +
- end
- --
- 1.5.6.3
- From 8a10e7306deb6b7e2f6489190f19ed36c2746140 Mon Sep 17 00:00:00 2001
- From: Taryn East <git@taryneast.org>
- Date: Wed, 2 Dec 2009 22:36:11 +0000
- Subject: [PATCH] Should symbolise schema keys to normalise
- Rather than hashes with indifferent access and all the inherant errors, we
- need to settle on either strings or symbols. I may change it all over to
- strings instead (as there seems to be a common theme in ARes for that), but
- for now - it's all symbols.
- ---
- activeresource/lib/active_resource/base.rb | 3 ++-
- activeresource/test/cases/base/schema_test.rb | 25 +++++++++++++++++++------
- 2 files changed, 21 insertions(+), 7 deletions(-)
- diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb
- index b8134cd..a470a39 100644
- --- a/activeresource/lib/active_resource/base.rb
- +++ b/activeresource/lib/active_resource/base.rb
- @@ -287,7 +287,8 @@ module ActiveResource
- # p.respond_to? :name # > true
- #
- def schema=(the_schema)
- - @schema = the_schema
- + @schema = nil
- + @schema = the_schema.map(&:to_sym) if the_schema.present?
- end
- # Gets the URI of the REST resources to map for this class. The site variable is required for
- # Active Resource's mapping to work.
- diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb
- index a3517d4..9d297b5 100644
- --- a/activeresource/test/cases/base/schema_test.rb
- +++ b/activeresource/test/cases/base/schema_test.rb
- @@ -54,12 +54,31 @@ class SchemaTest < ActiveModel::TestCase
- Person.user = nil
- Person.password = nil
- end
- + def teardown
- + Person.schema = nil # hack to stop test bleedthrough...
- + end
- test "schema on a new model should be empty" do
- assert Person.schema.blank?, "should have a blank class schema"
- assert Person.new.schema.blank?, "should have a blank instance schema"
- end
- + test "schema should accept array of syms" do
- + new_schema = [:age, :name]
- +
- + assert_nothing_raised { Person.schema = new_schema }
- + assert_equal new_schema, Person.schema
- + end
- +
- + test "schema should symbolise array of strings" do
- + new_schema = ['name', 'age']
- + new_schema_syms = new_schema.map(&:to_sym)
- +
- + assert_nothing_raised { Person.schema = new_schema }
- + assert_equal new_schema_syms, Person.schema
- + end
- +
- +
- test "schema on a found model should return all the attributes of that model instance" do
- p = Person.find(1)
- s = p.schema
- @@ -91,8 +110,6 @@ class SchemaTest < ActiveModel::TestCase
- assert_equal new_schema, Person.schema, "should have saved the schema on the class"
- assert_equal new_schema, Person.new.schema, "should have mde the schema available to every instance"
- }
- -
- - Person.schema = nil # hack to stop test bleedthrough...
- end
- test "defining a schema, then fetching a model should still match the defined schema" do
- @@ -108,8 +125,6 @@ class SchemaTest < ActiveModel::TestCase
- Person.schema = new_schema
- assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema"
- }
- -
- - Person.schema = nil # hack to stop test bleedthrough...
- end
- test "should respond positively to attributes only in a schema" do
- @@ -122,7 +137,6 @@ class SchemaTest < ActiveModel::TestCase
- Person.schema = [new_attr_name]
- assert Person.new.respond_to?(new_attr_name), "should respond to the schema's methods, but failed on: #{new_attr_name}"
- }
- - Person.schema = nil # hack to stop test bleedthrough...
- end
- test "should not give method_missing for attribute only in schema" do
- @@ -138,7 +152,6 @@ class SchemaTest < ActiveModel::TestCase
- assert_nothing_raised do
- Person.new.send(new_attr_name)
- end
- - Person.schema = nil # hack to stop test bleedthrough...
- end
- end
- --
- 1.5.6.3
Add Comment
Please, Sign In to add comment