Skip to content

Instantly share code, notes, and snippets.

View dalezak's full-sized avatar

Dale Zak dalezak

View GitHub Profile
@dalezak
dalezak / cacheable.rb
Last active March 14, 2023 21:39
Rails Cacheable Concern
# Rails concern that tracks and purges cache keys to avoid using Rails.cache.delete_matched since its very costly on Redis.
#
# @countries = Country.query_cached('countries:index', "limit:#{@limit}", "offset:#{@offset}") do
# query.limit(@limit).offset(@offset).all.to_a
# end
module Cacheable
extend ActiveSupport::Concern
included do
before_save :purge_cache
@dalezak
dalezak / _quill.html.erb
Last active March 5, 2023 20:44
Using Tables In Quill.js With Rails and Stimulus
<%= form.hidden_field field, data: { quill: { target: "input" } } %>
<div style="min-height:500px;" data-quill-target="editor" data-action="click->quill#focus"></div>
@dalezak
dalezak / application_controller.rb
Last active February 23, 2022 11:31
Lazy Load CanCanCan Abilities In Rails
def current_ability
@current_ability ||= begin
current_ability = Ability.new(current_user)
controller_names.to_a.each do |controller_name|
model_name = controller_name.classify
model_ability = "#{model_name}Ability".constantize rescue nil
if model_ability.present? && model_abilities[model_ability].nil?
model_abilities[model_ability] = model_ability.new(current_user)
current_ability.merge(model_abilities[model_ability])
end
@dalezak
dalezak / _form.html.erb
Last active April 15, 2024 12:13
Stimulus.js Toggle Controller to show and hide form elements based on select value
<div class="form-group">
<%= form.label :type, "Type", class: "font-weight-bold" %>
<%= form.select :type, ['TextQuestion', 'UrlQuestion'], { include_blank: true }, { class: "form-control", data: { action: "input->toggle#changed", target: "toggle.select" } } %>
</div>
<div class="form-group">
<%= form.label :name, "Name", class: "font-weight-bold" %>
<%= form.text_field :name, class: "form-control" %>
</div>
<div class="form-group" data-target="toggle.element" data-values="UrlQuestion">
<%= form.label :url, "URL", class: "font-weight-bold" %>
@dalezak
dalezak / README.md
Last active September 11, 2023 09:51
Ionic Capacitor Resources Generator
  1. Run npm install cordova-res --save-dev
  2. Create 1024x1024px icon at resources/icon.png
  3. Create 2732x2732px splash at resources/splash.png
  4. Add "resources": "cordova-res ios && cordova-res android && node scripts/resources.js" to scripts in package.json
  5. Copy resources.js file to scripts/resources.js
  6. Run sudo chmod -R 777 scripts/resources.js
  7. Run npm run resources