Skip to content

Instantly share code, notes, and snippets.

@PragmaticEd
PragmaticEd / application.html.erb
Created February 12, 2018 10:29 — forked from the-bass/application.html.erb
Using Google Analytics with Rails 5 and Turbolinks 5. This code is taken from the conversation between @preetpalS and @packagethief on https://github.com/turbolinks/turbolinks/issues/73.
<%# Put this code snippet between the <head></head>-tags in your application layout and %>
<%# replace 'UA-XXXXXXXX-X' with your own unique Google Analytics Tracking ID %>
<%# ... %>
<head>
<%# ... %>
<% if Rails.env.production? %>
<script type="text/javascript">
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
@PragmaticEd
PragmaticEd / gist:eb6600c8eaf8df217ac20301142f54f1
Created January 25, 2018 15:37 — forked from niquepa/gist:4c59b7d52a15dde2367a
Ruby rails extract youtube ID from URL
def youtube_id(youtube_url)
regex = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
match = regex.match(youtube_url)
if match && !match[1].blank?
match[1]
else
nil
end
end
@PragmaticEd
PragmaticEd / nested_content_snippet.rb
Created January 1, 2018 15:54 — forked from bunnymatic/nested_content_snippet.rb
nested content in rails view helpers
# because i can never remember exactly how and when to use concat
# when building content in helpers
def nested_content
content_tag 'div' do
concat(content_tag 'span', 'span block')
concat(tag 'br')
concat(link_to 'root link', root_path)
concat(tag 'br')
concat(link_to('#') do
concat(content_tag 'h2', 'Head \'em off')
@PragmaticEd
PragmaticEd / arra.rb
Last active June 9, 2017 13:43
Ansestry + Globalize gem data in one query
class Array
# Converts array:
# [
# {id: 1, parent_id: nil, any_other_key: 'lorem'},
# {id: 2, parent_id: 1, any_other_key: 'ipsum'},
# {id: 3, parent_id: 2, any_other_key: 'abc'},
# ]
# to following format:
# [
@PragmaticEd
PragmaticEd / generate.rb
Last active May 6, 2017 14:05
Generate bootstrap-like padding & margin classes
minify = true
px_steps = 2
min_px = 0
max_px = 200
# ----------------------------------------------------------------------------------------
margin_css = ''
padding_css = ''
arr = ([0] + (min_px..max_px).step(px_steps).to_a + [max_px]).uniq # make sure min and max are included
@PragmaticEd
PragmaticEd / precompile.md
Created March 10, 2017 09:59 — forked from mrbongiolo/precompile.md
HOW TO: Rails 4.2 add 'vendor/asset' to precompile list

To enable the precompilation of all non.js/.css assets within vendor/assets just add this to config/initializers/assets.rb:

Rails.application.config.assets.precompile << Proc.new { |path, fn| fn =~ /vendor\/assets/ && !%w(.js .css).include?(File.extname(path)) }

Be aware that this will precompile ALL non .js/.css assets that you have there, some plugins or libraries might have .txt or other files around, and those would end up into your precompiled list also.

If you need to precompile images only, you could use this:

Rails.application.config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)
@PragmaticEd
PragmaticEd / application.rb
Created February 23, 2017 07:01 — forked from steve9001/application.rb
Rails middleware to provide information about errors during requests
module MyApp
class Application < Rails::Application
if Rails.env == 'test'
require 'diagnostic'
config.middleware.use(MyApp::DiagnosticMiddleware)
end
end
end
# Make nano default editor:
git config --global core.editor 'nano'
# git log2 | better log (one liners):
git config --global alias.log2 "log --pretty=format:'%C(yellow) %h %C(cyan) %cd %C(white) %s %C(red) (%an)'"
# git network | network (github network in terminal):
git config --global alias.network "log -20 --pretty=format:'%C(yellow)%h%Creset\\ %C(green)%ar%C(cyan)%d\\ %Creset%s%C(yellow)\\ [%cn]' --graph --decorate --all"
find /var/www -not -type d -exec file "{}" ";" | grep CRLF
@PragmaticEd
PragmaticEd / windows_ubuntu.md
Created September 22, 2016 11:12
Windows Ubuntu

reinstall: lxrun /install /y

Uninstall: lxrun /uninstall /full /y

Uninstall, but keep user folder: lxrun /uninstall /y