./lib/lux/application/README.md in lux-fw-0.5.37 vs ./lib/lux/application/README.md in lux-fw-0.6.2

- old
+ new

@@ -1,59 +1,17 @@ -## Lux::Application - main application controller and router +## Lux.app (Lux::Application) -* can capture errors with `on_error` instance method +Main application controller and router + +* can capture errors with `rescue_from` class method * calls `before`, `routes` and `after` class filters on every request -* routes requests to controllers via `map`, `root` and `call` methods -### Instance methods - -#### root - -executes if nav.root is empty - -#### map - -map specific nav root to Controller and calls if root mathes - -for example if path is /blogs - -`map blogs: Main::BlogController` - -will call instance method call with @path expanded - -`Main::BlogController.new.call(*@path)` - -more examples - -* `map blog: BlogController` will call `BlogController.action(:blog)` -* `map blog: 'blog#single'` will call `BlogController.action(:single)` -* `map blog: -> { BlogController.custom(:whatever) }` - -### call - -Calls specific controller action inside call. - ```ruby - call 'main/links#index' - call [Main::LinksController, :index] - call -> { [200, {}, ['OK']]} -``` - -### Router example - -For Lux routing you need to know only few things - -* taget can be 5 object variants, look at root example -* "root" method calls object if nav.root is blank? -* "map" method calls object if nav.first == match -* "namespace" method accepts block that wraps map calls. - -```ruby Lux.app do def api_router - error :forbiden, 'Only POST requests are allowed' if Lux.prod? && !post? + error :forbiden, 'Only POST requests are allowed' if Lux.env.prod? && !post? Lux::Api.call nav.path end before do check_subdomain @@ -61,92 +19,91 @@ after do error 404 end + rescue_from :all do |error| + case error + when PG::ConnectionBad + # ... + when Lux::Error + # ... + else + end + ### - routes do |r| + routes do # we show on root method, that target can be multiple object types, 5 variants + # this target is valid target for any of the follwing methods: get, post, map, call, root root [RootController, :index] # calls RootController#index - root 'root#call' # calls RootController#call - root :call_root # calls "call_root" method in current scope - root 'root' # calls RootController#index - root 'root#foo' # calls RootController#foo + root 'root#index' # calls RootController#index + root 'root' # calls RootController#call - # we can route based on the user status - root User.current ? 'main/root' : 'guest' + root 'main/root' - # simple route - r.about 'static#about' + # simple route, only for GET + map.about 'static#about' if get? - # map "/api" to "api_router" method - r.api :api_router - # or - map api: :api_router - - # with MainController - # map MainController do - map 'main' do - map :search # map "/search" to MainController#search - map '/login' # map "/login" to MainController#login + # execute blok if request_type is POST + post? do + # map "/api" to "api_router" method + map.api :api_router + # or + map api: :api_router end # map "/foo/dux/baz" route to MainController#foo with params[:bar] == 'dux' map '/foo/:bar/baz' => 'main#foo' - # if method "city" in current scope returns true - namespace :city do + # call method "city_map", if it returns true, proceed + map :city do # call MainController#city if request.method == 'GET' - map 'main#city' if get? + map 'main#city' end # if we match '/foo' route - namespace 'foo' do + map 'foo' do # call MainController#foo with params[:bar] == '...' map '/baz/:bar' => 'main#foo' end end end ``` -### Router rescues example +#### Instance methods -```ruby -Lux.app do - def on_error error +* routes requests to controllers via `map`, `root` and `call` methods +* taget can be 3 object variants, look at the `call` example +* `map` maps requests to controller actions + * `map.about => 'main#about' if get?` -> map '/about' to `MainControler#about` if request is `GET` + * `map about: 'main#about'` -> map '/about' to `MainControler#about` +* `root` will call only for root + * `map.about => 'main#about' if get?` -> map '/about' to `MainControler#about` if request is `GET` + * `map about: 'main#about'` -> map '/about' to `MainControler#about` +* `call` calls specific controller action inside call - stops routing parsing + * `call 'main/links#index'` - call `Main::LinksController#index` + * `call [Main::LinksController, :index]` - call `Main::LinksController#index` + * `call -> { [200, {}, ['OK']]}` - return HTTP 200 - OK - message = case error - when PG::ConnectionBad - msg = error.message || 'DB connection error, please refresh page.' - msg = "PG: #{msg}" - Lux.logger(:db_error).error msg - msg +#### Class filters - when Lux::Error - # for handled errors - # show HTTP errors in a browser without a log - Main::RootController.action(:error, error) +There are a few route filtes +* `config` # pre boot app config +* `boot` # after rack app boot (web only) +* `info` # called by "lux config" cli +* `before` # before any page load +* `routes` # routes resolve +* `after` # after any page load +* `rescue_from` # on routing error - else - # raise errors in developmet - raise error if Lux.dev? - key = Lux.error.log error - "#{error.class}: #{error.message} \n\nkey: #{key}" - end +#### Router example - # use default error formater - Lux.error message - end -end +For Lux routing you need to know only few things -if Lux.prod? - Lux.config.error_logger = proc do |error| - # log and show error page in a production - key = SimpleException.log error - Lux.cache.fetch('error-mail-%s' % key) { Mailer.error(error, key).deliver } - Lux.logger(:exceptions).error [key, User.current.try(:email).or('guest'), error.message].join(' - ') - key - end -end -``` \ No newline at end of file +* `get?`, `post?`, `delete?`, ... will be true of false based HTTP_REQUEST type + * `get? { @exec_if_true }` works as well +* `map"`method accepts block that wraps map calls. + * `map :city do ...` will call `city_map` method. it has to return falsey if no match + * `map 'city' do ...` will check if we are under `/city/*` nav namespace +