Skip to content

Instantly share code, notes, and snippets.

View Marchino's full-sized avatar

Marco Crepaldi Marchino

View GitHub Profile
@Marchino
Marchino / application.html.erb
Created May 3, 2011 22:01
Change page locale and translate url
<ul id="choose_language">
<% I18n.available_locales.each do |locale| %>
<li class="<%=locale.to_s%>">
<a href="http://<%=locale==I18n.default_locale ? 'www' : locale.to_s%>.mydomain.dev/pages/change_locale?url=<%=request.path%>" rel="nofollow"><span>en</span></a>
</li>
<% end %>
</ul>
@Marchino
Marchino / I18n_routing_common.rb
Created May 3, 2011 22:23
How I made I18n_routing working with Devise. Couldn't translate routing scopes in any way, so I added a little workaround to the gem and...
# I18n_routing/lib/I18n_routing_common.rb
# Return the correct translation for given values
def self.translation_for(name, type = :resources, option = nil)
# First, if an option is given, try to get the translation in the routes scope
if option
default = "{option}Noi18nRoutingTranslation"
t = I18n.t(option, :scope => "routes.#{name}.#{type}", :default => default)
return (t == default ? nil : t)
else
@Marchino
Marchino / registration_controller.rb
Created May 9, 2011 08:36
Extension of devise RegistrationController
require 'mmplus/mmplus_users_resolver'
class Users::RegistrationsController < Devise::RegistrationsController
append_view_path MmplusUsersResolver.new
protected
def render_with_scope(action, path=self.controller_path)
build_nested_attributes if request.get? or resource.errors.empty?
super
end
@Marchino
Marchino / routes.txt
Created May 12, 2011 09:48
Translated routes
(in /Users/marchino/Sites/mmplus)
en_search /search-results {:i18n_locale=>"en", :method=>:get, :controller=>"items/items", :action=>"search"}
it_search /risultati-della-ricerca {:i18n_locale=>"it", :method=>:get, :controller=>"items/items", :action=>"search"}
search /search(.:format) {:method=>:get, :action=>"search", :controller=>"items/items"}
en_books /books {:i18n_locale=>"en", :controller=>"items/books", :action=>"index"}
it_books /books {:i18n_locale=>"it", :controller=>"items/books", :action=>"index"}
books /books(.:format) {:controller=>"items/books", :action=>"index"
@Marchino
Marchino / rsend.rb
Created June 20, 2011 08:20
Recursive send method for Ruby Objects
# Lets you call object.rsend('method1.method2.method3') instead of object.send('method1').send('method2).send('method3)
class Object
def rsend(method, *params)
method.to_s.split('.').inject(self) { |obj, mth| obj.send(mth.to_sym, *params) }
end
end
@Marchino
Marchino / wwwt.rb
Created July 18, 2011 09:46
what's wrong with this?
class ItemImage < ActiveRecord::Base
belongs_to :item, :polymorphic => true
mount_uploader :image, ImageUploader
scope :the_cover, where(:cover => true)
end
class Book < ActiveRecord::Base
has_many :item_images, :as => :item
end
@Marchino
Marchino / core_extensions.rb
Created July 29, 2011 13:47
Search with squeel
class String
def to_query_params
words = []
self.split(/\W/).each do |word|
words << "%#{word}%"
end
return words
end
end
@Marchino
Marchino / mmplus.rb
Created September 9, 2011 09:55
Module inclusion
module Mmplus
module Models
autoload :Items, 'mmplus/models/items.rb'
autoload :Askable, 'mmplus/models/askable.rb'
autoload :Profile, 'mmplus/models/profile.rb'
autoload :ShippingCosts, 'mmplus/models/shipping_costs.rb'
autoload :BookshopConstraints, 'mmplus/models/bookshop_constraints.rb'
end
module Controllers
autoload :Search, 'mmplus/controllers/search.rb'
module Mmplus
module Controllers
module Items
extend ActiveSupport::Concern
included do
before_filter :set_instance
def index
do_search(params[:search] || {}, @instance)
end
class Items::RareBooksController < Items::ItemsController
include Mmplus::Controllers::Items
end