21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/hippo/api/route_set.rb', line 21
def resources(model, options = {})
path = options[:path] || model.api_path
controller = options[:controller] || Hippo::API::GenericController
format = options[:format] || '.json'
if options[:under]
options[:parent_attribute] = options[:under].underscore.singularize+'_id'
end
prefix = options[:parent_attribute] ? options[:parent_attribute] + '/' : ''
valid_methods = []
bind = lambda{ |method, route, wrapper_method = method|
valid_methods.push(method)
self.send(method, route + format,
&RequestWrapper.send(wrapper_method, model, controller, options))
}
if controller.method_defined?(:show)
bind[:get, "#{prefix}#{path}/?:id?"]
end
if controller.method_defined?(:create)
bind[:post, "#{prefix}#{path}"]
end
unless options[:immutable]
if controller.method_defined?(:update)
bind[:patch, "#{prefix}#{path}/?:id?", :update]
bind[:put, "#{prefix}#{path}/?:id?", :update]
end
if controller.method_defined?(:destroy) and not options[:indestructible]
bind[:delete, "#{prefix}#{path}/?:id?"]
end
end
if options[:cors] && valid_methods.any?
cors = options[:cors].is_a?(Hash) ? otions[:cors] : {origins: options[:cors]}
cors_path = "#{prefix}#{path}/?:id?#{format}"
Hippo.logger.debug("CORS: #{cors_path} #{valid_methods}")
enable_cors cors_path, cors.merge(methods: valid_methods)
end
end
|