Skip to content

Instantly share code, notes, and snippets.

View jonathandean's full-sized avatar

Jonathan Dean jonathandean

View GitHub Profile
@jonathandean
jonathandean / USAGE
Last active September 10, 2023 00:16
ViewComponent Lookbook preview generator
Description:
Make ViewComponent previews for Lookbook
Example:
bin/rails generate component_preview my_new_component
This will create:
spec/components/previews/my_new_component_preview.rb
spec/components/previews/my_new_component_preview/default.html.erb
module UrlHelpers
def domain(url)
unless url =~ /^http(s)?:\/\//
url = "https://#{url}"
end
uri = URI.parse(url)
uri.host&.gsub(/^(www|m)\./, '')
rescue => e
puts "WARNING: failed to retrieve domain from URL #{url}: #{e.message}"
nil
@jonathandean
jonathandean / dump_fixtures.rb
Created July 12, 2021 21:10
Adds dump_all_fixtures and dump_fixture to ApplicationRecord
class ApplicationRecord < ActiveRecord::Base
# ...
def self.dump_all_fixtures
self.all.map(&:dump_fixture)
end
def dump_fixture
fixture_file = "#{Rails.root}/spec/fixtures/#{self.class.table_name}.yml"
File.open(fixture_file, "a+") do |f|
f.puts({ "#{self.class.table_name.singularize}_#{id}" => attributes.reject{|key| %w(id created_at updated_at).include?(key)} }.

Keybase proof

I hereby claim:

  • I am jonathandean on github.
  • I am jondean (https://keybase.io/jondean) on keybase.
  • I have a public key whose fingerprint is 434D 26AD 85A0 8482 E228 DC75 1ECE DE72 83BD EDA6

To claim this, I am signing this object:

@jonathandean
jonathandean / gist:7449772
Last active December 8, 2021 19:15
Installing postgres gem pointing to homebrew installation

I needed to reinstall my postgres gem after upgrading to OS X Mavericks. I used homebrew to install postgres.

Find the path to your postgres config with

brew info postgres

And look for something like: postgres -D /usr/local/var/postgres # serve that database

Now use the --with-pg-config option to point to that directory and use gem install directly instead of bundler for just this one:

@jonathandean
jonathandean / gist:5817623
Created June 19, 2013 20:11
Generic objects in CoffeeScript
NameSpace = NameSpace or {}
NameSpace.ClassName = (options) ->
# default options
defaultOpts = ->
defaultOpts:: =
some_option: "some value",
another_option: "another value"
# private vars
@jonathandean
jonathandean / ruby_remote_file_download
Created January 5, 2011 17:06
Ruby remote file download
require 'net/http'
Net::HTTP.start('www.example.com') { |http|
resp = http.get('/url/to/file.ext')
open('/path/to/local/file.ext', "wb") { |file|
file.write(resp.body)
}
}