translation_center 1.7.2 → 2.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +38 -2
  3. data/Rakefile +8 -13
  4. data/app/assets/javascripts/translation_center/application.js +1 -1
  5. data/app/assets/stylesheets/translation_center/application.css +1 -1
  6. data/app/assets/stylesheets/translation_center/translation_keys.css +3 -1
  7. data/app/controllers/translation_center/application_controller.rb +2 -2
  8. data/app/controllers/translation_center/translation_keys_controller.rb +4 -4
  9. data/app/models/translation_center/activity_query.rb +2 -3
  10. data/app/models/translation_center/category.rb +7 -2
  11. data/app/models/translation_center/translation.rb +54 -32
  12. data/app/models/translation_center/translation_key.rb +29 -18
  13. data/app/views/layouts/translation_center/application.html.haml +0 -2
  14. data/app/views/translation_center/translation_keys/_translation_keys.html.haml +3 -1
  15. data/app/views/translation_center/translations/_show.html.haml +3 -3
  16. data/config/routes.rb +3 -3
  17. data/db/development.sqlite3 +0 -0
  18. data/{test/dummy/db/migrate/20130410082611_devise_create_users.rb → db/migrate/20130410082600_devise_create_users.rb} +0 -0
  19. data/{test/dummy/db/migrate/20130410082701_create_articles.rb → db/migrate/20130410082601_create_articles.rb} +0 -0
  20. data/{test/dummy/db/migrate/20130410084711234392_create_translation_center_categories.rb → db/migrate/20130410084602_create_translation_center_categories.rb} +0 -0
  21. data/{test/dummy/db/migrate/20130410084711235054_create_translation_center_translation_keys.rb → db/migrate/20130410084603_create_translation_center_translation_keys.rb} +0 -0
  22. data/{test/dummy/db/migrate/20130410084711235601_create_translation_center_translations.rb → db/migrate/20130410084604_create_translation_center_translations.rb} +0 -0
  23. data/{test/dummy/db/migrate/20130410084711_acts_as_votable_migration.rb → db/migrate/20130410084605_acts_as_votable_migration.rb} +1 -0
  24. data/{test/dummy/db/migrate/20130410084711235602_install_audited.rb → db/migrate/20130410084606_install_audited.rb} +0 -0
  25. data/{test/dummy/db/migrate/20130410113111070575_add_de_status_translation_center_translation_keys.rb → db/migrate/20130410113607_add_de_status_translation_center_translation_keys.rb} +0 -0
  26. data/{test/dummy/db/migrate/20130417134539377014_add_ar_status_translation_center_translation_keys.rb → db/migrate/20130417134608_add_ar_status_translation_center_translation_keys.rb} +0 -0
  27. data/{test/dummy/db/migrate/20130506103956_fix_translation_user_relation.rb → db/migrate/20130506103609_fix_translation_user_relation.rb} +0 -0
  28. data/{test/dummy/db/migrate/20130801102022_add_indicies.rb → db/migrate/20130801102610_add_indicies.rb} +0 -0
  29. data/{test/dummy/db → db}/schema.rb +13 -13
  30. data/{test/dummy/public/favicon.ico → db/test.sqlite3} +0 -0
  31. data/lib/generators/translation_center/add_lang/add_lang_generator.rb +5 -2
  32. data/lib/generators/translation_center/install/install_generator.rb +4 -4
  33. data/lib/translation_center/acts_as_translator.rb +1 -1
  34. data/lib/translation_center/engine.rb +7 -0
  35. data/lib/translation_center/translation_helpers.rb +1 -1
  36. data/lib/translation_center/translations_transfer.rb +6 -5
  37. data/lib/translation_center/version.rb +1 -1
  38. data/spec/controllers/categories_controller_spec.rb +47 -0
  39. data/spec/controllers/center_controller_spec.rb +77 -0
  40. data/spec/controllers/translation_keys_controller_spec.rb +87 -0
  41. data/spec/controllers/translations_controller_spec.rb +70 -0
  42. data/{test → spec}/dummy/README.rdoc +0 -0
  43. data/{test → spec}/dummy/Rakefile +0 -0
  44. data/{test → spec}/dummy/app/assets/javascripts/application.js +0 -1
  45. data/{test → spec}/dummy/app/assets/stylesheets/application.css +0 -1
  46. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  47. data/{test → spec}/dummy/app/helpers/application_helper.rb +0 -0
  48. data/{test → spec}/dummy/app/models/user.rb +2 -5
  49. data/{test → spec}/dummy/app/views/layouts/application.html.erb +0 -1
  50. data/{test → spec}/dummy/config.ru +0 -0
  51. data/{test → spec}/dummy/config/application.rb +10 -5
  52. data/{test → spec}/dummy/config/boot.rb +0 -0
  53. data/spec/dummy/config/database.yml +25 -0
  54. data/{test → spec}/dummy/config/environment.rb +2 -0
  55. data/{test → spec}/dummy/config/environments/development.rb +0 -0
  56. data/{test → spec}/dummy/config/environments/production.rb +0 -0
  57. data/{test → spec}/dummy/config/environments/test.rb +2 -2
  58. data/{test → spec}/dummy/config/initializers/backtrace_silencers.rb +0 -0
  59. data/{test → spec}/dummy/config/initializers/devise.rb +3 -1
  60. data/{test → spec}/dummy/config/initializers/inflections.rb +0 -0
  61. data/{test → spec}/dummy/config/initializers/mime_types.rb +0 -0
  62. data/{test → spec}/dummy/config/initializers/secret_token.rb +1 -1
  63. data/{test → spec}/dummy/config/initializers/session_store.rb +1 -1
  64. data/{test → spec}/dummy/config/initializers/wrap_parameters.rb +1 -1
  65. data/{test → spec}/dummy/config/locales/devise.en.yml +0 -0
  66. data/spec/dummy/config/locales/en.yml +5 -0
  67. data/spec/dummy/config/routes.rb +7 -0
  68. data/{test → spec}/dummy/config/translation_center.yml +23 -6
  69. data/spec/dummy/db/migrate/20130410082611_devise_create_users.translation_center.rb +46 -0
  70. data/spec/dummy/db/migrate/20130410082701_create_articles.translation_center.rb +10 -0
  71. data/spec/dummy/db/migrate/20130410084702_create_translation_center_categories.translation_center.rb +9 -0
  72. data/spec/dummy/db/migrate/20130410084703_create_translation_center_translation_keys.translation_center.rb +14 -0
  73. data/spec/dummy/db/migrate/20130410084704_create_translation_center_translations.translation_center.rb +13 -0
  74. data/spec/dummy/db/migrate/20130410084711_acts_as_votable_migration.translation_center.rb +24 -0
  75. data/spec/dummy/db/migrate/20130410084712_install_audited.translation_center.rb +28 -0
  76. data/spec/dummy/db/migrate/20130410113113_add_de_status_translation_center_translation_keys.translation_center.rb +5 -0
  77. data/spec/dummy/db/migrate/20130417134534_add_ar_status_translation_center_translation_keys.rb +5 -0
  78. data/spec/dummy/db/migrate/20130506103956_fix_translation_user_relation.translation_center.rb +12 -0
  79. data/spec/dummy/db/migrate/20130801102021_add_indicies.translation_center.rb +8 -0
  80. data/spec/dummy/db/schema.rb +111 -0
  81. data/{test → spec}/dummy/public/404.html +0 -0
  82. data/{test → spec}/dummy/public/422.html +0 -0
  83. data/{test → spec}/dummy/public/500.html +0 -0
  84. data/spec/dummy/public/favicon.ico +0 -0
  85. data/{test → spec}/dummy/script/rails +0 -0
  86. data/spec/factories/category_factory.rb +5 -0
  87. data/spec/factories/translation_factory.rb +8 -0
  88. data/spec/factories/translation_key_factory.rb +5 -0
  89. data/spec/factories/user_factory.rb +6 -0
  90. data/spec/factories/vote_factory.rb +13 -0
  91. data/spec/lib/generators/translation_center/add_lang/add_lang_generator_spec.rb +26 -0
  92. data/spec/models/translation_center/category_spec.rb +39 -0
  93. data/spec/models/translation_center/translation_key_spec.rb +145 -0
  94. data/spec/models/translation_center/translation_spec.rb +117 -0
  95. data/spec/schema.rb +112 -0
  96. data/spec/spec_helper.rb +52 -0
  97. data/spec/support/devise.rb +3 -0
  98. metadata +360 -208
  99. data/test/dummy/app/assets/images/translation_center_logo.png +0 -0
  100. data/test/dummy/app/assets/javascripts/articles.js +0 -2
  101. data/test/dummy/app/assets/stylesheets/articles.css +0 -4
  102. data/test/dummy/app/assets/stylesheets/scaffold.css +0 -56
  103. data/test/dummy/app/controllers/application_controller.rb +0 -6
  104. data/test/dummy/app/controllers/articles_controller.rb +0 -85
  105. data/test/dummy/app/helpers/articles_helper.rb +0 -2
  106. data/test/dummy/app/models/article.rb +0 -7
  107. data/test/dummy/app/views/articles/_form.html.haml +0 -16
  108. data/test/dummy/app/views/articles/edit.html.haml +0 -7
  109. data/test/dummy/app/views/articles/index.html.haml +0 -32
  110. data/test/dummy/app/views/articles/new.html.haml +0 -5
  111. data/test/dummy/app/views/articles/show.html.haml +0 -14
  112. data/test/dummy/config/database.yml +0 -42
  113. data/test/dummy/config/locales/ar.yml +0 -6
  114. data/test/dummy/config/locales/de.yml +0 -2
  115. data/test/dummy/config/locales/en.yml +0 -361
  116. data/test/dummy/config/routes.rb +0 -10
  117. data/test/dummy/spec/spec_helper.rb +0 -38
  118. data/test/dummy/test/fixtures/articles.yml +0 -9
  119. data/test/dummy/test/fixtures/posts.yml +0 -9
  120. data/test/dummy/test/fixtures/users.yml +0 -11
  121. data/test/dummy/test/functional/articles_controller_test.rb +0 -49
  122. data/test/dummy/test/functional/posts_controller_test.rb +0 -49
  123. data/test/dummy/test/unit/article_test.rb +0 -7
  124. data/test/dummy/test/unit/helpers/articles_helper_test.rb +0 -4
  125. data/test/dummy/test/unit/helpers/posts_helper_test.rb +0 -4
  126. data/test/dummy/test/unit/post_test.rb +0 -7
  127. data/test/dummy/test/unit/user_test.rb +0 -7
  128. data/test/fixtures/translation_center/categories.yml +0 -7
  129. data/test/fixtures/translation_center/translation_keys.yml +0 -11
  130. data/test/fixtures/translation_center/translations.yml +0 -15
  131. data/test/functional/translation_center/categories_controller_test.rb +0 -51
  132. data/test/functional/translation_center/center_controller_test.rb +0 -9
  133. data/test/functional/translation_center/translation_keys_controller_test.rb +0 -51
  134. data/test/functional/translation_center/translations_controller_test.rb +0 -51
  135. data/test/integration/navigation_test.rb +0 -10
  136. data/test/test_helper.rb +0 -15
  137. data/test/translation_center_test.rb +0 -7
  138. data/test/unit/helpers/translation_center/categories_helper_test.rb +0 -6
  139. data/test/unit/helpers/translation_center/center_helper_test.rb +0 -6
  140. data/test/unit/helpers/translation_center/translation_keys_helper_test.rb +0 -6
  141. data/test/unit/helpers/translation_center/translations_helper_test.rb +0 -6
  142. data/test/unit/translation_center/category_test.rb +0 -9
  143. data/test/unit/translation_center/translation_key_test.rb +0 -9
  144. data/test/unit/translation_center/translation_test.rb +0 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8e4abb561664f8d78d8a31df030415a8201bfd5f
4
- data.tar.gz: 165949a1ad4ede3782f45b221c56f536e19ae2ba
3
+ metadata.gz: 73cc7ca99fab28be936e25cb27ca191d73ea67cf
4
+ data.tar.gz: 6ddc9259b02e7c53a6b7d30a66be2026d4f544a2
5
5
  SHA512:
6
- metadata.gz: e321ff0bf63b43f3e55c7f4a39a17bd66c243526f53736069e663a5ae43c176d9bc9fd03dce2fc9001a701e8869f299fd3fd2df800503c1e395984e4a6b4c239
7
- data.tar.gz: ba4c021ca164d611732ed14ce6f31c41300447efd05ee2cd69dda0e949b4079c8c17ebc61816bb31de79c194427d4ec00465c9b7f6b0efd0b0d2f016545abc8e
6
+ metadata.gz: 6af06086f8b3dc23bbbe7658d041a4d27ce52dc1675032f1cb6808dcb4e7b28b9f0f02917963a5235d9287e0a2dc8331384c3e81322dc03beeea153abc58f598
7
+ data.tar.gz: ad2d4fa823c7713718b38aa8acc06f5ec542be5b61438dea8f2b953e90db4ed5556d1ec51379775760ce336cad668e13a20dcd2ffd5630665f71aecea2cd1c97
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  ## Introduction
7
7
 
8
- Translation Center is a multi lingual web engine for Rails 3 apps. It builds a translation center community with translators and admins from your system users.
8
+ Translation Center is a multi lingual web engine for Rails apps. It builds a translation center community with translators and admins from your system users.
9
9
 
10
10
  ![Alt text](https://raw.github.com/BadrIT/translation_center/master/samples/view_keys.png "View category translations")
11
11
 
@@ -36,7 +36,7 @@ Manage all app translations; collect stats, accept, add, edit, remove translatio
36
36
 
37
37
  ## Getting started
38
38
 
39
- TranslationCenter works with Rails 3.1 onwards. You can add it to your Gemfile with:
39
+ TranslationCenter works with Rails 4.x onwards. You can add it to your Gemfile with:
40
40
 
41
41
  ```ruby
42
42
  gem 'translation_center'
@@ -106,6 +106,37 @@ def can_admin_translations?
106
106
  self.email == 'admin@tc.com'
107
107
  end
108
108
  ```
109
+ ## Rails 3
110
+ If you are Using rails 3 you can revert back to tag 1.7.2
111
+
112
+ ## MongoDB Support
113
+ * Create a SQL database so your app connects to both MongoDB and this new SQL database.
114
+ * Create a new SQL table (ex: translator_users) that will be considered as translator user and has a foreign key for your MongoDB user (ex: mongo_user_id)
115
+ * Add `acts_as_translator` to your ActiverRecord user (ex: TranslatorUser) model not Mogno User
116
+ * Update translation_center.yml to the translator model name if needed
117
+ ```ruby
118
+ tranlator_type: 'TranslatorUser'
119
+ ```
120
+ * You also need to add these methods in an initializer in case Devise is not existing in Mongo, for example `translation_authentication.rb` :
121
+ ```ruby
122
+ module TranslationCenter
123
+ class ApplicationController < ActionController::Base
124
+
125
+ # current_user is needed in views
126
+ helper_method :current_user
127
+
128
+ def authenticate_user!
129
+ # redirect to login if user not signed in
130
+ end
131
+
132
+ def current_user
133
+ # write code that returns the current ActiveRecord user in session
134
+ end
135
+
136
+ end
137
+ end
138
+ ```
139
+ in the current_user method make sure that you return the ActiveRecord user not the Mongo user because that's what we will use in translation center engine.
109
140
 
110
141
  ## How to use
111
142
 
@@ -285,6 +316,11 @@ http://www.youtube.com/watch?v=BTy6ZI31JmU
285
316
 
286
317
  Translation Center is maintained and developed by [BadrIT](http://badrit.com/)
287
318
 
319
+ ## Contributing
320
+
321
+ We hope that you will consider contributing to Translation Center.
322
+ You will usually want to write tests for your changes. To run the test suite, go into Translation Center's top-level directory and run "bundle install" and then "rspec spec"
323
+
288
324
  ## Support
289
325
 
290
326
  Help us make this engine better by submitting any bugs or enhancments in the issues page for this project.
data/Rakefile CHANGED
@@ -12,6 +12,7 @@ rescue LoadError
12
12
  RDoc::Task = Rake::RDocTask
13
13
  end
14
14
 
15
+ desc 'Generating documentation'
15
16
  RDoc::Task.new(:rdoc) do |rdoc|
16
17
  rdoc.rdoc_dir = 'rdoc'
17
18
  rdoc.title = 'TranslationCenter'
@@ -20,21 +21,15 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
21
  rdoc.rdoc_files.include('lib/**/*.rb')
21
22
  end
22
23
 
23
- APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
24
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
24
25
  load 'rails/tasks/engine.rake'
25
26
 
26
-
27
-
28
27
  Bundler::GemHelper.install_tasks
28
+ Dir[File.join(File.dirname(__FILE__), 'tasks/**/*.rake')].each {|f| load f }
29
29
 
30
- require 'rake/testtask'
31
-
32
- Rake::TestTask.new(:test) do |t|
33
- t.libs << 'lib'
34
- t.libs << 'test'
35
- t.pattern = 'test/**/*_test.rb'
36
- t.verbose = false
37
- end
38
-
30
+ desc 'RSpec tests'
31
+ require 'rspec/core'
32
+ require 'rspec/core/rake_task'
39
33
 
40
- task :default => :test
34
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
35
+ task :default => :spec
@@ -12,7 +12,7 @@
12
12
  //
13
13
  //= require jquery
14
14
  //= require jquery_ujs
15
- //= require jquery.ui.all
15
+ //= require jquery-ui
16
16
  //= require_tree .
17
17
 
18
18
 
@@ -9,7 +9,7 @@
9
9
  * compiled file, but it's generally better to create a new file per style scope.
10
10
  *
11
11
  *= require_self
12
- *= require jquery.ui.all
12
+ *= require jquery-ui
13
13
  *= require_tree .
14
14
  */
15
15
 
@@ -2,7 +2,9 @@
2
2
  Place all the styles related to the matching controller here.
3
3
  They will automatically be included in application.css.
4
4
  */
5
-
5
+ #keys_ul{
6
+ height: 100%;
7
+ }
6
8
  .translation_keys_listing {
7
9
  height: 410px;
8
10
  overflow: auto;
@@ -12,7 +12,7 @@ module TranslationCenter
12
12
 
13
13
  render "translation_center/errors/exception"
14
14
  end
15
-
15
+
16
16
  end
17
17
 
18
18
  # defaults
@@ -24,7 +24,7 @@ module TranslationCenter
24
24
  end
25
25
 
26
26
  protected
27
-
27
+
28
28
  def can_admin?
29
29
  current_user.can_admin_translations?
30
30
  end
@@ -11,14 +11,14 @@ module TranslationCenter
11
11
  @key_before_status = @translation_key.status(session[:lang_to])
12
12
  respond_to do |format|
13
13
  # only admin can edit accepted translations
14
- if (current_user.can_admin_translations? || !@translation.accepted?) && !params[:value].strip.blank?
14
+ if (current_user.can_admin_translations? || !@translation.accepted?) && !params[:value].to_s.strip.blank?
15
15
  # use yaml.load to handle arrays
16
- @translation.update_attributes(value: YAML.load(params[:value].strip), status: 'pending')
16
+ @translation.update(value: YAML.load(params[:value].strip), status: 'pending')
17
17
  # translation added by admin is considered the accepted one as it is trusted
18
18
  @translation.accept if current_user.can_admin_translations? && CONFIG['accept_admin_translations']
19
19
  format.json {render json: { value: @translation.value, status: @translation.key.status(@translation.lang), key_before_status: @key_before_status } }
20
20
  else
21
- render nothing: true
21
+ format.any {render nothing: true}
22
22
  end
23
23
  end
24
24
  end
@@ -42,7 +42,7 @@ module TranslationCenter
42
42
  # PUT /translation_keys/1
43
43
  # PUT /translation_keys/1.json
44
44
  def update
45
- params[:value].strip!
45
+ params[:value].to_s.strip!
46
46
  @old_value = @translation_key.category.name
47
47
  respond_to do |format|
48
48
  if !params[:value].blank? && @translation_key.update_attribute(:name, params[:value])
@@ -18,7 +18,7 @@ module TranslationCenter
18
18
 
19
19
  # retuns and ActiveRecord Relation of Audit(s) that matches this search criteria
20
20
  def activities
21
- query = Audited::Adapters::ActiveRecord::Audit.where(auditable_id: translation_ids).scoped
21
+ query = Audited::Adapters::ActiveRecord::Audit.where(auditable_id: translation_ids).all
22
22
  query = query.where("DATE(created_at) <= DATE(?)", created_at_lteq) unless created_at_lteq.blank?
23
23
  query = query.where("DATE(created_at) >= DATE(?)", created_at_gteq) unless created_at_gteq.blank?
24
24
  query.order('created_at DESC')
@@ -28,7 +28,7 @@ module TranslationCenter
28
28
 
29
29
  # return translation ids that matches this search criteria
30
30
  def translation_ids
31
- query = Translation.scoped
31
+ query = Translation.all
32
32
  query = query.where(lang: lang) unless lang.blank?
33
33
  query = query.joins(:translation_key).where("translation_center_translation_keys.name LIKE ?", "%#{translation_key_name}%") unless translation_key_name.blank?
34
34
 
@@ -41,6 +41,5 @@ module TranslationCenter
41
41
 
42
42
  query.map(&:id)
43
43
  end
44
-
45
44
  end
46
45
  end
@@ -1,6 +1,6 @@
1
1
  module TranslationCenter
2
2
  class Category < ActiveRecord::Base
3
- attr_accessible :name
3
+ # attr_accessible :name
4
4
  has_many :translation_keys, dependent: :destroy
5
5
 
6
6
  alias_method :keys, :translation_keys
@@ -10,7 +10,7 @@ module TranslationCenter
10
10
 
11
11
  # gets how much complete translation of category is in a certain language
12
12
  def complete_percentage_in(lang)
13
- if self.keys.empty?
13
+ if self.keys.blank?
14
14
  100
15
15
  else
16
16
  accepted_keys = accepted_keys(lang)
@@ -43,5 +43,10 @@ module TranslationCenter
43
43
  self.name.titleize
44
44
  end
45
45
 
46
+ private
47
+
48
+ def category_params
49
+ params.require(:category).permit(:name)
50
+ end
46
51
  end
47
52
  end
@@ -1,84 +1,100 @@
1
1
  module TranslationCenter
2
2
  class Translation < ActiveRecord::Base
3
3
 
4
- attr_accessible :value, :lang, :translation_key_id, :user_id, :status
4
+ # attr_accessible :value, :lang, :translation_key_id, :user_id, :status
5
5
  # serialize as we could store arrays
6
- serialize :value
7
-
8
6
  CHANGES_PER_PAGE = 5
9
7
  NUMBER_PER_PAGE = 15
10
8
 
9
+ # Statuses
10
+ ACCEPTED = "accepted"
11
+ PENDING = "pending"
12
+
13
+ # Relations
11
14
  belongs_to :translation_key
12
15
  belongs_to :translator, polymorphic: true
13
16
 
14
- alias_method :key, :translation_key
15
- acts_as_votable
16
- audited
17
-
18
- # validations
17
+ # Validations
19
18
  validates :translation_key_id, :lang, :status, :value, presence: true
20
19
  validate :one_translation_per_lang_per_key, on: :create
21
20
 
22
- # returns accepted transations
23
- scope :accepted, where(status: 'accepted')
21
+ # Scopes
22
+ # Returns accepted transations
23
+ scope :accepted, -> { where(status: ACCEPTED) }
24
24
 
25
- # returns translations in a certain language
26
- scope :in, lambda { |lang| where(lang: lang.to_s.strip) }
25
+ # Returns translations in a certain language
26
+ scope :in, ->(lang) { where(lang: lang.to_s.strip) }
27
27
 
28
- # sorts translations by number of votes
29
- scope :sorted_by_votes, where('votable_type IS NULL OR votable_type = ?', 'TranslationCenter::Translation').select('translation_center_translations.*, count(votes.id) as votes_count').joins('LEFT OUTER JOIN votes on votes.votable_id = translation_center_translations.id').group('translation_center_translations.id').order('votes_count desc')
28
+ # Sorts translations by number of votes
29
+ scope :sorted_by_votes, -> do
30
+ where('votable_type IS NULL OR votable_type = ?', 'TranslationCenter::Translation')
31
+ .select('translation_center_translations.*, count(votes.id) as votes_count')
32
+ .joins('LEFT OUTER JOIN votes on votes.votable_id = translation_center_translations.id')
33
+ .group('translation_center_translations.id')
34
+ .order('votes_count desc')
35
+ end
30
36
 
37
+ # Callbacks
31
38
  after_save :update_key_status
32
39
  after_destroy :notify_key
33
40
 
41
+ alias_method :key, :translation_key
42
+ acts_as_votable
43
+ audited
44
+
45
+ # Serialize as we could store arrays
46
+ serialize :value
47
+
34
48
  # called after save to update the key status
35
49
  def update_key_status
36
- self.key.update_status self.lang
50
+ self.key.update_status(self.lang)
37
51
  end
38
52
 
39
53
  # called before destory to update the key status
40
54
  def notify_key
41
- self.key.update_status self.lang
42
- self.audits.destroy_all
55
+ self.key.update_status(self.lang)
56
+ self.audits.destroy
43
57
  end
44
58
 
45
59
  # returns true if the status of the translation is accepted
46
60
  def accepted?
47
- self.status == 'accepted'
61
+ self.status == ACCEPTED
48
62
  end
49
63
 
50
64
  # returns true if the status of the translation is pending
51
65
  def pending?
52
- self.status == 'pending'
66
+ self.status == PENDING
53
67
  end
54
68
 
55
- # accept translation by changing its status and if there is an accepting translation
69
+ # Accept translation by changing its status and if there is an accepting translation
56
70
  # make it pending
57
71
  def accept
58
- # if translation is accepted do nothing
72
+ # If translation is accepted do nothing
59
73
  unless self.accepted?
60
- self.translation_key.accepted_translation_in(self.lang).try(:update_attribute, :status, 'pending')
74
+ self.translation_key.accepted_translation_in(self.lang)
75
+ .try(:update_attribute, :status, TranslationKey::PENDING)
76
+
61
77
  # reload the translation key as it has changed
62
78
  self.translation_key.reload
63
- self.update_attribute(:status, 'accepted')
79
+ self.update_attribute(:status, ACCEPTED)
64
80
  end
65
-
66
81
  end
67
82
 
68
83
  # unaccept a translation
69
84
  def unaccept
70
- self.update_attribute(:status, 'pending')
71
- end
72
-
73
- # gets recent changes on translations
74
- # TODO: remove this method as it is not being used elsewhere
75
- def self.recent_changes
76
- Audited::Adapters::ActiveRecord::Audit.where('auditable_type = ?', 'TranslationCenter::Translation').search(params).relation.reorder('created_at DESC')
85
+ self.update_attribute(:status, PENDING)
77
86
  end
78
87
 
79
88
  # make sure user has one translation per key per lang
80
89
  def one_translation_per_lang_per_key
81
- if Translation.where(lang: self.lang, translator_id: self.translator.id, translator_type: self.translator.class.name, translation_key_id: self.key.id).empty?
90
+ translation_exists = Translation.exists?(
91
+ lang: self.lang,
92
+ translator_id: self.translator.id,
93
+ translator_type: self.translator.class.name,
94
+ translation_key_id: self.key.id
95
+ )
96
+
97
+ unless translation_exists
82
98
  true
83
99
  else
84
100
  false
@@ -86,5 +102,11 @@ module TranslationCenter
86
102
  end
87
103
  end
88
104
 
105
+ private
106
+
107
+ def translation_params
108
+ params.require(:translation).permit(:value, :lang, :translation_key_id, :user_id, :status)
109
+ end
110
+
89
111
  end
90
112
  end
@@ -1,47 +1,52 @@
1
1
  module TranslationCenter
2
2
 
3
3
  class TranslationKey < ActiveRecord::Base
4
- attr_accessible :name, :last_accessed, :category_id
4
+ # Constants
5
+ ACCEPTED = "accepted"
6
+ PENDING = "pending"
7
+ TRANSLATED = "translated"
8
+ UNTRANSLATED = "untranslated"
9
+ PER_PAGE = 7
10
+
11
+ # Relations
12
+ # attr_accessible :name, :last_accessed, :category_id
5
13
  belongs_to :category
6
14
  has_many :translations, dependent: :destroy
7
15
 
8
- # validations
16
+ # Validations
9
17
  validates :name, uniqueness: true
10
18
  validates :name, presence: true
11
19
 
12
20
  # called after key is created or updated
13
21
  before_save :add_category
14
22
 
15
- PER_PAGE = 7
16
-
17
- scope :translated, lambda { |lang| where("#{lang.to_s}_status" => 'translated') }
18
- scope :pending, lambda { |lang| where("#{lang.to_s}_status" => 'pending') }
19
- scope :untranslated, lambda { |lang| where("#{lang.to_s}_status" => 'untranslated') }
20
-
23
+ scope :translated, ->(lang) { where("#{lang.to_s}_status" => TRANSLATED) }
24
+ scope :pending, ->(lang) { where("#{lang.to_s}_status" => PENDING) }
25
+ scope :untranslated, ->(lang) { where("#{lang.to_s}_status" => UNTRANSLATED) }
21
26
 
22
27
  # add a category of this translation key
23
28
  def add_category
24
29
  category_name = self.name.to_s.split('.').first
25
30
  # if one word then add to general category
26
31
  category_name = self.name.to_s.split('.').size == 1 ? 'general' : self.name.to_s.split('.').first
27
- self.category = TranslationCenter::Category.find_or_create_by_name(category_name)
32
+ self.category = TranslationCenter::Category.where(name: category_name).first_or_create
28
33
  self.last_accessed = Time.now
29
34
  end
30
35
 
31
36
  # updates the status of the translation key depending on the translations
32
37
  def update_status(lang)
33
38
  if self.translations.in(lang).blank?
34
- self.update_attribute("#{lang}_status", 'untranslated')
39
+ self.update_attribute("#{lang}_status", UNTRANSLATED)
35
40
  elsif !self.translations.in(lang).accepted.blank?
36
- self.update_attribute("#{lang}_status", 'translated')
41
+ self.update_attribute("#{lang}_status", TRANSLATED)
37
42
  else
38
- self.update_attribute("#{lang}_status", 'pending')
43
+ self.update_attribute("#{lang}_status", PENDING)
39
44
  end
40
45
  end
41
46
 
42
47
  # returns true if the key is translated (has accepted translation) in this lang
43
48
  def accepted_in?(lang)
44
- self.send("#{lang}_status") == 'translated'
49
+ self.send("#{lang}_status") == TRANSLATED
45
50
  end
46
51
  alias_method :translated_in?, :accepted_in?
47
52
 
@@ -52,7 +57,7 @@ module TranslationCenter
52
57
 
53
58
  # returns true if the translation key is untranslated (has no translations) in the language
54
59
  def no_translations_in?(lang)
55
- self.send("#{lang}_status") == 'untranslated'
60
+ self.send("#{lang}_status") == UNTRANSLATED
56
61
  end
57
62
  alias_method :untranslated_in?, :no_translations_in?
58
63
 
@@ -63,17 +68,17 @@ module TranslationCenter
63
68
 
64
69
  # returns true if the key is pending (has translations but none is accepted)
65
70
  def pending_in?(lang)
66
- self.send("#{lang}_status") == 'pending'
71
+ self.send("#{lang}_status") == PENDING
67
72
  end
68
73
 
69
74
  # returns the status of the key in a language
70
75
  def status(lang)
71
76
  if accepted_in?(lang)
72
- 'translated'
77
+ TRANSLATED
73
78
  elsif pending_in?(lang)
74
- 'pending'
79
+ PENDING
75
80
  else
76
- 'untranslated'
81
+ UNTRANSLATED
77
82
  end
78
83
  end
79
84
 
@@ -162,6 +167,12 @@ module TranslationCenter
162
167
  end
163
168
 
164
169
  private
170
+
171
+
172
+ def translation_key_params
173
+ params.require(:translation_key).permit(:name, :last_accessed, :category_id)
174
+ end
175
+
165
176
  def add_to_hash_rec(all_translations, levels, lang)
166
177
  current_level = levels.first
167
178
  # if we are at the bottom level just return the translation