Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / store_stylesheet.rb
Created March 5, 2018 12:16 — forked from manuelmeurer/store_stylesheet.rb
How to compile custom Sass stylesheets during runtime
# lib/store_stylesheet.rb
class StoreStylesheet
def initialize(store)
@store = store
end
# The path of the compiled stylesheet, i.e. stores/id_timestamp.css
def stylesheet_file
filename = [
@store.id,
@PragmaticEd
PragmaticEd / setup.md
Created May 17, 2018 11:32 — forked from kpheasey/setup.md
WSL, RVM & RubyMine; ubuntu on windows, bash on windows

Add inbound firewall rule for TCP 2222

  • Windows 10 has 2 new services, SSH Server Proxy and SSH Server Broker which will already be bound to port 22
  • Do not allow public connection on this rule, WSL is not proven safe

ConEmu

Add as cmd startup with bash.exe --login

Install the SSH server and some Rails essentials libraries in bash

sudo apt-get update && sudo apt-get upgrade -y
@PragmaticEd
PragmaticEd / readme.md
Created May 27, 2018 09:20
diacritic & case insensitive search (Rails, MySql, MongoDB).md
Word.destroy_all
WordMongo.destroy_all

Word.create(name: 'xа́е́x')
WordMongo.create(name: 'xа́е́x')

search  = 'Xа́е́x'
pattern = /#{RegexpExtension.accent_insensitive_pattern Regexp.escape(search)}/
wm = WordMongo.where(name: pattern).first
@PragmaticEd
PragmaticEd / Capfile
Last active June 10, 2019 04:06
resque & scheduler in rails 5 #rails #resque #scheduler #jobs
require 'capistrano-resque'
@PragmaticEd
PragmaticEd / translation_uniqueness_validator.rb
Last active June 14, 2018 12:26
Globalize gem uniqueness validation
# Usage in model:
#
# validates :title, translation_uniqueness: true # will assume it must be uniq across all languages
# validates :title, translation_uniqueness: {scope: :locale, message: ''}
#
class TranslationUniquenessValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.translations.each do |t|
@PragmaticEd
PragmaticEd / translation_presence_validator.rb
Created June 14, 2018 10:04
Globalize gem presence validation
# Usage in model:
#
# validates :title, translation_presence: true,
# validates :title, translation_presence: {message: ''}
#
class TranslationPresenceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.translations.each do |translation|
if translation[attribute].blank?