Skip to content

Instantly share code, notes, and snippets.

Avatar

Paul Smith paulcsmith

View GitHub Profile
View Mount with block and m.cr
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 Mar 20, 2020
Invite user to team with Lucky
View actions.cr
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
View admin.cr
class Admin < BaseModel
table do
column name : String
belongs_to user : User
end
end
View lucky_serializer_example.cr
# 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 Apr 19, 2019
Lucky search form
View lucky.cr
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
View Rails to lucky.md

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
View Quickstart.md

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.

View session_form.cr
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
View d simplified.cr
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
View Params
Lucky::Params.new.set(:user, name: "paul")