sinatra-backstage 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8a336e2efd07826aee03db67b0fd4b2d36e05a8b
4
- data.tar.gz: ead15d642f67df09166af00a60f17552b2d5dcac
3
+ metadata.gz: 947f4e7b2f4016ed98e09fad8d85106becf578b5
4
+ data.tar.gz: 6dc8daa64880d9f682f28e5ee15171a7cf072f52
5
5
  SHA512:
6
- metadata.gz: 457071766152b00c10cdc876f7d09f12bd9cd7c9bce1c8545f12554e43c1ee8e3a624ce535bb88777620f733c6306a08fe121a741af1f2e1fbdf483a92f31294
7
- data.tar.gz: 8e60f5b03b02739e29fbdb1e51127930c5ffb381febfc0c0ad8e81ebeaa9fd874d3e33081dcf5a3b4c9b78525b47d75355929b4d9f79649f82f45e02c0abbc5d
6
+ metadata.gz: bf6fdb9a38234ae6901a7e8fc7fca1b10f88dbf282fcb9189c9a38b92a6006179c18f9d2f7e0fe195a0400267f0422bb8c831a35e8a4102a5d4f73ed8307168f
7
+ data.tar.gz: 4cd69a8db7d9b86c2b6551898a7ac8c60dbe80c8778e66bf82e8eb92774d83da53ab010a212f8076f21c38e5fc1d34b0b40a81dab8b93c3189663ceea34b5dca
@@ -9,17 +9,17 @@ module Sinatra
9
9
  controller.extend self
10
10
  end
11
11
 
12
- def rest_routes(klass, namespace)
12
+ def rest_routes(klass, namespace, display_list = false)
13
13
 
14
14
  ## Routes
15
15
  ### get list
16
16
  get namespace do
17
- @objects = klass.all params.inject({}){ | hash_sym, (key, val) | hash_sym[key.to_sym] = val; hash_sym }
17
+ @objects = rest_get_all klass, params
18
18
  request.accept.each do |type|
19
19
  # puts "type.to_s = #{type.to_s}"
20
20
  case type.to_s
21
21
  when 'text/html'
22
- halt eval "#{settings.render_engine} '#{klass.name.split('::').last.downcase}_list'"
22
+ halt eval "#{settings.render_engine} '#{demodulize(klass).downcase}/list'"
23
23
  when 'text/json', 'application/json'
24
24
  halt @objects.to_a.to_json
25
25
  end
@@ -27,12 +27,23 @@ module Sinatra
27
27
  error 406
28
28
  end
29
29
 
30
+ ### get add-form
31
+ get "#{namespace}/new" do
32
+ template = ["#{demodulize(klass).downcase}/new"]
33
+ if display_list
34
+ @objects = rest_get_all(klass)
35
+ template.unshift "#{demodulize(klass).downcase}/list"
36
+ end
37
+ eval "#{settings.render_engine} ['#{template.join('\',\'')}']"
38
+ end
39
+
30
40
  ### create object
31
- post namespace do
41
+ post "#{namespace}/new" do
32
42
  # puts "params = #{params}"
33
43
  # puts "-- rest_routes post namespace = #{namespace}"
34
44
  # puts "-- rest_routes post klass = #{klass}"
35
45
  # halt
46
+ @errors = []
36
47
  begin
37
48
  object = klass.create params[:object]
38
49
  redirect namespace
@@ -40,8 +51,21 @@ module Sinatra
40
51
  puts e.resource.errors.inspect
41
52
  e.resource.errors.each do |error|
42
53
  puts error.inspect
54
+ @errors << error
43
55
  end
56
+ @errors.flatten!
57
+ puts @errors.inspect
58
+ end
59
+ end
60
+
61
+ get "#{namespace}/:id" do |id|
62
+ @object = klass.get!(id)
63
+ template = ["#{demodulize(klass).downcase}/edit"]
64
+ if display_list
65
+ @objects = rest_get_all(klass)
66
+ template.unshift "#{demodulize(klass).downcase}/list"
44
67
  end
68
+ eval "#{settings.render_engine} ['#{template.join('\',\'')}']"
45
69
  end
46
70
 
47
71
  ### update object
@@ -58,6 +82,10 @@ module Sinatra
58
82
 
59
83
  end
60
84
 
85
+ def rest_get_all(klass, params = {})
86
+ klass.all params.inject({}){ | hash_sym, (key, val) | hash_sym[key.to_sym] = val; hash_sym }
87
+ end
88
+
61
89
  end
62
90
 
63
91
  def self.included(app)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra-backstage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexander Popov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-19 00:00:00.000000000 Z
11
+ date: 2015-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra