Skip to content

Instantly share code, notes, and snippets.

View noahhendrix's full-sized avatar

Noah Hendrix noahhendrix

View GitHub Profile
@noahhendrix
noahhendrix / total_time.sql
Created November 19, 2010 20:34
sum of difference of two datetime columns
SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(s.ended_at, s.started_at))))
@noahhendrix
noahhendrix / ContentBlock Class
Created June 16, 2011 22:39
Content Block Class
class ContentBlock
attr_accessor :template
delegate :content_tag, :to => :template
def initialize(template, options = {}, block)
@template = template
@title = options[:title] || ''
@block = block
@tabs = []
end
@noahhendrix
noahhendrix / gist:1035064
Created June 20, 2011 03:18
Sample Log File with Sprockets
Started GET "/clients/5" for 127.0.0.1 at 2011-06-19 22:02:54 -0500
Processing by ClientsController#show as HTML
Parameters: {"id"=>"5"}
...
Completed 200 OK in 495ms (Views: 401.1ms | ActiveRecord: 2.0ms)
Started GET "/assets/application.js" for 127.0.0.1 at 2011-06-19 22:02:55 -0500
Compiled ~/Sites/invoicey/app/assets/javascripts/application.js (3ms) (pid 2725)
Compiled ~/.rvm/gems/ruby-1.9.2-p180@invoicey/gems/jquery-rails-1.0.11/vendor/assets/javascripts/jquery.js (0ms) (pid 2725)
class CreateProducts < ActiveRecord::Migration
def self.up
create_table :products do |t|
t.string :title
t.text :description
t.string :image_url
t.decimal :price, :precision => 8, :scale => 2
t.timestamps
end
@noahhendrix
noahhendrix / db.rake
Created July 11, 2011 00:03
My favorite custom Rake task for all new Rails projects
#lib/tasks/db.rake
namespace :db do
desc 'Drops the DB, migrates it, and finally seeds it'
task :reload => [:drop, :migrate, :seed]
end
@noahhendrix
noahhendrix / _search.html.erb
Created July 21, 2011 17:56
Ransack with Polymorphic Relationships
<%= search_form_for(@report.search, :url => report_path('sales', 'by_contact')) do |f| %>
<div class="input string optional">
<%= f.label :aggregator_of_User_type_name_eq, 'Name' %>
<%= f.text_field :aggregator_of_User_type_name_eq %>
</div>
<%= f.submit %>
<% end %>
@noahhendrix
noahhendrix / style.css
Created July 28, 2011 18:52
Multiple backgrounds in CSS3
html, body { min-height: 100%; } /* force the footer to always be at the bottom */
body {
background:
url(layout/header.jpg) no-repeat scroll center top,
url(layout/header-bg.jpg) repeat-x scroll left top, /* allows screen to be resized w/o eventual cutoff */
url(layout/footer.jpg) no-repeat scroll center bottom,
url(layout/footer-bg.jpg) repeat-x scroll left bottom; /* allows screen to be resized w/o eventual cutoff */
}
@noahhendrix
noahhendrix / message.rb
Created September 2, 2011 06:50
Message Example
class Message < ActiveRecord::Base
belongs_to :recipient, :class => 'User'
belongs_to :sender, :class => 'User'
end
@noahhendrix
noahhendrix / employees_controller_test.rb
Created September 29, 2011 20:41
Testing a regular user can not create a user
require 'test_helper'
require 'clearance/testing'
class EmployeesControllerTest < ActionController::TestCase
test "an ASM can not create a user" do
sign_in
post :create, :employee => { :name => 'Feed Store' }
assert_raise CanCan::AccessDenied
end
@noahhendrix
noahhendrix / task_list_test.rb
Created December 16, 2011 06:43
Testing with datamapper
describe TaskList do
let(:list) { TaskList.new }
let(:todo) { mock('todo').as_null_object }
describe '#tasks' do
context 'without tasks' do
it 'returns an empty array' do
list.tasks.should be_empty
end
end