Skip to content

Instantly share code, notes, and snippets.

View bigfive's full-sized avatar

Jonathan Williams (jdub) bigfive

  • Envato
  • Perth, Australia
View GitHub Profile
@bigfive
bigfive / gist:1399762
Created November 28, 2011 09:33
Ruby on Rails. Module to handle 'quantity' attributes by overriding the << and delete methods of the has_many association
class Package < ActiveRecord::Base
has_many :products, :through => :packagings, extend => QuantityAssociation
# ... do stuff
end
module QuantityAssociation
def include_duplicates
@bigfive
bigfive / 20120118012543_create_site_configuration.rb
Created January 20, 2012 02:01
Ruby on Rails. Key value table for site configuration integrated with Active admin and formtastic forms
# db/migrations/20120118012543_create_site_configuration.rb
class CreateSiteConfigurations < ActiveRecord::Migration
def change
create_table :site_configurations do |t|
t.string :key
t.text :value
t.string :form_type
t.string :form_collection_command
@bigfive
bigfive / change_viewport_meta.js
Created December 11, 2012 08:36
Change viewport initial scale and width depending on device width
changeViewportMeta = function() {
$('meta[name=viewport]').attr('content', 'user-scalable=yes, initial-scale=1, maximum-scale=1.3, width=device-width');
if ($(window).width() <= 320) {
return $('meta[name=viewport]').attr('content', 'user-scalable=yes, initial-scale=0.63, maximum-scale=1.3, width=480');
} else if ($(window).width() <= 480) {
return $('meta[name=viewport]').attr('content', 'user-scalable=yes, initial-scale=0.89, maximum-scale=1.3, width=480');
} else if ($(window).width() <= 768) {
return $('meta[name=viewport]').attr('content', 'user-scalable=yes, initial-scale=0.8, maximum-scale=1.3, width=920');
} else if ($(window).width() <= 1024) {
return $('meta[name=viewport]').attr('content', 'user-scalable=yes, initial-scale=0.85, maximum-scale=1.3, width=920');
@bigfive
bigfive / create_template.rb
Last active December 19, 2015 16:08
Creates a git template dir that will allow all folders that you run 'git init' in will now use a pre-commit hook that forbids commits directly on master and staging
#!/usr/bin/env ruby
def add_new_git_template
# Paths
home_dir = File.expand_path('~')
template_dir = File.join(home_dir, ".git_template")
hooks_dir = File.join(template_dir, "/hooks")
pre_commit_file = File.join(hooks_dir, "pre-commit")
@bigfive
bigfive / active_admin_helper.rb
Last active November 15, 2018 21:33
Active admin reskin
module ActiveAdminHelper
def admin_arbre_context
@admin_arbre_context ||= Arbre::Context.new(assigns, self)
end
def default_renderer
case controller.send(:resource_class).name
when "ActiveAdmin::Page"
"page"
@bigfive
bigfive / application_helper.rb
Last active December 21, 2015 17:08
Tupleize a number.
# app/helpers/application_helper.rb
module ApplicationHelper
def tupleize number
units = {
1 => "single",
2 => "double",
3 => "triple",
4 => "quadruple",
5 => "quintuple",
@bigfive
bigfive / example_mailer_spec.rb
Created January 7, 2014 08:14
ActionMailer RSpec helpers. Makes mailer test more similar to controller tests in that you can test that instance variables are assigned for rendering
require "spec_helper"
describe ExampleMailer do
describe :new_user do
let(:user) { FactoryGirl.create :user }
it "sends to the correct addresses" do
# :mail is similar to the controller specs 'get', 'post' etc methods
mail :new_user, user
@bigfive
bigfive / association_counter.rb
Last active August 29, 2015 13:56
count_sql_for
module AssociationCounter
def count_sql_for(association_sym)
association_table_name = reflect_on_association(association_sym).klass.table_name
select("count(#{association_table_name}.id) as #{association_sym}_count").joins(association_sym).to_sql
end
end
ActiveRecord::Base.send(:extend, AssociationCounter)
@bigfive
bigfive / deploy.rb
Last active August 29, 2015 13:56
Remove Digests from rails 4 assets
namespace :deploy do
namespace :assets do
task :remove_digests do
run %( cd #{latest_release} && bundle exec rake assets:remove_digests RAILS_ENV=#{rails_env}), :once => true
end
end
end
after 'deploy:assets:precompile', 'deploy:assets:remove_digests'
@bigfive
bigfive / deploy_local_assets.rb
Last active August 29, 2015 13:56
Capistrano local asset precompile
set :local_path, Dir.pwd
namespace :deploy do
namespace :assets do
desc 'Run asset procompilation locally'
task :precompile_locally, :roles => :web, :except => {:no_release => true} do
run_locally("bundle exec rake assets:precompile")
end