purzelrakete-restful 0.2.14 → 0.2.15

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
@@ -21,4 +21,8 @@
21
21
 
22
22
  * arrays names now use base_class of content models
23
23
  * restful_path defaults to using base_class in path
24
- * if array responds to name, use this as collection name
24
+ * if array responds to name, use this as collection name
25
+
26
+ - 0.2.15
27
+
28
+ * :includes like active record to_xml to_json
@@ -4,6 +4,32 @@
4
4
  module Restful
5
5
  module Rails
6
6
  module ActiveRecord
7
+ module Utils
8
+
9
+ def self.get_includes(config)
10
+ if config && config.is_a?(Hash) && config.keys.size == 1 && includes = config[:include]
11
+ add_to_whitelist = [*includes].map { |el| el.is_a?(String) ? el.to_sym : el }
12
+ return nil, add_to_whitelist
13
+ else
14
+ return config, []
15
+ end
16
+ end
17
+
18
+ def self.convert_to_single_resource(config_parameter, el)
19
+ config = if config_parameter
20
+ returning Configuration::Config.new(config_parameter) do |c|
21
+ config, includes = Utils.get_includes(config_parameter)
22
+ c.whitelisted += el.class.restful_config.whitelisted unless config
23
+ c.whitelisted += includes unless includes.blank?
24
+ end
25
+ else
26
+ el.class.restful_config
27
+ end
28
+
29
+ Restful::Converters::ActiveRecord.convert(el, config)
30
+ end
31
+ end
32
+
7
33
  module Configuration
8
34
  def self.included(base)
9
35
  base.send :class_inheritable_accessor, :restful_config
@@ -36,13 +62,7 @@ module Restful
36
62
  # by passing in something like @pet.to_restful(:name, :species).
37
63
  #
38
64
  def to_restful(config_parameter = nil)
39
- config = config_parameter
40
- add_to_whitelist = []
41
-
42
- if config && config.is_a?(Hash) && config.keys.size == 1 && includes = config[:include]
43
- add_to_whitelist = [*includes].map { |el| el.is_a?(String) ? el.to_sym : el }
44
- config = nil
45
- end
65
+ config, add_to_whitelist = Utils.get_includes(config_parameter)
46
66
 
47
67
  config ||= self.class.restful_config.clone if self.class.respond_to?(:restful_config)
48
68
  config ||= []
@@ -73,7 +93,7 @@ module Restful
73
93
 
74
94
  elements = self.map do |el|
75
95
  raise TypeError.new("Not all array elements respond to #to_restful. ") unless el.respond_to?(:to_restful)
76
- Restful::Converters::ActiveRecord.convert(el, !config_parameter.nil? ? Config.new(config_parameter) : el.class.restful_config)
96
+ Utils.convert_to_single_resource(config_parameter, el)
77
97
  end
78
98
 
79
99
  returning Restful.collection(element_name, elements, :array) do |collection|
@@ -81,11 +101,12 @@ module Restful
81
101
  end
82
102
 
83
103
  elsif self.is_a?(Hash)
104
+
84
105
  elements = self.map do |k,v|
85
- if v.respond_to?(:to_restful) and v.class.respond_to?(:restful_config)
86
- value = Restful::Converters::ActiveRecord.convert(v, !config_parameter.nil? ? Config.new(config_parameter) : v.class.restful_config)
106
+ value = if v.respond_to?(:to_restful) and v.class.respond_to?(:restful_config)
107
+ Utils.convert_to_single_resource(config_parameter, v)
87
108
  else
88
- value = v.respond_to?(:to_restful) ? v.to_restful : v
109
+ v.respond_to?(:to_restful) ? v.to_restful : v
89
110
  end
90
111
  Restful::ApiModel::Attribute.new(k, value, :map)
91
112
  end
@@ -97,7 +118,7 @@ module Restful
97
118
  Restful::Converters::ActiveRecord.convert(self, config)
98
119
  end
99
120
  end
100
-
121
+
101
122
  # FIXME: read Restful::Serializers::Base.serializers. Load order problems?
102
123
  [:atom_like, :hash, :json, :params, :xml].each do |format|
103
124
  define_method("to_restful_#{ format }") do |*args|
@@ -149,7 +170,7 @@ module Restful
149
170
  end
150
171
 
151
172
  private
152
-
173
+
153
174
  def split_into_whitelist_and_restful_options(array)
154
175
  options = {}
155
176
 
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.14"
3
+ s.version = "0.2.15"
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"
@@ -131,5 +131,17 @@ context "active record converter" do
131
131
 
132
132
  xml_should_eql_fixture(@person.to_restful_xml(:wallet), "people", :joe_with_zwiebelleder)
