Skip to content

Instantly share code, notes, and snippets.

View ismasan's full-sized avatar

Ismael Celis ismasan

View GitHub Profile
# 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 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
#
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
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|
# 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
# 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|
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',
# 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
# 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 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'