purzelrakete-restful 0.2.4 → 0.2.5

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.
@@ -4,6 +4,8 @@
4
4
  module Restful
5
5
  module ApiModel
6
6
  class Collection < Attribute
7
+ attr_accessor :total_entries
8
+
7
9
  def initialize(name, resources, extended_type)
8
10
  super
9
11
 
@@ -9,17 +9,17 @@ module Restful
9
9
  nested = config.nested?
10
10
 
11
11
  resource = Restful.resource(
12
- model.class.to_s.tableize.demodulize.singularize, {
13
- :base => Restful::Rails.api_hostname,
12
+ model.class.to_s.tableize.demodulize.singularize, {
13
+ :base => Restful::Rails.api_hostname,
14
14
  :path => model.restful_path,
15
15
  :url => model.restful_url
16
16
  })
17
17
 
18
18
  # simple attributes
19
- resource.values += Restful::Rails.tools.simple_attributes_on(model).map do |key, value|
19
+ resource.values += Restful::Rails.tools.simple_attributes_on(model).map do |key, value|
20
20
  convert_to_simple_attribute(key, value, config, published, model)
21
21
  end.compact
22
-
22
+
23
23
  # has_many, has_one
24
24
  resource.values += model.class.reflections.keys.map do |key|
25
25
  if config.published?(key.to_sym)
@@ -50,13 +50,18 @@ module Restful
50
50
 
51
51
  # array
52
52
  if self.is_a?(Array)
53
- elements = self.map do |el|
53
+ elements = self.map do |el|
54
54
  raise TypeError.new("Not all array elements respond to #to_restful. ") unless el.respond_to?(:to_restful)
55
55
  Restful::Converters::ActiveRecord.convert(el, el.class.restful_config)
56
56
  end
57
57
 
58
58
  element_name = elements.first ? elements.first.name.pluralize : "nil-classes"
59
- Restful.collection(element_name, elements, :array)
59
+
60
+ collection = returning Restful.collection(element_name, elements, :array) do |collection|
61
+ collection.total_entries = self.total_entries if self.respond_to?(:total_entries)
62
+ end
63
+
64
+ collection
60
65
  else
61
66
  Restful::Converters::ActiveRecord.convert(self, config)
62
67
  end
@@ -136,4 +141,5 @@ module Restful
136
141
  end
137
142
 
138
143
  ActiveRecord::Base.send :include, Restful::Rails::ActiveRecord::Configuration
139
- Array.send :include, Restful::Rails::ActiveRecord::Configuration::CommonInstanceMethods
144
+ Array.send :include, Restful::Rails::ActiveRecord::Configuration::CommonInstanceMethods
145
+ Hash.send :include, Restful::Rails::ActiveRecord::Configuration::CommonInstanceMethods
data/lib/restful/rails.rb CHANGED
@@ -17,6 +17,6 @@ module Restful
17
17
  def self.tools
18
18
  Restful::Rails::ActiveRecord::MetadataTools::Utils
19
19
  end
20
-
20
+
21
21
  end
22
22
  end
@@ -11,30 +11,32 @@ module Restful
11
11
 
12
12
  def serialize(resource, options = {})
13
13
  resource.is_a?(Restful::ApiModel::Collection) ?
14
- serialize_array(resource.value) :
15
- serialize_collection(resource)
14
+ serialize_collection(resource) :
15
+ serialize_tuples(resource.values, resource.full_url)
16
16
  end
17
-
17
+
18
18
  private
19
-
20
- def hashify_key(original_key)
21
- original_key.to_s.tr("-", "_").to_sym
22
- end
23
19
 
24
- def serialize_array(resources)
25
- resources.map { |r| serialize(r) }
20
+ def serialize_collection(collection)
21
+ values = collection.value.map { |r| serialize(r) }
22
+
23
+ if entries = collection.total_entries
24
+ values = { :total_entries => entries, collection.name => values }
25
+ end
26
+
27
+ values
26
28
  end
27
-
28
- def serialize_collection(resource)
29
- resource.values.inject({ "restful_url" => resource.full_url }) do |params, value|
30
- params[hashify_key(value.name)] = serialize_value(value)
29
+
30
+ def serialize_tuples(tuples, url)
31
+ tuples.inject({ "restful_url" => url }) do |params, value|
32
+ params[value.name.to_s.tr("-", "_").to_sym] = serialize_value(value)
31
33
  params
32
34
  end
33
35
  end
34
36
 
35
37
  def serialize_value(value)
36
38
  case value.type
37
- when :collection then serialize_array(value.value)
39
+ when :collection then serialize_collection(value)
38
40
  when :link then Restful::Rails.tools.dereference(value.value)
39
41
  when :resource then serialize(value)
40
42
  else formatted_value(value)
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.4"
3
+ s.version = "0.2.5"
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"
@@ -118,4 +118,5 @@ context "active record converter" do
118
118
 
119
119
  xml_should_eql_fixture(@person.to_restful_xml(:wallet), "people", :joe_with_zwiebelleder)
120
120
  end
121
+
121
122
  end
@@ -1,5 +1,5 @@
1
1
  class Pet < ActiveRecord::Base
2
- belongs_to :owner, :class_name => "Person", :foreign_key => "person_id"
2
+ belongs_to :owner, :class_name => "Person", :foreign_key => "person_id"
3
3
 
4
4
  apiable
5
5
  end
@@ -41,5 +41,4 @@ context "Configuration" do
41
41
  config = Restful.cfg(:one, :restful_options => {:nested => true})
42
42
  config.nested?.should.== true
43
43
  end
44
-
45
44
  end
@@ -21,6 +21,7 @@ context "json serializer" do
21
21
 
22
22
  specify "should be able to serialize objects with empty collections" do
23
23
  @person.pets = []
24
+
24
25
  assert_nothing_raised do
25
26
  json_should_eql_fixture(@person.to_restful_json, "people", :bloggs_da_pet_hater)
26
27
  end
@@ -61,4 +62,11 @@ context "json serializer" do
61
62
  specify "should serialize collections correctly" do
62
63
  json_should_eql_fixture(@person.pets.to_restful_json, "pets", :pets_array)
63
64
  end
65
+
66
+ specify "should be able to serialize collections with total entries info" do
67
+ pets = PaginatedCollection.new(@person.pets)
68
+ pets.total_entries = 1001
69
+
70
+ json_should_eql_fixture(pets.to_restful_json, "pets", :pets_array_with_total_entries)
71
+ end
64
72
  end
data/test/test_helper.rb CHANGED
@@ -64,6 +64,7 @@ require plugin_root.join 'init'
64
64
  require 'fixtures/models/pet'
65
65
  require 'fixtures/models/wallet'
66
66
  require 'fixtures/models/person'
67
+ require 'fixtures/models/paginated_collection'
67
68
 
68
69
  Restful::Rails.api_hostname = "http://example.com:3000"
69
70
 
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.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bornkessel