make_restful 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.
- data/lib/make_restful.rb +5 -1
- data/lib/make_restful/class_methods.rb +17 -2
- data/lib/make_restful/loaders.rb +42 -19
- data/lib/make_restful/rendering.rb +34 -4
- data/lib/make_restful/version.rb +1 -1
- metadata +2 -3
data/lib/make_restful.rb
CHANGED
@@ -14,7 +14,6 @@ module MakeRestful
|
|
14
14
|
autoload :PermissibleActions
|
15
15
|
|
16
16
|
extend Callbacks
|
17
|
-
include Loaders
|
18
17
|
include RestMethods
|
19
18
|
include Rendering
|
20
19
|
include Sugar
|
@@ -22,6 +21,9 @@ module MakeRestful
|
|
22
21
|
delegate :allow_method?, :allow_format?, :resource_name, :instance_name, to: :klass
|
23
22
|
|
24
23
|
included do
|
24
|
+
class_attribute :json_root_node, :collection_loader, :instance_loader, instance_writer: false
|
25
|
+
|
26
|
+
extend ClassMethods
|
25
27
|
|
26
28
|
class_attribute :allowed_actions, :allowed_formats, :finder, :pagination, :resource_class, instance_writer: false
|
27
29
|
attr_reader :resource, :instance, :collection
|
@@ -35,6 +37,8 @@ module MakeRestful
|
|
35
37
|
resource name.underscore.gsub(/(.*)_controller/,'\1').singularize rescue nil
|
36
38
|
find_by :id
|
37
39
|
|
40
|
+
include Loaders
|
41
|
+
|
38
42
|
end
|
39
43
|
|
40
44
|
def klass
|
@@ -10,9 +10,16 @@ module MakeRestful::ClassMethods
|
|
10
10
|
|
11
11
|
private
|
12
12
|
|
13
|
+
def json_collection_root_node(true_or_name=true)
|
14
|
+
self.json_root_node = if true_or_name === true
|
15
|
+
resource_name
|
16
|
+
elsif true_or_name.is_a?(Symbol) || true_or_name.is_a?(String)
|
17
|
+
true_or_name
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
13
21
|
def resource(klass)
|
14
22
|
|
15
|
-
before_filter :verify_method!
|
16
23
|
before_filter :load_resource
|
17
24
|
before_filter :load_collection, only: [:index]
|
18
25
|
before_filter :load_instance, only: [:show, :destroy, :update]
|
@@ -25,6 +32,14 @@ module MakeRestful::ClassMethods
|
|
25
32
|
self.finder = key.to_sym
|
26
33
|
end
|
27
34
|
|
35
|
+
def collection(&block)
|
36
|
+
self.collection_loader = block if block_given?
|
37
|
+
end
|
38
|
+
|
39
|
+
def instance(&block)
|
40
|
+
self.collection_loader = block if block_given?
|
41
|
+
end
|
42
|
+
|
28
43
|
def actions(*actions)
|
29
44
|
|
30
45
|
options = actions.extract_options!
|
@@ -46,7 +61,7 @@ module MakeRestful::ClassMethods
|
|
46
61
|
end
|
47
62
|
|
48
63
|
def paginates!(opts={})
|
49
|
-
opts.
|
64
|
+
opts.assert_valid_keys :node
|
50
65
|
self.pagination = opts
|
51
66
|
end
|
52
67
|
|
data/lib/make_restful/loaders.rb
CHANGED
@@ -1,4 +1,36 @@
|
|
1
1
|
module MakeRestful::Loaders
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
|
6
|
+
before_filter :verify_method!
|
7
|
+
before_filter :verify_format!
|
8
|
+
|
9
|
+
collection do
|
10
|
+
if pagination && !params.has_key?(:all)
|
11
|
+
paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || pagination[:per_page] || 25)
|
12
|
+
{ resource_name.to_sym => paginated_collection,
|
13
|
+
pagination: {
|
14
|
+
current_page: paginated_collection.current_page,
|
15
|
+
per_page: paginated_collection.per_page,
|
16
|
+
total_entries: paginated_collection.total_entries,
|
17
|
+
total_pages: paginated_collection.total_pages
|
18
|
+
}
|
19
|
+
}
|
20
|
+
else
|
21
|
+
@resource.all
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
instance do
|
26
|
+
if finder && @resource.respond_to?(:where)
|
27
|
+
@resource.where(finder.to_sym => finder_value).first
|
28
|
+
else
|
29
|
+
@resource.find(finder_value)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
2
34
|
|
3
35
|
private
|
4
36
|
|
@@ -16,21 +48,9 @@ module MakeRestful::Loaders
|
|
16
48
|
end
|
17
49
|
|
18
50
|
def load_collection
|
19
|
-
@collection =
|
20
|
-
paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || pagination[:per_page] || 25)
|
21
|
-
{ resource_name.to_sym => paginated_collection,
|
22
|
-
pagination: {
|
23
|
-
current_page: paginated_collection.current_page,
|
24
|
-
per_page: paginated_collection.per_page,
|
25
|
-
total_entries: paginated_collection.total_entries,
|
26
|
-
total_pages: paginated_collection.total_pages
|
27
|
-
}
|
28
|
-
}
|
29
|
-
else
|
30
|
-
@resource.all
|
31
|
-
end
|
51
|
+
@collection = instance_eval(&collection_loader)
|
32
52
|
|
33
|
-
|
53
|
+
unless @collection.present?
|
34
54
|
@response = { error: 'no results found' }
|
35
55
|
@status = :not_found
|
36
56
|
render(load_template: false) and return
|
@@ -43,11 +63,7 @@ module MakeRestful::Loaders
|
|
43
63
|
finder = opts[:finder] || send(:finder)
|
44
64
|
finder_value = params[finder]
|
45
65
|
|
46
|
-
@instance =
|
47
|
-
@resource.where(finder.to_sym => finder_value).first
|
48
|
-
else
|
49
|
-
@resource.find(finder_value)
|
50
|
-
end
|
66
|
+
@instance = instance_eval(&instance_loader)
|
51
67
|
eval "@#{instance_name} = @instance"
|
52
68
|
|
53
69
|
unless @instance
|
@@ -65,6 +81,13 @@ module MakeRestful::Loaders
|
|
65
81
|
render(load_template: false)
|
66
82
|
end
|
67
83
|
|
84
|
+
def verify_format!
|
85
|
+
return if allowed_formats.allowed?(request.format.symbol)
|
86
|
+
@response = { error: "Format:#{request.format} is unsupported" }
|
87
|
+
@status = :unsupported_media_type
|
88
|
+
render(load_template: false)
|
89
|
+
end
|
90
|
+
|
68
91
|
alias :verify_action! :verify_method!
|
69
92
|
|
70
93
|
end
|
@@ -9,14 +9,20 @@ module MakeRestful::Rendering
|
|
9
9
|
options[:load_template] = true if options[:load_template].nil?
|
10
10
|
cleaned_options = options.reject{ |k,v| [:load_template, :status].include?(k) }
|
11
11
|
|
12
|
-
super and return if (( template_present && options[:load_template]) || cleaned_options.present? )
|
12
|
+
super and return if (( template_present && options[:load_template]) || cleaned_options.present? )
|
13
13
|
|
14
14
|
# Set the response from the chain
|
15
|
-
response = @response
|
15
|
+
response = if @response.present?
|
16
|
+
@response
|
17
|
+
elsif @instance.present?
|
18
|
+
@instance
|
19
|
+
elsif @collection.present?
|
20
|
+
wrap_collection
|
21
|
+
end
|
16
22
|
|
17
23
|
respond_to do |format|
|
18
|
-
format.json { super json: response, status: @status, callback: ( params[:callback] if allowed_formats.allowed? :jsonp )}
|
19
|
-
format.xml { super xml: response, status: @status }
|
24
|
+
format.json { super json: response, status: @status, callback: ( params[:callback] if allowed_formats.allowed? :jsonp )}
|
25
|
+
format.xml { super xml: response, status: @status }
|
20
26
|
format.any do
|
21
27
|
head @status and return if @status
|
22
28
|
super
|
@@ -24,4 +30,28 @@ module MakeRestful::Rendering
|
|
24
30
|
end
|
25
31
|
end
|
26
32
|
|
33
|
+
def wrap_collection
|
34
|
+
|
35
|
+
response = if json_root_node
|
36
|
+
{ json_root_node.to_sym => @collection }
|
37
|
+
else
|
38
|
+
@collection
|
39
|
+
end
|
40
|
+
|
41
|
+
if @collection.respond_to? :current_page
|
42
|
+
|
43
|
+
pagination = {}
|
44
|
+
pagination[:current_page] = @collection.current_page if @collection.respond_to? :current_page
|
45
|
+
pagination[:per_page] = @collection.current_page if @collection.respond_to? :per_page
|
46
|
+
pagination[:total_entries] = @collection.total_entries if @collection.respond_to? :total_entries
|
47
|
+
pagination[:total_pages] = @collection.total_pages if @collection.respond_to? :total_pages
|
48
|
+
|
49
|
+
pagination = { klass.pagination[:node] => pagination } if klass.pagination[:node]
|
50
|
+
|
51
|
+
response.merge!(pagination)
|
52
|
+
end
|
53
|
+
|
54
|
+
response
|
55
|
+
end
|
56
|
+
|
27
57
|
end
|
data/lib/make_restful/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: make_restful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
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-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -107,4 +107,3 @@ signing_key:
|
|
107
107
|
specification_version: 3
|
108
108
|
summary: Enables rest on a resource
|
109
109
|
test_files: []
|
110
|
-
has_rdoc:
|