Skip to content

Instantly share code, notes, and snippets.

Installing rails 6.1.5
Installing react-rails 2.6.1
Installing webpacker 6.0.0.rc.5
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory:
/Users/helenhood/.asdf/installs/ruby/3.0.3/lib/ruby/gems/3.0.0/gems/mysql2-0.5.3/ext/mysql2
/Users/helenhood/.asdf/installs/ruby/3.0.3/bin/ruby -I
/Users/helenhood/.asdf/installs/ruby/3.0.3/lib/ruby/3.0.0 -r
./siteconf20220311-17083-lpahbc.rb extconf.rb
@hchood
hchood / dj.rb
Last active October 25, 2020 15:25
dj.rb
# assumes Reminder has attribute `send_at`
# 1. When Reminder is created, schedule first job in after create hook
# 2. Each time the job is run, schedule a new one for the next day (or whenever)
class Reminder < ApplicationRecord
after_create :schedule_first_job # use Rails after_create callback to call the `schedule` method after the record is created
def schedule_first_job
schedule_next_job(send_at) # or whenever you next want it to run
(erb):5:in `fetch': Cannot load database configuration:
key not found: "DATABASE_NAME"
Did you mean? "DATABASE_PORT"
"DATABASE_HOST"
"DATABASE_USERNAME" (KeyError)
from (erb):5:in `<main>'
from /Users/helenhood/.asdf/installs/ruby/2.7.1/lib/ruby/2.7.0/erb.rb:905:in `eval'
from /Users/helenhood/.asdf/installs/ruby/2.7.1/lib/ruby/2.7.0/erb.rb:905:in `result'
from /Users/helenhood/.asdf/installs/ruby/2.7.1/lib/ruby/gems/2.7.0/gems/railties-5.2.4.4/lib/rails/application/configuration.rb:172:in `database_configuration'
from /Users/helenhood/.asdf/installs/ruby/2.7.1/lib/ruby/gems/2.7.0/gems/activerecord-5.2.4.4/lib/active_record/railtie.rb:133:in `block (2 levels) in <class:Railtie>'
345065
439148
439151
439153
520362
521949
524334
531977
546451
587433

Keybase proof

I hereby claim:

  • I am hchood on github.
  • I am hchood (https://keybase.io/hchood) on keybase.
  • I have a public key ASBI2PyH2e9q9NiImwZuTGWkeYIHMYptign2J8bj2R0H0Ao

To claim this, I am signing this object:

Rails API Workshop - May 6, 2016

Pre-workshop setup

  • Install the following, either via Thoughtbot's laptop script which will install all of these, or individually:
    • Homebrew
    • A Ruby version manager (we recommend rbenv) and Ruby 2.3.0.
    • Bundler, Rails, and Suspenders gems

$ gem install bundler

# app/models/user.rb
class User < ActiveRecord::Base
# ...
def self.for_authentication(token)
where(authentication_token: token).
where('authentication_token_expires_at > ?', Time.current).
first
end
config.middleware.use Warden::Manager do |manager|
manager.failure_app = lambda { |e| [ 401, {'Content-Type' => 'application/json'}, ['Authorization Failed'] ] }
end
# app/strategies/token_authentication_strategy.rb
class TokenAuthenticationStrategy < Warden::Strategies::Base
def valid?
env['HTTP_AUTHORIZATION'].present?
end
def authenticate!
if user
success!(user)
# routes.rb
constraints AuthenticatedConstraint.new do
resources :widgets, only: :index
end
# app/constraints/authenticated_constraint.rb
class AuthenticatedConstraint
def matches?(request)