Permalink
Browse files

simple localization mechanizm added

  • Loading branch information...
1 parent a3bda1c commit 3a670c867a107971cb470104d7d73025d668b7eb @khustochka committed Jan 9, 2010
View
15 app/controllers/application/application_controller.rb
@@ -6,10 +6,19 @@ class ApplicationController < ActionController::Base
include ErrorController
include SessionController
- #helper :all # include all helpers, all the time
-
protect_from_forgery # See ActionController::RequestForgeryProtection for details
# Scrub sensitive parameters from your log
- # filter_parameter_logging :password
+ filter_parameter_logging :password
+
+ before_filter :set_locale
+
+ private
+ def set_locale
+ I18n.locale = ["en", "uk"].include?(params[:hl]) ? params[:hl] : "ru"
+ end
+
+ def default_url_options(options={})
+ { :hl => I18n.locale } if ["en", "uk"].include?(I18n.locale)
+ end
end
View
8 app/models/taxonomy/taxon.rb
@@ -6,7 +6,7 @@ class Taxon < ActiveRecord::Base
validates_uniqueness_of :name_la, :name_ru, :name_uk
# Class methods
-
+
def self.prepare_hierarchy
proceed_methods = []
initial_model = self
@@ -23,11 +23,15 @@ def self.prepare_hierarchy
end
# Instance methods
-
+
def to_param
name_la
end
+ def name
+ send("name_#{I18n.locale}")
+ end
+
def insert_mind_sorting
latest = (self.respond_to?(:supertaxon) ? self.supertaxon.subtaxa.count : self.class.count) + 1
self.sort = latest if self.sort > latest || self.sort == 0
View
4 app/views/taxa/add_edit.html.haml
@@ -1,8 +1,8 @@
- content_for :page_title do
- = @taxon.name_la.if_present {|s| "#{model_name.capitalize} #{s}"} rescue "#{action_name.humanize} #{model_name}"
+ = @taxon.name_la.if_present! {|s| "#{model_name.capitalize} #{s}"} rescue "#{action_name.humanize} #{model_name}"
%h1
- = @taxon.name_la.if_present {|s| "#{@taxon.name_ru} / #{content_tag :em, s}"} rescue "#{action_name.humanize} #{model_name}"
+ = @taxon.name_la.if_present! {|s| "#{@taxon.name.if_present {|s2| "#{s2} / "}}#{content_tag :em, s}"} rescue "#{action_name.humanize} #{model_name}"
= render( :partial => "taxa/table" )
View
6 lib/core_ext/object.rb
@@ -9,7 +9,11 @@ def if_present(value = nil, &block)
self
end
else
- raise
+ self
end
end
+
+ def if_present!(value = nil, &block)
+ raise "Object not present (nil, false, or empty)." if if_present(value, &block).blank?
+ end
end
View
15 test/unit/core_ext/object_test.rb
@@ -9,13 +9,18 @@ class ObjectTest < ActiveSupport::TestCase
assert_equal "true", temp
# failure tests
- temp = "".if_present { |val| "<b>#{val}</b>" } rescue ""
- assert_equal "", temp
- temp = "".if_present { |val| "<b>#{val}</b>" } rescue "empty"
+ temp = "".if_present! { |val| "<b>#{val}</b>" } rescue "empty"
assert_equal "empty", temp
- temp = nil.if_present { |val| "<b>#{val}</b>" } rescue "empty2"
+ temp = nil.if_present! { |val| "<b>#{val}</b>" } rescue "empty2"
assert_equal "empty2", temp
- temp = false.if_present { |val| "<b>#{val}</b>" } rescue "empty3"
+ temp = false.if_present! { |val| "<b>#{val}</b>" } rescue "empty3"
assert_equal "empty3", temp
+
+ temp = "".if_present { |val| "<b>#{val}</b>" }
+ assert_equal "", temp
+ temp = nil.if_present { |val| "<b>#{val}</b>" }
+ assert_equal nil, temp
+ temp = false.if_present { |val| "<b>#{val}</b>" }
+ assert_equal false, temp
end
end

0 comments on commit 3a670c8

Please sign in to comment.