Skip to content

Instantly share code, notes, and snippets.

View ayrton's full-sized avatar

Ayrton ayrton

View GitHub Profile
@ayrton
ayrton / gist:954171
Created May 3, 2011 20:34
Determine file extension based on mimetype
def determine_file_extension_with_mime_type(mimetype)
fe = `grep #{mimetype} -i mime.types`.gsub(mimetype, '').strip.split(' ')[0]
if fe == '' || fe.nil?
''
else
".#{fe}"
end
end
@ayrton
ayrton / gist:2292579
Created April 3, 2012 14:45
and & &&
@error = :invalid_type and return if (type = params[:type]) and (type != :iphone && type != :android)
# exactly the same as: (if prefer the former)
if type = params[:type] and (type != :iphone && type != :android)
@error = :invalid_type
return
end
# or
@ayrton
ayrton / ajax.coffee
Created April 10, 2012 14:50
TypeError
twfbCounters = ->
$twitterFacebook = $('#twfb')
$.ajax {
url: 'http://api.twitter.com/1/users/show.json',
data: 'screen_name=zeptojs',
dataType: 'jsonp'
success: (data, status, xhr) ->
$twitterFacebook.html(data.followers_count)
error: (xhr, errorType, error) ->
@ayrton
ayrton / cards.coffee
Created August 22, 2012 11:35
Coffeescript (after) vs. JavaScript (before)
class Card
constructor: (selector) ->
@$container = $('.cards')
@$element = @$container.find(selector)
@$text = @$element.find('.card-menu li:first-child a span')
toggleClass: ->
@$element.toggleClass('is-favorited')
changeText: ->
cardText = if @$element.favorited then 'Unfavorite' else 'Favorite'
@$text.text(cardText)
class Duration
attr_accessor :duration
def initialize(string)
@duration = string
end
def to_i
value_units.map(&:to_i).reduce(&:+)
end
@ayrton
ayrton / taggable.rb
Last active September 29, 2020 20:42
Testing Active Support concerns with rspec in isolation. See first comment for more information.
module Taggable
extend ActiveSupport::Concern
included do
attr_accessible :tags
after_save :set_tags
after_destroy :unset_tags
end
@ayrton
ayrton / gist:5929299
Last active December 19, 2015 08:59 — forked from jordelver/gist:5806460

Sharing files using netcat

The receiver

nc -l 5566 > data-dump.sql

Listen on port 5566 and redirect output to data-dump.sql

The sender

# Single object
<%= render partial: 'thumbnail', object: video, as: :video %>
<%= render partial: 'thumbnail', locals: { video: video } %>
<%= render 'thumbnail', video: video %>
# Collection
<%= render partial: 'thumbnail', collection: videos, as: :video %>
@ayrton
ayrton / stateless-component.js
Created October 11, 2015 14:26
Stateless React component
import PureRenderMixin from 'react-addons-pure-render-mixin';
import React from 'react';
import Audio from './Audio';
import Image from './Image';
import MediaRecord from 'ph/records/MediaRecord';
import PostRecord from 'ph/records/PostRecord';
import Video from './Video';
import {MEDIA_TYPE_AUDIO, MEDIA_TYPE_IMAGE, MEDIA_TYPE_VIDEO} from 'ph/constants/MediaConstants';
class ApplicationController < ActionController::Base
include EtagWithLayout
end