lit 0.2.4 → 0.2.5

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.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +17 -0
  3. data/Rakefile +1 -4
  4. data/app/controllers/lit/api/v1/base_controller.rb +5 -5
  5. data/app/controllers/lit/api/v1/locales_controller.rb +2 -2
  6. data/app/controllers/lit/api/v1/localization_keys_controller.rb +2 -3
  7. data/app/controllers/lit/api/v1/localizations_controller.rb +2 -2
  8. data/app/controllers/lit/application_controller.rb +12 -11
  9. data/app/controllers/lit/dashboard_controller.rb +1 -1
  10. data/app/controllers/lit/incomming_localizations_controller.rb +13 -6
  11. data/app/controllers/lit/locales_controller.rb +5 -5
  12. data/app/controllers/lit/localization_keys_controller.rb +57 -57
  13. data/app/controllers/lit/localizations_controller.rb +16 -15
  14. data/app/controllers/lit/sources_controller.rb +13 -12
  15. data/app/helpers/lit/application_helper.rb +0 -1
  16. data/app/helpers/lit/localizations_helper.rb +1 -1
  17. data/app/models/lit/incomming_localization.rb +29 -26
  18. data/app/models/lit/locale.rb +21 -11
  19. data/app/models/lit/localization.rb +27 -26
  20. data/app/models/lit/localization_key.rb +19 -20
  21. data/app/models/lit/localization_version.rb +1 -1
  22. data/app/models/lit/source.rb +51 -47
  23. data/app/views/lit/incomming_localizations/accept.js.erb +2 -0
  24. data/app/views/lit/incomming_localizations/destroy.js.erb +1 -0
  25. data/app/views/lit/incomming_localizations/index.html.erb +5 -5
  26. data/app/views/lit/localization_keys/index.html.erb +1 -1
  27. data/app/views/lit/localizations/_form.html.erb +1 -1
  28. data/app/views/lit/localizations/_previous_versions_rows.html.erb +2 -2
  29. data/app/views/lit/sources/index.html.erb +4 -1
  30. data/lib/generators/lit/install_generator.rb +16 -16
  31. data/lib/lit.rb +13 -8
  32. data/lib/lit/adapters/hash_storage.rb +1 -1
  33. data/lib/lit/adapters/redis_storage.rb +30 -22
  34. data/lib/lit/cache.rb +128 -125
  35. data/lib/lit/engine.rb +1 -2
  36. data/lib/lit/i18n_backend.rb +59 -12
  37. data/lib/lit/loader.rb +1 -2
  38. data/lib/lit/railtie.rb +5 -5
  39. data/lib/lit/version.rb +1 -1
  40. data/lib/tasks/lit_tasks.rake +3 -3
  41. metadata +38 -32
@@ -0,0 +1,2 @@
1
+ $('table.incomming-localizations-table tr[data-id="<%= @incomming_localization.id %>"]').remove();
2
+
@@ -0,0 +1 @@
1
+ $('table.incomming-localizations-table tr[data-id="<%= @incomming_localization.id %>"]').remove();
@@ -10,20 +10,20 @@
10
10
  </span>
11
11
  </div>
12
12
 
13
- <table class="table table-bordered table-striped">
13
+ <table class="table table-bordered table-striped incomming-localizations-table">
14
14
  <tr>
15
15
  <th>Current value</th>
16
16
  <th>Imported value</th>
17
17
  <th width="200px"></th>
18
18
  </tr>
19
19
  <% @incomming_localizations.each do |il| %>
20
- <tr>
20
+ <tr data-id="<%= il.id %>">
21
21
  <td colspan="3">
22
22
  <%= image_tag "lit/famfamfam_flags/#{il.locale_str[0,2]}.png" %>
23
23
  <strong><%= il.full_key %></strong>
24
24
  </td>
25
25
  </tr>
26
- <tr>
26
+ <tr data-id="<%= il.id %>">
27
27
  <td>
28
28
  <% if il.localization %>
29
29
  <%= render :partial=>"/lit/localization_keys/localization_row", :locals=>{:localization=>il.localization.get_value} %>
@@ -33,11 +33,11 @@
33
33
  <%= render :partial=>"/lit/localization_keys/localization_row", :locals=>{:localization=>il.get_value} %>
34
34
  </td>
35
35
  <td>
36
- <%= link_to accept_source_incomming_localization_path(@source, il), :class=>"btn btn-success btn-sm" do %>
36
+ <%= link_to accept_source_incomming_localization_path(@source, il), :class=>"btn btn-success btn-sm", :remote=>true do %>
37
37
  <%= draw_icon "check", :class=>"icon-white" %>
38
38
  <%= t('lit.common.accept', :default=>"Accept") %>
39
39
  <% end %>
40
- <%= link_to source_incomming_localization_path(@source, il), :class=>"btn btn-danger btn-sm", :method=>:delete, :data=>{:confirm=>t('lit.common.you_sure', :default=>"Are you sure?")} do %>
40
+ <%= link_to source_incomming_localization_path(@source, il), :class=>"btn btn-danger btn-sm", :remote=>true, :method=>:delete, :data=>{:confirm=>t('lit.common.you_sure', :default=>"Are you sure?")} do %>
41
41
  <%= draw_icon "times", :class=>"icon-white" %>
42
42
  <%= t('lit.common.reject', :default=>"Reject") %>
43
43
  <% end %>
@@ -88,7 +88,7 @@
88
88
  <li class="nav-header">Narrow with prefix</li>
89
89
  <% if @search_options[:key_prefix].present? %>
90
90
  <li>
91
- <%= link_to url_for(@search_options.merge(:key_prefix=>p)), :title=>(@parent_prefix.present? ? @parent_prefix : 'show all') do %>
91
+ <%= link_to url_for(@search_options.merge(:key_prefix=>@parent_prefix.present? ? @parent_prefix : nil)), :title=>(@parent_prefix.present? ? @parent_prefix : 'show all') do %>
92
92
  <%= draw_icon('chevron-left') %>
93
93
  <%= @parent_prefix.present? ? @parent_prefix.split('.').last : 'show all' %>
94
94
  <% end %>
@@ -18,6 +18,6 @@
18
18
  <% end %>
19
19
 
20
20
  <button class="btn btn-primary btn-sm" type="submit"><%= t('lit.common.update', :default=>"Update") %></button>
21
- <button class="btn btn-default btn-sm"><%= t('lit.common.cancel', :default=>'Cancel') %></button>
21
+ <button class="btn btn-default btn-sm cancel" type="reset"><%= t('lit.common.cancel', :default=>'Cancel') %></button>
22
22
  <% end %>
23
23
 
@@ -2,14 +2,14 @@
2
2
  <table class="table table-condensed">
3
3
  <tr>
4
4
  <th><%= t('lit.common.previous_versions') %></th>
5
- <th width="25%"><%= t('lit.common.created_at') %></th>
5
+ <th width="25%"><%= t('lit.common.changed_at') %></th>
6
6
  </tr>
7
7
  <% @versions.each do |v| %>
8
8
  <tr>
9
9
  <td>
10
10
  <%= render :partial=>"/lit/localization_keys/localization_row", :locals=>{:localization=>v.translated_value} %>
11
11
  </td>
12
- <td><%= v.created_at %></td>
12
+ <td><%= I18n.l v.created_at, format: :long %></td>
13
13
  </tr>
14
14
  <% end %>
15
15
  </table>
@@ -5,7 +5,7 @@
5
5
  <th>Identifier</th>
6
6
  <th>Last synchronization</th>
7
7
  <th>Last change (remote)</th>
8
- <th width="80px"></th>
8
+ <th width="95px"></th>
9
9
  </tr>
10
10
 
11
11
  <% @sources.each do |source| %>
@@ -14,6 +14,9 @@
14
14
  <td><%= source.last_updated_at.to_s(:db) unless source.last_updated_at.nil? %></td>
15
15
  <td><%= source.get_last_change %></td>
16
16
  <td>
17
+ <%= link_to lit.source_incomming_localizations_path(source), :title=>t('lit.common.browse_incomming', :default=>"Browse incomming localizations") do %>
18
+ <%= draw_icon 'search' %>
19
+ <% end %>
17
20
  <%= link_to lit.synchronize_source_path(source), :title=>t('lit.common.synchronize', :default=>"Synchronize") do %>
18
21
  <%= draw_icon 'refresh' %>
19
22
  <% end %>
@@ -2,39 +2,39 @@ require 'rails/generators'
2
2
  module Lit
3
3
  module Generators
4
4
  class InstallGenerator < ::Rails::Generators::Base
5
- class_option "key-value-engine", :type => :string
6
- class_option "authentication-function", :type => :string
7
- class_option "no-migrate", :type => :boolean
5
+ class_option 'key-value-engine', type: :string
6
+ class_option 'authentication-function', type: :string
7
+ class_option 'no-migrate', type: :boolean
8
8
 
9
- source_root File.expand_path("../install/templates", __FILE__)
9
+ source_root File.expand_path('../install/templates', __FILE__)
10
10
 
11
- desc "Automates Lit installation"
11
+ desc 'Automates Lit installation'
12
12
 
13
13
  def copy_migrations
14
- puts "Copying Lit migrations..."
14
+ puts 'Copying Lit migrations...'
15
15
  Dir.chdir(::Rails.root) do
16
16
  `rake lit:install:migrations`
17
17
  end
18
18
  end
19
19
 
20
20
  def set_authentication_function
21
- @authentication_function = options["authentication-function"].presence ||
21
+ @authentication_function = options['authentication-function'].presence ||
22
22
  ask("What's the authentication function, ie. :authenticate_user! :").presence ||
23
23
  nil
24
24
  end
25
25
 
26
26
  def set_key_value_engine
27
- @key_value_engine = options["key-value-engine"].presence ||
27
+ @key_value_engine = options['key-value-engine'].presence ||
28
28
  ask("What's the key value engine? ([hash] OR redis):").presence ||
29
29
  :hash
30
30
  end
31
31
 
32
32
  def add_redis_dependency
33
33
  if @key_value_engine == 'redis'
34
- puts "Adding redis dependency"
34
+ puts 'Adding redis dependency'
35
35
  gem 'redis'
36
36
  Bundler.with_clean_env do
37
- run "bundle install"
37
+ run 'bundle install'
38
38
  end
39
39
  end
40
40
  end
@@ -45,17 +45,17 @@ module Lit
45
45
 
46
46
  def add_lit_initializer
47
47
  path = "#{::Rails.root}/config/initializers/lit.rb"
48
- if File.exists?(path)
49
- puts "Skipping config/initializers/lit.rb creation, file already exists!"
48
+ if File.exist?(path)
49
+ puts 'Skipping config/initializers/lit.rb creation, file already exists!'
50
50
  else
51
- puts "Adding lit initializer (config/initializers/lit.rb)..."
52
- template "initializer.rb", path
51
+ puts 'Adding lit initializer (config/initializers/lit.rb)...'
52
+ template 'initializer.rb', path
53
53
  end
54
54
  end
55
55
 
56
56
  def run_migrations
57
- unless options["no-migrate"]
58
- puts "Running rake db:migrate"
57
+ unless options['no-migrate']
58
+ puts 'Running rake db:migrate'
59
59
  `rake db:migrate`
60
60
  end
61
61
  end
data/lib/lit.rb CHANGED
@@ -1,4 +1,4 @@
1
- require "lit/engine"
1
+ require 'lit/engine'
2
2
  require 'lit/loader'
3
3
 
4
4
  module Lit
@@ -6,6 +6,7 @@ module Lit
6
6
  mattr_accessor :key_value_engine
7
7
  mattr_accessor :storage_options
8
8
  mattr_accessor :humanize_key
9
+ mattr_accessor :ignored_keys
9
10
  mattr_accessor :fallback
10
11
  mattr_accessor :api_enabled
11
12
  mattr_accessor :api_key
@@ -16,10 +17,16 @@ module Lit
16
17
  attr_accessor :loader
17
18
  end
18
19
  def self.init
19
- @@table_exists ||= self.check_if_table_exists
20
- if self.loader.nil? && @@table_exists
20
+ @@table_exists ||= check_if_table_exists
21
+ if loader.nil? && @@table_exists
21
22
  self.loader ||= Loader.new
22
23
  Lit.humanize_key = false if Lit.humanize_key.nil?
24
+ Lit.fallback = true if Lit.fallback.nil?
25
+ if Lit.ignored_keys.is_a?(String)
26
+ keys = Lit.ignored_keys.split(',').map(&:strip)
27
+ Lit.ignored_keys = keys
28
+ end
29
+ Lit.ignored_keys = [] unless Lit.ignored_keys.is_a?(Array)
23
30
  # if loading all translations on start, migrations have to be already
24
31
  # performed, fails on first deploy
25
32
  # self.loader.cache.load_all_translations
@@ -29,11 +36,9 @@ module Lit
29
36
  end
30
37
 
31
38
  def self.check_if_table_exists
32
- begin
33
- Lit::Locale.table_exists?
34
- rescue
35
- false
36
- end
39
+ Lit::Locale.table_exists?
40
+ rescue
41
+ false
37
42
  end
38
43
 
39
44
  def self.get_key_value_engine
@@ -5,4 +5,4 @@ module Lit
5
5
  self[key] += 1
6
6
  end
7
7
  end
8
- end
8
+ end
@@ -2,9 +2,14 @@ require 'redis'
2
2
  module Lit
3
3
  extend self
4
4
  def redis
5
- $redis = Redis.new(url: ENV['REDIS_URL']) unless $redis
5
+ $redis = Redis.new(url: determine_redis_provider) unless $redis
6
6
  $redis
7
7
  end
8
+
9
+ def determine_redis_provider
10
+ ENV[ENV['REDIS_PROVIDER'] || 'REDIS_URL']
11
+ end
12
+
8
13
  class RedisStorage
9
14
  def initialize
10
15
  Lit.redis
@@ -21,16 +26,15 @@ module Lit
21
26
  end
22
27
 
23
28
  def []=(k, v)
24
-
25
29
  delete(k)
26
30
  if v.is_a?(Array)
27
- Lit.redis.set(_prefixed_key_for_array(k), "1")
31
+ Lit.redis.set(_prefixed_key_for_array(k), '1')
28
32
  v.each do |ve|
29
33
  Lit.redis.rpush(_prefixed_key(k), ve.to_s)
30
34
  end
31
35
  elsif v.nil?
32
- Lit.redis.set(_prefixed_key_for_nil(k), "1")
33
- Lit.redis.set(_prefixed_key(k), "")
36
+ Lit.redis.set(_prefixed_key_for_nil(k), '1')
37
+ Lit.redis.set(_prefixed_key(k), '')
34
38
  else
35
39
  Lit.redis.set(_prefixed_key(k), v)
36
40
  end
@@ -43,11 +47,11 @@ module Lit
43
47
  end
44
48
 
45
49
  def clear
46
- Lit.redis.del(self.keys) if self.keys.length > 0
50
+ Lit.redis.del(keys) if keys.length > 0
47
51
  end
48
52
 
49
53
  def keys
50
- Lit.redis.keys(_prefixed_key+"*")
54
+ Lit.redis.keys(_prefixed_key + '*')
51
55
  end
52
56
 
53
57
  def has_key?(key)
@@ -65,21 +69,25 @@ module Lit
65
69
  end
66
70
 
67
71
  private
68
- def _prefix
69
- prefix = "lit:"
70
- if Lit.storage_options.is_a?(Hash)
71
- prefix += "#{Lit.storage_options[:prefix]}:" if Lit.storage_options.has_key?(:prefix)
72
- end
73
- prefix
74
- end
75
- def _prefixed_key(key="")
76
- _prefix+key.to_s
77
- end
78
- def _prefixed_key_for_array(key="")
79
- _prefix+"array_flags:"+key.to_s
80
- end
81
- def _prefixed_key_for_nil(key="")
82
- _prefix+"nil_flags:"+key.to_s
72
+
73
+ def _prefix
74
+ prefix = 'lit:'
75
+ if Lit.storage_options.is_a?(Hash)
76
+ prefix += "#{Lit.storage_options[:prefix]}:" if Lit.storage_options.key?(:prefix)
83
77
  end
78
+ prefix
79
+ end
80
+
81
+ def _prefixed_key(key = '')
82
+ _prefix + key.to_s
83
+ end
84
+
85
+ def _prefixed_key_for_array(key = '')
86
+ _prefix + 'array_flags:' + key.to_s
87
+ end
88
+
89
+ def _prefixed_key_for_nil(key = '')
90
+ _prefix + 'nil_flags:' + key.to_s
91
+ end
84
92
  end
85
93
  end
@@ -18,16 +18,14 @@ end
18
18
 
19
19
  module Lit
20
20
  class Cache
21
-
22
21
  def initialize
23
- @localizations = Lit.get_key_value_engine
24
22
  @hits_counter = Lit.get_key_value_engine
25
23
  @hits_counter_working = true
26
24
  end
27
25
 
28
26
  def [](key)
29
27
  update_hits_count(key)
30
- ret = @localizations[key]
28
+ ret = localizations[key]
31
29
  ret
32
30
  end
33
31
 
@@ -40,28 +38,28 @@ module Lit
40
38
  end
41
39
 
42
40
  def has_key?(key)
43
- @localizations.has_key?(key)
41
+ localizations.has_key?(key)
44
42
  end
45
43
 
46
44
  def sync
47
- @localizations.clear
45
+ localizations.clear
48
46
  end
49
47
 
50
48
  def keys
51
- @localizations.keys
49
+ localizations.keys
52
50
  end
53
51
 
54
- def update_locale(key, value, force_array=false)
52
+ def update_locale(key, value, force_array = false)
55
53
  key = key.to_s
56
54
  locale_key, key_without_locale = split_key(key)
57
55
  locale = find_locale(locale_key)
58
56
  localization = find_localization(locale, key_without_locale, value, force_array, true)
59
- @localizations[key] = localization.get_value if localization
57
+ localizations[key] = localization.get_value if localization
60
58
  end
61
59
 
62
60
  def update_cache(key, value)
63
61
  key = key.to_s
64
- @localizations[key] = value
62
+ localizations[key] = value
65
63
  end
66
64
 
67
65
  def delete_locale(key)
@@ -74,11 +72,11 @@ module Lit
74
72
  def load_all_translations
75
73
  first = Localization.order('id ASC').first
76
74
  last = Localization.order('id DESC').first
77
- if not first or not last or (not @localizations.has_key?(first.full_key) or
78
- not @localizations.has_key?(last.full_key))
75
+ if !first || !last || (!localizations.has_key?(first.full_key) ||
76
+ !localizations.has_key?(last.full_key))
79
77
 
80
78
  Localization.includes([:locale, :localization_key]).find_each do |l|
81
- @localizations[l.full_key] = l.get_value
79
+ localizations[l.full_key] = l.get_value
82
80
  end
83
81
  end
84
82
  end
@@ -88,23 +86,21 @@ module Lit
88
86
  locale_key, key_without_locale = split_key(key)
89
87
  locale = find_locale(locale_key)
90
88
  localization = find_localization(locale, key_without_locale)
91
- @localizations[key] = localization.get_value if localization
89
+ localizations[key] = localization.get_value if localization
92
90
  end
93
91
 
94
92
  def delete_key(key)
95
93
  key = key.to_s
96
- @localizations.delete(key)
94
+ localizations.delete(key)
97
95
  key_without_locale = split_key(key).last
98
- @localization_keys.delete(key_without_locale)
96
+ localization_keys.delete(key_without_locale)
99
97
  I18n.backend.reload!
100
98
  end
101
99
 
102
100
  def reset
103
101
  @locale_cache = {}
104
- @localizations = Lit.get_key_value_engine
105
- @localizations.clear
106
- @localization_keys = Lit.get_key_value_engine
107
- @localization_keys.clear
102
+ localizations.clear
103
+ localization_keys.clear
108
104
  load_all_translations
109
105
  end
110
106
 
@@ -114,13 +110,12 @@ module Lit
114
110
  locale_key = locale_key.to_s
115
111
  @locale_cache ||= {}
116
112
  unless @locale_cache.has_key?(locale_key)
117
- locale = Lit::Locale.where(:locale=>locale_key).first_or_create!
113
+ locale = Lit::Locale.where(locale: locale_key).first_or_create!
118
114
  @locale_cache[locale_key] = locale
119
115
  end
120
116
  @locale_cache[locale_key]
121
117
  end
122
118
 
123
-
124
119
  # this comes directly from copycopter.
125
120
  def export
126
121
  reset
@@ -140,27 +135,27 @@ module Lit
140
135
 
141
136
  def nested_string_keys_to_hash(db_localizations)
142
137
  # http://subtech.g.hatena.ne.jp/cho45/20061122
143
- deep_proc = Proc.new { |k, s, o|
144
- if s.kind_of?(Hash) && o.kind_of?(Hash)
138
+ deep_proc = proc do |_k, s, o|
139
+ if s.is_a?(Hash) && o.is_a?(Hash)
145
140
  next s.merge(o, &deep_proc)
146
141
  end
147
142
  next o
148
- }
143
+ end
149
144
  keys = {}
150
- db_localizations.sort.each do |k,v|
145
+ db_localizations.sort.each do |k, v|
151
146
  key_parts = k.to_s.split('.')
152
- converted = key_parts.reverse.inject(v) { |a, n| { n => a } }
147
+ converted = key_parts.reverse.reduce(v) { |a, n| { n => a } }
153
148
  keys.merge!(converted, &deep_proc)
154
149
  end
155
150
  keys
156
151
  end
157
152
 
158
153
  def get_global_hits_counter(key)
159
- @hits_counter['global_hits_counter.'+key]
154
+ @hits_counter['global_hits_counter.' + key]
160
155
  end
161
156
 
162
157
  def get_hits_counter(key)
163
- @hits_counter['hits_counter.'+key]
158
+ @hits_counter['hits_counter.' + key]
164
159
  end
165
160
 
166
161
  def stop_hits_counter
@@ -173,122 +168,130 @@ module Lit
173
168
 
174
169
  private
175
170
 
176
- def find_localization(locale, key_without_locale, value=nil, force_array=false, update_value=false)
177
- unless value.is_a?(Hash)
178
- localization_key = find_localization_key(key_without_locale)
179
- localization = Lit::Localization.where(:locale_id=>locale.id).
180
- where(:localization_key_id=>localization_key.id).first_or_initialize
181
- if update_value || localization.new_record?
182
- if value.is_a?(Array)
183
- unless force_array
184
- new_value = nil
185
- value_clone = value.dup
186
- while (v = value_clone.shift) && v.present?
187
- pv = parse_value(v, locale)
188
- new_value = pv unless pv.nil?
189
- end
190
- value = new_value
171
+ def localizations
172
+ @localizations ||= Lit.get_key_value_engine
173
+ end
174
+
175
+ def localization_keys
176
+ @localization_keys ||= Lit.get_key_value_engine
177
+ end
178
+
179
+ def find_localization(locale, key_without_locale, value = nil, force_array = false, update_value = false)
180
+ unless value.is_a?(Hash)
181
+ localization_key = find_localization_key(key_without_locale)
182
+ localization = Lit::Localization.where(locale_id: locale.id).
183
+ where(localization_key_id: localization_key.id).first_or_initialize
184
+ if update_value || localization.new_record?
185
+ if value.is_a?(Array)
186
+ unless force_array
187
+ new_value = nil
188
+ value_clone = value.dup
189
+ while (v = value_clone.shift) && v.present?
190
+ pv = parse_value(v, locale)
191
+ new_value = pv unless pv.nil?
191
192
  end
