Skip to content

Instantly share code, notes, and snippets.

View dmitry's full-sized avatar
🇪🇪
Water, earth and air.

Dmitry Polushkin dmitry

🇪🇪
Water, earth and air.
View GitHub Profile
module Rack
class LocaleSetter
def initialize app
@app = app
end
def call env
locale = env["HTTP_HOST"].match(/^[a-z]{2}(?=\.)/)
I18n.locale = locale ? locale.to_s : 'es'
@app.call env
Rails CMS alternatives
======================
Note: project activity was checked on 11/26/09 for most of these projects, and the "last update" field has not been kept up to date since then.
Active projects:
---------------
adva-cms
repo: http://github.com/svenfuchs/adva_cms/
site: http://adva-cms.org/
Last update: 11/24/09
# Run rake db:size to get a print of your database size in bytes.
# Run rake db:tables:size to get the sizes for individual tables
# Works for MySQL and PostgreSQL. Not tested elsewhere.
namespace :db do
desc 'Print data size for entire database'
task :size => :environment do
database_name = ActiveRecord::Base.connection.instance_variable_get("@config")[:database]
adapter = ActiveRecord::Base.connection.adapter_name.downcase
diff --git a/lib/simple_enum.rb b/lib/simple_enum.rb
index 9c1fa3f..68792a1 100644
--- a/lib/simple_enum.rb
+++ b/lib/simple_enum.rb
@@ -165,8 +165,8 @@ module SimpleEnum
# generate setter
define_method("#{enum_cd}=") do |new_value|
- v = new_value.nil? ? nil : values[new_value.to_sym]
- raise(ArgumentError, "Invalid enumeration value: #{new_value}") if (options[:whiny] and v.nil? and !new_value.nil?)
module Paperclip
class Attachment
class UploadedPath
attr_reader :original_filename, :content_type, :size, :path
def initialize(uploaded_file)
@original_filename = uploaded_file["name"].downcase
@content_type = uploaded_file["content_type"].to_s.strip
@file_size = uploaded_file["size"].to_i
@path = uploaded_file["path"]
@dmitry
dmitry / .irbrc.rb
Created April 8, 2010 15:50 — forked from dekart/.irbrc.rb
# Put this content to ~/.irbrc file (no extension)
require "rubygems"
begin
require "ap"
rescue LoadError => err
puts "Cannot find awesome_print gem. Please run 'gem install awesome_print' to install it."
end
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.role? :admin
can :manage, :all
else
can :read, :all
module FormtasticExtensions
module Formtastic
module TextileEditor
def self.included(base)
base.class_eval do
@javascript_included = false
end
end
@dmitry
dmitry / simple_breadcrumbs.rb
Created November 8, 2010 15:11
Simple Breadcrumbs
module SimpleBreadcrumbs
module Breadcrumbs
Element = Struct.new(:name, :path, :controller)
class Builder
def initialize(context, elements, options)
@context = context
@elements = elements
@options = {:separator => " » ", :wrapper => nil}.merge(options)
end
@dmitry
dmitry / has_many_ids.rb
Created February 5, 2011 01:54
has_many_ids
class ActiveRecord::Base
def self.has_many_ids(association, ids, options={})
options = {:typecast => :to_sym}.merge(options)
method_name = :"#{association.to_s.singularize}_#{ids.to_s.pluralize}"
define_method method_name do
self.send(association).map(&ids)
end