interpreter 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,12 +2,45 @@ class Interpreter::TranslationsController < ApplicationController
2
2
  layout "translations"
3
3
 
4
4
  def index
5
- @translations = Interpreter::Translation.all
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
11
+ end
12
+
13
+ def search
14
+ @categories = InterpreterTranslation.categories
15
+ @translations = InterpreterTranslation.find_like_key params[:query]
16
+ @search_notice = "#{@translations.length} translation#{'s' if @translations.length > 1} found."
17
+ render :action => :index
18
+ end
19
+
20
+ def new
21
+ @translation = InterpreterTranslation.new
22
+ end
23
+
24
+ def show
25
+ @key = params[:id].gsub('-','.')
26
+ @translations = InterpreterTranslation.find_all_by_key(@key)
6
27
  end
7
28
 
8
29
  def create
9
- Interpreter::Translation.create(params[:locale], params[:key], params[:value])
10
- redirect_to interpreter_translations_url, :notice => "Translation added."
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]
34
+ if @translation.save
35
+ redirect_to interpreter_translations_url, :notice => "Translation added."
36
+ else
37
+ render :action => :new
38
+ end
39
+ end
40
+
41
+ def destroy
42
+ count = InterpreterTranslation.destroy(params[:id].gsub('-','.'))
43
+ redirect_to :back, :notice => "#{count} translation#{'s' if count > 1} destroyed."
11
44
  end
12
45
 
13
46
  end
@@ -0,0 +1,11 @@
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
7
+
8
+ def human_name
9
+ "Translation"
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ :css
2
+ div#translations .key{
3
+ float:left;
4
+ width:400px;
5
+ overflow:hidden;
6
+ padding-left:10px;
7
+ }
8
+
9
+ div#translations .value{
10
+ float:left;
11
+ overflow:hidden;
12
+ padding-left:10px;
13
+ }
14
+
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{
24
+ background:#eee;
25
+ }
@@ -0,0 +1,8 @@
1
+ - if object.errors.any?
2
+ #error_explanation
3
+ %h2
4
+ = pluralize object.errors.count, "error"
5
+ = "prohibited this #{object.human_name} from being saved:"
6
+ %ul
7
+ - object.errors.full_messages.each do |msg|
8
+ %li= msg
@@ -1,16 +1,16 @@
1
- = form_tag interpreter_translations_path do
2
-
1
+ = form_for @translation do |f|
2
+ = render :partial => 'errors', :locals => {:object => @translation}
3
3
  .field
4
- = label_tag :locale
4
+ = f.label :locale
5
5
  %br
6
- = text_field_tag :locale
6
+ = f.text_field :locale
7
7
  .field
8
- = label_tag :key
8
+ = f.label :key
9
9
  %br
10
- = text_field_tag :key
10
+ = f.text_field :key
11
11
  .field
12
- = label_tag :value
12
+ = f.label :value
13
13
  %br
14
- = text_field_tag :value
14
+ = f.text_field :value
15
15
  .actions
16
- = submit_tag "Save"
16
+ = f.submit "Save"
@@ -1,16 +1,28 @@
1
- %h1 Translations
2
- #translations
3
- .header
4
- .key Key
5
- .value Value
6
- %br.clearfloat
7
- .body
8
- - @translations.each do |t|
9
- .translation
10
- .key= t.key
11
- .value= t.value
12
- %br.clearfloat
1
+ %h1= link_to 'Translations', interpreter_translations_url
2
+
3
+ %p= @search_notice
4
+
5
+ #search_translations
6
+ = form_tag search_interpreter_translations_path do
7
+ .field
8
+ = text_field_tag :query
9
+ .actions
10
+ = submit_tag "Search"
13
11
 
12
+ %h3 Categories
13
+ #categories
14
+ - @categories.each do |c|
15
+ = link_to c.capitalize, category_interpreter_translations_path(c)
16
+
17
+ %p= link_to "New Translation", new_interpreter_translation_path
18
+
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
14
27
 
15
- %h2 Add Translation
16
- = render :partial => "form"
28
+ %p= link_to "New Translation", new_interpreter_translation_path
@@ -0,0 +1,4 @@
1
+ %h2 Add Translation
2
+ = render :partial => "form"
3
+
4
+ = link_to "Cancel", interpreter_translations_path
@@ -0,0 +1,9 @@
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]
@@ -5,6 +5,7 @@
5
5
  = stylesheet_link_tag 'translations'
6
6
  = javascript_include_tag :defaults
7
7
  = csrf_meta_tag
8
+ = render :partial => "/interpreter/shared/css"
8
9
  %body
9
10
  - flash.each do |name, msg|
10
11
  = content_tag :div, msg.html_safe, :id => "flash_#{name}"
@@ -1,5 +1,10 @@
1
1
  Rails.application.routes.draw do
2
2
  namespace :interpreter do
3
- resources :translations
3
+ resources :translations do
4
+ collection do
5
+ post 'search'
6
+ get '/group/:category' => 'translations#index', :as => 'category'
7
+ end
8
+ end
4
9
  end
5
10
  end
@@ -1,9 +1,12 @@
1
1
  module Interpreter
2
- autoload :Translation, "interpreter/translation"
2
+ autoload :Base, "interpreter/base"
3
3
 
4
4
  mattr_reader :backend
5
5
  @@backend = nil
6
6
 
7
+ mattr_reader :locales
8
+ @@locales = I18n.backend.available_locales
9
+
7
10
  class Engine < Rails::Engine
8
11
  end
9
12
 
@@ -15,4 +18,8 @@ module Interpreter
15
18
  @@backend = backend
16
19
  I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(@@backend), I18n.backend)
17
20
  end
21
+
22
+ def self.locales=(locales = [])
23
+ @@locales = locales
24
+ end
18
25
  end
@@ -0,0 +1,108 @@
1
+ class Interpreter::Base
2
+ include ActiveModel::Conversion
3
+ extend ActiveModel::Naming
4
+ extend ActiveModel::Translation
5
+ include ActiveModel::Validations
6
+ include ActiveModel::AttributeMethods
7
+
8
+ class_attribute :_attributes
9
+ self._attributes = []
10
+
11
+ attribute_method_suffix '?'
12
+
13
+ def self.attributes(*names)
14
+ attr_accessor *names
15
+ define_attribute_methods names
16
+ self._attributes += names
17
+ end
18
+
19
+ def attributes
20
+ self._attributes.inject({}) do |hash, attr|
21
+ hash[attr.to_s] = send(attr)
22
+ hash
23
+ end
24
+ end
25
+
26
+ 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
97
+ end
98
+
99
+ def self.available_locales
100
+ Interpreter.locales
101
+ end
102
+
103
+ protected
104
+
105
+ def attribute?(attribute)
106
+ send(attribute).present?
107
+ end
108
+ end
@@ -1,3 +1,3 @@
1
1
  module Interpreter
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,3 +1,4 @@
1
1
  Interpreter.setup do |config|
2
- config.backend = Redis.new
2
+ config.backend = Redis.new(:db => 9)
3
+ config.locales = [:en, :pt, :sp]
3
4
  end
@@ -48,7 +48,7 @@ Dummy::Application.routes.draw do
48
48
 
49
49
  # You can have the root of your site routed with "root"
50
50
  # just remember to delete public/index.html.
51
- # root :to => "welcome#index"
51
+ root :to => "interpreter/translations#index"
52
52
 
53
53
  # See how all your routes lay out with "rake routes"
54
54
 
@@ -3,12 +3,41 @@ Feature: Manage translations
3
3
  As an user
4
4
  I want to view, add and edit translations
5
5
 
