Skip to content

Instantly share code, notes, and snippets.

Rails naming conventions

General Ruby conventions

Class names are CamelCase.

Methods and variables are snake_case.

Methods with a ? suffix will return a boolean.

@devkinoti
devkinoti / show.html.erb
Created October 21, 2022 08:16 — forked from davidphasson/show.html.erb
ERB and the case statement
# Doesn't work
<p>
<% case @request.author_hosted %>
<% when "yes" %>
The school <b>has</b> hosted an author before.
<% when "no" %>
The school <b>has not</b> hosted an author before.
<% end %>
</p>
@devkinoti
devkinoti / gist:76f6a8f18f34df73c928afd23cc1c4e3
Created October 18, 2022 13:08 — forked from arjunvenkat/gist:1115bc41bf395a162084
Seeding a Rails database with a CSV file

How to seed a Rails database with a CSV file

1. Setup

First, Create a folder inside of lib called seeds

Put your CSV file example.csv into the lib/seeds folder. In the example below, the file is called real_estate_transactions.csv

Make sure you've created a resource with the appropriate columns to match your seed data. The names don't have to match up.

@devkinoti
devkinoti / web-fonts-asset-pipeline.md
Created August 6, 2020 10:32 — forked from anotheruiguy/web-fonts-asset-pipeline.md
Custom Web Fonts and the Rails Asset Pipeline

Web fonts are pretty much all the rage. Using a CDN for font libraries, like TypeKit or Google Fonts, will be a great solution for many projects. For others, this is not an option. Especially when you are creating a custom icon library for your project.

Rails and the asset pipeline are great tools, but Rails has yet to get caught up in the custom web font craze.

As with all things Rails, there is more then one way to skin this cat. There is the recommended way, and then there are the other ways.

The recommended way

Here I will show how to update your Rails project so that you can use the asset pipeline appropriately and resource your files using the common Rails convention.

@devkinoti
devkinoti / graphicsmagick.sh
Created July 29, 2020 10:56 — forked from witooh/graphicsmagick.sh
Install Graphicsmagick
sudo apt-get install python-software-properties
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:rwky/graphicsmagick
sudo apt-get update
sudo apt-get install graphicsmagick
def self.import(file)
conn = ActiveRecord::Base.connection
rc = conn.raw_connection
rc.exec("COPY callers (your_attributes) FROM STDIN WITH CSV")
file = File.open("#{file.path}", 'r')
while !file.eof?
rc.put_copy_data(file.readline)
end
@devkinoti
devkinoti / routes.rb
Created June 29, 2016 05:58
routes file for multi tenant app and subdomains
class SubdomainPresent
def self.matches?(request)
request.subdomain.present?
end
end
class SubdomainBlank
def self.matches?(request)
request.subdomain.blank?
end
@devkinoti
devkinoti / rescued_apartment_middleware.rb
Created June 29, 2016 05:56
tenant middleware to catch apartment tenant not found cases
class TenantMiddleware < Apartment::Elevators::Subdomain
def call(*args)
begin
super
rescue Apartment::TenantNotFound
Rails.logger.error "Error: Apartment Tenant Not found: #{Apartment::Tenant.current.inspect}"
return [404, {"Content-Type" => "application/json"},["Error => Tenant not found"]]
end
end
@devkinoti
devkinoti / apartment.rb
Created June 29, 2016 05:54
apartment initializer
# You can have Apartment route to the appropriate Tenant by adding some Rack middleware.
# Apartment can support many different "Elevators" that can take care of this routing to your data.
# Require whichever Elevator you're using below or none if you have a custom one.
#
# require 'apartment/elevators/generic'
# require 'apartment/elevators/domain'
require 'apartment/elevators/subdomain'
require 'rescued_apartment_middleware'
#