19
20
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
|
# File 'lib/resource_helpers.rb', line 19
def self.resource(*resources)
options = resources.pop if resources.last.is_a?(Hash)
options ||= {}
resources.each do |resource|
resource = resource.to_s
klass = "backstage/#{resource}".constantize
view_path = options[:view_path] || resource.pluralize
get "/#{resource.pluralize}" do
@collection = klass.all
if html_requested?
haml :"#{view_path}/index"
else
content_type :json
collection_to_json( @collection )
end
end
get "/#{resource}/:name" do
@object = klass.find( Util.decode_name( params[:name] ) )
if html_requested?
haml :"#{view_path}/show"
else
content_type :json
object_to_json( @object )
end
end
(options[:actions] || []).each do |action|
post "/#{resource}/:name/#{action}" do
object = klass.find( Util.decode_name( params[:name] ) )
send_args = [action]
send_args << params if object.respond_to?( action ) && object.method( action ).arity == 1
action_response = object.__send__( *send_args )
if html_requested?
redirect_to object_path( object )
else
content_type :json
object_to_json( action_response )
end
end
end
end
end
|