6
- Scenario: Register new translation
7
- Given I am on translations page
6
+ Scenario: Add new translation
7
+ Given I am on the new interpreter translation page
8
8
  When I fill in "Locale" with "locale 1"
9
9
  And I fill in "Key" with "key 1"
10
10
  And I fill in "Value" with "value 1"
11
11
  And I press "Save"
12
- Then I should see "locale 1"
12
+ Then I should see "Translation added."
13
+ And I should see "locale 1"
13
14
  And I should see "key 1"
14
15
  And I should see "value 1"
16
+
17
+ 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"
23
+ And I press "Save"
24
+ Then I should see "hello"
25
+ And I should see "hello again"
26
+
27
+ Scenario: Remove translation
28
+ Given a translation is present with key: hello, value: hello and locale: en
29
+ When I go to the interpreter translations page
30
+ Then I should see "hello"
31
+ And I should see "Remove"
32
+ When I follow "Remove"
33
+ Then I should not see "hello"
34
+ And I should see "1 translation destroyed."
35
+ And I should be on the interpreter translations page
36
+
37
+ Scenario: Search translations
38
+ Given a translation is present with key: hello, value: hello and locale: en
39
+ When I go to the interpreter translations page
40
+ And I fill in "query" with "hello" within "#search_translations"
41
+ And I press "Search"
42
+ Then I should see "hello" within "#translations"
43
+ And I should see "1 translation found."
@@ -12,3 +12,11 @@ end
12
12
  Then /^I should see the following translations:$/ do |expected_translations_table|
13
13
  expected_translations_table.diff!(tableish('table tr', 'td,th'))
14
14
  end
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
21
+ i.save
22
+ end
@@ -25,9 +25,6 @@ module NavigationHelpers
25
25
  when /^#{capture_model}(?:'s)? (.+?) page$/ # eg. the forum's posts page
26
26
  polymorphic_path(model($1), :action => $2) # or the forum's edit page
27
27
 
28
- when /translations page/
29
- interpreter_translations_path
30
-
31
28
  # Add more mappings here.
32
29
  # Here is an example that pulls values out of the Regexp:
33
30
  #
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: interpreter
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.3
5
+ version: 0.0.4
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-04 00:00:00 +05:30
13
+ date: 2011-05-17 00:00:00 +05:30
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -52,13 +52,18 @@ files:
52
52
  - README.rdoc
53
53
  - Rakefile
54
54
  - app/controllers/interpreter/translations_controller.rb
55
+ - app/models/interpreter_translation.rb
56
+ - app/views/interpreter/shared/_css.html.haml
57
+ - app/views/interpreter/translations/_errors.html.haml
55
58
  - app/views/interpreter/translations/_form.html.haml
56
59
  - app/views/interpreter/translations/index.html.haml
60
+ - app/views/interpreter/translations/new.html.haml
61
+ - app/views/interpreter/translations/show.html.haml
57
62
  - app/views/layouts/translations.html.haml
58
63
  - config/routes.rb
59
64
  - interpreter.gemspec
60
65
  - lib/interpreter.rb
61
- - lib/interpreter/translation.rb
66
+ - lib/interpreter/base.rb
62
67
  - lib/interpreter/version.rb
63
68
  - test/dummy/.gitignore
64
69
  - test/dummy/Gemfile
@@ -1,21 +0,0 @@
1
- class Interpreter::Translation
2
- attr_reader :locale, :key, :value
3
-
4
- def initialize locale, key, value
5
- @locale = locale
6
- @key = key
7
- @value = value
8
- end
9
-
10
- def self.all
11
- collection = []
12
- Interpreter.backend.keys.each do |key|
13
- collection << self.new(key.split('.')[0], key, Interpreter.backend[key])
14
- end
15
- return collection.sort{|a, b| a.key <=> b.key}
16
- end
17
-
18
- def self.create locale, key, value
19
- I18n.backend.store_translations(locale, { key => value }, :escape => false)
20
- end
21
- end