Skip to content

Instantly share code, notes, and snippets.

View andyferra's full-sized avatar

Andy Ferra andyferra

View GitHub Profile
@andyferra
andyferra / gist:12991
Created September 25, 2008 23:55
Generates a per_page select for will_paginate
def per_page_select(collection, name=nil, value=nil, options={})
unless collection.empty?
per_page = collection.first.class.per_page
name = name || 'per_page'
value = value || params[:per_page] || per_page.to_s
before = options[:before_default] || ["#{per_page/2}"]
after = options[:after_default] || ["#{per_page*2}"]
choices = options_for_select(before + ["#{per_page}"] + after, value)
select_tag name, choices, options
end
@andyferra
andyferra / gist:13036
Created September 26, 2008 04:28
Generates per_page links for will_paginate
def per_page_links(collection, options={})
unless collection.empty?
separator = options[:seperator] || ' '
css_class = options[:class] || 'per_page_links'
param = options[:param] || :per_page
per_page = collection.first.class.per_page
options = [per_page/2, per_page, per_page*2]
value = params[param] || per_page.to_s
link = lambda {|p| url_for(params.merge({param => p}))}
item = lambda {|p, current_value| current_value == p.to_s ? content_tag(:span, p, :class => 'current') : link_to(p, link.call(p)) }
## In application.js
$(document).ready(function() {
$('table.search_results tbody tr').click(function() {
alert($(this).children('a:first-child').attr('href'));
});
});
## In view.html
Event.addBehavior({
'#my_selector': function() {
// code that only gets executed when something with the id 'my_selector' exists
}
});
jQuery.fn.exists = function(fn) {
if (this.length) fn(this);
return this;
}
## models/post.rb
class Post
include DataMapper::Resource
property :id, Serial
property :title, String, :nullable => false
property :body, Text
property :created_at, DateTime
property :updated_at, DateTime
## config/router.rb
Merb.logger.info("Compiling routes...")
Merb::Router.prepare do
match("/(:args)", :args => /.*/).
to(:controller => "default", :action => 'index')
end
## app/controllers/default.rb
## /Library/Application Support/Macromedia/mm.cfg
ErrorReportingEnable=1
TraceOutputFileEnable=1
No tests matched config/initializers/load_app_config.rb
No tests matched spec/controllers/sessions_controller_spec.rb
No tests matched spec/helpers/sessions_helper_spec.rb
No tests matched spec/models/comment_spec.rb
No tests matched spec/spec_helper.rb
No tests matched config/app_config.yml
No tests matched config/initializers/new_rails_defaults.rb
No tests matched lib/tasks/rspec.rake
No tests matched config/locales/en.yml
No tests matched spec/fixtures/comments.yml
# -*- ruby -*-
module Autotest::RedGreen
Autotest.send(:alias_method, :real_ruby, :ruby)
Autotest.send(:define_method, :ruby) do |*args|
real_ruby + %[ -rrubygems -e "require 'redgreen'" ]
end
# Clean the output so other modules can work correctly
Autotest.add_hook :ran_command do |at|