192
- else
193
- value = parse_value(value, locale) unless value.nil?
193
+ value = new_value
194
194
  end
195
- if value.nil?
196
- if Lit.fallback
197
- @locale_cache.keys.each do |lc|
198
- if lc != locale.locale
199
- nk = "#{lc}.#{key_without_locale}"
200
- v = @localizations[nk]
201
- value = v if v.present? and value.nil?
202
- end
195
+ else
196
+ value = parse_value(value, locale) unless value.nil?
197
+ end
198
+ if value.nil?
199
+ if Lit.fallback
200
+ @locale_cache.keys.each do |lc|
201
+ if lc != locale.locale
202
+ nk = "#{lc}.#{key_without_locale}"
203
+ v = localizations[nk]
204
+ value = v if v.present? && value.nil?
203
205
  end
204
206
  end
205
- if value.nil? && Lit.humanize_key
206
- value = key_without_locale.split('.').last.humanize
207
- end
208
207
  end
209
- localization.update_default_value(value)
208
+ if value.nil? && Lit.humanize_key
209
+ value = key_without_locale.split('.').last.humanize
210
+ end
210
211
  end
211
- return localization
212
- else
213
- nil
212
+ localization.update_default_value(value)
214
213
  end
214
+ return localization
215
+ else
216
+ nil
215
217
  end
218
+ end
216
219
 
217
- def find_localization_for_delete(locale, key_without_locale)
218
- localization_key = find_localization_key_for_delete(key_without_locale)
219
- return nil unless localization_key
220
- Lit::Localization.where(:locale_id=>locale.id).
221
- where(:localization_key_id=>localization_key.id).first
222
- end
220
+ def find_localization_for_delete(locale, key_without_locale)
221
+ localization_key = find_localization_key_for_delete(key_without_locale)
222
+ return nil unless localization_key
223
+ Lit::Localization.where(locale_id: locale.id).
224
+ where(localization_key_id: localization_key.id).first
225
+ end
223
226
 
224
- def delete_localization(locale, key_without_locale)
225
- localization = find_localization_for_delete(locale, key_without_locale)
226
- if localization
227
- @localizations.delete("#{locale.locale}.#{key_without_locale}")
228
- @localization_keys.delete(key_without_locale)
229
- localization.destroy # or localization.default_value = nil; localization.save!
230
- end
231
- end
232
-
233
- ## checks parameter type and returns value basing on it
234
- ## symbols are beeing looked up in db
235
- ## string are returned directly
236
- ## procs are beeing called (once)
237
- ## hashes are converted do string (for now)
238
- def parse_value(v, locale)
239
- new_value = nil
240
- case v
241
- when Symbol then
242
- lk = Lit::LocalizationKey.where(:localization_key=>v.to_s).first
243
- if lk
244
- loca = Lit::Localization.where(:locale_id=>locale.id).
245
- where(:localization_key_id=>lk.id).first
246
- new_value = loca.get_value if loca and loca.get_value.present?
247
- end
248
- when String then
249
- new_value = v
250
- when Hash then
251
- new_value = nil
252
- when Proc then
253
- new_value = nil # was v.call - requires more love
254
- else
255
- new_value = v.to_s
256
- end
257
- new_value
227
+ def delete_localization(locale, key_without_locale)
228
+ localization = find_localization_for_delete(locale, key_without_locale)
229
+ if localization
230
+ localizations.delete("#{locale.locale}.#{key_without_locale}")
231
+ localization_keys.delete(key_without_locale)
232
+ localization.destroy # or localization.default_value = nil; localization.save!
258
233
  end
