Skip to content

Instantly share code, notes, and snippets.

@davidpelaez
Created December 9, 2015 17:19
Show Gist options
  • Save davidpelaez/1a689b4e6dab87dce0d3 to your computer and use it in GitHub Desktop.
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.
# 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
# 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
# add this gems to your Gemfile
gem 'trailblazer', '~> 1.0.4'
gem 'lotus-validations', '~> 0.3.3'
gem 'hashie'
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
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