Skip to content

Instantly share code, notes, and snippets.

View paulcsmith's full-sized avatar

Paul Smith paulcsmith

View GitHub Profile
def mount(component, *args, **named)
component.new(*args, **named).render do |*yield_args|
yield *yield_args
end
end
class Component
def initialize(@name : String)
end
@paulcsmith
paulcsmith / actions.cr
Last active March 20, 2020 13:55
Invite user to team with Lucky
class UserInvitations::New < BrowserAction
get "/teams/:team_id/user_invitations/new" do
html NewPage, operation: InviteUser.new
end
end
class UserInvitations::Create < BrowserAction
post "/teams/:team_id/user_invitations" do
team = TeamQuery.new.team_id(team_id).find
@paulcsmith
paulcsmith / admin.cr
Last active December 28, 2019 21:32 — forked from shortly-portly/admin.cr
class Admin < BaseModel
table do
column name : String
belongs_to user : User
end
end
# src/actions/rss/show.cr
#...
xml ArticlesXmlSerializer.new(articles, title: "Feed Name", description: "Here's the descr", path: request.path).render
#...
# src/actions/xml_action.cr
abstract class XMLAction < Lucky::Action
private def xml(body : String)
Lucky::TextResponse.new(context, content_type: "text/xml; charset=utf-8", body: body, status: 200)
end
@paulcsmith
paulcsmith / lucky.cr
Last active April 19, 2019 19:05
Lucky search form
class Candidates::Searches::Show < BrowserAction
get "/candidates/search" do
CandidateSearchForm.new(params).submit do |form, candidates|
render Candidates::IndexPage, search_form: form, candidates: candidates
end
end
end
class CandidateSearchForm < Avram::VirtualForm
virtual query : String

From Rails to Lucky (Guide)

#lucky

Controllers and routing

In Rails you have controllers, and controllers have actions. In Lucky, we just have “Actions.” Another different is that Lucky handles routing in the action instead of in a different routes file

What’s different?

  • Instead of a controller class with methods for actions, Lucky just has “actions” that are single classes. E.g. (UsersController#index in Rails is Users::Index in Lucky)
  • Lucky defines routes in the action, not in a separate routes file

Guides: Quickstart - build a discussion forum

#lucky

First things first

  • If you want to follow along, install Lucky
  • Learn some Crystal from the official docs if you’re not already familiar

You can also just read through this to see if Lucky looks interesting.

class SessionForm < LuckyRecord::VirtualForm
virtual email : String
virtual password : String
def submit
user = find_user
validate_required email, password
validate_user_and_password_match(user)
yield self, user
d "font-small justify-content" do
render_stuff
end
# If you need an id, pass it as an extra arg. I think it is a bit more rare to use id so this might be ok for most people
d "my-class", id: "ok-fine-i-need-an-id" do
end
private def d(classes : String, **other_args)
div(class: classes, **other_args) do
@paulcsmith
paulcsmith / Params
Created November 5, 2018 02:04
Params
Lucky::Params.new.set(:user, name: "paul")