Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active April 19, 2019 19:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulcsmith/8cf03926cd9265ba095e2c976f39001f to your computer and use it in GitHub Desktop.
Save paulcsmith/8cf03926cd9265ba095e2c976f39001f to your computer and use it in GitHub Desktop.
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
def submit
yield self, candidates
end
private def candidates
CandidateQuery.new.tap do |candidate_query|
query.value.try do |query_string|
candidate_query.search(query_string)
end
end
end
end
class CandidateQuery < Candidate::BaseQuery
def search(query : String)
name.ilike("%#{query}%")
end
end
class Candidates::IndexPage < MainLayout
needs candidates : CandidateQuery
needs search_form : CandidateSearchForm
quick_def page_title, "Home"
def content
render_search_form
render_candidates
end
private def render_search_form
form_for Candidates::Searches::Show, class: "w-3/5 lg:w-2/5" do
text_input \
@search_form.query,
placeholder: "Search candidates...",
class: "bg-grey-lightest appearance-none border-2 border-grey-light rounded w-full -ml-1 p-3 text-grey-darker leading-tight focus:outline-none focus:bg-white focus:border-blue"
end
end
def render_candidates
div class: "table w-full" do
@candidates.each do |candidate|
link Candidates::Show.with(candidate), class: "table-row no-underline text-grey-darker hover:bg-grey-lighter" do
div class: "table-cell w-1/2 px-10 py-3 font-bold" do
text candidate.name
end
div class: "table-cell w-1/2 px-10 py-3" do
text candidate.github_username
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment