Skip to content

Instantly share code, notes, and snippets.

@thomsbg
thomsbg / gist:8847050
Created February 6, 2014 16:00
each_slice
<% data.stories.each_slice(4) do |chunk| %>
<div class="m-box">
<% chunk.each do |d| %>
<div class="m-boxes__quarter" style="background-image: url('<%= d.image %>');">
<h6><a href="<%= d.url %>" target="new"><%= d.headline %></a></h6>
</div>
<% end %>
</div>
<% end %>
(
select transactions.*, exchange_rate.rate
join exchange_rate on transcations.month = exchange_rate.month
)
union
(
select predictions.*, (select rate from exchange_rate order by month desc limit 1) as rate
)
@thomsbg
thomsbg / gist:7216377
Created October 29, 2013 15:04
Left outer join + if statement
select stuff.*, if(
exchange_rate.rate is null,
(select rate from exchange_rates order by month desc limit 1),
exchange_rate.rate
)
from stuff
left outer join exchange_rate on exchange_rate.stuff_id = stuff.id;
select billings.cost, employees.name
from (
select cost, employee_id from past_billings
union
select (hours * rate) as cost from future_billings
) billings
join employees on employees.id = billings.employee_id;
<script src="http://assets.sbnation.com/features/2013-NHL-Preview/tentpole.min.js"></script>
<script type="text/javascript"><!--
jQuery(function($) { $('.feature-body').css("height",$(window).height()) });
// --></script>
@thomsbg
thomsbg / tentpole.js
Created September 27, 2013 18:50
goTo function
function goTo(page) {
var callback = function(e, adMarkup) {
if (page < bb.current) {
// Insert AFTER the requested page, if moving backwards
page += 1;
}
insertAdBeforePage(adMarkup, page);
};
$(document).on('renderTentpoleFeatureAd', callback);
$(document).trigger('tentpoleFeaturePageFlip', pagesSeen, bb.current, page);
@thomsbg
thomsbg / comment.js
Last active December 22, 2015 22:39
Using this.ajax inside a model
App.Models.Comment = Backbone.Model.extend(_.extend({}, App.Mixins.Ajax, {
urls: {
flag: '/comments/flag_comment/:id'
},
flag: function(formData) {
this.set('is_flagged', true);
return this.ajax('flag', {
data: formData,
error: function() {
@thomsbg
thomsbg / delegate_events.js
Created September 11, 2013 18:38
Redefining delegateEvents() on a collection view to handle children's events too
App.Views.CommentList = Backbone.View.extend({
childrenById: {},
childSelector: 'div',
childView: function() {
return App.Views.Comment;
},
// Bind child event handlers once, instead of once per child view
delegateEvents: function() {
// Call super
@thomsbg
thomsbg / ajax.js
Last active December 22, 2015 20:29
Backbone ajax mixin
App.Mixins.Ajax = {
// Override urls in the target object to contain a map of
// actionName => /url/path/with/:bound/:segments
urls: {},
// Any options specified here are merged with those you pass to
// this.ajax({...}) before being passed to jQuery.ajax
ajaxOptions: {},
// Return the matching url from the 'urls' object, with /:path/:segments
@thomsbg
thomsbg / main.js
Created September 11, 2013 17:48
Mixing shared ajax functionality into a model
App.Mixins.Ajax = {
...
};
App.Models.Comment = Backbone.extend(_.extend({},
// Mixins
App.Mixins.Ajax, {
// Methods
initialize: function() {