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::Command
s.
Getting started
gem install sequent-sinatra
class MyApp < Sinatra::Base
register Sequent::Web::Sinatra::App
end
See 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
end
A minimal example of your initializers/sequent.rb
Sequent.configure do |config|
config.event_handlers = [MyEventHandler.new]
config.command_handlers = [MyCommandHandler.new]
end
Formhelpers
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
end
class SignupCommand < Sequent::Core::Command
attrs username: String
validates_presence_of :username
end
You 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_input
raw_checkbox
raw_password
raw_textarea
raw_hidden
raw_select
raw_radio
You can provide the following parameters to these helper methods
-
class
the css class -
value
default 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