interpreter 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
1
  require 'bundler'
2
2
  Bundler::GemHelper.install_tasks
3
3
 
4
- task :default => "test:all"
4
+ task :default => :test
5
5
 
6
6
  namespace :test do
7
7
  desc "Run all cucumber features"
8
- task :all do
8
+ task :cukes do
9
9
  system "cucumber test/dummy/features"
10
10
  end
11
11
 
@@ -14,3 +14,12 @@ namespace :test do
14
14
  system "rake test:prepare -f test/dummy/Rakefile"
15
15
  end
16
16
  end
17
+
18
+ require 'rake/testtask'
19
+
20
+ Rake::TestTask.new(:test) do |t|
21
+ t.libs << 'lib'
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
@@ -2,17 +2,11 @@ class Interpreter::TranslationsController < ApplicationController
2
2
  layout "translations"
3
3
 
4
4
  def index
5
- @categories = InterpreterTranslation.categories
6
- if params[:category]
7
- @translations = InterpreterTranslation.find_like_key("en.#{params[:category]}.*")
8
- else
9
- @translations = InterpreterTranslation.all
10
- end
5
+ @translations = InterpreterTranslation.all
11
6
  end
12
7
 
13
8
  def search
14
- @categories = InterpreterTranslation.categories
15
- @translations = InterpreterTranslation.find_like_key params[:query]
9
+ @translations = InterpreterTranslation.find_all_by_value_like params[:query]
16
10
  @search_notice = "#{@translations.length} translation#{'s' if @translations.length > 1} found."
17
11
  render :action => :index
18
12
  end
@@ -21,16 +15,8 @@ class Interpreter::TranslationsController < ApplicationController
21
15
  @translation = InterpreterTranslation.new
22
16
  end
23
17
 
24
- def show
25
- @key = params[:id].gsub('-','.')
26
- @translations = InterpreterTranslation.find_all_by_key(@key)
27
- end
28
-
29
18
  def create
30
- @translation = InterpreterTranslation.new
31
- @translation.locale = params[:interpreter_translation][:locale]
32
- @translation.key = params[:interpreter_translation][:key]
33
- @translation.value = params[:interpreter_translation][:value]
19
+ @translation = InterpreterTranslation.new params[:interpreter_translation]
34
20
  if @translation.save
35
21
  redirect_to interpreter_translations_url, :notice => "Translation added."
36
22
  else
@@ -38,9 +24,28 @@ class Interpreter::TranslationsController < ApplicationController
38
24
  end
39
25
  end
40
26
 
27
+ def edit
28
+ @translation = InterpreterTranslation.find_by_key(params[:id].gsub('-','.'))
29
+ end
30
+
31
+ def update
32
+ @translation = InterpreterTranslation.find_by_key(parse_id(params[:id]))
33
+ @translation.update_attributes(params[:interpreter_translation])
34
+ if @translation.save
35
+ redirect_to interpreter_translations_url, :notice => "Translation updated."
36
+ else
37
+ render :action => :edit
38
+ end
39
+ end
40
+
41
41
  def destroy
42
42
  count = InterpreterTranslation.destroy(params[:id].gsub('-','.'))
43
- redirect_to :back, :notice => "#{count} translation#{'s' if count > 1} destroyed."
43
+ redirect_to :back, :notice => "Translation destroyed."
44
44
  end
45
45
 
46
+ protected
47
+
48
+ def parse_id id
49
+ id.gsub('-','.')
50
+ end
46
51
  end
@@ -1,9 +1,4 @@
1
- class InterpreterTranslation < Interpreter::Base
2
- attributes :locale, :key, :value
3
-
4
- validates :locale, :presence => true
5
- validates :key, :presence => true
6
- validates :value, :presence => true
1
+ class InterpreterTranslation < Interpreter::Translation
7
2
 
8
3
  def human_name
9
4
  "Translation"
@@ -1,25 +1,14 @@
1
1
  :css
