Created
November 20, 2018 21:51
-
-
Save scottymac/ca5235353b9c63b1ea066145645bf0d3 to your computer and use it in GitHub Desktop.
Lucky Framework (Crystal) Basic CRUD setup
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
# MODEL | |
class Foo < BaseModel | |
table :foos do | |
column name : String | |
end | |
end | |
# FORM | |
class FooForm < Foo::BaseForm | |
fillable name | |
def prepare | |
validate_required name | |
end | |
end | |
# ACTIONS | |
# index.cr | |
class Foos::Index < BrowserAction | |
route do | |
foos = FooQuery.new | |
render IndexPage, foos: foos | |
end | |
end | |
# show.cr | |
class Foos::Show < BrowserAction | |
route do | |
foo = FooQuery.new.find(id) | |
render ShowPage, foo: foo | |
end | |
end | |
# new.cr | |
class Foos::New < BrowserAction | |
route do | |
render NewPage, form: FooForm.new | |
end | |
end | |
# create.cr | |
class Foo::Create < BrowserAction | |
route do | |
FooForm.create(params) do |form, foo| | |
if foo | |
render Foos::ShowPage, foo: foo | |
else | |
render Foos::NewPage, form: form | |
end | |
end | |
end | |
end | |
# edit.cr | |
class Foos::Edit < BrowserAction | |
route do | |
foo = FooQuery.new.find(id) | |
render EditPage, foo: foo, form: FooForm.new(foo) | |
end | |
end | |
# update.cr | |
class Foos::Update < BrowserAction | |
route do | |
foo = FooQuery.new.find(id) | |
FooForm.update(foo, params) do |form, foo| | |
if form.saved? | |
render Foos::ShowPage, foo: foo | |
else | |
render Foos::EditPage, form: form, foo: foo | |
end | |
end | |
end | |
end | |
# delete.cr | |
class Foos::Delete < BrowserAction | |
route do | |
foo = VenueQuery.new.find(id) | |
foo.delete | |
redirect Foos::Index | |
end | |
end | |
# PAGES | |
# index_page.cr | |
class Foos::IndexPage < MainLayout | |
needs foos : FooQuery | |
def content | |
h1 "Foos" | |
ul do | |
@foos.each do |foo| | |
li do | |
link foo.name, to: Foos::Show.with(foo.id) | |
end | |
end | |
end | |
end | |
end | |
# show_page.cr | |
class Foos::ShowPage < MainLayout | |
needs foo : Foo | |
def content | |
h1 "Foo: #{@foo.name}" | |
link "EDIT", to: Foos::Edit.with(@foo.id) | |
link "DELETE", to: Foos::Delete.with(@foo.id) | |
end | |
end | |
# new_page.cr | |
class Foos::NewPage < MainLayout | |
needs form : FooForm | |
def content | |
h1 "New Foo" | |
render_foo_form(@form) | |
end | |
private def render_foo_form(f) | |
form_for Foo::Create do | |
field(f.name) { |i| text_input i } | |
submit "Save", flow_id: "save-button" | |
end | |
end | |
end | |
# edit_page.cr | |
class Foos::EditPage < MainLayout | |
needs foo : Foo | |
needs form : FooForm | |
def content | |
h1 "Edit Foo" | |
render_foo_form(@form) | |
end | |
private def render_foo_form(f) | |
form_for Foos::Update.with(@foo.id) do | |
field(f.name) { |i| text_input i } | |
submit "Save", flow_id: "save-button" | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found the Lucky Guides helpful, but very disjointed as someone coming from Rails who just wanted to get simple a CRUD/REST setup going. This took far too long to piece together. Hope it's helpful to someone else!