Skip to content

Instantly share code, notes, and snippets.

View volontarian's full-sized avatar

Mathias Gawlista volontarian

View GitHub Profile
@volontarian
volontarian / CoffeeScript
Last active August 29, 2015 14:27
Twitter Bootstrap 3 alerts per JavaScript
window.bootstrapAlert = (type, message) ->
$('#alert_message').text message
$('#alert').addClass "alert-#{type}"
$("#alert").slideDown()
setTimeout (->
$("#alert").slideUp(400, ->
$('#alert').removeClass "alert-#{type}"
)
), 5000
@volontarian
volontarian / api_provider_host.rb
Created March 14, 2015 21:05
Conventional API Provider Host Setting by Environment Fallback and Alias Mapping
class ApiProviderHost
ENVIRONMENTS = [:development, :test, :staging, :production]
ALIASES = {
dev: :development, testing: :test, stage: :staging, show: :staging,
live: :production, prod: :production
}
FALLBACKS = {
development: [:development, :test, :staging, :production],
test: [:test, :development, :staging, :production],
staging: [:staging, :production],
@volontarian
volontarian / _javascript_response.js.erb
Created March 10, 2015 19:41
Multi step form / wizard responses for Ajax (modals) and HTML requests.
<% message = flash[:notice] || flash[:alert] %>
<% flash.delete(:notice); flash.delete(:alert) %>
<% alert = message.present? ? "alert('#{message}');" : '' %>
<% if <a href="/path">@path</a>.present? %>
$.ajax({ url: "<%= @path %>", data: <%= raw @data.to_json %>, type: "<%= @method.to_s.upcase %>", dataType: "script"}).done(function(data) {
eval(data);
<%= raw alert %>
})
.fail(function(data) {
@volontarian
volontarian / example.html.erb_spec.rb
Last active August 29, 2015 14:16
RSpec: compare rendered text like HTML or XML (use diffchecker.com for comparison of expected and got text if example fails)
describe 'example.html.erb' do
it 'renders HTML like this' do
render
compare_texts rendered, 'example.html'
end
end
@volontarian
volontarian / example.rb
Last active August 29, 2015 14:16
Wrapper of Ruby gem lastfm for tolerant requests with 3 tries used at http://volontari.at/music
class MusicArtist < ActiveRecord::Base
include LastfmRequest
end
lastfm = Lastfm.new(LastfmApiKey, LastfmApiSecret)
# Simplest option
lastfm_artist = artist.lastfm_request(lastfm, :artist, :get_info, 'The artist you supplied could not be found', artist: 'Dummy')
# Option raise_if_response_is_just_nil
require 'rubygems'
require 'octokit' # gem install octokit
page = 1
catch(:done) do
while (repositories = Octokit.repositories('railscasts', page: page, per_page: 100, sort: 'created', direction: 'desc')).length > 0
repositories.each do |repository|
throw :done if File.exist? repository.name
@volontarian
volontarian / app-presenters-presenter.rb
Created June 4, 2012 14:10
Layout presenter inheritance on Rails
class Presenter
def initialize(subject, options = {})
@subject = subject
init_haml_helpers
end
private
def method_missing(*args, &block)
@subject.send(*args, &block)