Skip to content

Commit

Permalink
Fixed tolk to work on 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
drogus committed May 29, 2011
1 parent 96e3ddc commit b51ad4d
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 45 deletions.
8 changes: 4 additions & 4 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
source "http://rubygems.org"

gem "rails", :git => "http://github.com/rails/rails.git"
gem "arel", :git => "http://github.com/rails/arel.git"
#gem "rails", "3.1.0.beta1"
gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'will_paginate', :git => 'https://github.com/JackDanger/will_paginate.git'
gem 'kaminari', :git => "https://github.com/amatsuda/kaminari.git"
gem "ya2yaml"

gem "capybara", :git => "https://github.com/jnicklas/capybara.git"
gem "factory_girl_rails"
gem "capybara", ">= 0.3.9"
gem "sqlite3-ruby", :require => "sqlite3"
gem "mocha"

Expand Down
File renamed without changes.
File renamed without changes.
33 changes: 15 additions & 18 deletions app/models/tolk/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Locale < ActiveRecord::Base
'nl' => 'Dutch',
'no' => 'Norwegian',
'pl' => 'Polish',
'pt-BR' => 'Portuguese (Brazilian)',
'pt-br' => 'Portuguese (Brazilian)',
'pt-PT' => 'Portuguese (Portugal)',
'ro' => 'Romanian',
'ru' => 'Russian',
Expand All @@ -52,6 +52,7 @@ class Locale < ActiveRecord::Base

has_many :phrases, :through => :translations, :class_name => 'Tolk::Phrase'
has_many :translations, :class_name => 'Tolk::Translation', :dependent => :destroy

accepts_nested_attributes_for :translations, :reject_if => proc { |attributes| attributes['text'].blank? }
before_validation :remove_invalid_translations_from_target, :on => :update

