Skip to content

Instantly share code, notes, and snippets.

View coorasse's full-sized avatar
❤️
I will react...but slowly

Alessandro Rodi coorasse

❤️
I will react...but slowly
View GitHub Profile
@coorasse
coorasse / check_keys.rake
Created November 13, 2024 20:47
Ruby On Rails - Rake task to check for missing foreign keys
namespace :db do
desc "Check for missing foreign keys and generate migration commands"
task check_keys: :environment do |_task, _args|
Rails.application.eager_load!
schema = ApplicationRecord.connection
missing_foreign_keys = []
puts ApplicationRecord.descendants
# Loop through each ActiveRecord model
ApplicationRecord.descendants.each do |model|
@coorasse
coorasse / choices.scss
Created February 16, 2024 15:19
choices.js - bootstrap5 theme
@use "sass:color";
$choices-selector: "choices" !default;
$choices-font-size-lg: $font-size-lg !default;
$choices-font-size-md: $font-size-base !default;
$choices-font-size-sm: $font-size-sm !default;
$choices-border-radius: var(--bs-border-radius) !default;
$choices-border-radius-item: 20px !default;
$choices-bg-color: var(--bs-body-bg) !default;
$choices-bg-color-disabled: #eaeaea !default;
@coorasse
coorasse / sentry.rb
Last active November 22, 2023 14:24
Sentry - skip fast transactions
# /config/initializers/sentry.rb
config.before_send_transaction = lambda do |event, _hint|
relevant_span = event.spans.find { |s| s[:op] == 'view.process_action.action_controller' }
if relevant_span
sampling_config = { 50 => 1, 200 => 10, 500 => 20, 1000 => 50 }
duration_in_ms = (relevant_span[:timestamp] - relevant_span[:start_timestamp]) * 1000
sampling_percentage = sampling_config.
@coorasse
coorasse / data_json_dump.rb
Created November 17, 2021 11:24
Dump large table to JSON file in ruby
File.open(file_path, 'w') do |file|
file.write("[")
if records.any?
last_id = records.last.id
records.find_each do |model|
file.write(model.to_json(except: excluded_columns))
file.write(",\n") if model.id != last_id
end
end
file.write("]\n")
@coorasse
coorasse / _head.html.slim
Last active March 25, 2021 11:57
Use Font Awesome 5 with RailsAdmin
/app/views/layouts/rails_admin/_head.html.slim
/we override the default head to add our custom rails_admin stylesheet pack tag
meta[content="IE=edge" http-equiv="X-UA-Compatible"]
meta[content="text/html; charset=utf-8" http-equiv="Content-Type"]
meta[content="width=device-width, initial-scale=1" name="viewport; charset=utf-8"]
meta[content="NONE,NOARCHIVE" name="robots"]
= csrf_meta_tag
= stylesheet_link_tag "rails_admin/rails_admin.css", media: :all
= stylesheet_pack_tag "rails_admin", media: :all
= javascript_include_tag "rails_admin/rails_admin.js"
@coorasse
coorasse / unpermitted_parameters_listener.rb
Created October 20, 2020 15:39
A Rails controller concern to give hints to clients about unpermitted parameters.
module UnpermittedParametersListener
extend ActiveSupport::Concern
included do
before_action :instrument_listener_for_unpermitted_params
after_action :merge_unpermitted_params_hints
attr_reader :permitted_action_params
end
@coorasse
coorasse / json_api_public_exceptions.rb
Created October 20, 2020 15:17
A Rack Middleware to catch errors in Rails API
module ActionDispatch
class JsonApiPublicExceptions < PublicExceptions
def call(env)
request = ActionDispatch::Request.new(env)
status = request.path_info[1..].to_i
exception = env['action_dispatch.exception']
message = returned_message(exception, status)
attribute = exception.try(:param) || :base
code = convert_to_code(status)
@coorasse
coorasse / errors_mapper.rb
Created October 20, 2020 15:13
An error renderer for ActiveModel models errors
class ErrorsMapper
class << self
def call(model, *prefix)
model.errors.details.map { |attribute, detail| map_details(model, prefix, attribute, detail) }.flatten
end
def single(status: 422, attribute: :base, code: :invalid, message: I18n.t('activerecord.errors.messages.invalid'))
{ status: status, pointer: attribute, code: code, detail: message }
end
ListView(
children: <Widget>[
BeeHeader(i18n.t('inspections.contactSection')),
BeeInput(
i18n.t('inspections.firstName'),
validator: Validators.presence(i18n.t('inspections.firstNameEmpty')),
onSaved: (String value) {
beekeeper.firstname = value;
},
),
TextFormField(
onSaved: (String value) {
myModel.street = value;
},
),
TextFormField(
onSaved: (String value) {
myModel.houseNo = value;
},
),