From 06bf235dab1c602c886849a034520e062e282bd8 Mon Sep 17 00:00:00 2001 From: Hubert Date: Sun, 13 Mar 2011 11:47:05 +0100 Subject: [PATCH] redis store done, whoah --- Gemfile | 1 + Gemfile.lock | 2 + app/modles/translator/redis_store.rb | 25 ++++++ .../translations_management_spec.rb | 83 +++++++++++-------- spec/dummy/config/initializers/translator.rb | 7 +- spec/spec_helper.rb | 4 - spec/unit/mongo_store_spec.rb | 3 +- spec/unit/redis_store_spec.rb | 21 +++++ 8 files changed, 103 insertions(+), 43 deletions(-) create mode 100644 app/modles/translator/redis_store.rb create mode 100644 spec/unit/redis_store_spec.rb diff --git a/Gemfile b/Gemfile index cb569d2..5326201 100644 --- a/Gemfile +++ b/Gemfile @@ -3,6 +3,7 @@ source "http://rubygems.org" gem "rails", "3.0.5" gem "mongo", "1.1" gem "bson_ext", ">=1.0.5" +gem "redis" if RUBY_VERSION < '1.9' gem "ruby-debug", ">= 0.10.3" diff --git a/Gemfile.lock b/Gemfile.lock index 476fa7e..63ae3c0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,6 +92,7 @@ GEM rake (>= 0.8.7) thor (~> 0.14.4) rake (0.8.7) + redis (2.1.1) rspec (2.5.0) rspec-core (~> 2.5.0) rspec-expectations (~> 2.5.0) @@ -129,6 +130,7 @@ DEPENDENCIES mongo (= 1.1) mongrel (= 1.2.0.pre2) rails (= 3.0.5) + redis rspec-rails steak! timecop (= 0.3.5) diff --git a/app/modles/translator/redis_store.rb b/app/modles/translator/redis_store.rb new file mode 100644 index 0000000..f9a8415 --- /dev/null +++ b/app/modles/translator/redis_store.rb @@ -0,0 +1,25 @@ +module Translator + class RedisStore + def initialize(redis) + @redis = redis + end + + def keys + @redis.keys + end + + def []=(key, value) + value = nil if value.blank? + @redis[key] = ActiveSupport::JSON.encode(value) + end + + def [](key) + @redis[key] + end + + def clear_database + @redis.keys.clone.each {|key| @redis.del key } + end + end +end + diff --git a/spec/acceptance/translations_management_spec.rb b/spec/acceptance/translations_management_spec.rb index eef49f9..760ef70 100644 --- a/spec/acceptance/translations_management_spec.rb +++ b/spec/acceptance/translations_management_spec.rb @@ -7,42 +7,55 @@ I want to } do - background do - visit translations_path - end - - scenario "see translations keys specified in main language yaml file" do - page.should have_content "hello.world" - end - - scenario "see translations provided in language files" do - visit root_path - page.should have_content "Hello world!" - visit root_path(:locale => "pl") - page.should have_content "Witaj, Świecie" - end - - scenario "editing translations" do - within :css, "#pl-hello-world" do - fill_in "value", with: "Elo ziomy" - click_button "Save" + [:mongodb, :redis].each do |db| + context "with #{db}" do + background do + if db == :redis + Translator.current_store = Translator::RedisStore.new(Redis.new) + else + conn = Mongo::Connection.new.db("translator_test").collection("translations") + Translator.current_store = Translator::MongoStore.new(conn) + end + + I18n.backend = Translator.setup_backend(I18n::Backend::Simple.new) + Translator.current_store.clear_database + visit translations_path + end + + scenario "see translations keys specified in main language yaml file" do + page.should have_content "hello.world" + end + + scenario "see translations provided in language files" do + visit root_path + page.should have_content "Hello world!" + visit root_path(:locale => "pl") + page.should have_content "Witaj, Świecie" + end + + scenario "editing translations" do + within :css, "#pl-hello-world" do + fill_in "value", with: "Elo ziomy" + click_button "Save" + end + + within :css, "#en-hello-world" do + fill_in "value", with: "Yo hommies" + click_button "Save" + end + + visit root_path + page.should have_content("Yo hommies") + visit root_path(:locale => "pl") + page.should have_content("Elo ziomy") + end + + scenario "see only app translations by default, Rails ones after changing tab" do + page.should_not have_content("date.formats") + click_link "Framework Translations" + page.should have_content("date.formats") + end end - - within :css, "#en-hello-world" do - fill_in "value", with: "Yo hommies" - click_button "Save" - end - - visit root_path - page.should have_content("Yo hommies") - visit root_path(:locale => "pl") - page.should have_content("Elo ziomy") - end - - scenario "see only app translations by default, Rails ones after changing tab" do - page.should_not have_content("date.formats") - click_link "Framework Translations" - page.should have_content("date.formats") end end diff --git a/spec/dummy/config/initializers/translator.rb b/spec/dummy/config/initializers/translator.rb index 29f8248..10453b7 100644 --- a/spec/dummy/config/initializers/translator.rb +++ b/spec/dummy/config/initializers/translator.rb @@ -1,7 +1,8 @@ -conn = Mongo::Connection.new.db("translator_test").collection("translations") -Translator.current_store = Translator::MongoStore.new(conn) +#conn = Mongo::Connection.new.db("translator_test").collection("translations") +#Translator.current_store = Translator::MongoStore.new(conn) +#Translator.current_store = Translator::RedisStore.new(Redis.new) -I18n.backend = Translator.setup_backend(I18n.backend) +#I18n.backend = Translator.setup_backend(I18n::Backend::Simple.new) #Translator.auth_handler = proc { # authenticate_or_request_with_http_basic do |user_name, password| diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index c150c1e..7f47852 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -26,8 +26,4 @@ # == Mock Framework config.mock_with :rspec - - config.before :each do - Translator.current_store.clear_database - end end diff --git a/spec/unit/mongo_store_spec.rb b/spec/unit/mongo_store_spec.rb index 57a4977..c752029 100644 --- a/spec/unit/mongo_store_spec.rb +++ b/spec/unit/mongo_store_spec.rb @@ -3,7 +3,8 @@ describe Translator::MongoStore do before :each do - @store = Translator.current_store + conn = Mongo::Connection.new.db("translator_test").collection("translations") + @store = Translator::MongoStore.new(conn) @store.clear_database end diff --git a/spec/unit/redis_store_spec.rb b/spec/unit/redis_store_spec.rb new file mode 100644 index 0000000..c6c00ea --- /dev/null +++ b/spec/unit/redis_store_spec.rb @@ -0,0 +1,21 @@ +# encoding: UTF-8 +require 'spec_helper' + +describe Translator::RedisStore do + before :each do + @store = Translator::RedisStore.new(Redis.new) + @store.clear_database + end + + it "should be possible to set translation value" do + @store["pl.hello.world"] = "Witaj, świecie!" + @store["pl.hello.world"].should eql("\"Witaj, \\u015bwiecie!\"") + end + + it "should list all keys" do + @store["pl.hello.world"] = "Witaj, świecie!" + @store["en.hello.world"] = "Hello, World!" + @store.keys.should include("pl.hello.world") + @store.keys.should include("en.hello.world") + end +end