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.
- data/lib/restful/apimodel/collection.rb +2 -0
- data/lib/restful/converters/active_record.rb +4 -4
- data/lib/restful/rails/active_record/configuration.rb +9 -3
- data/lib/restful/rails.rb +1 -1
- data/lib/restful/serializers/hash_serializer.rb +16 -14
- data/restful.gemspec +1 -1
- data/test/converters/active_record_converter_test.rb +1 -0
- data/test/fixtures/models/pet.rb +1 -1
- data/test/rails/configuration_test.rb +0 -1
- data/test/serializers/json_serializer_test.rb +8 -0
- data/test/test_helper.rb +1 -0
- metadata +1 -1
@@ -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
|
-
|
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
@@ -11,30 +11,32 @@ module Restful
|
|
11
11
|
|
12
12
|
def serialize(resource, options = {})
|
13
13
|
resource.is_a?(Restful::ApiModel::Collection) ?
|
14
|
-
|
15
|
-
|
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
|
25
|
-
|
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
|
29
|
-
|
30
|
-
params[
|
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
|
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.
|
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"
|
data/test/fixtures/models/pet.rb
CHANGED
@@ -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