The Lotus gem wasn't released yet, but all the core frameworks are out at the version 0.1.0. This is a guide to build a full stack Lotus application today.
Clone this gist:
% git clone https://gist.github.com/9830002.git lotus
Setup:
% cd lotus && bash setup.sh
Run the specs:
% bundle exec rake
This guide has two files to look at:
lotus.rb
application.rb
The code in this file is a simplfied version of the gem that will be released in the near future.
Lotus' frameworks are known to have few conventions and they are agnostic by design. This characteristic let them to be used in already existing projects.
However, I want Lotus apps to be different. They will take small decisions for the developers, in order to help in their day to day work.
For this reason, Lotus gem brings some behind the scenes enhancements of their capabilities. It will inject glue code to make all the components to work together.
One case is Lotus::View.load!
: I don't expect developers to deal with this low level details, Lotus will do this job for you.
This is the meaning of the FullStackPatch
module, it adds some behaviors to Lotus::Action
.
Another convention that you will see is the matching between action and view names (see RenderingPolicy
).
Given a HomeController::Index
, Lotus expects to find Home::Index
view.
This mechanism may not match your thinking, that's why a Lotus::Application
let to inject this policy.
What you see here is the code of a Lotus application. Generally, all these classes are split across files, but here I kept them together to emphatize the matching parts.
HomeController::Index
is a typical example of Lotus::Controller.
When called, it sets the value of @planet
, it will be available accesible using #planet
or #exposures
.
The most important aspect of Home::Index
is #greet
.
The implementation of this method uses a concrete method #salutation
with something that cames from the action: #planet
.
That information can be accessed here because Action#to_rendering
returns a context that matches Lotus::View
needs (see Lotus::View::Rendering#render
).
If you try:
action = HomeController::Index.new
action.call({'HTTP_ACCEPT' => 'text/html'})
action.to_rendering # => { planet: 'World', format: :html }
That output is exactly what Home::Index.render
expects.
The rest of the file deals with loading mechanisms and a compatibility with Rack::Builder
.
Please notice that Application
constant is assigned as Capybara.app
in spec/spec_helper.rb
.
This makes possible to run features tests with RSpec.
If you have any question or feedback, please leave a comment here or ping me on Twitter: @jodosha.
Happy hacking!
The ordering in the default SUFFIX in Lotus::Router made me think that the recommended structure would be something like:
Which reminded me of that alternative file-structure where the MVC parts are grouped by resources (I can't find a link to the article in which I originally found this idea):
Would something like this be easily supported by Lotus?
And I can't help but wonder about naming collision for singleton routes between container module for views and the model itself (e.g. a post model would be in
Post
class, butPost
would also be a container module for view classes, right?