133
133
  end
134
+
135
+ specify "should include attributes when include parameter is passed to to_restful" do
136
+ Person.restful_publish(:name)
137
+ Pet.restful_publish(:name)
138
+
139
+ @person = Person.create
140
+ @pet = @person.pets.create(:name => "Mietze")
141
+
142
+ @pet.to_restful(:include => :owner).values.map(&:name).should.include :owner
143
+ Pet.restful_config.whitelisted.include?(:owner).should.equal false
144
+ end
134
145
 
135
- end
146
+ end
147
+
@@ -66,4 +66,34 @@ context "basic types converter" do
66
66
  collection = people.to_restful
67
67
  collection.name.should.== "people"
68
68
  end
69
+ end
70
+
71
+ context "basic types converter :includes" do
72
+
73
+ specify "should include extra attributes for hashes" do
74
+ Person.restful_publish(:name)
75
+ Pet.restful_publish(:name)
76
+
77
+ @person = Person.create
78
+ @pet = @person.pets.create(:name => "Mietze")
79
+
80
+ map = { :pet => @pet }.to_restful(:include => :owner)
81
+ map.values.first.name.should.== :pet
82
+ map.values.first.value.values.map(&:name).should.include :owner
83
+
84
+ Pet.restful_config.whitelisted.include?(:owner).should.equal false
85
+ end
86
+
87
+ specify "should include extra attributes for arrays" do
88
+ Person.restful_publish(:name)
89
+ Pet.restful_publish(:name)
90
+
91
+ @person = Person.create
92
+ @pet = @person.pets.create(:name => "Mietze")
93
+
94
+ collection = [@pet].to_restful(:include => :owner)
95
+ collection.value.first.values.map(&:name).should.include :owner
96
+
97
+ Pet.restful_config.whitelisted.include?(:owner).should.equal false
98
+ end
69
99
  end
@@ -91,4 +91,17 @@ hash_with_people:
91
91
  "restful_url": "http://example.com:3000/people/<%= @person.to_param %>",
92
92
  "name": "Joe Bloggs"
93
93
  }]
94
+ }
95
+
96
+ hash_with_rich_person:
97
+ |
98
+ {
99
+ "person": {
100
+ "restful_url": "http://example.com:3000/people/<%= @person.to_param %>",
101
+ "name": "Joe Bloggs",
102
+ "wallet": {
103
+ "restful_url": "http://example.com:3000/wallets/<%= @person.wallet.to_param %>",
104
+ "contents": "<%= @person.wallet.contents %>"
105
+ }
106
+ }
94
107
  }
@@ -17,17 +17,6 @@ context "restful publish" do
17
17
  Person.restful_publish(:name, :pets => [:name, :species])
18
18
  Person.restful_config.restful_options.should.==({})
19
19
  end
20
-
21
- specify "should include attributes when publishe parameter is passed to to_restful" do
22
- Person.restful_publish(:name)
23
- Pet.restful_publish(:name)
24
-
25
- @person = Person.create
26
- @pet = @person.pets.create(:name => "Mietze")
27
-
28
- @pet.to_restful(:include => :owner).values.map(&:name).should.include :owner
29
- Pet.restful_config.whitelisted.include?(:owner).should.equal false
30
- end
31
20
  end
32
21
 
33
22
  context "api publishing with nesting" do
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/../test_helper.rb'
2
2
 
3
3
  context "json serializer" do
4
-
4
+
5
5
  setup do
6
6
  Person.restful_publish(:name, :current_location, :pets, :wallet, :created_at)
7
7
  Pet.restful_publish(:name)
@@ -66,7 +66,7 @@ context "json serializer" do
66
66
  specify "should be able to serialize collections with total entries info" do
67
67
  pets = PaginatedCollection.new(@person.pets)
68
68
  pets.total_entries = 1001
69
-
69
+
70
70
  json_should_eql_fixture(pets.to_restful_json, "pets", :pets_array_with_total_entries)
71
71
  end
72
72
 
@@ -79,4 +79,12 @@ context "json serializer" do
79
79
  Person.restful_publish(:name)
80
80
  json_should_eql_fixture({ "total_hits" => 2, "people" => [ @person, @person ] }.to_restful_json, "people", :hash_with_people)
81
81
  end
82
- end
82
+
83
+ specify 'should serialize a hash with include option correctly - the include option should be passed to the values' do
84
+ Person.restful_publish(:name)
85
+ Wallet.restful_publish(:contents)
86
+
87
+ json = {:person => @person}.to_restful_json(:include => :wallet)
88
+ json_should_eql_fixture(json, "people", :hash_with_rich_person)
89
+ end
90
+ end
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.14
4
+ version: 0.2.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bornkessel