Skip to content

Instantly share code, notes, and snippets.

@shortly-portly
Last active December 28, 2019 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shortly-portly/bb6eb4db1eb0c5ea32dce047eddd9107 to your computer and use it in GitHub Desktop.
Save shortly-portly/bb6eb4db1eb0c5ea32dce047eddd9107 to your computer and use it in GitHub Desktop.
class Admin < BaseModel
table do
column name : String
column user_id : Int64
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.save_admin_op, user: operation
end
end
end
end
class Admins::New < BrowserAction
route do
foo = SignUpUser.new
html NewPage, operation: foo.save_admin_op, user: foo
end
end
class Admins::NewPage < MainLayout
needs operation : SaveAdmin
needs user : 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.name), &.text_input(autofocus: "true")
mount Shared::Field.new(@user.email), &.email_input(autofocus: "true")
mount Shared::Field.new(@user.password), &.password_input
mount Shared::Field.new(@user.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
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
@save_admin_op = SaveAdmin.new
def save_admin_op
@save_admin_op
end
def save_admin_op=(value)
@save_admin_op = value
end
private def create_admin(user : User)
SaveAdmin.create(params) do |operation, admin|
if !admin
save_admin_op = operation
AppDatabase.rollback
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment