ruby - How to map routes to modules without the use of multiple Sinatra apps? -


i have structure:

 module analytics     def self.registered(app)        module departmentlevel         departmentparticipation = lambda         end         departmentstatistics = lambda         end          app.get '/participation', &departmentparticipation       end        module courselevel         courseparticipation = lambda         end       end     end 

and @ end of module analytics route each piece of request specific submodule. if requested

'analytics/department' 

it should redirect module departmentlevel has own routes as

app.get 'participation', &departmentparticipation 

i first thought on using map. how use without having run new or inherit sinatra::base object?

not sure if need, here's how build modular sinatra apps: using use

first, have applicationcontroller. it's base class other controllers. lives in controllers/application_controller.rb

class applicationcontroller < sinatra::base   # settings valid controllers   set :views, file.expand_path('../../views', __file__)   set :public_folder, file.expand_path('../../public', __file__)   enable :sessions    # helpers   helpers bootstraphelpers   helpers applicationhelpers   helpers databasehelpers    configure :production     enable :logging   end end 

now, other controllers/modules inherit applicationcontroller. example controllers/website_controller.rb:

require 'controllers/application_controller'

class websitecontroller < applicationcontroller   helpers websitehelpers    get('/') { slim :home }   get('/updates') { slim :website_updates }   get('/test') { binding.pry; 'foo' } if settings.development? end 

at last, in app.rb comes together:

# require stuff require 'yaml' require 'bundler' bundler.require require 'logger'  # require own stuff app_root = file.expand_path('..', __file__) $load_path.unshift app_root require 'lib/core_ext/string' require 'controllers/application_controller.rb'  # run-time configuration... applicationcontroller.configure   # db connections, logging , stuff end  # require models, controllers , helpers dir.glob("#{app_root}/{helpers,models,controllers}/*.rb").each { |file| require file }  # here glue class myapp < sinatra::base   use websitecontroller   use othercontroller   use thingcontroller    not_found     slim :'404'   end end 

with setup, need in config.ru is

require './app.rb' run myapp 

hope helps!


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -