This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Easy ActiveRecord sorting with named_scopes and resources_controller | |
# this goes in your initializers, lib directory or as a plugin | |
module ClassMethods | |
def sortable_with(*fields) | |
sorts = fields.inject({}) do |h,f| | |
h[:"#{f}_asc"] = "#{f} ASC" | |
h[:"#{f}_desc"] = "#{f} DESC" | |
h | |
end | |
write_inheritable_attribute(:sortable_fields,sorts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this facades a model with all helpers and controller data available | |
# | |
class RssModel | |
def initialize(model, request, controller) | |
@model, @request, @controller = model, request, controller | |
end | |
# you have request available as well | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Spec | |
module Rails | |
module Matchers | |
def have_sane_through_associations | |
return simple_matcher(":through associations not to have the :dependent options}") do |model| | |
model = model.class if model.is_a? ActiveRecord::Base | |
not model.reflect_on_all_associations.any?{|a| a.options[:through] && a.options[:dependent]} | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Post < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :shop | |
named_scope :by_author, lambda {|user_id| | |
return {} if user_id.blank? | |
{:conditions => {:user_id => user_id}} | |
} | |
named_scope :like, lambda {|q| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Put this in an initializer (config/initializers/common_scopes.rb) or library file. | |
# | |
module CommonScopes | |
module ClassMethods | |
# Sort given fields ASC or DESC | |
# | |
# Example: | |
# | |
# class Post < ActiveRecord::Base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Cache Headers | |
# Middleware for configuring HTTP cache policy headers in Rack based web applications. | |
# Apply cache headers to HTTP responses corresponding to requests that match defined | |
# URI patterns. | |
# | |
# Example usage: | |
# | |
# use Rack::CacheHeaders | |
# | |
# Rack::CacheHeaders.configure do |cache| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ManyLevels | |
extend HashMapper | |
map from('/name'), to('/tag_attributes/name') | |
map from('/properties/type'), to('/tag_attributes/type') | |
map from('/tagid'), to('/tag_id') | |
map from('/properties/egg'), to('/chicken') | |
end | |
input = { | |
:name => 'ismael', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Common pattern for including instance and class level modules | |
module SomeModule | |
def self.included(my_class) | |
my_class.extend ClassMethods | |
end | |
module ClassMethods | |
def some_class_method(*args) | |
# do something at class level | |
puts args.inspect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Free RESTful interface for your Ruby objects with Rackable | |
# http://github.com/madx/rackable/tree/master | |
# | |
# + Free HTTP caching with rack-cache | |
# http://github.com/rtomayko/rack-cache/tree/master | |
require 'rackable' | |
require 'rack/cache' | |
APP_ROOT = File.dirname(__FILE__) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This module allows you to prepend a version prefix to your Sinatra URLs | |
# Example: | |
# | |
# require 'rubygems' | |
# require 'sinatra/base' | |
# | |
# class App < Sinatra::Base | |
# register Versioned | |
# | |
# set :version, 'v1' |