restpack-resource 0.1.4 → 0.1.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.
@@ -4,7 +4,6 @@ module RestPack
|
|
4
4
|
module Resource
|
5
5
|
module Pageable
|
6
6
|
def paged_resource(options = {})
|
7
|
-
p "paged_resource options: #{options}"
|
8
7
|
options.reverse_merge!(
|
9
8
|
:scope => self.all,
|
10
9
|
:page => 1,
|
@@ -23,7 +22,6 @@ module RestPack
|
|
23
22
|
protected
|
24
23
|
|
25
24
|
def get_paged_resource(options)
|
26
|
-
p "options: #{options}"
|
27
25
|
paged_models = get_paged_models(options)
|
28
26
|
|
29
27
|
paged_resource = {
|
@@ -4,22 +4,20 @@ module RestPack
|
|
4
4
|
module Service
|
5
5
|
|
6
6
|
def paged_resource(klass, params, options = {})
|
7
|
-
|
8
|
-
options[:
|
9
|
-
options[:
|
10
|
-
options[:
|
11
|
-
options[:sort_direction] ||= params[:sort_direction].to_sym if params[:sort_direction]
|
7
|
+
options[:page] ||= params['page'].to_i if params['page']
|
8
|
+
options[:includes] ||= extract_includes_from_params(params) unless params['includes'].nil?
|
9
|
+
options[:sort_by] ||= params['sort_by'].to_sym if params['sort_by']
|
10
|
+
options[:sort_direction] ||= params['sort_direction'].to_sym if params['sort_direction']
|
12
11
|
|
13
12
|
filters = extract_filters_from_params(klass, params)
|
14
|
-
p "paged_resource: filters: #{filters}"
|
15
13
|
options[:filters] ||= filters unless filters.empty?
|
16
14
|
|
17
15
|
klass.paged_resource(options)
|
18
16
|
end
|
19
17
|
|
20
18
|
def single_resource(klass, params, options = {})
|
21
|
-
options[:id] ||= params[
|
22
|
-
options[:includes] ||= extract_includes_from_params(params) unless params[
|
19
|
+
options[:id] ||= params['id'] if params['id']
|
20
|
+
options[:includes] ||= extract_includes_from_params(params) unless params['includes'].nil?
|
23
21
|
|
24
22
|
klass.single_resource(options)
|
25
23
|
end
|
@@ -27,12 +25,12 @@ module RestPack
|
|
27
25
|
private
|
28
26
|
|
29
27
|
def extract_filters_from_params(klass, params)
|
30
|
-
extracted = params.extract!(*klass.resource_filterable_by)
|
28
|
+
extracted = params.with_indifferent_access.extract!(*klass.resource_filterable_by)
|
31
29
|
extracted.delete_if { |k, v| v.nil? }
|
32
30
|
end
|
33
31
|
|
34
32
|
def extract_includes_from_params(params)
|
35
|
-
params[
|
33
|
+
params['includes'].split(',').map {|i| i.to_sym }
|
36
34
|
end
|
37
35
|
|
38
36
|
end
|
data/spec/service_spec.rb
CHANGED
@@ -12,52 +12,52 @@ describe RestPack::Service do
|
|
12
12
|
context "with params" do
|
13
13
|
it "extracts the page number" do
|
14
14
|
Artist.should_receive(:paged_resource).with({ :page => 66 })
|
15
|
-
service.paged_resource(Artist, {
|
15
|
+
service.paged_resource(Artist, { 'page' => '66' })
|
16
16
|
end
|
17
17
|
|
18
18
|
it "gives options[:page] precidence" do
|
19
19
|
Artist.should_receive(:paged_resource).with({ :page => 2 })
|
20
|
-
service.paged_resource(Artist, {
|
20
|
+
service.paged_resource(Artist, { 'page' => '66' }, { :page => 2 })
|
21
21
|
end
|
22
22
|
|
23
23
|
it "extracts the includes" do
|
24
24
|
Artist.should_receive(:paged_resource).with({ :includes => [:songs, :manager] })
|
25
|
-
service.paged_resource(Artist, {
|
25
|
+
service.paged_resource(Artist, { 'includes' => 'songs,manager' })
|
26
26
|
end
|
27
27
|
|
28
28
|
it "gives options[:includes] precidence" do
|
29
29
|
Artist.should_receive(:paged_resource).with({ :includes => [:specified] })
|
30
|
-
service.paged_resource(Artist, {
|
30
|
+
service.paged_resource(Artist, { 'includes' => 'songs,manager' }, { :includes => [:specified] })
|
31
31
|
end
|
32
32
|
|
33
33
|
it "extracts the sort_by" do
|
34
34
|
Artist.should_receive(:paged_resource).with({ :sort_by => :id })
|
35
|
-
service.paged_resource(Artist, {
|
35
|
+
service.paged_resource(Artist, { 'sort_by' => 'id' })
|
36
36
|
end
|
37
37
|
|
38
38
|
it "gives options[:sort_by] precidence" do
|
39
39
|
Artist.should_receive(:paged_resource).with({ :sort_by => :name })
|
40
|
-
service.paged_resource(Artist, {
|
40
|
+
service.paged_resource(Artist, { 'sort_by' => 'id' }, { :sort_by => :name })
|
41
41
|
end
|
42
42
|
|
43
43
|
it "extracts the sort_direction" do
|
44
44
|
Artist.should_receive(:paged_resource).with({ :sort_direction => :ascending })
|
45
|
-
service.paged_resource(Artist, {
|
45
|
+
service.paged_resource(Artist, { 'sort_direction' => 'ascending' })
|
46
46
|
end
|
47
47
|
|
48
48
|
it "gives options[:sort_direction] precidence" do
|
49
49
|
Artist.should_receive(:paged_resource).with({ :sort_direction => :descending })
|
50
|
-
service.paged_resource(Artist, {
|
50
|
+
service.paged_resource(Artist, { 'sort_direction' => 'ascending' }, { :sort_direction => :descending })
|
51
51
|
end
|
52
52
|
|
53
53
|
it "extracts the filters" do
|
54
|
-
Artist.should_receive(:paged_resource).with({ :filters => { :id => '
|
55
|
-
service.paged_resource(Artist, {
|
54
|
+
Artist.should_receive(:paged_resource).with({ :filters => { :id => '142857', :name => 'Radiohead' } })
|
55
|
+
service.paged_resource(Artist, { 'id' => '142857', 'name' => 'Radiohead', 'something' => 'Else' })
|
56
56
|
end
|
57
57
|
|
58
58
|
it "gives options[:filters] precidence" do
|
59
59
|
Artist.should_receive(:paged_resource).with({ :filters => { :id => '999' } })
|
60
|
-
service.paged_resource(Artist, {
|
60
|
+
service.paged_resource(Artist, { 'id' => '148258', 'name' => 'Radiohead', 'something' => 'Else' }, { :filters => { :id => '999' }})
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -71,12 +71,12 @@ describe RestPack::Service do
|
|
71
71
|
context "with params" do
|
72
72
|
it "extracts the includes" do
|
73
73
|
Artist.should_receive(:single_resource).with({ :includes => [:songs, :manager] })
|
74
|
-
service.single_resource(Artist, {
|
74
|
+
service.single_resource(Artist, { 'includes' => 'songs,manager' })
|
75
75
|
end
|
76
76
|
|
77
77
|
it "gives options[:includes] precidence" do
|
78
78
|
Artist.should_receive(:single_resource).with({ :includes => [:specified] })
|
79
|
-
service.single_resource(Artist, {
|
79
|
+
service.single_resource(Artist, { 'includes' => 'songs,manager' }, { :includes => [:specified] })
|
80
80
|
end
|
81
81
|
end
|
82
82
|
end
|