Sequent-Sinatra
Use the sequent gem with the Sinatra web framework.
Provides functionality to initialize sequent and form helpers to bind forms to Sequent::Core::Commands.
Getting started
gem install sequent-sinatraclass MyApp < Sinatra::Base
register Sequent::Web::Sinatra::App
endSee the sample application for sequent-sinatra in action.
Documentation
Configuration
sequent_config_dir contains the location of initializers/sequent.rb file that is uses to initialize sequent.
By default it will use the value root configured in your sinatra app.
Example
class MyApp < Sinatra::Base
set :sequent_config_dir, "#{root}/config"
register Sequent::Web::Sinatra::App
endA minimal example of your initializers/sequent.rb
Sequent.configure do |config|
config.event_handlers = [MyEventHandler.new]
config.command_handlers = [MyCommandHandler.new]
endFormhelpers
Sequent sinatra provides basic form helpers to bind forms to Commands.
Example:
Given this application
class MyApp < Sinatra::Base
set :sequent_config_dir, "#{root}/config"
register Sequent::Web::Sinatra::App
get '/' do
@command = SignupCommand.new
erb :index
end
post '/' do
@command = CreateInvoiceCommand.from_params(params[:signup_command])
execute_command @command do |errors|
if errors
erb :index
else
redirect '/some-other-page'
end
end
end
endclass SignupCommand < Sequent::Core::Command
attrs username: String
validates_presence_of :username
endYou can use the form helpers as follows
<% html_form_for(@command, "/", :post) do |form| %>
<% form.fieldset(@command.class.to_s.underscore.to_sym) do |f| %>
<%= f.raw_input :username, class: "form-input" %>
<% end %>
<input type="submit" value="Save">
<% end %>This outputs to the following HTML
<form action="/" method="POST" role="form">
<input type="hidden" name="_csrf" value="MAF4FQZPM4lssJnB7nyn4UlEssTAQnbVVsMRdfmLcmY=" />
<input class="form-input" id="signup_command_username" name="signup_command[username]" type="text" />
<input type="submit" value="Save">
</form>Various for helpers exist
raw_inputraw_checkboxraw_passwordraw_textarearaw_hiddenraw_selectraw_radio
You can provide the following parameters to these helper methods
-
classthe css class -
valuedefault value of the field. This is used when the actual value isnil
Contributing
Fork and send pull requests
Running the specs
rspec
License
Sequent Sinatra is released under the MIT License