Created
December 9, 2015 17:19
-
-
Save davidpelaez/1a689b4e6dab87dce0d3 to your computer and use it in GitHub Desktop.
Reference steps to use Trailblazer inside a Lotus app. Only includes important parts for Lotus 0.5, not full files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# apps/web/application.rb | |
# include in your load paths whatever directories | |
# you want to use for creating your trailblazer | |
# related classes, this is just an example | |
load_paths << [ | |
'controllers', | |
'views', | |
'operations', | |
'policies' | |
] | |
# include in every action the controller helpers | |
# so that you have access to `run MyOperation`... | |
controller.prepare do | |
include ::Trailblazer::Operation::Controller | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# config/environment.rb | |
# Load the gems in your application config/environment.rb | |
require 'trailblazer' | |
require 'trailblazer/autoloading' | |
require 'lotus/validations' | |
require "reform/form/lotus" | |
# Make reform use lotus validations | |
Reform::Form.class_eval do | |
include Reform::Form::Lotus | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add this gems to your Gemfile | |
gem 'trailblazer', '~> 1.0.4' | |
gem 'lotus-validations', '~> 0.3.3' | |
gem 'hashie' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Web::Comment | |
class Create < Trailblazer::Operation | |
include Policy::Guard | |
policy do |params| | |
not params[:greeting].blank? | |
end | |
contract do | |
property :greeting, validates: {presence: true} | |
property :name | |
end | |
def process(params) | |
validate(params) do |pm| | |
pm.save and return | |
end | |
end | |
private | |
def setup_params!(params) | |
# Params are Lotus Params, not a hash | |
# we convert it to_h and used only symbols | |
# to access the fields, e.g. in the policy | |
@params = Hashie.symbolize_keys! params.to_h | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Web::Controllers::Root | |
class Index | |
include Web::Action | |
# give access to the view | |
# from there use the objects as any other in a view | |
expose :model, :operation | |
def call(params) | |
run Web::Comment::Create | |
rescue ::Trailblazer::NotAuthorizedError | |
unauthorized! | |
end | |
private | |
def unauthorized! | |
status 401, "UNAUTHORIZED" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment