kelredd-resourceful 0.7.4 → 0.7.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.
@@ -12,6 +12,8 @@ module Resourceful
|
|
12
12
|
self.get_collection("#{findable_index}.#{format}", opts, findable_search, force)
|
13
13
|
when :first
|
14
14
|
self.get_collection("#{findable_index}.#{format}", opts, findable_search, force).first
|
15
|
+
when :last
|
16
|
+
self.get_collection("#{findable_index}.#{format}", opts, findable_search, force).last
|
15
17
|
else
|
16
18
|
self.get("#{findable_index}/#{id}.#{format}", opts, findable_search, force)
|
17
19
|
end
|
@@ -23,6 +25,9 @@ module Resourceful
|
|
23
25
|
def first
|
24
26
|
find(:first)
|
25
27
|
end
|
28
|
+
def last
|
29
|
+
find(:last)
|
30
|
+
end
|
26
31
|
|
27
32
|
def findable_index
|
28
33
|
raise NotImplementedError, "Findable expects a public class method 'findable_index'"
|
@@ -37,6 +42,54 @@ module Resourceful
|
|
37
42
|
|
38
43
|
module InstanceMethods
|
39
44
|
|
45
|
+
def save
|
46
|
+
super do |attribute_hash|
|
47
|
+
if new_record?
|
48
|
+
self.push_data('post', \
|
49
|
+
"#{self.class.findable_index}.#{self.class.format}", \
|
50
|
+
{:params => self.class.findable_default_opts.merge(push_data_params)}, \
|
51
|
+
nested_attr_hash(attribute_hash), \
|
52
|
+
self.class.findable_search \
|
53
|
+
)
|
54
|
+
else
|
55
|
+
self.push_data('put', \
|
56
|
+
"#{self.class.findable_index}/#{self.id}.#{self.class.format}", \
|
57
|
+
{:params => self.class.findable_default_opts.merge(push_data_params)}, \
|
58
|
+
nested_attr_hash(attribute_hash), \
|
59
|
+
self.class.findable_search \
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def destroy
|
66
|
+
super do |attribute_hash|
|
67
|
+
unless new_record?
|
68
|
+
self.push_data('delete', \
|
69
|
+
"#{self.class.findable_index}/#{self.id}.#{self.class.format}", \
|
70
|
+
{:params => self.class.findable_default_opts.merge(push_data_params)}, \
|
71
|
+
{} \
|
72
|
+
)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def push_data_params
|
78
|
+
# override to customize save url params
|
79
|
+
{}
|
80
|
+
end
|
81
|
+
|
82
|
+
def push_data_nesting
|
83
|
+
# default, override to customize, set to nil for push data hash style nesting
|
84
|
+
self.class.name.demodulize.underscore
|
85
|
+
end
|
86
|
+
|
87
|
+
private
|
88
|
+
|
89
|
+
def nested_attr_hash(attr_hash)
|
90
|
+
push_data_nesting ? {push_data_nesting => attr_hash} : attr_hash
|
91
|
+
end
|
92
|
+
|
40
93
|
end
|
41
94
|
|
42
95
|
def self.included(receiver)
|
@@ -35,6 +35,10 @@ module Resourceful
|
|
35
35
|
|
36
36
|
protected
|
37
37
|
|
38
|
+
def push_data(verb, path, opts, data, search=nil)
|
39
|
+
self.class.get_result_data(super(verb, path, opts, data), search)
|
40
|
+
end
|
41
|
+
|
38
42
|
def self.get_result_data(result, search)
|
39
43
|
hsh_keys = search.kind_of?(String) ? search.split(/\s+/) : nil
|
40
44
|
result_data = !hsh_keys.nil? && !hsh_keys.empty? && result.respond_to?(:get_value) ? result.get_value(hsh_keys) || result : result
|
@@ -22,6 +22,7 @@ module Resourceful
|
|
22
22
|
|
23
23
|
def should_be_findable(index)
|
24
24
|
should_have_class_methods :find
|
25
|
+
should_have_instance_methods :save, :destroy
|
25
26
|
klass = described_type
|
26
27
|
should "have '#{index}' as a findable index" do
|
27
28
|
assert_equal index, klass.findable_index, "#{klass} findable index is not '#{index}'"
|
data/lib/resourceful/version.rb
CHANGED