Skip to content

Instantly share code, notes, and snippets.

@lazybios
lazybios / custom_logger.rb
Created May 6, 2022 10:31 — forked from kinopyo/custom_logger.rb
Custom logger file in Rails
# lib/custom_logger.rb
class CustomLogger < Logger
def format_message(severity, timestamp, progname, msg)
"#{timestamp.to_formatted_s(:db)} #{severity} #{msg}\n"
end
end
logfile = File.open("#{Rails.root}/log/custom.log", 'a') # create log file
logfile.sync = true # automatically flushes data to file
CUSTOM_LOGGER = CustomLogger.new(logfile) # constant accessible anywhere
@bpolaszek
bpolaszek / tailwind-breakpoint-debug.html
Created September 11, 2021 14:01
Quickly debug your current Tailwind CSS breakpoint
<!-- Temporary breakpoint debugger -->
<span class="sm:hidden">XS</span>
<span class="hidden sm:inline md:hidden">SM</span>
<span class="hidden md:inline lg:hidden">MD</span>
<span class="hidden lg:inline xl:hidden">LG</span>
<span class="hidden xl:inline">XL</span>
<!-- / Temporary breakpoint debugger -->
@cbilgili
cbilgili / _messages.html.erb
Last active August 25, 2021 17:14
Simple Chat with Hotwire Turbo Stimulusjs
<%= turbo_frame_tag "new_message", target: "_top" do %>
<%= form_with(model: [@message.room, @message], data: { controller: "reset-form", action: "turbo:submit-end->reset-form#reset" }) do |form| %>
<%= form.text_field :content, autocomplete: 'off', placeholder: 'Write your message...' %>
<%= button_tag(type: :submit) do %>
<i class="fa fa-paper-plane" aria-hidden="true"></i>
<% end %>
<%= form.submit "Send", data: {disable_with: false} %>
<% end %>
<% end %>
// DISCLAIMER : You can now probably use `data-turbo-action="advance"` on your frame to perform what this controller is aiming to do
// https://turbo.hotwired.dev/handbook/frames#promoting-a-frame-navigation-to-a-page-visit
// Note that you probably want to disable turbo cache as well for those page to make popstate work properly
import { navigator } from '@hotwired/turbo'
import { Controller } from '@hotwired/stimulus'
import { useMutation } from 'stimulus-use'
export default class extends Controller {
connect (): void {
@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" %>
@jesster2k10
jesster2k10 / base_provider_spec.rb
Created April 26, 2020 15:17
Social Login Specs
require 'rails_helper'
RSpec.describe External::Provider::Base do
let(:subject) { described_class.new SecureRandom.hex }
describe '#for' do
it 'returns Facebook provider' do
class_instance = described_class.for :facebook
expect(class_instance).to eq(External::Provider::Facebook)
end
@jesster2k10
jesster2k10 / README.md
Last active May 4, 2024 14:15
Rails API Social Login

Rails API-Only Social Login

This is another piece of code I've extrapolated from a Ruby on Rails project I'm currently working on. The code implmenets social login with a RoR API-based application, targeted at API clients.

The setup does not involve any browser-redirects or sessions as you would have to use working with Omniauth. Instead, what it does is takes an access_token generated on client-side SDKs, retireves user info from the access token and creates a new user and Identity in the database.

This setup works with native applications as described in the Google iOS Sign In Docs (see Authenticating with a backend server)

@jesster2k10
jesster2k10 / README.md
Last active April 25, 2024 00:54
JWT Auth + Refresh Tokens in Rails

JWT Auth + Refresh Tokens in Rails

This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.

I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)

Before trying it out DIY, I considered using:

@Gaolz
Gaolz / 如何封装公共方法到 Rails Engine 中及其注意事项.md
Last active September 26, 2020 15:38
如何封装公共方法到 Rails Engine 中及其注意事项

1. 什么是 Rails Engine

  • Rails Guides:Engine 可以看作为宿主应用提供附加功能的微型应用(如 feedmob-rescue, feedmob-sidebar)

  • 而 Rails Application 实质上就是一个加强版的 Engine

2. Rails Engine 优缺点

1. 背景:当宿主应用过于复杂且有某一功能耦合度不高时可以单独抽出来做成一个 Engine.

2. 优点:

  • 解耦 (避免宿主应用代码过多,导致难以维护)
  • 可以针对 engine 单独测试