Skip to content

Instantly share code, notes, and snippets.

View lazaronixon's full-sized avatar

Lázaro Nixon lazaronixon

View GitHub Profile
@lazaronixon
lazaronixon / latest_by.rb
Last active April 24, 2025 19:16
has_one_latest_by
class Member
has_one :latest_activity, -> { from(Activity.latest_by_member, :activities) }, class_name: "Activity"
end
class Activity
scope :latest_by_member, -> { select("DISTINCT ON (member_id) *").order(:member_id, created_at: :desc) }
end
@lazaronixon
lazaronixon / linux.json
Last active April 28, 2025 02:57
zed_settings.json
{
"theme": "One Light",
"base_keymap": "Atom",
"buffer_font_family": "DejaVu Sans Mono",
"buffer_font_size": 16,
"ui_font_size": 16,
"auto_indent_on_paste": false,
"show_edit_predictions": false,
"show_whitespaces": "none",
"gutter": { "code_actions": false, "runnables": false, "breakpoints": false },
@lazaronixon
lazaronixon / positionable.rb
Created April 22, 2025 13:36
positionable.rb
module Element::Positionable
extend ActiveSupport::Concern
included do
before_create :set_position
before_update :reposition_before_update
validates_numericality_of :position, greater_than_or_equal_to: 1, on: :update
end
@lazaronixon
lazaronixon / _form.html.erb
Last active April 19, 2025 13:32
Hotwire Event-Driven Update Pattern
<%= form_with model: citizen, class: "card flex flex-col gap", data: { controller: "form" } do |form| %>
<div class="flex flex-col gap mb-2">
<div class="flex flex-col gap-half">
<% countries = Country.order(:name) %>
<%= label_tag :country_id, "Country", class: "text-sm font-medium leading-none" %>
<%= select_tag :country_id, options_from_collection_for_select(countries, :id, :name, citizen.country_id), include_blank: "Select one", class: "input", data: { action: "form#submit", form_submitter_param: "on_country_change" } %>
</div>
<div class="flex flex-col gap-half">
<% states = State.where(country_id: citizen.country_id).order(:name) %>
@lazaronixon
lazaronixon / tagbuilder.rb
Created September 9, 2024 08:09
TagBuilder
class TagBuilder
def initialize(context, tag_name, content = nil, klass: nil, default_attributes: {}, **attributes, &block)
@context = context
@tag_name = tag_name
@content = content
@klass = klass
@default_attributes = default_attributes
@attributes = attributes
@block = block
end
@lazaronixon
lazaronixon / tabs.css
Last active August 30, 2024 07:02
Tab bar rails
.tabs {
display: flex;
flex-direction: column;
gap: var(--size-2);
}
.tabs__list {
background-color: var(--color-border-light);
block-size: var(--size-10);
border-radius: var(--rounded-md);
@lazaronixon
lazaronixon / application_helper.rb
Last active August 14, 2024 11:33
Current page helpers
module ApplicationHelper
def current_page_with_controllers?(controllers)
controllers.any? { |controller| current_page? controller: controller }
end
def current_page_with_actions?(actions)
actions.any? { |action| current_page? action: action }
end
def current_page_with_controllers_and_actions?(controllers, actions)
<div data-controller="popover">
<button class="btn" popovertarget="popover" data-popover-target="button">
Open popover
</button>
<div popover id="popover" class="popover" data-popover-target="menu" data-action="beforetoggle->popover#update">
<%= render "dimensions_form" %>
</div>
</div>
@lazaronixon
lazaronixon / medical_record.rb
Last active June 22, 2024 17:01
Api upload
class MedicalRecord < ApplicationRecord
enum kind: %i[ imaging results notes ]
belongs_to :patient, class_name: "User", inverse_of: :medical_records
has_one_attached :document, dependent: :detach
scope :with_kind, -> (kind) { where kind: kind }
after_create_commit :deliver_updation
@lazaronixon
lazaronixon / database.yml
Last active May 28, 2024 01:50
config/initializers/sqlite3.rb
# config/database.yml
# SQLite. Versions 3.8.0 and up are supported.
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem "sqlite3"
#
default: &default
adapter: sqlite3