2
- div#translations .key{
3
- float:left;
4
- width:400px;
2
+ table#translations{
3
+ width:100%;
5
4
  overflow:hidden;
6
- padding-left:10px;
7
5
  }
8
6
 
9
- div#translations .value{
10
- float:left;
11
- overflow:hidden;
12
- padding-left:10px;
7
+ table#translations td{
8
+ padding:0px;
9
+ margin:0px;
13
10
  }
14
11
 
15
- div#translations .links{
16
- float:right;
17
- padding-bottom:10px;
18
- }
19
- div.translation{
20
- overflow: auto;
21
- width: 100%
22
- }
23
- div.translation:hover{
12
+ tr.translation:hover{
24
13
  background:#eee;
25
14
  }
@@ -1,16 +1,13 @@
1
1
  = form_for @translation do |f|
2
2
  = render :partial => 'errors', :locals => {:object => @translation}
3
- .field
4
- = f.label :locale
5
- %br
6
- = f.text_field :locale
7
3
  .field
8
4
  = f.label :key
9
5
  %br
10
6
  = f.text_field :key
11
- .field
12
- = f.label :value
13
- %br
14
- = f.text_field :value
7
+ - InterpreterTranslation.available_locales.each do |locale|
8
+ .field
9
+ = f.label locale
10
+ %br
11
+ = f.text_field locale
15
12
  .actions
16
13
  = f.submit "Save"
@@ -0,0 +1,6 @@
1
+ %h1= link_to 'Translations', interpreter_translations_url
2
+
3
+ %h2 Edit Translation
4
+ = render :partial => "form"
5
+
6
+ = link_to "Cancel", interpreter_translations_path
@@ -9,20 +9,18 @@
9
9
  .actions
10
10
  = submit_tag "Search"
11
11
 
12
- %h3 Categories
13
- #categories
14
- - @categories.each do |c|
15
- = link_to c.capitalize, category_interpreter_translations_path(c)
16
-
17
12
  %p= link_to "New Translation", new_interpreter_translation_path
18
13
 
19
- #translations
20
- - @translations.each do |t|
21
- .translation{:id => t.id}
22
- .key= t.id
23
- .value= t.value
24
- .links
25
- = link_to 'Show', interpreter_translation_path(t.key.gsub('.','-'))
26
- = link_to 'Remove', interpreter_translation_path(t.key.gsub('.','-')), :method => :delete
27
-
28
- %p= link_to "New Translation", new_interpreter_translation_path
14
+ %table#translations{:cellpadding => 0, :cellspacing => 0}
15
+ %thead
16
+ %tr
17
+ - InterpreterTranslation.available_locales.each do |locale|
18
+ %td= locale
19
+ %tbody
20
+ - @translations.each do |t|
21
+ %tr.translation{:id => t.key}
22
+ - InterpreterTranslation.available_locales.each do |locale|
23
+ %td.locale= t.send(locale)
24
+ %td.links
25
+ = link_to 'Edit', edit_interpreter_translation_path(t)
26
+ = link_to 'Remove', interpreter_translation_path(t), :method => :delete
@@ -1,3 +1,5 @@
1
+ %h1= link_to 'Translations', interpreter_translations_url
2
+
1
3
  %h2 Add Translation
2
4
  = render :partial => "form"
3
5
 
@@ -3,7 +3,6 @@ Rails.application.routes.draw do
3
3
  resources :translations do
4
4
  collection do
5
5
  post 'search'
6
- get '/group/:category' => 'translations#index', :as => 'category'
7
6
  end
8
7
  end
9
8
  end
@@ -1,5 +1,6 @@
1
1
  module Interpreter
2
2
  autoload :Base, "interpreter/base"
3
+ autoload :Translation, "interpreter/translation"
3
4
 
4
5
  mattr_reader :backend
5
6
  @@backend = nil
@@ -16,6 +16,12 @@ class Interpreter::Base
16
16
  self._attributes += names
17
17
  end
18
18
 
19
+ def self.locales(*names)
20
+ attr_accessor *names
21
+ define_attribute_methods names
22
+ self._attributes += names
23
+ end
24
+
19
25
  def attributes
20
26
  self._attributes.inject({}) do |hash, attr|
21
27
  hash[attr.to_s] = send(attr)
@@ -24,80 +30,11 @@ class Interpreter::Base
24
30
  end
25
31
 
26
32
  def persisted?
27
- if id?
28
- Interpreter.backend.get(id).nil? ? false : true
29
- else
30
- false
31
- end
32
- end
33
-
34
- def id
35
- locale + '.' + key
36
- end
37
-
38
- def id?
39
- locale? and key?
40
- end
41
-
42
- def save
43
- if self.valid?
44
- Interpreter.backend.set("#{locale}.#{key}", value)
45
- else
46
- return false
47
- end
48
- end
49
-
50
- def ==(other)
51
- self.locale == other.locale and self.key == other.key and self.value == other.value
52
- end
53
-
54
- def self.all
55
- collection = []
56
- Interpreter.backend.keys.each do |k|
57
- obj = find_by_key(k)
58
- if obj and obj.valid?# and obj.child_keys.empty?
59
- collection << obj
60
- end
61
- end
62
- collection.sort{|a, b| a.key <=> b.key}
63
- end
64
-
65
- def self.find_all_by_key key
66
- result = {}
67
- Interpreter.backend.keys("*.#{key}").map{|k| find_by_key(k)}.compact.each do |t|
68
- result[t.locale] = t.value
69
- end
70
- return result
71
- end
72
-
73
- def self.find_by_key full_key
74
- locale = full_key.split('.')[0]
75
- key = full_key.gsub("#{locale}.",'')
76
- obj = self.new
77
- obj.locale = locale
78
- obj.key = key
79
- obj.value = Interpreter.backend.get(full_key)
80
- obj.valid? ? obj : nil
81
- end
82
-
83
- def self.find_like_key str
84
- Interpreter.backend.keys("*#{str}*").map{|k| find_by_key(k)}.compact.sort{|a, b| a.locale <=> b.locale}
85
- end
86
-
87
- def child_keys
88
- Interpreter.backend.keys("#{id}.*")
89
- end
90
-
91
- def self.destroy key
92
- Interpreter.backend.keys("*.#{key}").sum{|k| Interpreter.backend.del(k)}
93
- end
94
-
95
- def self.categories
96
- Interpreter.backend.keys.map{|k| k.split('.')[1]}.uniq
33
+ false
97
34
  end
98
35
 
99
- def self.available_locales
100
- Interpreter.locales
36
+ def new_record?
37
+ !persisted?
101
38
  end
102
39
 
103
40
  protected
@@ -0,0 +1,104 @@
1
+ class Interpreter::Translation < Interpreter::Base
2
+ attributes :key
3
+ locales *Interpreter.locales
4
+
5
+ validates :key, :presence => true
6
+
7
+ def initialize options={}
8
+ self.key = options[:key]
9
+ self.class.available_locales.each do |locale|
10
+ self.send("#{locale}=", options[locale])
11
+ end
12
+ end
13
+
14
+ def persisted?
15
+ if key?
16
+ Interpreter.backend.keys("??.#{key}").present?
17
+ else
18
+ super
19
+ end
20
+ end
21
+
22
+ def id
23
+ self.key.gsub('.','-')
24
+ end
25
+
26
+ def save
27
+ if self.valid?
28
+ self.class.available_locales.each do |locale|
29
+ value = self.send(locale)
30
+ Interpreter.backend.set("#{locale}.#{key}", value.to_json) unless value.nil?
31
+ end
32
+ else
33
+ return false
34
+ end
35
+ end
36
+
37
+ def update_attributes options={}
38
+ self.key = options[:key]
39
+ self.class.available_locales.each do |locale|
40
+ self.send("#{locale}=", options[locale])
41
+ end
42
+ end
43
+
44
+ def ==(other)
45
+ self.key == other.key
46
+ end
47
+
48
+ def self.all
49
+ result = []
50
+ Interpreter.backend.keys("??.*").each do |full_key|
51
+ key, locale, value = parse_key(full_key)
52
+
53
+ obj = result.select{|r| r.key == key }.first || self.new
54
+ obj.send("#{locale}=", value)
55
+ obj.send('key=', key)
56
+ result << obj
57
+ end
58
+ return result.uniq
59
+ end
60
+
61
+ def self.find_by_key key
62
+ collection = Interpreter.backend.keys("??.#{key}")
63
+ if collection.present?
64
+ obj = self.new
65
+ obj.send('key=', key)
66
+ collection.each do |full_key|
67
+ key, locale, value = parse_key(full_key)
68
+ obj.send("#{locale}=", value)
69
+ end
70
+ return obj
71
+ else
72
+ return nil
73
+ end
74
+ end
75
+
76
+ def self.find_all_by_value_like str
77
+ keys = []
78
+ Interpreter.backend.keys.each do |full_key|
79
+ if Interpreter.backend.get(full_key).downcase.include?(str.downcase)
80
+ keys << parse_key(full_key).first
81
+ end
82
+ end
83
+ keys.map{|k| find_by_key k}
84
+ end
85
+
86
+ def self.destroy key
87
+ Interpreter.backend.keys("??.#{key}").sum{|k| Interpreter.backend.del(k)}
88
+ end
89
+
90
+ def self.available_locales
91
+ Interpreter.locales
92
+ end
93
+
94
+ protected
95
+
96
+ def self.parse_key full_key
97
+ arr = full_key.split('.')
98
+ locale = arr.shift
99
+ key = arr.join('.')
100
+ value = Interpreter.backend.get(full_key)
101
+ value = ActiveSupport::JSON.decode(value) unless value.nil?
102
+ return [key, locale, value]
103
+ end
104
+ end
@@ -1,3 +1,3 @@
1
1
  module Interpreter
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class ComplianceTest < ActiveSupport::TestCase
4
+ include ActiveModel::Lint::Tests
5
+ def setup
6
+ @model = InterpreterTranslation.new
7
+ end
8
+ end
@@ -1,5 +1,7 @@
1
1
  # Load the rails application
2
2
  require File.expand_path('../application', __FILE__)
3
3
 
4
+ TRANSLATIONS_BACKEND = Redis.new(:db => 9)
5
+
4
6
  # Initialize the rails application
5
7
  Dummy::Application.initialize!
@@ -1,4 +1,4 @@
1
1
  Interpreter.setup do |config|
2
- config.backend = Redis.new(:db => 9)
3
- config.locales = [:en, :pt, :sp]
2
+ config.backend = TRANSLATIONS_BACKEND
3
+ config.locales = [:en, :pt, :es]
4
4
  end
@@ -5,39 +5,52 @@ Feature: Manage translations
5
5
 
6
6
  Scenario: Add new translation
7
7
  Given I am on the new interpreter translation page
8
- When I fill in "Locale" with "locale 1"
9
- And I fill in "Key" with "key 1"
10
- And I fill in "Value" with "value 1"
8
+ When I fill in "Key" with "key1"
9
+ And I fill in "En" with "value1"
10
+ And I fill in "Pt" with "value2"
11
+ And I fill in "Es" with "value3"
11
12
  And I press "Save"
12
13
  Then I should see "Translation added."
13
- And I should see "locale 1"
14
- And I should see "key 1"
15
- And I should see "value 1"
14
+ And I should see "value1"
15
+ And I should see "value2"
16
+ And I should see "value3"
16
17
 
17
18
  Scenario: Edit translation
18
- Given a translation is present with key: hello, value: hello and locale: en
19
- When I go to the new interpreter translation page
20
- And I fill in "Key" with "hello"
21
- And I fill in "Value" with "hello again"
22
- And I fill in "Locale" with "en"
19
+ Given a translation is present with key: "hello", en: "hello", pt: "hey" and es: "hoola"
20
+ And I am on the interpreter translations page
21
+ When I follow "Edit" within "#hello"
22
+ And I fill in "En" with "hello again"
23
+ And I fill in "Pt" with "Hey there"
23
24
  And I press "Save"
24
- Then I should see "hello"
25
+ Then I should see "Translation updated."
25
26
  And I should see "hello again"
27
+ And I should see "Hey there"
26
28
 
27
29
  Scenario: Remove translation
28
- Given a translation is present with key: hello, value: hello and locale: en
30
+ Given a translation is present with key: "hello", en: "hello", pt: "hey" and es: "hoola"
29
31
  When I go to the interpreter translations page
30
- Then I should see "hello"
32
+ Then I should see "hello" within "#translations"
31
33
  And I should see "Remove"
32
34
  When I follow "Remove"
33
- Then I should not see "hello"
34
- And I should see "1 translation destroyed."
35
+ Then I should not see "hello" within "#translations"
36
+ And I should see "Translation destroyed."
35
37
  And I should be on the interpreter translations page
36
38
 
37
39
  Scenario: Search translations
38
- Given a translation is present with key: hello, value: hello and locale: en
40
+ Given a translation is present with key: "hello", en: "hello", pt: "hey" and es: "hoola"
39
41
  When I go to the interpreter translations page
40
42
  And I fill in "query" with "hello" within "#search_translations"
41
43
  And I press "Search"
42
44
  Then I should see "hello" within "#translations"
43
45
  And I should see "1 translation found."
46
+
47
+ Scenario: View all translations for each key on home page
48
+ Given a translation is present with key: "key1", en: "hello1", pt: "hey1" and es: "hoola1"
49
+ And a translation is present with key: "key2", en: "hello2", pt: "hey2" and es: "hoola2"
50
+ When I go to the interpreter translations page
51
+ Then I should see "hello1" within "#key1"
52
+ And I should see "hey1" within "#key1"
53
+ And I should see "hoola1" within "#key1"
54
+ And I should see "hello2" within "#key2"
55
+ And I should see "hey2" within "#key2"
56
+ And I should see "hoola2" within "#key2"
@@ -13,10 +13,8 @@ Then /^I should see the following translations:$/ do |expected_translations_tabl
13
13
  expected_translations_table.diff!(tableish('table tr', 'td,th'))
14
14
  end
15
15
 
16
- Given /^a translation is present with key: (.+), value: (.+) and locale: (.+)$/ do |key, value, locale|
17
- i = InterpreterTranslation.new
18
- i.locale = locale
19
- i.key = key
20
- i.value = value
16
+ Given /^a translation is present with key: "([^"]*)", en: "([^"]*)", pt: "([^"]*)" and es: "([^"]*)"$/ do |key, en, pt, es|
17
+ i = InterpreterTranslation.new(:key => key, :en => en, :pt => pt, :es => es)
21
18
  i.save
22
19
  end
20
+
@@ -0,0 +1,4 @@
1
+ # Make sure we flush redis database
2
+ After do
3
+ TRANSLATIONS_BACKEND.flushdb
4
+ end
@@ -1,7 +1,7 @@
1
1
  namespace :test do
2
2
  desc "Run all cucumber features"
3
3
  task :prepare do
4
- system "bundle update"
4
+ system "bundle install"
5
5
  Rake::Task["db:create"].invoke
6
6
  Rake::Task["db:migrate"].invoke
7
7
  Rake::Task["db:test:prepare"].invoke
@@ -0,0 +1,22 @@
1
+ # Configure Rails Envinronment
2
+ ENV["RAILS_ENV"] = "test"
3
+
4
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
+ require "rails/test_help"
6
+
7
+ ActionMailer::Base.delivery_method = :test
8
+ ActionMailer::Base.perform_deliveries = true
9
+ ActionMailer::Base.default_url_options[:host] = "test.com"
10
+
11
+ Rails.backtrace_cleaner.remove_silencers!
12
+
13
+ # Configure capybara for integration testing
14
+ require "capybara/rails"
15
+ Capybara.default_driver = :rack_test
16
+ Capybara.default_selector = :css
17
+
18
+ # Run any available migration
19
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
20
+
21
+ # Load support files
22
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: interpreter
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 0.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jagdeep Singh
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-17 00:00:00 +05:30
13
+ date: 2011-05-21 00:00:00 +05:30
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -56,15 +56,17 @@ files:
56
56
  - app/views/interpreter/shared/_css.html.haml
57
57
  - app/views/interpreter/translations/_errors.html.haml
58
58
  - app/views/interpreter/translations/_form.html.haml
59
+ - app/views/interpreter/translations/edit.html.haml
59
60
  - app/views/interpreter/translations/index.html.haml
60
61
  - app/views/interpreter/translations/new.html.haml
61
- - app/views/interpreter/translations/show.html.haml
62
62
  - app/views/layouts/translations.html.haml
63
63
  - config/routes.rb
64
64
  - interpreter.gemspec
65
65
  - lib/interpreter.rb
66
66
  - lib/interpreter/base.rb
67
+ - lib/interpreter/translation.rb
67
68
  - lib/interpreter/version.rb
69
+ - test/compliance_test.rb
68
70
  - test/dummy/.gitignore
69
71
  - test/dummy/Gemfile
70
72
  - test/dummy/Rakefile
@@ -97,6 +99,7 @@ files:
97
99
  - test/dummy/features/step_definitions/web_steps.rb
98
100
  - test/dummy/features/support/env.rb
99
101
  - test/dummy/features/support/factory_girl.rb
102
+ - test/dummy/features/support/hooks.rb
100
103
  - test/dummy/features/support/paths.rb
101
104
  - test/dummy/features/support/pickle.rb
102
105
  - test/dummy/features/support/selectors.rb
@@ -117,6 +120,7 @@ files:
117
120
  - test/dummy/script/cucumber
118
121
  - test/dummy/script/rails
119
122
  - test/dummy/vendor/plugins/.gitkeep
123
+ - test/test_helper.rb
120
124
  has_rdoc: true
121
125
  homepage: https://github.com/jagdeep/interpreter
122
126
  licenses: []
@@ -146,6 +150,7 @@ signing_key:
146
150
  specification_version: 3
147
151
  summary: Provides an interface for managing translations
148
152
  test_files:
153
+ - test/compliance_test.rb
149
154
  - test/dummy/.gitignore
150
155
  - test/dummy/Gemfile
151
156
  - test/dummy/Rakefile
@@ -178,6 +183,7 @@ test_files:
178
183
  - test/dummy/features/step_definitions/web_steps.rb
179
184
  - test/dummy/features/support/env.rb
180
185
  - test/dummy/features/support/factory_girl.rb
186
+ - test/dummy/features/support/hooks.rb
181
187
  - test/dummy/features/support/paths.rb
182
188
  - test/dummy/features/support/pickle.rb
183
189
  - test/dummy/features/support/selectors.rb
@@ -198,3 +204,4 @@ test_files:
198
204
  - test/dummy/script/cucumber
199
205
  - test/dummy/script/rails
200
206
  - test/dummy/vendor/plugins/.gitkeep
207
+ - test/test_helper.rb
@@ -1,9 +0,0 @@
1
- %h1 View Translations
2
-
3
- %h2= @key
4
-
5
- #translations
6
- - InterpreterTranslation.available_locales.each do |locale|
7
- .translation
8
- .locale= locale
9
- .value= @translations[locale.to_s]