Skip to content

Instantly share code, notes, and snippets.

View tcocca's full-sized avatar

Tom Cocca tcocca

  • Boston Logic Technology Partners, Inc.
  • Boston, MA
View GitHub Profile
# match method names like
# logged_id_super_admin_or_admin?
# or
# logged_in_admin_or_lead?
def method_missing(method_name, *args)
if method_name.to_s.match(/logged_in.*\?/)
roles = method_name.to_s.gsub('logged_in_', '').delete('?').split('_or_')
return false unless logged_in? && current_user.company_id == current_company.id
roles.each do |role|
@tcocca
tcocca / missing_spec_generator.rb
Created August 19, 2010 12:44
A library to generate missing rspec spec files for a rails project (just drop in lib)
require 'erb'
class MissingSpecGenerator
def spec_file(spec_path, file_name, spec_template, namespace)
spec_name = file_name.gsub('.rb', '') + '_spec.rb'
if File.exist?("#{spec_path}/#{spec_name}")
puts "#{spec_path}/#{spec_name} exists"
else
puts "#{spec_path}/#{spec_name} missing"
module ActionMailer
class Base
# call deliver! and then clear the theme out
# need to clear the theme on both success and failure do to errors
def deliver_with_clear_theme_path!(mail = @mail)
begin
deliver_without_clear_theme_path!
ensure
clear_theme_path
require 'active_support/core_ext/class/attribute_accessors'
require 'active_support/core_ext/array'
require 'active_support/inflector'
require 'active_support/core_ext/class/inheritable_attributes'
require 'active_support/core_ext/duplicable'
class Hash #:nodoc:
def deep_merge(other_hash)
self.merge(other_hash) do |key, oldval, newval|
<%= select_tag "searches", options_for_select([["Select a Search",""],["Tom",1],["Chris",2]]), :onchange => "search_redirect(this);" %>
<script type="text/javascript" charset="utf-8">
function search_redirect(field) {
if (field.value != "") {
window.location = "http://localhost:3000/searches/" + field.value;
}
}
</script>
namespace :associations do
desc "display a models associations"
task :model => :environment do
puts "\n"
ENV['model'].constantize.reflect_on_all_associations.each do |association|
assoc_str = "#{association.macro} :#{association.name}"
unless association.options.blank?
association.options.each do |key,val|
assoc_str += ", :#{key} => #{val}" unless val.blank? || key == :extend
class Recipe < ActiveRecord::Base
belongs_to :style, :counter_cache => true
attribute_method_suffix '_previous_change'
private
def attribute_previous_change(attr)
self.changes[attr]
class Post < ActiveRecord::Base
has_many :authors
has_many :post_comments
attr_accessor :post_authors_cache, :post_comments_cache
def post_authors
@post_authors_cache ||= begin
process_post
class Post < ActiveRecord::Base
has_many :authors
attr_accessor :post_authors_cache
def post_authors
process_post_authors if post_authors_cache.nil?
post_authors_cache
end
page << <<-JS
var manager = $('office_manager_attr').value;
var office_admin = $('office_office_admin_attr').value;
var managers_arr = Array();
var office_admins_arr = Array();
managers_arr.push(new Option('', ''));
office_admins_arr.push(new Option('', ''));
JS
@agents.each do |agent|
page << <<-JS