purzelrakete-restful 0.2.13 → 0.2.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES.markdown CHANGED
@@ -11,6 +11,14 @@
11
11
 
12
12
  * added ability to publish :wallet-restful-url (explicitly collapsed)
13
13
 
14
- 20. Aug 2009 - 0.2.13
14
+ 20. Aug 2009
15
+
16
+ - 0.2.13
15
17
 
16
- * hash serializer no longer dereferences ids
18
+ * hash serializer no longer dereferences ids
19
+
20
+ - 0.2.14
21
+
22
+ * arrays names now use base_class of content models
23
+ * restful_path defaults to using base_class in path
24
+ * if array responds to name, use this as collection name
@@ -61,13 +61,21 @@ module Restful
61
61
 
62
62
  # array
63
63
  if self.is_a?(Array)
64
+ element_name = if fst = self.first
65
+ fst.class.respond_to?(:base_class) ?
66
+ fst.class.base_class.to_s.tableize :
67
+ fst.class.to_s.pluralize
68
+ elsif self.respond_to?(:name)
69
+ self.name
70
+ else
71
+ "nil-classes"
72
+ end
73
+
64
74
  elements = self.map do |el|
65
75
  raise TypeError.new("Not all array elements respond to #to_restful. ") unless el.respond_to?(:to_restful)
66
76
  Restful::Converters::ActiveRecord.convert(el, !config_parameter.nil? ? Config.new(config_parameter) : el.class.restful_config)
67
77
  end
68
78
 
69
- element_name = elements.first ? elements.first.name.pluralize : "nil-classes"
70
-
71
79
  returning Restful.collection(element_name, elements, :array) do |collection|
72
80
  collection.total_entries = self.total_entries if self.respond_to?(:total_entries)
73
81
  end
@@ -106,7 +114,7 @@ module Restful
106
114
  end
107
115
 
108
116
  def restful_path
109
- "/#{ self.class.to_s.tableize }/#{ self.to_param }"
117
+ "/#{ self.class.base_class.to_s.tableize }/#{ self.to_param }"
110
118
  end
111
119
  end
112
120
 
@@ -53,7 +53,6 @@ module Restful
53
53
  else formatted_value(value)
54
54
  end
55
55
  end
56
-
57
56
  end
58
57
  end
59
58
  end
data/restful.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "restful"
3
- s.version = "0.2.13"
3
+ s.version = "0.2.14"
4
4
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
5
5
  s.authors = ["Daniel Bornkessel", "Rany Keddo"]
6
6
  s.date = "2009-08-11"
@@ -1,8 +1,21 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
3
  context "basic types converter" do
4
+
4
5
  teardown { reset_config }
5
6
 
7
+ specify "should be able to convert a hash to a resource map" do
8
+ Person.restful_publish(:name)
9
+ resource = { "zeperson" => @person = Person.create(:name => "fuddzle") }.to_restful
10
+ resource.should.is_a?(Restful::ApiModel::Map)
11
+
12
+ attrs = resource.simple_attributes
13
+ attrs.size.should.== 1
14
+ attrs.first.name.should.== "zeperson"
15
+
16
+ attrs.first.value.values.first.value.== "fuddzle"
17
+ end
18
+
6
19
  specify "should raise exception if not all array contents respond to .to_restful" do
7
20
  Person.restful_publish(:name)
8
21
 
@@ -15,6 +28,16 @@ context "basic types converter" do
15
28
  collection = [].to_restful
16
29
  collection.name.should.== "nil-classes"
17
30
  end
31
+
32
+ specify "should infer array name from first element class" do
33
+ collection = [Pet.new].to_restful
34
+ collection.name.should.== "pets"
35
+ end
36
+
37
+ specify "should infer array name from first element base_class if it is an active_record object" do
38
+ collection = [Emu.new, Pet.new].to_restful
39
+ collection.name.should.== "pets"
40
+ end
18
41
 
19
42
  specify "should convert an array to a restful collection" do
20
43
  Person.restful_publish(:name)
@@ -34,15 +57,13 @@ context "basic types converter" do
34
57
  collection.total_entries.should.== 1001
35
58
  end
36
59
 
37
- specify "should be able to convert a hash to a resource map" do
60
+ specify "should set name on collection if array responds to .name and has this set" do
38
61
  Person.restful_publish(:name)
39
- resource = { "zeperson" => @person = Person.create(:name => "fuddzle") }.to_restful
40
- resource.should.is_a?(Restful::ApiModel::Map)
41
-
42
- attrs = resource.simple_attributes
43
- attrs.size.should.== 1
44
- attrs.first.name.should.== "zeperson"
45
-
46
- attrs.first.value.values.first.value.== "fuddzle"
62
+ people = PaginatedCollection.new()
63
+ people.total_entries = 0
64
+ people.name = "people"
65
+
66
+ collection = people.to_restful
67
+ collection.name.should.== "people"
47
68
  end
48
69
  end
@@ -1,4 +1,3 @@
1
1
  class PaginatedCollection < Array
2
- attr_accessor :total_entries
3
- end
4
-
2
+ attr_accessor :total_entries, :name
3
+ end
data/test/test_helper.rb CHANGED
@@ -34,6 +34,7 @@ silence_stream(STDOUT) do
34
34
  t.string :name
35
35
  t.integer :species
36
36
  t.integer :person_id
37
+ t.string :type
37
38
 
38
39
  t.timestamp :created_at
39
40
  t.timestamp :updated_at
@@ -62,6 +63,7 @@ end
62
63
 
63
64
  require plugin_root.join 'init'
64
65
  require 'fixtures/models/pet'
66
+ require 'fixtures/models/emu'
65
67
  require 'fixtures/models/wallet'
66
68
  require 'fixtures/models/person'
67
69
  require 'fixtures/models/paginated_collection'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: purzelrakete-restful
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.2.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bornkessel