234
+ end
259
235
 
260
- def find_localization_key(key_without_locale)
261
- @localization_keys ||= Lit.get_key_value_engine
262
- unless @localization_keys.has_key?(key_without_locale)
263
- find_or_create_localization_key(key_without_locale)
236
+ ## checks parameter type and returns value basing on it
237
+ ## symbols are beeing looked up in db
238
+ ## string are returned directly
239
+ ## procs are beeing called (once)
240
+ ## hashes are converted do string (for now)
241
+ def parse_value(v, locale)
242
+ new_value = nil
243
+ case v
244
+ when Symbol then
245
+ lk = Lit::LocalizationKey.where(localization_key: v.to_s).first
246
+ if lk
247
+ loca = Lit::Localization.where(locale_id: locale.id).
248
+ where(localization_key_id: lk.id).first
249
+ new_value = loca.get_value if loca && loca.get_value.present?
250
+ end
251
+ when String then
252
+ new_value = v
253
+ when Hash then
254
+ new_value = nil
255
+ when Proc then
256
+ new_value = nil # was v.call - requires more love
264
257
  else
265
- Lit::LocalizationKey.find_by_id(@localization_keys[key_without_locale]) || find_or_create_localization_key(key_without_locale)
266
- end
258
+ new_value = v.to_s
267
259
  end
260
+ new_value
261
+ end
268
262
 
269
- def find_localization_key_for_delete(key_without_locale)
270
- @localization_keys ||= Lit.get_key_value_engine
271
- lk = Lit::LocalizationKey.find_by_id(@localization_keys[key_without_locale]) if @localization_keys.has_key?(key_without_locale)
272
- lk || Lit::LocalizationKey.where(:localization_key=>key_without_locale).first
263
+ def find_localization_key(key_without_locale)
264
+ unless localization_keys.has_key?(key_without_locale)
265
+ find_or_create_localization_key(key_without_locale)
266
+ else
267
+ Lit::LocalizationKey.find_by_id(localization_keys[key_without_locale]) || find_or_create_localization_key(key_without_locale)
273
268
  end
269
+ end
274
270
 
275
- def split_key(key)
276
- key.split('.', 2)
277
- end
271
+ def find_localization_key_for_delete(key_without_locale)
272
+ lk = Lit::LocalizationKey.find_by_id(localization_keys[key_without_locale]) if localization_keys.has_key?(key_without_locale)
273
+ lk || Lit::LocalizationKey.where(localization_key: key_without_locale).first
274
+ end
278
275
 
279
- def find_or_create_localization_key(key_without_locale)
280
- localization_key = Lit::LocalizationKey.where(:localization_key=>key_without_locale).first_or_create!
281
- @localization_keys[key_without_locale] = localization_key.id
282
- localization_key
283
- end
276
+ def split_key(key)
277
+ Lit::Cache.split_key(key)
278
+ end
284
279
 
285
- def update_hits_count(key)
286
- if @hits_counter_working
287
- key_without_locale = split_key(key).last
288
- @hits_counter.incr('hits_counter.'+key)
289
- @hits_counter.incr('global_hits_counter.'+key_without_locale)
290
- end
291
- end
280
+ def find_or_create_localization_key(key_without_locale)
281
+ localization_key = Lit::LocalizationKey.where(localization_key: key_without_locale).first_or_create!
282
+ localization_keys[key_without_locale] = localization_key.id
283
+ localization_key
284
+ end
292
285
 
286
+ def update_hits_count(key)
287
+ if @hits_counter_working
288
+ key_without_locale = split_key(key).last
289
+ @hits_counter.incr('hits_counter.' + key)
290
+ @hits_counter.incr('global_hits_counter.' + key_without_locale)
291
+ end
292
+ end
293
+ def self.split_key(key)
294
+ key.split('.', 2)
295
+ end
293
296
  end
294
297
  end