Skip to content

Instantly share code, notes, and snippets.

View minghz's full-sized avatar
👩‍💻
💭

Minghua Zhao minghz

👩‍💻
💭
View GitHub Profile
@minghz
minghz / override_devise_token_auth_render_error.rb
Created December 30, 2021 01:52
Overriding DeviseTokenAuth render_error()
# Create a module, and put this somewhere your controllers can access.
# In this example, its assumed to be in the same dir as application_controller.rb
module DeviseTokenAuthRenderErrorOverride
# 2. Overrides in application controller calling authenticate_user!
# (or authenticate_[model]! if its a custom model)
def render_authenticate_error
render_error(401, I18n.t('devise.failure.unauthenticated'))
end
@minghz
minghz / three_examples_of_dtos_in_ruby_method_1.rb
Created April 17, 2020 14:07
Three Examples of DTOs in Ruby - Method 1: Simple Immutable Object
#
# In this example, all required parameters must be provided during
# construction of the object, or it will fail to be created.
#
# The attributes can be read but not modified after object construction.
#
class BirthdayCard
attr_reader :from, :to, :messages
def initialize(from:, to:, messages:)
@minghz
minghz / three_examples_of_dtos_in_ruby_method_2.rb
Created April 17, 2020 05:41
Three Examples of DTOs in Ruby - Method 2: Hierarchical
#
# There are two classes here belonging to the same data transfer object
# They follow the first example, but are hierarcal. Meaning that the DTO called
# BirthdayCard has a sub-DTO called BirthdayCard::Template.
#
# It is possible to have more deeply nested hiearcies within
# BirthdayCard::Template depending on the use-case.
#
# Finally, if the DTO becomes overly-complex, i.e. say BirthdayCard had 3 layers
# of hiearchy, and more than one branch:
@minghz
minghz / three_examples_of_dtos_in_ruby_method_3.rb
Created April 17, 2020 05:39
Three Examples of DTOs in Ruby - Method 3: Mutable
#
# Mutable DTOs applies in case scenarios where the entire DTO may not be fully
# created in one go. Maybe it needs to be passed around several services and
# gradually enriched before being consumed.
#
# For example, passing around our birthday card to many friends to leave
# messages before finally delivering it to our recipient.
#
class BirthdayCard
attr_reader :from, :to
@minghz
minghz / minitest_quack_rspec_pt4.rb
Last active January 1, 2020 06:04
Making MiniTest work like RSpec - pt4
around(:all) do |&block|
ActiveRecord::Base.transaction do
super(&block)
raise ActiveRecord::Rollback
end
end
before(:all) do
setup_fixtures
# Creating a lot of DB entries for example
@minghz
minghz / minitest_quack_rspec_pt3.rb
Created January 1, 2020 05:52
Making MiniTest work like RSpec - pt3
require 'minitest/hooks'
require 'minitest/autorun'
class ActiveSupport::TestCase
extend MiniTest::Spec::DSL
end
class ActionDispatch::IntegrationTest
extend MiniTest::Spec::DSL
@minghz
minghz / minitest_quack_rspec_pt2.rb
Created January 1, 2020 04:39
Making MiniTest work like RSpec - pt2
# you actually need the "minitest-hooks" gem to get this lib =_="
require 'minitest/hooks'
require 'minitest/autorun'
class ExtendedMinitest < Minitest::Test
extend MiniTest::Spec::DSL
include Minitest::Hooks # You need to add this to your test classes
end
@minghz
minghz / minitest_quack_rspec_pt1.rb
Created January 1, 2020 04:09
Making MiniTest work like RSpec - pt1
# For a bare Minitest setup. your setup file, for example, extended_minitest.rb
require 'minitest/autorun'
class ExtendedMinitest < Minitest::Test
extend MiniTest::Spec::DSL
end
# Your test file my_test.rb
# if in a separate file, you may need to require 'extended_minitest.rb'
class MyTest < ExtendedMinitest
@minghz
minghz / rstoc.cpp
Created August 7, 2018 03:48
Example function for stochastic rounding in C++
float rstoc(x) {
float decimal = abs(x - trunc(x));
float random_selector = (float)rand() / RAND_MAX;
float adjustor;
if (random_selector < decimal) adjustor = 1;
else adjustor = 0;
// consider sign
@minghz
minghz / Capybara.md
Created April 26, 2018 20:44 — forked from tomas-stefano/Capybara.md
Capybara cheatsheet

Capybara Actions

# Anchor
click_link 'Save'

# Button
click_button 'awesome'

# Both above