Skip to content

Instantly share code, notes, and snippets.

@frankolson
frankolson / rails_helper.rb
Last active Jan 1, 2020
Stubbing constants that are used as a side effect of a let! function
View rails_helper.rb
# Rails helper config stuff...
# Because `let!' is always run before any `before` calls, you have to manually
# overide the constant in the rails helper
SomeClass.send :remove_const, 'FILE_NAME'
SomeClass.const_set 'FILE_NAME', 'spec/fixtures/files/something/cool.yml'
@frankolson
frankolson / some_model.rb
Last active Oct 31, 2019
Email validation in rails
View some_model.rb
class SomeModel < ApplicationRecord
validates :email, format: {
with: URI::MailTo::EMAIL_REGEXP,
message: 'requires a valid email format'
}
end
View trix_editor_view_example.erb
<%= form_with model: @article do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title, placeholder: 'Title' %>
</div>
<div class="field">
<input id="article_content" type="hidden" name="article[content]"
value="<%= form.content %>">
<trix-editor input="article_content"></trix-editor>
View app\controllers\application_controller.rb
class ApplicationController < ActionController::Base
# While in such a small app, this extraction into a concern seems unnecessary.
# I included this abstraction as an example of DRY and modular code..
include SetRequest
end
@frankolson
frankolson / post_test.rb
Last active Oct 27, 2018
Capybara/Trix Editor example tests
View post_test.rb
# test/system/article/post_test.rb
require 'application_system_test_case'
class Articles::PostTest < ApplicationSystemTestCase
setup do
sign_in_as users(:will)
visit articles_path
end
test 'post a new article' do
@frankolson
frankolson / test_helper.rb
Created Oct 27, 2018
Auto-load support files into your test helper
View test_helper.rb
# test/test_helper.rb
ENV['RAILS_ENV'] ||= 'test'
require_relative '../config/environment'
require 'rails/test_help'
# Be sure to add this line to include all support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
@frankolson
frankolson / articles.yml
Created Oct 27, 2018
Capybara/Trix Editor example fixtures
View articles.yml
# test/fixtures/articles.yml
trix_capybara:
title: Testing the Trix Editor with Capybara
content: <div>We are going to learn some cool stuff!</div>
@frankolson
frankolson / capybara_helpers.rb
Created Oct 27, 2018
Capybara helpers for the Trix Editor
View capybara_helpers.rb
# test/support/capybara_helpers.rb
def fill_in_trix_editor(id, with:)
find(:xpath, "//trix-editor[@input='#{id}']").click.set(with)
end
def find_trix_editor(id)
find(:xpath, "//*[@id='#{id}']", visible: false)
end
@frankolson
frankolson / linkedin_industries.rb
Created Jul 14, 2018
Grab the list of LinkedIn industries.
View linkedin_industries.rb
# Updated version of the following gist:
# https://gist.github.com/stevenzeiler/1895583
#
# list = Linkedin::IndustryList.new
# industry = list.industries.first
#
# industry.description
# => "Accounting"
#
# industry.code
View memory_logger.rb
module MemoryLogger
def log_memory(&block)
begin
log_start
thread = create_memory_log_thread
yield
ensure
Thread.kill(thread)
log_finish
end
You can’t perform that action at this time.