purzelrakete-restful 0.2.10 → 0.2.11

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
@@ -6,3 +6,7 @@
6
6
 
7
7
  * fixed issue where configurations where overwriting each other.
8
8
  * added hash#to_restful
9
+
10
+ 19. Aug 2009 - 0.2.11
11
+
12
+ * added ability to publish :wallet-restful-url (explicitly collapsed)
data/README.markdown CHANGED
@@ -33,6 +33,9 @@ restful_publish :name, :pets, :wallet => :contents, :restful_options => { :expan
33
33
  restful_publish :name, :pets, :restful_options => { :collapsed => :pets } # collapsed pets, even though they are on the second level.
34
34
  restful_publish :name, :pets, :restful_options => { :force_expanded => [:pets, :wallet] }
35
35
 
36
+ ## explicitly collapsed
37
+ restful_options :wallet-restful-url
38
+
36
39
  # Pet
37
40
  restful_publish :name, :person # expands person per default because it is on the second level. Does not expand person.pets.first.person, since this is higher than second level.
38
41
 
@@ -15,6 +15,10 @@ module Restful
15
15
  :url => model.restful_url
16
16
  })
17
17
 
18
+ explicit_links = config.whitelisted.select { |x| x.class == Symbol && x.to_s.ends_with?("_restful_url") }
19
+ explicit_links.each { |link| config.whitelisted.delete(link) }
20
+ explicit_links.map! { |link| link.to_s.chomp("_restful_url").to_sym }
21
+
18
22
  # simple attributes
19
23
  resource.values += Restful::Rails.tools.simple_attributes_on(model).map do |key, value|
20
24
  convert_to_simple_attribute(key, value, config, published, model)
@@ -22,31 +26,27 @@ module Restful
22
26
 
23
27
  # has_many, has_one
24
28
  resource.values += model.class.reflections.keys.map do |key|
25
- if config.published?(key.to_sym)
29
+ explicit_link = !!explicit_links.include?(key)
30
+
31
+ if config.published?(key.to_sym) || explicit_link
26
32
 
27
- # grab the associated resource(s) and run them through conversion
28
33
  nested_config = config.nested(key.to_sym)
29
34
  published << key.to_sym
30
35
 
31
- if model.class.reflections[key].macro == :has_many && !nested
36
+ if has_many?(model, key) && !nested
32
37
  convert_to_collection(model, key, nested_config, published) do |key, resources, extended_type|
33
38
  Restful.collection(key, resources, extended_type)
34
39
  end
35
- elsif model.class.reflections[key].macro == :has_one or model.class.reflections[key].macro == :belongs_to
40
+ elsif has_one?(model, key) or belongs_to?(model, key)
36
41
 
37
- if(config.expanded?(key, nested))
42
+ if config.expanded?(key, nested) && !explicit_link
38
43
  convert_to_collection(model, key, nested_config, published) do |key, resources, extended_type|
39
44
  returning(resources.first) do |res|
40
45
  res.name = key
41
46
  end
42
47
  end
43
48
  else
44
-
45
- value = model.send(key)
46
- restful_path = value ? value.restful_path : nil
47
- basename = value ? Restful::Rails.api_hostname : nil
48
-
49
- Restful.link("#{ key }-restful-url", basename, restful_path, compute_extended_type(model, key))
49
+ link_to(model, key)
50
50
  end
51
51
  end
52
52
  end
@@ -54,7 +54,9 @@ module Restful
54
54
 
55
55
  # Links
56
56
  if model.class.apiable_association_table
57
+
57
58
  resource.values += model.class.apiable_association_table.keys.map do |key|
59
+
58
60
  if config.published?(key.to_sym)
59
61
  published << key.to_sym
60
62
  base, path = model.resolve_association_restful_url(key)
@@ -65,17 +67,20 @@ module Restful
65
67
 
66
68
  # public methods
67
69
  resource.values += (model.public_methods - Restful::Rails.tools.simple_attributes_on(model).keys.map(&:to_s)).map do |method_name|
68
- if config.published?(method_name.to_sym) and not published.include?(method_name.to_sym)
70
+
71
+ explicit_link = !!explicit_links.include?(method_name.to_sym)
72
+
73
+ if (config.published?(method_name.to_sym) && !published.include?(method_name.to_sym)) || explicit_link
69
74
  value = model.send(method_name.to_sym)
70
75
  sanitized_method_name = method_name.tr("!?", "").tr("_", "-").to_sym
71
76
 
72
77
  if value.is_a? ::ActiveRecord::Base
73
- if config.expanded?(method_name.to_sym, nested)
78
+ if config.expanded?(method_name.to_sym, nested) && !explicit_link
74
79
  returning Restful::Rails.tools.expand(value, config.nested(method_name.to_sym)) do |expanded|
75
80
  expanded.name = sanitized_method_name
76
81
  end
77
82
  else
78
- Restful.link("#{ sanitized_method_name }-restful-url", Restful::Rails.api_hostname, value ? value.restful_path : "", compute_extended_type(model, key))
83
+ Restful.link("#{ sanitized_method_name }-restful-url", Restful::Rails.api_hostname, value ? value.restful_path : "", compute_extended_type(model, method_name.to_sym))
79
84
  end
80
85
  else
81
86
  Restful.attr(sanitized_method_name, value, compute_extended_type(model, method_name))
@@ -86,6 +91,26 @@ module Restful
86
91
  resource
87
92
  end
88
93
 
94
+ def self.has_one?(model, key)
95
+ macro(model, key) == :has_one
96
+ end
97
+
98
+ def self.has_many?(model, key)
99
+ macro(model, key) == :has_many
100
+ end
101
+
102
+ def self.belongs_to?(model, key)
103
+ macro(model, key) == :belongs_to
104
+ end
105
+
106
+ def self.link_to(model, key)
107
+ value = model.send(key)
108
+ restful_path = value ? value.restful_path : nil
109
+ basename = value ? Restful::Rails.api_hostname : nil
110
+
111
+ Restful.link("#{ key }-restful-url", basename, restful_path, compute_extended_type(model, key))
112
+ end
113
+
89
114
  def self.convert_to_simple_attribute(key, value, config, published, model = nil)
90
115
  if config.published?(key.to_sym)
91
116
  published << key.to_sym
@@ -95,6 +120,10 @@ module Restful
95
120
  end
96
121
 
97
122
  private
123
+
124
+ def self.macro(model, key)
125
+ model.class.reflections[key].macro
126
+ end
98
127
 
99
128
  def self.convert_to_collection(model, key, nested_config, published)
100
129
  if resources = Restful::Rails.tools.convert_collection_to_resources(model, key, nested_config)
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.10"
3
+ s.version = "0.2.11"
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"
@@ -17,6 +17,16 @@ context "active record converter" do
17
17
  reset_config
18
18
  end
19
19
 
20
+ specify "should publish link and not resource when :oldest_pet_restful_url, where oldest_pet is a defined method" do
21
+ Person.restful_publish(:oldest_pet_restful_url)
22
+ @person.to_restful.links.should.not.be.empty
23
+ end
24
+
25
+ specify "should publish link and not a nested resource with :wallet_restful_url" do
26
+ Person.restful_publish(:wallet_restful_url)
27
+ @person.to_restful.links.should.not.be.empty
28
+ end
29
+
20
30
  specify "should be able to force expansion. force expanded attributes can never be collapsed. " do
21
31
  Wallet.restful_publish(:contents)
22
32
  Person.restful_publish(:name, :wallet, :current_location, { :pets => [:name, :species], :restful_options => { :force_expand => :wallet } })
@@ -4,7 +4,7 @@ context "restful publish" do
4
4
  teardown do
5
5
  reset_config
6
6
  end
7
-
7
+
8
8
  specify "should result in a method .published?(:attr_key) return true for published attributes" do
9
9
  Pet.restful_publish(:person_id, :name) # person_id gets converted to a link automagically.
10
10
 
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.10
4
+ version: 0.2.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Bornkessel