Skip to content

Instantly share code, notes, and snippets.

View Marchino's full-sized avatar

Marco Crepaldi Marchino

View GitHub Profile
@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 / 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 / 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 / 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 / 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>