Skip to content

Instantly share code, notes, and snippets.

Avatar

Mathias Gawlista volontarian

View GitHub Profile
@volontarian
volontarian / CoffeeScript
Last active Aug 29, 2015
Twitter Bootstrap 3 alerts per JavaScript
View CoffeeScript
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 Mar 14, 2015
Conventional API Provider Host Setting by Environment Fallback and Alias Mapping
View api_provider_host.rb
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 Mar 10, 2015
Multi step form / wizard responses for Ajax (modals) and HTML requests.
View _javascript_response.js.erb
<% 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 Aug 29, 2015
RSpec: compare rendered text like HTML or XML (use diffchecker.com for comparison of expected and got text if example fails)
View example.html.erb_spec.rb
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 Aug 29, 2015
Wrapper of Ruby gem lastfm for tolerant requests with 3 tries used at http://volontari.at/music
View example.rb
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
View railscasts_episodes.rb
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 Jun 4, 2012
Layout presenter inheritance on Rails
View app-presenters-presenter.rb
class Presenter
def initialize(subject, options = {})
@subject = subject
init_haml_helpers
end
private
def method_missing(*args, &block)
@subject.send(*args, &block)
You can’t perform that action at this time.