Expand Down Expand Up @@ -133,8 +134,7 @@ def phrases_without_translation(page = nil, options = {})
existing_ids = self.translations.all(:select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
phrases = phrases.scoped(:conditions => ['tolk_phrases.id NOT IN (?)', existing_ids]) if existing_ids.present?

result = phrases.paginate({:page => page, :per_page => Phrase.per_page}.merge(options))
Tolk::Phrase.send :preload_associations, result, :translations
result = phrases.page(page).per(Tolk::Phrase.per_page)
result
end

Expand All @@ -148,22 +148,21 @@ def search_phrases(query, scope, page = nil, options = {})
self.translations.containing_text(query)
end

phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')
phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')
phrases = phrases.scoped(:conditions => ['tolk_phrases.id IN(?)', translations.map(&:phrase_id).uniq])
phrases.paginate({:page => page}.merge(options))
phrases.page(page)
end

def search_phrases_without_translation(query, page = nil, options = {})
return phrases_without_translation(page, options) unless query.present?

phrases = Tolk::Phrase.scoped(:order => 'tolk_phrases.key ASC')

found_translations_ids = Tolk::Locale.primary_locale.translations.all(:conditions => ["tolk_translations.text LIKE ?", "%#{query}%"], :select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
existing_ids = self.translations.all(:select => 'tolk_translations.phrase_id').map(&:phrase_id).uniq
phrases = phrases.scoped(:conditions => ['tolk_phrases.id NOT IN (?) AND tolk_phrases.id IN(?)', existing_ids, found_translations_ids]) if existing_ids.present?

result = phrases.paginate({:page => page}.merge(options))
Tolk::Phrase.send :preload_associations, result, :translations
result = phrases.page(page)
result
end

Expand All @@ -189,15 +188,15 @@ def language_name
MAPPING[self.name] || self.name
end

def [](key)
def get(key)
if phrase = Tolk::Phrase.find_by_key(key)
t = self.translations.find_by_phrase_id(phrase.id)
t = self.translations.where(:phrase_id => phrase.id).first
t.text if t
end
end

def translations_with_html
translations = self.translations.all(:conditions => "tolk_translations.text LIKE '%>%' AND
translations = self.translations.all(:conditions => "tolk_translations.text LIKE '%>%' AND
tolk_translations.text LIKE '%<%' AND tolk_phrases.key NOT LIKE '%_html'", :joins => :phrase)
Translation.send :preload_associations, translations, :phrase
translations
Expand All @@ -206,9 +205,9 @@ def translations_with_html
private

def remove_invalid_translations_from_target
self.translations.proxy_target.each do |t|
unless t.valid?
self.translations.proxy_target.delete(t)
self.translations.target.dup.each do |t|
unless t.valid?
self.translations.target.delete(t)
else
t.updated_at = Time.current # Silly hax to fool autosave into saving the record
end
Expand All @@ -218,12 +217,10 @@ def remove_invalid_translations_from_target
end

def find_phrases_with_translations(page, conditions = {})
result = Tolk::Phrase.paginate(:page => page,
result = Tolk::Phrase.page(page).find(:all,
:conditions => { :'tolk_translations.locale_id' => self.id }.merge(conditions),
:joins => :translations, :order => 'tolk_phrases.key ASC')

Tolk::Phrase.send :preload_associations, result, :translations

result.each do |phrase|
phrase.translation = phrase.translations.for(self)
end
Expand Down
6 changes: 4 additions & 2 deletions app/models/tolk/translation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ class Translation < ActiveRecord::Base

scope :containing_text, lambda {|query| where("tolk_translations.text LIKE ?", "%#{query}%") }

serialize :text, :previous_text
serialize :text
validates_presence_of :text, :if => proc {|r| r.primary.blank? && !r.explicit_nil }
validate :check_matching_variables, :if => proc { |tr| tr.primary_translation.present? }

validates_uniqueness_of :phrase_id, :scope => :locale_id

belongs_to :phrase, :class_name => 'Tolk::Phrase'
belongs_to :locale, :class_name => 'Tolk::Locale'
validates_presence_of :locale_id


attr_accessor :force_set_primary_update
before_save :set_primary_updated
Expand Down Expand Up @@ -55,7 +57,7 @@ def value

def self.detect_variables(search_in)
case search_in
when String then Set.new(search_in.scan(/\{\{(\w+)\}\}/).flatten + search_in.scan(/\%\{(\w+)\}/).flatten)
when String then Set.new(search_in.scan(/\{\{(\w+)\}\}/).flatten + search_in.scan(/\%\{(\w+)\}/).flatten)
when Array then search_in.inject(Set[]) { |carry, item| carry + detect_variables(item) }
when Hash then search_in.values.inject(Set[]) { |carry, item| carry + detect_variables(item) }
else Set[]
Expand Down
5 changes: 3 additions & 2 deletions app/views/layouts/tolk/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<%= javascript_include_tag :defaults %>
<% end %>
<%= stylesheet_link_tag "/tolk/reset", "/tolk/screen" %>
<%= stylesheet_link_tag "tolk/reset" %>
<%= stylesheet_link_tag "tolk/screen" %>
<%= yield :head %>
</head>

Expand All @@ -23,7 +24,7 @@
</div>
<%= yield %>
</div>

<script type="text/javascript">
$$('td textarea').each(function(element) {
element.setStyle({height: element.up('td').measure('height')+'px'});
Expand Down
2 changes: 1 addition & 1 deletion app/views/tolk/locales/all.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
<p><%= locale_form.submit "Save changes" %></p>
</div>
<div class="paginate">
<%= will_paginate @phrases %>
<%= paginate @phrases %>
</div>
<% end %>
<% else %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/tolk/locales/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</div>
<% end %>
<div class="paginate">
<%= will_paginate @phrases %>
<%= paginate @phrases %>
</div>
<% else %>
<p style="text-align: left">There aren't any missing or updated phrases that need translation.</p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/tolk/searches/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</div>
<% end %>
<div class="paginate">
<%= will_paginate @phrases %>
<%= paginate @phrases %>
</div>
<% else %>
<p style="text-align: left">No search results.</p>
Expand Down
2 changes: 1 addition & 1 deletion lib/tolk.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'will_paginate'
require 'kaminari'
require 'ya2yaml'
require 'tolk/engine'
require 'tolk/sync'
Expand Down
1 change: 0 additions & 1 deletion test/dummy/config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

# Show full error reports and disable caching
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false

# Don't care if the mailer can't send
Expand Down
3 changes: 3 additions & 0 deletions test/dummy/log/development.log
Original file line number Diff line number Diff line change
Expand Up @@ -616,3 +616,6 @@ Rendered /Users/drogus/.rvm/gems/ruby-1.9.2-p0@rails/bundler/gems/rails-7e4852f8
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
Tolk::Translation Load (0.4ms) SELECT "tolk_translations"."id" FROM "tolk_translations" WHERE ("tolk_translations"."phrase_id" IS NULL) AND ("tolk_translations"."locale_id" IS NULL) LIMIT 1
Tolk::Translation Load (0.3ms) SELECT "tolk_translations"."id" FROM "tolk_translations" WHERE ("tolk_translations"."phrase_id" IS NULL) AND ("tolk_translations"."locale_id" IS NULL) LIMIT 1
Tolk::Locale Load (0.2ms) SELECT "tolk_locales".* FROM "tolk_locales" LIMIT 1
Tolk::Phrase Load (0.2ms) SELECT "tolk_phrases".* FROM "tolk_phrases" WHERE "tolk_phrases"."key" = 'id' LIMIT 1
Tolk::Locale Load (0.2ms) SELECT "tolk_locales".* FROM "tolk_locales" LIMIT 1
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
ENV["RAILS_ENV"] = "test"

require File.expand_path("../dummy/config/environment.rb", __FILE__)
ActiveRecord::IdentityMap.enabled = false
require "rails/test_help"

ActiveSupport::TestCase.fixture_path = Rails.root.to_s + "/../fixtures"
Expand Down
22 changes: 11 additions & 11 deletions test/unit/format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ def setup
def test_all_formats_are_loaded_properly
# TODO : Investigate why the fuck does this test fail
# assert_equal 1, @en['number']
assert_equal 'I am just a stupid string :(', @en['string']
assert_equal [1, 2, 3], @en['number_array']
assert_equal ['sun', 'moon'], @en['string_array']
assert_equal 'I am just a stupid string :(', @en.get('string')
assert_equal [1, 2, 3], @en.get('number_array')
assert_equal ['sun', 'moon'], @en.get('string_array')
end

def test_pluaralization
result = {'other' => 'Hello'}
assert_equal result, @en['pluralization']
assert_equal result, @en.get('pluralization')

assert ! @en['not_pluralization']
assert_equal 'World', @en['not_pluralization.other']
assert_equal 'fifo', @en['not_pluralization.lifo']
assert ! @en.get('not_pluralization')
assert_equal 'World', @en.get('not_pluralization.other')
assert_equal 'fifo', @en.get('not_pluralization.lifo')
end

# def test_specail_activerecord_keys_and_prefixes
Expand All @@ -60,7 +60,7 @@ def test_creating_translations_fails_on_mismatch_with_primary_translation
success.save!
assert_equal [1, 2], success.text
end

def test_creating_translations_fails_with_unmatching_variables
# Check that variable detection works correctly
assert_equal Set['hello', 'world'], ph('variables').translations.primary.variables
Expand Down Expand Up @@ -105,9 +105,9 @@ def test_bulk_translations_update_with_some_invalid_formats

@spanish.reload

assert_equal 'spanish string', @spanish['string']
assert_equal '2', @spanish['number']
assert ! @spanish['string_array']
assert_equal 'spanish string', @spanish.get('string')
assert_equal '2', @spanish.get('number')
assert ! @spanish.get('string_array')
end

def test_bulk_update_saves_unchanged_record
Expand Down
6 changes: 3 additions & 3 deletions test/unit/locale_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class LocaleTest < ActiveSupport::TestCase
end

test "turning locale with nested phrases into a hash" do
assert_equal({ "en" => {
assert_equal({ "en" => {
"number"=>{"human"=>{"format"=>{"precision"=>1}}},
"hello_world" => "Hello World",
"nested" => {
"hello_world" => "Hello World",
"nested" => {
"hello_world" => "Nested Hello World",
"hello_country" => "Nested Hello Country"
}
Expand Down

0 comments on commit b51ad4d

Please sign in to comment.