Skip to content

Instantly share code, notes, and snippets.

/*
Copyright (c) 2018 Pete Michaud, github.com/PeteMichaud
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@PeteMichaud
PeteMichaud / nested_toc_macro.rb
Created July 20, 2016 21:35
A Gollum Wiki macro for better Global Table of Contents
module Gollum
# monkey patching Gollum::Page is a questionable decision. Could be refactored as a container or super class
class Page
attr_accessor :children
def pretty_title
# is this the right way to get th wiki_options from here?
@pt ||= if Precious::App.settings.wiki_options[:h1_title]
find_header_node(text_data)
@PeteMichaud
PeteMichaud / gist:fd5ccea6f4d792188f459d3405d5e18d
Last active June 10, 2016 21:22 — forked from crova/'campaign_records'
iterates through each row but keep updating on the same db row
# fetchs SIB Campaigns
def fetch
# update/create
campaign_records.each do |record|
shoot = Shoot.find_or_initialize_by(campaign_id: record[:id])
unless shoot.update(record)
# the data was invalid, so the shoot wasn't saved, do something about that here
end
end
class Widget < ActiveRecord::Base
include Thingable
end
@PeteMichaud
PeteMichaud / jekyll_import_converter.rb
Created September 7, 2013 17:23
After importing from Wordpress to Jekyll, you get your posts and pages in html format, without any hard line wraps. I wanted to get the files into markdown format and wrap the lines at 120 characters wide so I could edit it nicely in my IDE. I also have like 500 pages to convert, so doing it by hand was a nonstarter...
require 'html2markdown' #gem install html2markdown
dirs = %w(_pages _posts)
class String
def word_wrap(line_width = 120)
self.split("\n").collect do |line|
line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
end * "\n"
end
@PeteMichaud
PeteMichaud / draper_kaminari_patch.rb
Created July 19, 2013 17:08
Getting Draper and Kaminari to play nice together isn't straightforward. Add this file to the config/initializers/extensions/ path and it'll stop the errors about the DraperCollection not having various methods.
module Draper
class CollectionDecorator
delegate :current_page, :total_pages, :limit_value, :model_name, :total_count, :offset_value, :last_page?
end
end
ActiveRecord::RecordNotSaved in Admin::UsersController#update
Failed to remove the existing associated subscription. The record failed to save when after its foreign key was set to nil.
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"...",
"user"=>{"email"=>"[email protected]",
"password"=>"[FILTERED]",
"first_name"=>"Bob",
"last_name"=>"Jones",
"subscription"=>{"plan_id"=>"5"}},
"password_confirmation"=>"[FILTERED]",
"commit"=>"Update User",
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :organization, :email, :password, :password_confirmation, :remember_me, :is_admin, :plan
<fieldset>
<legend><%= form_legend %></legend>
<div class='control-group<%= " error" if @user.errors.has_key?(:email) %>'>
<label class='control-label'>Email</label>
<div class='controls'>
<%= f.text_field :email, :class => 'input-xlarge' %>
<% if @user.errors.has_key?(:email) %>
<span class='help-inline'>Email <%= @user.errors.get(:email).first %></span>