Skip to content

Commit

Permalink
redis store done, whoah
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertlepicki committed Mar 13, 2011
1 parent a794a83 commit 06bf235
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 43 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
25 changes: 25 additions & 0 deletions 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

83 changes: 48 additions & 35 deletions spec/acceptance/translations_management_spec.rb
Expand Up @@ -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

7 changes: 4 additions & 3 deletions 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|
Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Expand Up @@ -26,8 +26,4 @@

# == Mock Framework
config.mock_with :rspec

config.before :each do
Translator.current_store.clear_database
end
end
3 changes: 2 additions & 1 deletion spec/unit/mongo_store_spec.rb
Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions 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

0 comments on commit 06bf235

Please sign in to comment.