Skip to content

Instantly share code, notes, and snippets.

@johnkoht
johnkoht / config.rb
Created January 31, 2015 22:50
Paginating categories in middleman blog
# In config.rb
def get_locals(category, total_pages, i, articles, total_articles)
prev_page = false
# Is the a previous page and if so what does it look like
if (i - 1) == 1
prev_page = "/blog/categories/#{category}"
elsif (i - 1) > 1
prev_page = "/blog/categories/#{category}/page/#{i-1}"
end
$ ->
$('.btn-ajax').on 'click', (e)->
e.preventDefault()
$btn = $(this)
$btn.attr('data-default-text', $btn.html())
if $btn.attr('data-processing-text')
$btn.html($btn.attr('data-processing-text'))
$('<%= j render partial: "cart_form", locals: {product: @product} %>').modal('show');
@johnkoht
johnkoht / date_time.rb
Last active December 22, 2015 22:49
Adds ActiveSupport convenience method for Date and Time objects for identifying if a date is within this year or not.
# Patch for Date and Time that adds a conveneince method for identifying
# if a date is within this year
# config/initializers/date_time.rb
class Date
def this_year?
self.year == ::Date.current.year
end
end
@johnkoht
johnkoht / grid.css.sass
Last active January 1, 2022 23:21
Bootstrap 3 Style Grid built on Bourbon Neat
// Main containers
.container
@include outer-container
// Rows
.row
@include row()
// A basic column without a defined width or height
# In your deploy.rb add the following code. This will run the rake task
# when deployment beings
before :deploy do
run_locally 'rake sms:send["We\'re doing the thing - kohbot"]'
end
# And then send another after the deployment is over
after :deploy do
run_locally 'rake sms:send["The thing is over :( - kohbot)"]'
end
@johnkoht
johnkoht / sms.rake
Last active December 14, 2015 15:48
Capistrano deployment notifications via SMS
# Create a rake task that sends the message using Twilio. You'll
# need a Twilio SID and Token as well as number
namespace :sms do
desc "Send SMS message"
task :send, [:message] => [:environment] do |t, args|
client = Twilio::REST::Client.new TWILIO_SID, TWILIO_TOKEN
client.account.sms.messages.create(
from: TWILIO_NUMBER,
to: PHONE_NUMBER,
body: args[:message]
@johnkoht
johnkoht / patch_arracy.rb
Last active December 14, 2015 06:09
Monkey patch Array so that we can find values. Used when caching some arrays
class Array
# Given an Array, find if a value exists for a given key. This is helpful in our settings
# since we cache the entire settings array, but occasionally we wan to find a specific
# setting, i.e. "Site Title", "Posts per page", etc....
# USAGE::: array.find_value? "key", "Value is Case Sensitive"
def find_value key, value
if self.first.respond_to? key.parameterize.underscore.to_sym
self.select { |item| item.send(key.parameterize.underscore) == value }.first rescue nil
@johnkoht
johnkoht / emailitize.rb
Created February 26, 2013 00:21
Little script that takes a string and will return the same string but with email addresses encoded and hyperlinked
# takes a string and will return the same string but with email addresses encoded and hyperlinked
def emailitize text
text.gsub(/([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})/i) {|m|
mail_to(m, m.gsub("@", "[at]"), :encode=>:hex)
}
end
@johnkoht
johnkoht / Rails Common Scopes
Created July 18, 2012 00:33
Rails Apps Common Scopes
module CommonScopes
def self.included(base)
base.class_eval {
# Filtering by the state of the record
scope :active, where(:parent_id => nil, :deleted_at => nil) #.order('sort_order asc')
scope :published, where('published_at IS NOT NULL', :deleted_at => nil)
scope :unpublished, where(:published_at => nil, :deleted_at => nil)