Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Forked from shortly-portly/admin.cr
Last active December 28, 2019 21:32
Show Gist options
  • Save paulcsmith/92610c9a48bc3c763534504cd402297c to your computer and use it in GitHub Desktop.
Save paulcsmith/92610c9a48bc3c763534504cd402297c to your computer and use it in GitHub Desktop.
class Admin < BaseModel
table do
column name : String
belongs_to user : User
end
end
Compiling...
Done compiling
Invalid memory access (signal 11) at address 0x0
[0x5576e3726776] *CallStack::print_backtrace:Int32 +118
[0x5576e36eb559] __crystal_sigfault_handler +217
[0x7fc6754de890] ???
[0x5576e36d6624] __crystal_main +14212
[0x5576e3a4c476] *Crystal::main_user_code<Int32, Pointer(Pointer(UInt8))>:Nil +6
[0x5576e3a4c3d9] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +41
[0x5576e36e2836] main +6
[0x7fc674887b97] __libc_start_main +231
[0x5576e36d2dba] _start +42
[0x0] ???
class Admins::Create < BrowserAction
route do
SignUpUser.create(params) do |operation, admin|
if admin
flash.success = "The record has been saved"
redirect Show.with(admin.id)
else
flash.failure = "It looks like the form is not valid"
html NewPage, operation: operation
end
end
end
end
class Admins::New < BrowserAction
route do
html NewPage, operation: SignUpUser.new
end
end
class Admins::NewPage < MainLayout
needs operation : SignUpUser
quick_def page_title, "New"
def content
h1 "New"
render_admin_form(@operation)
end
def render_admin_form(op)
form_for Admins::Create do
mount Shared::Field.new(op.save_admin_op.name), &.text_input(autofocus: "true")
mount Shared::Field.new(op.email), &.email_input(autofocus: "true")
mount Shared::Field.new(op.password), &.password_input
mount Shared::Field.new(op.password_confirmation), &.password_input
submit "Save", data_disable_with: "Saving..."
end
end
end
class SignUpUser < User::SaveOperation
param_key :user
# Change password validations in src/operations/mixins/password_validations.cr
include PasswordValidations
@_save_admin_op : SaveAdmin?
def save_admin_op
@_save_admin_op ||= SaveAdmin.new(params)
end
permit_columns email
attribute password : String
attribute password_confirmation : String
before_save do
validate_uniqueness_of email
Authentic.copy_and_encrypt password, to: encrypted_password
end
after_save create_admin
private def create_admin(user : User)
save_admin_op.user_id.value = user.id
if !save_admin_op.save
database.rollback
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment