Module: Sinatra::Backstage::REST::Routing

Defined in:
lib/sinatra/backstage/rest/rest_routes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(controller) ⇒ Object



16
17
18
# File 'lib/sinatra/backstage/rest/rest_routes.rb', line 16

def self.included(controller)
	controller.extend self
end

Instance Method Details

#rest_get_all(klass, params = {}) ⇒ Object



103
104
105
# File 'lib/sinatra/backstage/rest/rest_routes.rb', line 103

def rest_get_all(klass, params = {})
	klass.all params.to_sym_keys
end

#rest_routes(klass, namespace, display_list = false) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/sinatra/backstage/rest/rest_routes.rb', line 20

def rest_routes(klass, namespace, display_list = false)

	## Routes
	### get list
	get namespace do
		## get format
		# puts "-- REST.Routing get namespace ( params = #{params})"
		format = params.delete('format') || 'html'
		# puts "-- REST.Routing get namespace after format ( params = #{params})"
		format = 'json' if request.accept.include_any?('text/json', 'application/json')
		## get json options
		json_method = params.delete('json_method')
		json_opts = {:methods => ([json_method] || [])}
		## get objects
		@objects = rest_get_all klass, params
		# puts "-- REST.Routing get namespace ( @objects = #{@objects})"
		# puts "-- REST.Routing get namespace ( @objects.to_a = #{@objects.to_a})"
		# puts "-- REST.Routing get namespace ( @objects.to_json = #{@objects.to_json})"
		# puts "-- REST.Routing get namespace ( @objects.to_a.to_json = #{@objects.to_a.to_json})"
		## return
		case format
		when 'html'
			halt eval "#{settings.render_engine} '#{klass.demodulize.downcase}/list'"
		when 'json'
			halt @objects.to_json(json_opts)
		end
		error 406
	end

	### get add-form
	get "#{namespace}/new" do
		template = ["#{klass.demodulize.downcase}/new"]
		if display_list
			@objects = rest_get_all(klass)
			template.unshift "#{klass.demodulize.downcase}/list"
		end
		eval "#{settings.render_engine} ['#{template.join('\',\'')}']"
	end

	### create object
	post "#{namespace}/new" do
		# puts "params = #{params}"
		# puts "-- rest_routes post namespace = #{namespace}"
		# puts "-- rest_routes post klass = #{klass}"
		# halt
		@errors = []
		begin
			object = klass.create params[:object]
			redirect namespace
		rescue DataMapper::SaveFailureError => e
			puts e.resource.errors.inspect
			e.resource.errors.each do |error|
				puts "-- REST.Routing new ( error = #{error} )"
				@errors << error
			end
			@errors.flatten!
		end
	end

	get "#{namespace}/:id" do |id|
		@object = klass.get!(id)
		template = ["#{klass.demodulize.downcase}/edit"]
		if display_list
			@objects = rest_get_all(klass)
			template.unshift "#{klass.demodulize.downcase}/list"
		end
		eval "#{settings.render_engine} ['#{template.join('\',\'')}']"
	end

	### update object
	put "#{namespace}/:id" do |id|
		klass.get!(id).update params[:object]
		redirect (display_list ? "#{namespace}/#{id}" : namespace)
	end

	### delete object
	delete "#{namespace}/:id" do |id|
		klass.get!(id).destroy
		redirect namespace
	end

end