Skip to content

Instantly share code, notes, and snippets.

I learned a little bit about building queries (even with Rails/ARel) of postgres JSONB columns in my side project that I wanted to share.

I have a model that represents a Japanese word:

{"id"=>4111,
 "type"=>"vocabulary",
 "characters"=>"洗う",
 "meanings"=>[{
    "meaning"=>"To Wash", 

Idea

Top level API

ApplicationPolicy.can? user, :new, Post
ApplicationPolicy.can? user, :create, post
ApplicationPolicy.can? user, :edit, post
ApplicationPolicy.can? user, :register_ship

ApplicationPolicy.authorize! user, :new, Post
# Note(rstankov):
#
# Preload associations
#
# **Don't use for `connection`s, only for `field` attributes**
#
# supported definitions:
# - [x] :assoc
# - [x] { assoc: :assoc }
# - [x] { assoc: { assoc: :assoc } }
@thbar
thbar / 005_add_vat_rates.rb
Last active December 17, 2019 10:01
Creating a foreign data wrapper with Postgres and Sequel
# NOTE: several improvements to be made but I'm in a VAT preparation hurry
# - use "up" and "down" to make the migration reversible
# - use Sequel methods for extension/server create
Sequel.migration do
change do
file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data', 'vat_rates.csv'))
run 'CREATE EXTENSION file_fdw;'
run 'CREATE SERVER file_fdw_server FOREIGN DATA WRAPPER file_fdw;'
create_table(:vat_rates, foreign: 'file_fdw_server', options: {format: 'csv', header: 'true', filename: file, delimiter: ','}) do
String :month, null: false
# Hello, and welcome to makefile basics.
#
# You will learn why `make` is so great, and why, despite its "weird" syntax,
# it is actually a highly expressive, efficient, and powerful way to build
# programs.
#
# Once you're done here, go to
# http://www.gnu.org/software/make/manual/make.html
# to learn SOOOO much more.
@maxim
maxim / rails_load_path_tips.md
Last active August 15, 2024 15:17
How to use rails load paths, app, and lib directories.

In Rails 3

NOTE: This post now lives (and kept up to date) on my blog: http://hakunin.com/rails3-load-paths

If you add a dir directly under app/

Do nothing. All files in this dir are eager loaded in production and lazy loaded in development by default.

If you add a dir under app/something/

@jed
jed / how-to-set-up-stress-free-ssl-on-os-x.md
Last active August 30, 2024 08:37
How to set up stress-free SSL on an OS X development machine

How to set up stress-free SSL on an OS X development machine

One of the best ways to reduce complexity (read: stress) in web development is to minimize the differences between your development and production environments. After being frustrated by attempts to unify the approach to SSL on my local machine and in production, I searched for a workflow that would make the protocol invisible to me between all environments.

Most workflows make the following compromises:

  • Use HTTPS in production but HTTP locally. This is annoying because it makes the environments inconsistent, and the protocol choices leak up into the stack. For example, your web application needs to understand the underlying protocol when using the secure flag for cookies. If you don't get this right, your HTTP development server won't be able to read the cookies it writes, or worse, your HTTPS production server could pass sensitive cookies over an insecure connection.

  • Use production SSL certificates locally. This is annoying

input "app/assets"
output "public"
# convert coffee files into JS files
matches "*.coffee" do
filter(CoffeeScriptFilter) do |file|
"#{file}.js"
end
end
require 'erb'
class ERBIO < ERB
def set_eoutvar compiler, eoutvar
compiler.put_cmd = "#{eoutvar}.write"
compiler.insert_cmd = "#{eoutvar}.write"
compiler.pre_cmd = []
compiler.post_cmd = []
end
end
@carlosantoniodasilva
carlosantoniodasilva / gist:709301
Created November 21, 2010 23:44
SimpleForm submit with cancel link example
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
# You can just override the default submit button and add the functionality.
#
# Simple usage:
# f.button :submit 'Send!', :cancel_link => root_path
#
def submit(*args)
cancel_link = args.last.is_a?(Hash) && args.last.delete(:cancel_link)
submit_button = super