Module: CRUDRequest
- Included in:
- RequestHandler
- Defined in:
- lib/rack/requests/crud.rb
Instance Method Summary collapse
- #create_for(klass) ⇒ Object
- #delete_for(klass) ⇒ Object
- #edit_for(klass) ⇒ Object
- #get_for(klass) ⇒ Object
- #new_for(klass) ⇒ Object
- #put_for(klass) ⇒ Object
- #show_for(klass) ⇒ Object
Instance Method Details
#create_for(klass) ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/rack/requests/crud.rb', line 14 def create_for klass object = klass.create( @params ) return Response.ok if object.valid? Response.unprocessable_entity_for object.errors. end |
#delete_for(klass) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rack/requests/crud.rb', line 64 def delete_for klass object = klass.find( @url_params[ :id ]) if object.nil? Log.info "#{ __method__ } #{ klass } could not delete id #{ @url_params[ :id ]}" else object.delete end Response.ok end |
#edit_for(klass) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rack/requests/crud.rb', line 41 def edit_for klass id = @url_params[ :id ] attributes = klass.find( id ).attributes submit_method = "putObject('#{ klass.to_s.downcase }', '#{ id }')" data = { id:id, instance:attributes, submit_method:submit_method, header:"Edit #{ klass }" } Response.ok_for render( klass.to_s.downcase, :form, data ) end |
#get_for(klass) ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rack/requests/crud.rb', line 22 def get_for klass instances = klass.all .map( &:attributes ) .sort_by{| instance | instance[ :display_name ]} fields = klass.new.editable_fields count = instances.count data = { fields:fields, instances:instances, count:count } Response.ok_for render( klass.to_s.downcase, :all, data ) end |
#new_for(klass) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 |
# File 'lib/rack/requests/crud.rb', line 2 def new_for klass fields = klass.new.editable_fields attributes = Hash[ *fields.map{| f | [ f.to_sym, '' ]}.flatten ] submit_method = "postObject('#{ klass.to_s.downcase }')" data = { instance:attributes , submit_method:submit_method , header:"Create New #{ klass }" } Response.ok_for render( klass.to_s.downcase, :form, data ) end |
#put_for(klass) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/rack/requests/crud.rb', line 54 def put_for klass instance = klass.find( @url_params[ :id ]) if instance.update_attributes( @params ) return Response.ok end Response.unprocessable_entity_for instance.errors. end |