rack-crud 0.0.4 → 0.0.6
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.
- checksums.yaml +4 -4
- data/lib/rack/app/request.rb +26 -0
- data/lib/rack/app/response.rb +19 -0
- data/lib/rack/crud.rb +4 -0
- data/lib/rack/models/crud_model.rb +13 -0
- data/lib/rack/requests/crud.rb +75 -0
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b963945623b1f520255e729f4d491267548bce1f
|
4
|
+
data.tar.gz: 126c9dace6be448ae48b316a288dbf00c1f0aa35
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fc7bd90a0cabe43c3a909b46285a6594aa4854d40273cfca468116a891ef60bd008a24cef88f34a70b61011544d16ed32f451db75ef6af69673057da4cec01d1
|
7
|
+
data.tar.gz: 663176b11322e4c89c0682f8a644f31d832674eb934fd9d2f0510fa71d6250ba7c73516814a71e06275d71fac5e8bdcf019398dbf675b53953031456c609d259
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Request
|
2
|
+
def initialize env
|
3
|
+
@env = env
|
4
|
+
@request = Rack::Request.new( env )
|
5
|
+
@params = params_for( env )
|
6
|
+
end
|
7
|
+
|
8
|
+
def params_for env
|
9
|
+
case env[ 'REQUEST_METHOD' ].to_sym
|
10
|
+
when :GET
|
11
|
+
Rack::Utils.parse_nested_query( env[ 'QUERY_STRING' ])
|
12
|
+
when :POST, :PUT
|
13
|
+
body = @request.body.read.to_s
|
14
|
+
|
15
|
+
Rack::Utils.parse_nested_query( body ).symbolize_keys
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def response
|
20
|
+
routing = Rack::Routing::Router.for( @env )
|
21
|
+
@url_params = routing[ :params ]
|
22
|
+
|
23
|
+
rh = RequestHandler.new( @env, @params, @url_params )
|
24
|
+
rh.send routing[ :method ]
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Response
|
2
|
+
class << self
|
3
|
+
def ok
|
4
|
+
Rack::Response.new '', 200
|
5
|
+
end
|
6
|
+
|
7
|
+
def ok_for body
|
8
|
+
Rack::Response.new body, 200
|
9
|
+
end
|
10
|
+
|
11
|
+
def not_found_for body
|
12
|
+
Rack::Response.new body, 404
|
13
|
+
end
|
14
|
+
|
15
|
+
def unprocessable_entity_for body
|
16
|
+
Rack::Response.new body, 422
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
data/lib/rack/crud.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
module CRUDModel
|
2
|
+
def editable_fields
|
3
|
+
editable = fields.map( &:first )
|
4
|
+
|
5
|
+
editable.delete( '_id' )
|
6
|
+
editable.delete( '_type' )
|
7
|
+
editable.delete( 'connection_id' )
|
8
|
+
editable.delete( 'created_at' )
|
9
|
+
editable.delete( 'updated_at' )
|
10
|
+
|
11
|
+
editable.map( &:to_sym )
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module CRUDRequest
|
2
|
+
def new_for klass
|
3
|
+
fields = klass.new.editable_fields
|
4
|
+
attributes = Hash[ *fields.map{| f | [ f.to_sym, '' ]}.flatten ]
|
5
|
+
|
6
|
+
submit_method = "postObject('#{ klass.to_s.downcase }')"
|
7
|
+
data = { instance:attributes ,
|
8
|
+
submit_method:submit_method ,
|
9
|
+
header:"Create New #{ klass }" }
|
10
|
+
|
11
|
+
Response.ok_for render( klass.to_s.downcase, :form, data )
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_for klass
|
15
|
+
object = klass.create( @params )
|
16
|
+
|
17
|
+
return Response.ok if object.valid?
|
18
|
+
|
19
|
+
Response.unprocessable_entity_for object.errors.full_messages
|
20
|
+
end
|
21
|
+
|
22
|
+
def get_for klass
|
23
|
+
instances = klass.all
|
24
|
+
.map( &:attributes )
|
25
|
+
.sort_by{| instance | instance[ :display_name ]}
|
26
|
+
|
27
|
+
fields = klass.new.editable_fields
|
28
|
+
count = instances.count
|
29
|
+
data = { fields:fields, instances:instances, count:count }
|
30
|
+
Response.ok_for render( klass.to_s.downcase, :all, data )
|
31
|
+
end
|
32
|
+
|
33
|
+
def show_for klass
|
34
|
+
attributes = klass.find( @url_params[ :id ]).attributes
|
35
|
+
|
36
|
+
data = { id:attributes[ '_id' ], instance:attributes }
|
37
|
+
|
38
|
+
Response.ok_for render( klass.to_s.downcase, :show, data )
|
39
|
+
end
|
40
|
+
|
41
|
+
def edit_for klass
|
42
|
+
id = @url_params[ :id ]
|
43
|
+
attributes = klass.find( id ).attributes
|
44
|
+
|
45
|
+
submit_method = "putObject('#{ klass.to_s.downcase }', '#{ id }')"
|
46
|
+
data = { id:id,
|
47
|
+
instance:attributes,
|
48
|
+
submit_method:submit_method,
|
49
|
+
header:"Edit #{ klass }" }
|
50
|
+
|
51
|
+
Response.ok_for render( klass.to_s.downcase, :form, data )
|
52
|
+
end
|
53
|
+
|
54
|
+
def put_for klass
|
55
|
+
instance = klass.find( @url_params[ :id ])
|
56
|
+
|
57
|
+
if instance.update_attributes( @params )
|
58
|
+
return Response.ok
|
59
|
+
end
|
60
|
+
|
61
|
+
Response.unprocessable_entity_for instance.errors.full_messages
|
62
|
+
end
|
63
|
+
|
64
|
+
def delete_for klass
|
65
|
+
object = klass.find( @url_params[ :id ])
|
66
|
+
|
67
|
+
if object.nil?
|
68
|
+
Log.info "#{ __method__ } #{ klass } could not delete id #{ @url_params[ :id ]}"
|
69
|
+
else
|
70
|
+
object.delete
|
71
|
+
end
|
72
|
+
|
73
|
+
Response.ok
|
74
|
+
end
|
75
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-crud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- George Ulmer
|
@@ -100,8 +100,12 @@ executables: []
|
|
100
100
|
extensions: []
|
101
101
|
extra_rdoc_files: []
|
102
102
|
files:
|
103
|
+
- lib/rack/app/request.rb
|
103
104
|
- lib/rack/app/request_handler.rb
|
105
|
+
- lib/rack/app/response.rb
|
104
106
|
- lib/rack/crud.rb
|
107
|
+
- lib/rack/models/crud_model.rb
|
108
|
+
- lib/rack/requests/crud.rb
|
105
109
|
homepage: http://rubygems.org/gems/rack-crud
|
106
110
|
licenses:
|
107
111
|
- MIT
|