restly 0.0.1.beta.5 → 0.0.1.beta.6
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/restly/associations/base/loaders.rb +8 -6
- data/lib/restly/base.rb +1 -0
- data/lib/restly/base/fields.rb +1 -1
- data/lib/restly/base/includes.rb +6 -1
- data/lib/restly/base/instance.rb +11 -5
- data/lib/restly/base/instance/persistence.rb +1 -1
- data/lib/restly/base/resource/finders.rb +3 -4
- data/lib/restly/nested_attributes.rb +4 -4
- data/lib/restly/version.rb +1 -1
- metadata +2 -2
@@ -26,14 +26,16 @@ module Restly::Associations::Base::Loaders
|
|
26
26
|
def load_instance(parent, association_class = self.association_class)
|
27
27
|
raise Restly::Error::AssociationError, "Not an instance" if collection?
|
28
28
|
return nil if embedded?
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
29
|
+
foreign_key = options[:foreign_key] || "#{name}_id"
|
30
|
+
instance = if parent.attributes.has_key? foreign_key
|
31
|
+
id = parent.attributes[foreign_key]
|
32
|
+
return nil unless id
|
33
|
+
association_class.find(id)
|
33
34
|
else
|
34
|
-
association_class.instance_from_response association_class.connection.get(association_class.
|
35
|
+
association_class.instance_from_response association_class.connection.get(association_class.path_with_format)
|
35
36
|
end
|
36
37
|
Restly::Proxies::Associations::Instance.new(instance, parent)
|
37
38
|
end
|
38
39
|
|
39
|
-
|
40
|
+
|
41
|
+
end
|
data/lib/restly/base.rb
CHANGED
data/lib/restly/base/fields.rb
CHANGED
data/lib/restly/base/includes.rb
CHANGED
@@ -2,7 +2,7 @@ module Restly::Base::Includes
|
|
2
2
|
extend ActiveSupport::Concern
|
3
3
|
|
4
4
|
included do
|
5
|
-
class_attribute :current_specification, instance_writer: false
|
5
|
+
class_attribute :finder, :current_specification, instance_writer: false
|
6
6
|
end
|
7
7
|
|
8
8
|
module ClassMethods
|
@@ -24,6 +24,11 @@ module Restly::Base::Includes
|
|
24
24
|
@client
|
25
25
|
end
|
26
26
|
|
27
|
+
def find_by(field)
|
28
|
+
raise Restly::Error::InvalidField, "#{field} was not a valid field." unless field.in? fields
|
29
|
+
self.finder = field
|
30
|
+
end
|
31
|
+
|
27
32
|
def has_specification
|
28
33
|
self.current_specification = Restly::Base::Resource::Specification.new(self)
|
29
34
|
|
data/lib/restly/base/instance.rb
CHANGED
@@ -71,11 +71,12 @@ module Restly::Base::Instance
|
|
71
71
|
end
|
72
72
|
|
73
73
|
def path
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
74
|
+
if @path
|
75
|
+
@path
|
76
|
+
elsif path_from_response
|
77
|
+
path_from_response
|
78
|
+
elsif send self.finder
|
79
|
+
[self.class.path, send self.finder].join('/')
|
79
80
|
else
|
80
81
|
self.class.path
|
81
82
|
end
|
@@ -99,6 +100,11 @@ module Restly::Base::Instance
|
|
99
100
|
end
|
100
101
|
end
|
101
102
|
|
103
|
+
def path_from_response
|
104
|
+
return nil unless response
|
105
|
+
response.response.env[:url].try(:path).try(:gsub, /\.\w+$/,'')
|
106
|
+
end
|
107
|
+
|
102
108
|
def parsed_response(response=self.response)
|
103
109
|
return {} unless response
|
104
110
|
parsed = response.parsed || {}
|
@@ -2,11 +2,10 @@ module Restly::Base::Resource::Finders
|
|
2
2
|
|
3
3
|
Collection = Restly::Collection
|
4
4
|
|
5
|
-
def find(
|
5
|
+
def find(finder, *args)
|
6
|
+
finder = nil unless self.finder.in? fields
|
6
7
|
options = args.extract_options!
|
7
|
-
|
8
|
-
#params[pagination_options[:params][:page]] = options[:page] if pagination
|
9
|
-
instance_from_response connection.get(path_with_format(id), params: params)
|
8
|
+
instance_from_response connection.get(path_with_format(finder), params: params)
|
10
9
|
end
|
11
10
|
|
12
11
|
def all
|
@@ -45,7 +45,7 @@ module Restly::NestedAttributes
|
|
45
45
|
|
46
46
|
if attributes_collection.is_a? Hash
|
47
47
|
keys = attributes_collection.keys
|
48
|
-
attributes_collection = if keys.include?(
|
48
|
+
attributes_collection = if keys.include?(self.finder.try(:to_s)) || keys.include?(self.finder.try(:to_sym))
|
49
49
|
Array.wrap(attributes_collection)
|
50
50
|
else
|
51
51
|
attributes_collection.values
|
@@ -57,9 +57,9 @@ module Restly::NestedAttributes
|
|
57
57
|
|
58
58
|
attributes_collection.each do |attributes|
|
59
59
|
attributes = attributes.with_indifferent_access
|
60
|
-
if attributes[
|
61
|
-
send(association_name) << association.build(self, attributes.except(
|
62
|
-
elsif (existing_record = existing_records.find{ |record| record.id.to_s == attributes[
|
60
|
+
if attributes[self.finder].blank?
|
61
|
+
send(association_name) << association.build(self, attributes.except(self.finder))
|
62
|
+
elsif (existing_record = existing_records.find{ |record| record.id.to_s == attributes[self.finder].to_s })
|
63
63
|
existing_record.attributes = attributes
|
64
64
|
end
|
65
65
|
end
|
data/lib/restly/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restly
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.beta.
|
4
|
+
version: 0.0.1.beta.6
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: oauth2
|