Skip to content

Instantly share code, notes, and snippets.

@marcusmalmberg
Created November 14, 2012 16:51
Show Gist options
  • Save marcusmalmberg/4073266 to your computer and use it in GitHub Desktop.
Save marcusmalmberg/4073266 to your computer and use it in GitHub Desktop.
Useful stuff for project
Permalinks/Pretty urls:
https://github.com/norman/friendly_id
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Globalization (untested):
https://github.com/svenfuchs/globalize3
CMS:
http://activeadmin.info/
Active_link_to:
https://github.com/twg/active_link_to
Localized routes:
https://github.com/kwi/i18n_routing
# lib/array_iterator.rb
# Contains some useful iteration methods that's not provided by Rails by defualt.
#
# One can for instance step through the items in the array by
# using a "next" method instead of a loop.
# Usage:
# require 'array_iterator'
# my_array = [1,2,3,4,5]
# array_itr = ArrayIterator.new(my_array)
# array_itr.next # => 1
# array_itr.next # => 2
# array_itr.step
# array_itr.next # => 4
class ArrayIterator
@iterator_content = []
@iterator_content_count = 0
@position = -1
def initialize(ary)
@iterator_content = ary
@iterator_content_count = @iterator_content.count
@position = -1
end
def next
@position = @position + 1
if @position < @iterator_content_count
return @iterator_content[@position]
end
return nil
end
def step(length = 1)
@postion = @position + length
end
def reset
@position = -1
end
end
# Methods to catch 404, 500 and all other errors that
# can occur and render a template with the application layout.
# config/routes.rb
unless Rails.application.config.consider_all_requests_local
match '*not_found', to: 'application#render_missing_page'
end
# app/controllers/application_controller.rb
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: lambda { |exception| render_error 500, exception }
rescue_from ActionController::RoutingError, ActionController::UnknownController, ::AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
end
def render_missing_page
render :template => "shared/404page", :layout => true, :status => 404
end
private
def render_error(status, exception)
respond_to do |format|
format.html { render template: "shared/#{status}page", layout: 'layouts/application', status: status }
format.all { render nothing: true, status: status }
end
end
# app/views/shared/404page.html.erb
Page missing.
# app/views/shared/500page.html.erb
Something went wrong.
# app/controllers/application_controller.rb
before_filter :set_locale
def set_locale
locale = params[:locale]
I18n.locale = LOCALES.include?(locale) ? locale : I18n.default_locale # Where LOCALES is a constant array containing all available locales for this app
end
def default_url_options(options={})
{ :locale => I18n.locale }
end
# any/other/file.rb
locale = params[:locale]
# or
locale = I18n.locale
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment