Skip to content

Instantly share code, notes, and snippets.

View jordaaash's full-sized avatar
🌴
Not on vacation, I just love palm trees

Jordan jordaaash

🌴
Not on vacation, I just love palm trees
View GitHub Profile
module FilterJson
extend ActiveSupport::Concern
module ClassMethods
def filter_json (*attributes)
(@filter_attributes ||= Set.new).merge(attributes.map { |a| a.to_s })
end
def filter_attributes
if superclass.respond_to?(:filter_attributes)
module AbstractClass
def self.extended (base)
base.instance_eval do
self.abstract_class = true if respond_to?(:abstract_class=)
@abstract_class = true unless instance_variable_defined?(:@abstract_class)
end
end
def new (*)
if @abstract_class
@jordaaash
jordaaash / 0_messages_controller.rb
Last active December 23, 2015 05:39
When providing an alternative action for rendering to respond_with, the action is ignored when the resourceful response is successful, because options[:action] is deleted for reasons I can't determine. Changing line 11 of responder.rb, as demonstrated in patched_responder.rb, fixes this so that the action is correctly passed to default_render. T…
class MessagesController < ActionController::Base
respond_to :html
def show
resource = Message.find(params[:id])
action = resource.sent? ? :edit : :show
respond_with(resource, :action => action)
end
def edit
@jordaaash
jordaaash / clamp_validator.rb
Created May 14, 2014 17:22
Grape: Use case for value coercion after or between running other validators
class ClampValidator < Grape::Validations::SingleOptionValidator
def validate_param!(attr_name, params)
params[attr_name] = clamp(params[attr_name], @option.min, @option.max)
end
def clamp (value, min, max)
[[value, max].min, min].max
end
end
@jordaaash
jordaaash / bad_time.rb
Created June 9, 2014 10:03
Thinking of silently swallowing null reference errors? You (and anyone whose Gemfile.lock ends up including your library) are gonna have a bad time.
class NilClass
def method_missing (*)
nil
end
end
@jordaaash
jordaaash / parse_format.coffee
Last active August 29, 2015 14:02
Bookshelf.js: camelize and underscore columns
parse: (attributes) ->
_.reduce attributes, (object, value, key) ->
camelized = _str.camelize(key)
object[camelized] = value
object
, {}
format: (attributes) ->
_.reduce attributes, (object, value, key) ->
underscored = _str.underscored(key)
@jordaaash
jordaaash / unique.coffee
Created June 26, 2014 20:22
Unique validation with LGTM and Bookshelf/Knex
unique = (message) ->
message ?= 'must be unique'
@using (value, attribute, model) ->
query = model.constructor.query()
query.count().where(attribute, value).first()
.then (row) ->
row.count isnt '0'
, message
@jordaaash
jordaaash / count.js
Last active June 19, 2021 13:03
Bookshelf.js/Knex.js innerJoin withRelated
var Promise = require('bluebird'),
User = require('./user'),
knex, query;
knex = User.prototype._builder(User.prototype.tableName);
query = function (q) {
q.distinct()
.innerJoin('orders', function () {
this.on('users.id', '=', 'orders.user_id')
@jordaaash
jordaaash / app.js
Last active August 29, 2015 14:04
Source of fnd.io's app.js for analysis
!function() {
"use strict";
var e = "undefined" != typeof window ? window : global;
if ("function" != typeof e.require) {
var t = {}, s = {}, r = function(e, t) {
return {}.hasOwnProperty.call(e, t)
}, a = function(e, t) {
var s, r, a = [];
s = /^\.\.?(\/|$)/.test(t) ? [e, t].join("/").split("/") : t.split("/");
for (var n = 0, i = s.length; i > n; n++)
@jordaaash
jordaaash / promise_loop.js
Last active August 29, 2015 14:05
Async while loop
'use strict';
var Promise = require('bluebird'),
slice = Array.prototype.slice,
promiseLoop;
promiseLoop = function (fn, thisArg) {
var argsArray = slice.call(arguments, 2);
return function () {
var pending = Promise.pending(),