Skip to content

Instantly share code, notes, and snippets.

View ManickYoj's full-sized avatar

Nick Francisci ManickYoj

View GitHub Profile
@ManickYoj
ManickYoj / nil_args_check_helper_example.rb
Last active September 16, 2018 22:20
Check for nil arguments automatically
#
# Hey folks! This Gist details how to use the `check_for_nil_arguments!` method,
# which is available to you via including NilArgsCheckHelper into the class of
# your choice.
#
# Briefly, it's a shorthand for checking if any of your arguments are nil without
# explicitly writing out checks for each one you care about. This is especially
# helpful with methods that use a long list of keyword required arguments.
#
# For real usage examples, check out `bulk_label_data.rb`
class Registry : MonoBehaviour {
// ...
private static Registry _instance;
public static Registry Instance { get { return _instance; }}
void Awake() {
if (_instance != null && _instance != this) {
Destroy(this.gameObject);
} else {
_instance = this;
class Registry : MonoBehaviour {
// ...
public static void GetNewReceiver(Transmitter transmitter) {
// If the controller is already controlling a receiver, we'll look for the next
// free receiver starting above that index. Otherwise, we'll start the search
// from receiver 0
int transmitterIdx = Array.IndexOf(_instance.transmitterAssignments, transmitter);
int nextFreeIndex = NextFreeRecvIdx(transmitterIdx);
class Registry : MonoBehaviour {
// ...
// Receivers and transmitterAssignments indexes must be the same length.
// It is assumed that every element in receivers is occupied. However, it's okay
// if some or all of the transmitters are not occupied
[SerializeField] private Receiver[] receivers = new Receiver[2];
private Transmitter[] transmitterAssignments = new Transmitter[2];
[RequireComponent(typeof(PlayerInput))]
public class Transmitter : MonoBehaviour {
private Receiver receiver;
private PlayerInput playerInput;
void Start() {
this.playerInput = this.GetComponent<PlayerInput>();
this.playerInput.enabled = true;
Registry.Instance.GetNewReceiver(this);
}
class Transmitter : MonoBehaviour {
// ...
public void OnSwitchReceiver(InputValue inputValue) {
Registry.GetNewReceiver(this);
}
public void Disconnect(Receiver receiver) {
receiver.SendCommand("OnDisconnect");
}
public class Receiver : MonoBehaviour {
public Camera receiverCamera;
public GameObject[] listeners;
public void SendCommand(string cmd, object val = null) {
foreach (GameObject listener in listeners) {
listener.SendMessage(cmd, val, SendMessageOptions.DontRequireReceiver);
};
}
}
@ManickYoj
ManickYoj / config\initializers\devise.rb
Last active January 9, 2022 03:54
Devise Config Changes for Rails 7
# /config/initializers/devise.rb
# Turbo doesn't work with devise by default.
# Keep tabs on https://github.com/heartcombo/devise/issues/5446 for a possible fix
# Fix from https://gorails.com/episodes/devise-hotwire-turbo
class TurboFailureApp < Devise::FailureApp
def respond
if request_format == :turbo_stream
redirect
else
@ManickYoj
ManickYoj / turbo_devise_controller.rb
Last active January 22, 2024 12:04
Devise Turbo Controller for Rails 7 Setup
# app/controllers/turbo_devise_controller.rb
class TurboDeviseController < ApplicationController
class Responder < ActionController::Responder
def to_turbo_stream
controller.render(options.merge(formats: :html))
rescue ActionView::MissingTemplate => error
if get?
raise error
elsif has_errors? && default_action
@ManickYoj
ManickYoj / turbo_devise_controller.rb
Created January 9, 2022 03:55
Devise Turbo Controller for Rails 7 Setup
class TurboDeviseController < ApplicationController
class Responder < ActionController::Responder
def to_turbo_stream
controller.render(options.merge(formats: :html))
rescue ActionView::MissingTemplate => error
if get?
raise error
elsif has_errors? && default_action
render rendering_options.merge(formats: :html, status: :unprocessable_entity)
else