Skip to content

Instantly share code, notes, and snippets.

View foucist's full-sized avatar

James Robey foucist

View GitHub Profile
class Message < Base
belongs_to :attachable, polymorphic: true, foreign_type: :attachable_class
end
class Photo < Base
has_many :messages, as: :attachable, foreign_type: :attachable_class
end
scope :either, ->(a, b){where(where(profile: a, block: b).where(profile: b, block: a).where_values.instance_exec { self[0].and(self[1]).or(self[2].and(self[3]))})}
# or
def self.either(a,b)
pb = Arel::Table.new(:profile_blocks)
pb[:profile_id].eq(a).and(pb[:block_id].eq(b)).or(pb[:profile_id].eq(b).and(pb[:block_id].eq(a)))
end
class PhotosController < ApplicationController
respond_to :json
def index
respond_with load_photos
end
def show
respond_with load_photo
end
@foucist
foucist / testing.md
Created April 3, 2014 19:21
Just A Test

Markdown

Whatever

class Gear
attr_reader :chainring, :cog, :wheel
def initialize(chainring, cog, rim, tire)
@chainring = chainring
@cog = cog
@wheel = Wheel.new(rim, tire)
end
def ratio
chainring / cog.to_f
end
@foucist
foucist / result
Last active May 3, 2016 00:55
generate table of times
- days = 0..7
- hours = 8..18
%table
- days.each do |d|
%th= (Time.now + d.day).strftime("%A")
- hours.each do |h|
%tr
- days.each do |d|
%td= Time.parse("0001-01-01 #{h}:00:00 UTC").strftime '%l %p'
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :email, :balance, :children, :balance
def attributes
data = super
data[:children] = User.find(data[:id]).subordinates.any?
data
end
end
$(document).ready ->
$(".simple_form").on("ajax:success", (e, data, status, xhr) ->
$(".list").append xhr.responseText
).bind "ajax:error", (e, xhr, status, error) ->
$(".list").prepend xhr.responseText
#
# Regular Expression for URL validation
#
# Converted Diego Perini's regexp to ruby
#
# Author: James Robey
# Updated: 2013/02/07
#
# Credits to Matt (mparodi)
#
@foucist
foucist / gist:4127990
Created November 21, 2012 21:40 — forked from wmoxam/gist:4127672
Spiral Fun - slight refactoring
def method(grid,root,rev = false)
grid.send(rev ? :reverse_each : :each) do |row|
row.send(rev ? :<< : :unshift, @num)
@num += 1
end
new_row = []
root.times do
new_row.send(rev ? :unshift : :<<, @num)
@num += 1