lib/ditty/controllers/users.rb in ditty-0.4.0 vs lib/ditty/controllers/users.rb in ditty-0.4.1
- old
+ new
@@ -6,10 +6,12 @@
require 'ditty/models/identity'
require 'ditty/policies/identity_policy'
module Ditty
class Users < Ditty::Component
+ SEARCHABLE = %i[name surname email].freeze
+
set model_class: User
set track_actions: true
def find_template(views, name, engine, &block)
super(views, name, engine, &block) # Root
@@ -18,15 +20,11 @@
# New
get '/new' do
authorize settings.model_class, :create
- locals = {
- title: heading(:new),
- entity: User.new,
- identity: Identity.new
- }
+ locals = { title: heading(:new), entity: User.new, identity: Identity.new }
haml :"#{view_location}/new", locals: locals
end
# Create
post '/' do
@@ -41,12 +39,20 @@
user = locals[:user] = User.new(user_params)
identity = locals[:identity] = Identity.new(identity_params)
DB.transaction(isolation: :serializable) do
- identity.save # Will trigger a Sequel::ValidationFailed exception if the model is incorrect
+ begin
+ identity.save
+ rescue Sequel::ValidationFailed
+ raise unless request.accept? 'text/html'
+ status 400
+ locals = { title: heading(:new), entity: user, identity: identity }
+ return haml(:"#{view_location}/new", locals: locals)
+ end
user.save
user.add_identity identity
+
if roles
roles.each do |role_id|
user.add_role(role_id) unless user.roles.map(&:id).include? role_id.to_i
end
end