Class: RaketaAdmin::SinatraAdapter
- Inherits:
-
Object
- Object
- RaketaAdmin::SinatraAdapter
- Defined in:
- lib/raketa_admin/sinatra_adapter.rb
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#handler ⇒ Object
readonly
Returns the value of attribute handler.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#resource ⇒ Object
readonly
Returns the value of attribute resource.
Class Method Summary collapse
Instance Method Summary collapse
- #define_route(http_method, path, action) ⇒ Object
- #generate_routes ⇒ Object
-
#initialize(app, resource, handler, parent = nil) ⇒ SinatraAdapter
constructor
A new instance of SinatraAdapter.
- #process_filters(ctx, filter_type, action) ⇒ Object
- #resource_path(action) ⇒ Object
Constructor Details
#initialize(app, resource, handler, parent = nil) ⇒ SinatraAdapter
Returns a new instance of SinatraAdapter.
29 30 31 32 33 34 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 29 def initialize(app, resource, handler, parent = nil) @app = app @resource = resource @handler = handler @parent = parent end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
27 28 29 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 27 def app @app end |
#handler ⇒ Object (readonly)
Returns the value of attribute handler.
27 28 29 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 27 def handler @handler end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
27 28 29 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 27 def parent @parent end |
#resource ⇒ Object (readonly)
Returns the value of attribute resource.
27 28 29 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 27 def resource @resource end |
Class Method Details
.generate_routes(app, resource, handler, parent = nil) ⇒ Object
22 23 24 25 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 22 def self.generate_routes(app, resource, handler, parent = nil) router = new(app, resource, handler, parent) router.generate_routes end |
Instance Method Details
#define_route(http_method, path, action) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 52 def define_route(http_method, path, action) # Action methods are defined on the "app" and the self changes inside the blocks. # Let's keep a reference to the router router = self app.send(http_method.downcase, "/#{path}") do @ctx = RaketaContext.new(router.resource, params, request, response, router.parent) router.process_filters(@ctx, :before, action) result = router.handler.send(action, @ctx) router.process_filters(@ctx, :after, action) if result[:redirect] redirect result[:redirect] else erb :"#{router.handler.type}/#{action}", layout: :base_layout end end end |
#generate_routes ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 42 def generate_routes define_route('GET', resource_path(:index), :index) define_route('GET', "#{resource_path(:new)}/new", :create_form) define_route('GET', "#{resource_path(:show)}/:id", :show) define_route('GET', "#{resource_path(:edit)}/:id/edit", :edit_form) define_route('POST', resource_path(:create), :create) define_route('PUT', "#{resource_path(:update)}/:id", :update) define_route('DELETE', "#{resource_path(:delete)}/:id", :destroy) end |
#process_filters(ctx, filter_type, action) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 71 def process_filters(ctx, filter_type, action) filters = handler.send("#{filter_type}_filter", ctx, action) filters.each do |filter| result = filter.call(ctx) halt 302, { 'Location' => result[:redirect] } if result.is_a?(Hash) && result[:redirect] end end |
#resource_path(action) ⇒ Object
36 37 38 39 40 |
# File 'lib/raketa_admin/sinatra_adapter.rb', line 36 def resource_path(action) return resource unless parent %i[index new create].include?(action) ? "#{parent}/:#{parent.singularize}_id/#{resource}" : resource end |