# Anchor
click_link 'Save'
# Button
click_button 'awesome'
# Both above
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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:) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/hooks' | |
require 'minitest/autorun' | |
class ActiveSupport::TestCase | |
extend MiniTest::Spec::DSL | |
end | |
class ActionDispatch::IntegrationTest | |
extend MiniTest::Spec::DSL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |