phrasing 3.2.2 → 3.2.3

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- phrasing (3.2.2)
4
+ phrasing (3.2.3)
5
5
  haml-rails
6
6
  jquery-cookie-rails
7
7
  jquery-rails
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Phrasing!
2
2
 
3
+ [![Build Status](https://travis-ci.org/infinum/phrasing.png)](https://travis-ci.org/infinum/phrasing)
4
+
3
5
  ![Phrasing](http://www.miataturbo.net/attachments/miata-parts-sale-trade-5/74257-lots-leftovers-near-boston-archer-phrasing2-300x225-jpg?dateline=1366600534)
4
6
 
5
7
  Phrasing is a gem for live editing phrases (copy) on websites.
@@ -52,11 +52,11 @@ class PhrasingPhrasesController < ActionController::Base
52
52
  app_name = Rails.application.class.to_s.split("::").first
53
53
  app_env = Rails.env
54
54
  filename = "phrasing_phrases_#{app_name}_#{app_env}_#{Time.now.strftime("%Y_%m_%d_%H_%M_%S")}.yml"
55
- send_data PhrasingPhrase.export_yaml, filename: filename
55
+ send_data Phrasing::Serializer.export_yaml, filename: filename
56
56
  end
57
57
 
58
58
  def upload
59
- number_of_changes = PhrasingPhrase.import_yaml(params["file"].tempfile)
59
+ number_of_changes = Phrasing::Serializer.import_yaml(params["file"].tempfile)
60
60
  redirect_to phrasing_phrases_path, notice: "YAML file uploaded successfully! Number of phrases changed: #{number_of_changes}."
61
61
  rescue Exception => e
62
62
  logger.info "\n#{e.class}\n#{e.message}"
@@ -86,7 +86,7 @@ class PhrasingPhrasesController < ActionController::Base
86
86
  yaml = read_remote_yaml(Phrasing.staging_server_endpoint)
87
87
 
88
88
  if yaml
89
- PhrasingPhrase.import_yaml(yaml)
89
+ Phrasing::Serializer.import_yaml(yaml)
90
90
  redirect_to :back, notice: "Translations synced from source server"
91
91
  else
92
92
  redirect_to :back
@@ -26,41 +26,10 @@ class PhrasingPhrase < ActiveRecord::Base
26
26
  phrasing_phrase
27
27
  end
28
28
 
29
- module Serialize
30
-
31
- def import_yaml(yaml)
32
- number_of_changes = 0
33
- hash = YAML.load(yaml)
34
- hash.each do |locale, data|
35
- data.each do |key, value|
36
- phrase = where(key: key, locale: locale).first || new(key: key, locale: locale)
37
- if phrase.value != value
38
- phrase.value = value
39
- number_of_changes += 1
40
- phrase.save
41
- end
42
- end
43
- end
44
- number_of_changes
45
- end
46
-
47
- def export_yaml
48
- hash = {}
49
- where("value is not null").each do |phrase|
50
- hash[phrase.locale] ||= {}
51
- hash[phrase.locale][phrase.key] = phrase.value
52
- end
53
- hash.to_yaml
54
- end
55
-
56
- end
57
-
58
- extend Serialize
59
-
60
29
  private
61
30
 
62
31
  def uniqueness_of_key_on_locale_scope
63
- errors.add(:key, "Duplicate entry #{key} for locale #{locale}") unless PhrasingPhrase.where(key: key).where(locale: locale).empty?
32
+ errors.add(:key, "Duplicate entry #{key} for locale #{locale}") unless PhrasingPhrase.where(key: key, locale: locale).empty?
64
33
  end
65
34
 
66
35
  def version_it
data/lib/phrasing.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'phrasing'
2
2
  require "phrasing/implementation"
3
3
  require "phrasing/simple"
4
+ require "phrasing/serializer"
4
5
  require 'jquery-rails'
5
6
  require 'jquery-cookie-rails'
6
7
  require 'haml-rails'
@@ -0,0 +1,48 @@
1
+ module Phrasing
2
+ module Serializer
3
+ class << self
4
+
5
+ def import_yaml(yaml)
6
+ number_of_changes = 0
7
+ hash = YAML.load(yaml)
8
+
9
+ hash.each do |locale, data|
10
+ flatten_the_hash(data).each do |key, value|
11
+ phrase = PhrasingPhrase.where(key: key, locale: locale).first || PhrasingPhrase.new(key: key, locale: locale)
12
+ if phrase.value != value
13
+ phrase.value = value
14
+ number_of_changes += 1
15
+ phrase.save
16
+ end
17
+ end
18
+ end
19
+
20
+ number_of_changes
21
+ end
22
+
23
+ def flatten_the_hash(hash)
24
+ new_hash = {}
25
+ hash.each do |key, value|
26
+ if value.is_a? Hash
27
+ flatten_the_hash(value).each do |k,v|
28
+ new_hash["#{key}.#{k}"] = v
29
+ end
30
+ else
31
+ new_hash[key] = value
32
+ end
33
+ end
34
+ new_hash
35
+ end
36
+
37
+ def export_yaml
38
+ hash = {}
39
+ PhrasingPhrase.where("value is not null").each do |phrase|
40
+ hash[phrase.locale] ||= {}
41
+ hash[phrase.locale][phrase.key] = phrase.value
42
+ end
43
+ hash.to_yaml
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module Phrasing
2
- VERSION = "3.2.2"
2
+ VERSION = "3.2.3"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  class SiteController < ApplicationController
2
2
  def index
3
-
4
3
  end
5
4
  end
@@ -7404,3 +7404,149 @@ Processing by PhrasingPhrasesController#download as HTML
7404
7404
  Rendered text template (0.0ms)
7405
7405
  Sent data phrasing_phrases_Dummy_development_2014_02_20_14_59_09.yml (12.6ms)
7406
7406
  Completed 200 OK in 45ms (Views: 12.3ms | ActiveRecord: 0.2ms)
7407
+ DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from service at /Users/damirsvrtan/.rbenv/versions/1.9.3-p362/lib/ruby/1.9.1/webrick/httpserver.rb:138)
7408
+
7409
+
7410
+ Started GET "/" for 127.0.0.1 at 2014-03-03 15:16:54 +0100
7411
+ Processing by SiteController#index as HTML
7412
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.header' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7413
+ PhrasingPhrase Load (0.3ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.intro' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7414
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND "phrasing_phrases"."key" = 'site.index.footer' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7415
+ PhrasingPhrase Load (0.1ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'models.errors.test' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7416
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.test' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7417
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND "phrasing_phrases"."key" = 'datetime.distance_in_words.less_than_x_minutes' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7418
+ Rendered site/index.html.erb within layouts/application (44.0ms)
7419
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_initializer.html.haml (9.0ms)
7420
+ Completed 200 OK in 279ms (Views: 274.6ms | ActiveRecord: 3.8ms)
7421
+
7422
+
7423
+ Started GET "/assets/phrasing.css?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7424
+
7425
+
7426
+ Started GET "/assets/phrasing_edit_mode_bubble.css?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7427
+
7428
+
7429
+ Started GET "/assets/jquery_ujs.js?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7430
+
7431
+
7432
+ Started GET "/assets/editor.js?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7433
+
7434
+
7435
+ Started GET "/assets/jquery.cookie.js?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7436
+
7437
+
7438
+ Started GET "/assets/phrasing.js?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7439
+
7440
+
7441
+ Started GET "/assets/application.css?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7442
+
7443
+
7444
+ Started GET "/assets/phrasing_fonts.css?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7445
+
7446
+
7447
+ Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7448
+
7449
+
7450
+ Started GET "/assets/application.js?body=1" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7451
+
7452
+
7453
+ Started GET "/assets/phrasing_icon_edit_all.png" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7454
+
7455
+
7456
+ Started GET "/assets/icomoon.woff" for 127.0.0.1 at 2014-03-03 15:16:55 +0100
7457
+
7458
+
7459
+ Started GET "/phrasing" for 127.0.0.1 at 2014-03-03 15:17:02 +0100
7460
+ Processing by PhrasingPhrasesController#index as HTML
7461
+ PhrasingPhrase Load (0.3ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND (value is not null) ORDER BY phrasing_phrases.key
7462
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND (value is null) ORDER BY phrasing_phrases.key
7463
+  (0.2ms) SELECT DISTINCT "phrasing_phrases"."locale" FROM "phrasing_phrases"
7464
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing_phrases/index.html.haml within layouts/phrasing (5.2ms)
7465
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_menu.html.haml (2.8ms)
7466
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_messages.html.haml (1.6ms)
7467
+ Completed 200 OK in 52ms (Views: 41.5ms | ActiveRecord: 0.7ms)
7468
+
7469
+
7470
+ Started GET "/assets/phrasing_engine.css?body=1" for 127.0.0.1 at 2014-03-03 15:17:02 +0100
7471
+
7472
+
7473
+ Started GET "/assets/phrasing_engine.js?body=1" for 127.0.0.1 at 2014-03-03 15:17:02 +0100
7474
+
7475
+
7476
+ Started GET "/phrasing/import_export" for 127.0.0.1 at 2014-03-03 15:17:04 +0100
7477
+ Processing by PhrasingPhrasesController#import_export as HTML
7478
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing_phrases/import_export.html.haml within layouts/phrasing (3.5ms)
7479
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_menu.html.haml (1.5ms)
7480
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_messages.html.haml (0.2ms)
7481
+ Completed 200 OK in 13ms (Views: 12.5ms | ActiveRecord: 0.0ms)
7482
+
7483
+
7484
+ Started GET "/phrasing/download" for 127.0.0.1 at 2014-03-03 15:17:07 +0100
7485
+ Processing by PhrasingPhrasesController#download as HTML
7486
+ PhrasingPhrase Load (0.5ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE (value is not null)
7487
+ Rendered text template (0.0ms)
7488
+ Sent data phrasing_phrases_Dummy_development_2014_03_03_15_17_07.yml (3.6ms)
7489
+ Completed 200 OK in 10ms (Views: 3.4ms | ActiveRecord: 0.5ms)
7490
+
7491
+
7492
+ Started GET "/phrasing/download" for 127.0.0.1 at 2014-03-03 15:17:18 +0100
7493
+ Processing by PhrasingPhrasesController#download as HTML
7494
+ PhrasingPhrase Load (0.3ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE (value is not null)
7495
+ Rendered text template (0.0ms)
7496
+ Sent data phrasing_phrases_Dummy_development_2014_03_03_15_17_18.yml (0.5ms)
7497
+ Completed 200 OK in 4ms (Views: 0.4ms | ActiveRecord: 0.3ms)
7498
+
7499
+
7500
+ Started POST "/phrasing/upload" for 127.0.0.1 at 2014-03-03 15:17:22 +0100
7501
+ Processing by PhrasingPhrasesController#upload as HTML
7502
+ Parameters: {"utf8"=>"✓", "authenticity_token"=>"3LjfrYRliYOJpG5+jrxNczvfGAWe9mG9VdLB69x9ZT0=", "file"=>#<ActionDispatch::Http::UploadedFile:0x000001032dc310 @tempfile=#<File:/var/folders/sr/sriroFQ2HY8-IgGJc7Y7qE+++To/-Tmp-/RackMultipart20140303-6179-rm2q9f>, @original_filename="phrasing_phrases_Dummy_development_2014_03_03_15_17_18.yml", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"file\"; filename=\"phrasing_phrases_Dummy_development_2014_03_03_15_17_18.yml\"\r\nContent-Type: application/octet-stream\r\n">, "commit"=>"Upload"}
7503
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.header' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7504
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.intro' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7505
+ PhrasingPhrase Load (0.1ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.footer' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7506
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'models.errors.test' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7507
+ PhrasingPhrase Load (0.1ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.test' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7508
+ Redirected to http://localhost:3000/phrasing
7509
+ Completed 302 Found in 5ms (ActiveRecord: 0.8ms)
7510
+
7511
+
7512
+ Started GET "/phrasing" for 127.0.0.1 at 2014-03-03 15:17:22 +0100
7513
+ Processing by PhrasingPhrasesController#index as HTML
7514
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND (value is not null) ORDER BY phrasing_phrases.key
7515
+ PhrasingPhrase Load (0.1ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND (value is null) ORDER BY phrasing_phrases.key
7516
+  (0.1ms) SELECT DISTINCT "phrasing_phrases"."locale" FROM "phrasing_phrases"
7517
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing_phrases/index.html.haml within layouts/phrasing (2.1ms)
7518
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_menu.html.haml (0.5ms)
7519
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_messages.html.haml (0.2ms)
7520
+ Completed 200 OK in 11ms (Views: 8.6ms | ActiveRecord: 0.5ms)
7521
+
7522
+
7523
+ Started GET "/phrasing" for 127.0.0.1 at 2014-03-03 15:17:24 +0100
7524
+ Processing by PhrasingPhrasesController#index as HTML
7525
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND (value is not null) ORDER BY phrasing_phrases.key
7526
+ PhrasingPhrase Load (0.1ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND (value is null) ORDER BY phrasing_phrases.key
7527
+  (0.1ms) SELECT DISTINCT "phrasing_phrases"."locale" FROM "phrasing_phrases"
7528
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing_phrases/index.html.haml within layouts/phrasing (2.4ms)
7529
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_menu.html.haml (0.4ms)
7530
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_messages.html.haml (0.1ms)
7531
+ Completed 200 OK in 12ms (Views: 10.0ms | ActiveRecord: 0.5ms)
7532
+
7533
+
7534
+ Started GET "/phrasing/import_export" for 127.0.0.1 at 2014-03-03 15:17:26 +0100
7535
+ Processing by PhrasingPhrasesController#import_export as HTML
7536
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing_phrases/import_export.html.haml within layouts/phrasing (1.3ms)
7537
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_menu.html.haml (0.5ms)
7538
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_messages.html.haml (0.1ms)
7539
+ Completed 200 OK in 25ms (Views: 25.0ms | ActiveRecord: 0.0ms)
7540
+
7541
+
7542
+ Started GET "/" for 127.0.0.1 at 2014-03-03 15:17:51 +0100
7543
+ Processing by SiteController#index as HTML
7544
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.header' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7545
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.index.intro' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7546
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND "phrasing_phrases"."key" = 'site.index.footer' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7547
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'models.errors.test' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7548
+ PhrasingPhrase Load (0.3ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."key" = 'site.test' AND "phrasing_phrases"."locale" = 'en' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7549
+ PhrasingPhrase Load (0.2ms) SELECT "phrasing_phrases".* FROM "phrasing_phrases" WHERE "phrasing_phrases"."locale" = 'en' AND "phrasing_phrases"."key" = 'datetime.distance_in_words.less_than_x_minutes' ORDER BY "phrasing_phrases"."id" ASC LIMIT 1
7550
+ Rendered site/index.html.erb within layouts/application (8.1ms)
7551
+ Rendered /Users/damirsvrtan/Documents/rails_infinum_projects/phrasing/app/views/phrasing/_initializer.html.haml (1.1ms)
7552
+ Completed 200 OK in 18ms (Views: 16.2ms | ActiveRecord: 1.2ms)
@@ -80,12 +80,12 @@ feature "yaml" do
80
80
 
81
81
  describe 'on first visit value should be' do
82
82
 
83
- it "same as keys" do
83
+ it "same as keys if there is no translation available" do
84
84
  visit root_path
85
85
  PhrasingPhrase.find_by_key('site.index.intro').value.should == 'site.index.intro'
86
86
  end
87
87
 
88
- it "same as translations in the yaml file" do
88
+ it "same as translations in the yaml file if there is a translation available" do
89
89
  visit root_path
90
90
  PhrasingPhrase.find_by_key('site.index.header').value.should == 'The Header'
91
91
  PhrasingPhrase.find_by_key('site.index.footer').value.should == 'The Footer'
@@ -251,8 +251,6 @@ feature "phrasing update" do
251
251
  end
252
252
 
253
253
  feature "downloading and uploading yaml files" do
254
- before do
255
- end
256
254
  after do
257
255
  PhrasingPhrase.delete_all
258
256
  end
@@ -0,0 +1,166 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'pry-debugger'
4
+
5
+ describe Phrasing::Serializer do
6
+
7
+ let(:english_phrases) do
8
+ PhrasingPhrase.where(locale: :en)
9
+ end
10
+
11
+ let(:french_phrases) do
12
+ PhrasingPhrase.where(locale: :fr)
13
+ end
14
+
15
+ describe 'flatten_the_hash' do
16
+
17
+ it 'with simple data' do
18
+ hash = { "site.intro" => "Hello", "site.outro" => "KthnxBye", "site.overture" => "The Butler"}
19
+ new_hash = Phrasing::Serializer.flatten_the_hash(hash)
20
+ expect(new_hash).to eq(hash)
21
+ end
22
+
23
+ it 'with nested data' do
24
+ hash = { "site.text" => {"intro" => "Hello", "outro" => "KthnxBye"}}
25
+ new_hash = Phrasing::Serializer.flatten_the_hash(hash)
26
+
27
+ predicted_new_hash = { "site.text.intro" => "Hello", "site.text.outro" => "KthnxBye" }
28
+ expect(new_hash).to eq(predicted_new_hash)
29
+ end
30
+
31
+ it 'with deeply nested data' do
32
+ hash = { "site" => {"text" => {"intro" => "Hello", "outro" => "KthnxBye", "overture" => {"intro" => "Overture intro", "outro" => "Overture outro"}}}}
33
+ new_hash = Phrasing::Serializer.flatten_the_hash(hash)
34
+
35
+ predicted_new_hash = { "site.text.intro" => "Hello", "site.text.outro" => "KthnxBye", "site.text.overture.intro" => "Overture intro", "site.text.overture.outro" => "Overture outro"}
36
+ expect(new_hash).to eq(predicted_new_hash)
37
+ end
38
+
39
+ end
40
+
41
+ context 'import_yaml' do
42
+
43
+ it 'simply formatted phrases' do
44
+ yaml = <<-YAML
45
+ en:
46
+ intro: "Hello World"
47
+ outro: "Kthnx Bye"
48
+ YAML
49
+ Phrasing::Serializer.import_yaml(StringIO.new(yaml))
50
+
51
+ expect(english_phrases.where(key: "intro").first.value).to eq("Hello World")
52
+ expect(english_phrases.where(key: "outro").first.value).to eq("Kthnx Bye")
53
+ end
54
+
55
+ it 'nested phrases' do
56
+ yaml = <<-YAML
57
+ en:
58
+ site:
59
+ intro: "Hello World"
60
+ outro: "Kthnx Bye"
61
+ YAML
62
+ Phrasing::Serializer.import_yaml(StringIO.new(yaml))
63
+
64
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Hello World")
65
+ expect(english_phrases.where(key: "site.outro").first.value).to eq("Kthnx Bye")
66
+ end
67
+
68
+ it 'deeply nested phrases' do
69
+ yaml = <<-YAML
70
+ en:
71
+ site:
72
+ text:
73
+ intro: "Hello World"
74
+ outro: "Kthnx Bye"
75
+ overture:
76
+ intro: "Overture intro"
77
+ outro: "Overture outro"
78
+ YAML
79
+ Phrasing::Serializer.import_yaml(StringIO.new(yaml))
80
+
81
+ expect(english_phrases.where(key: "site.text.intro").first.value).to eq("Hello World")
82
+ expect(english_phrases.where(key: "site.text.outro").first.value).to eq("Kthnx Bye")
83
+
84
+ expect(english_phrases.where(key: "site.text.overture.intro").first.value).to eq("Overture intro")
85
+ expect(english_phrases.where(key: "site.text.overture.outro").first.value).to eq("Overture outro")
86
+ end
87
+
88
+
89
+ it 'overrides old values' do
90
+ FactoryGirl.create(:phrasing_phrase, key: "site.intro", value: "Go Home")
91
+ FactoryGirl.create(:phrasing_phrase, key: "site.outro", value: "Kthnx Bye")
92
+
93
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Go Home")
94
+ expect(english_phrases.where(key: "site.outro").first.value).to eq("Kthnx Bye")
95
+
96
+ yaml = <<-YAML
97
+ en:
98
+ site:
99
+ intro: "Hello World"
100
+ YAML
101
+ number_of_changes = Phrasing::Serializer.import_yaml(StringIO.new(yaml))
102
+
103
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Hello World")
104
+ expect(english_phrases.where(key: "site.outro").first.value).to eq("Kthnx Bye")
105
+
106
+ expect(number_of_changes).to eq(1)
107
+ end
108
+
109
+ end
110
+
111
+
112
+ context 'imports and exports' do
113
+ it 'without changing, should return 0 number of changes' do
114
+ FactoryGirl.create(:phrasing_phrase, key: "site.intro", value: "Go Home")
115
+ FactoryGirl.create(:phrasing_phrase, key: "site.outro", value: "Kthnx Bye")
116
+
117
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Go Home")
118
+ expect(english_phrases.where(key: "site.outro").first.value).to eq("Kthnx Bye")
119
+
120
+ yaml = Phrasing::Serializer.export_yaml
121
+
122
+ number_of_changes = Phrasing::Serializer.import_yaml(StringIO.new(yaml))
123
+
124
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Go Home")
125
+ expect(english_phrases.where(key: "site.outro").first.value).to eq("Kthnx Bye")
126
+ expect(number_of_changes).to eq(0)
127
+ end
128
+ end
129
+
130
+
131
+ context 'export_yaml' do
132
+ it 'flattened phrases' do
133
+ FactoryGirl.create(:phrasing_phrase, key: "site.intro", value: "Go Home")
134
+ FactoryGirl.create(:phrasing_phrase, key: "site.outro", value: "Kthnx Bye")
135
+
136
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Go Home")
137
+ expect(english_phrases.where(key: "site.outro").first.value).to eq("Kthnx Bye")
138
+
139
+ yaml = Phrasing::Serializer.export_yaml
140
+
141
+ expect(yaml).to match(/site.intro:\sGo Home/)
142
+ expect(yaml).to match(/site.outro:\sKthnx Bye/)
143
+ end
144
+
145
+ it 'with different locales' do
146
+ FactoryGirl.create(:phrasing_phrase, key: "site.intro", value: "Hello", locale: :en)
147
+ FactoryGirl.create(:phrasing_phrase, key: "site.intro", value: "Bonjour", locale: :fr)
148
+
149
+ expect(english_phrases.where(key: "site.intro").first.value).to eq("Hello")
150
+ expect(french_phrases.where(key: "site.intro").first.value).to eq("Bonjour")
151
+
152
+ yaml = Phrasing::Serializer.export_yaml
153
+
154
+ expect(yaml).to match(/en:\s*\n\s*site.intro:\sHello/)
155
+ expect(yaml).to match(/fr:\s*\n\s*site.intro:\sBonjour/)
156
+ end
157
+
158
+ end
159
+
160
+
161
+
162
+ end
163
+
164
+
165
+
166
+
@@ -1,46 +1,32 @@
1
1
  #encoding: utf-8
2
2
  require 'spec_helper'
3
+ require 'pry-debugger'
3
4
 
4
5
  describe PhrasingPhrase do
5
6
 
6
7
  describe "database constraints" do
7
- it "validates uniqueness of key & locale" do
8
- PhrasingPhrase.new(key: "foo", locale: "en", value: "bar").save
9
- # a = PhrasingPhrase.new(key: "foo", locale: "en", value: "bar2")
10
- # expect { a.save }.to raise_error
11
- b = PhrasingPhrase.new(key: "foo", locale: "fa", value: "bar")
12
- expect { b.save }.not_to raise_error
13
- end
14
- end
15
-
16
- it "imports YAML" do
17
- FactoryGirl.create(:phrasing_phrase, :key => "sample_copy", :value => "copyfoo")
18
- FactoryGirl.create(:phrasing_phrase, :key => "sample_copy2", :value => "copybaz")
19
-
20
- assert PhrasingPhrase.find_by_key("sample_copy").value == "copyfoo"
21
- assert PhrasingPhrase.find_by_key("sample_copy2").value == "copybaz"
22
- assert PhrasingPhrase.find_by_key("hello").nil?
8
+ context "validates uniqueness of key & locale" do
9
+
10
+ it 'on #save! should raise errors if the key and locale are the same for two phrases' do
11
+ PhrasingPhrase.create(key: "foo", locale: "en", value: "bar")
12
+ new_phrase = PhrasingPhrase.new(key: "foo", locale: "en", value: "bar2")
13
+ expect { new_phrase.save! }.to raise_error
14
+ end
15
+
16
+ it 'on #save, if the key and locale are the same for two phrases, the latter one should be invalid' do
17
+ PhrasingPhrase.create(key: "foo", locale: "en", value: "bar")
18
+ new_phrase = PhrasingPhrase.create(key: "foo", locale: "en", value: "bar2")
19
+ expect(new_phrase).to be_invalid
20
+ end
21
+
22
+ it "shouldn't raise errors if the key is the same but locales are different" do
23
+ PhrasingPhrase.create(key: "foo", locale: "en", value: "bar")
24
+ new_phrase = PhrasingPhrase.new(key: "foo", locale: "fr", value: "bar")
25
+ expect { new_phrase.save! }.not_to raise_error
26
+ expect(new_phrase).to be_valid
27
+ end
23
28
 
24
- yaml = <<-YAML
25
- en:
26
- hello: "Hello world"
27
- sample_copy: "lorem ipsum"
28
- YAML
29
- PhrasingPhrase.import_yaml(StringIO.new(yaml))
30
-
31
- assert PhrasingPhrase.find_by_key("sample_copy").value == "lorem ipsum"
32
- assert PhrasingPhrase.find_by_key("sample_copy2").value == "copybaz"
33
- assert PhrasingPhrase.find_by_key("hello").value == "Hello world"
34
- end
35
-
36
- it "exports and then imports complicated YAML" do
37
- key = "moby_dick"
38
- value = %|<p>Lorem ipsum</p><p class="highlight">∆'≈:</p>|
39
- FactoryGirl.create(:phrasing_phrase, key: key, value: value)
40
- yaml = PhrasingPhrase.export_yaml
41
- PhrasingPhrase.destroy_all
42
- PhrasingPhrase.import_yaml(StringIO.new(yaml))
43
- PhrasingPhrase.find_by_key(key).value.should == value
29
+ end
44
30
  end
45
31
 
46
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phrasing
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-02-20 00:00:00.000000000 Z
13
+ date: 2014-03-03 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -184,6 +184,7 @@ files:
184
184
  - db/migrate/20131010101010_create_phrasing_phrase_versions.rb
185
185
  - lib/phrasing.rb
186
186
  - lib/phrasing/implementation.rb
187
+ - lib/phrasing/serializer.rb
187
188
  - lib/phrasing/simple.rb
188
189
  - lib/phrasing/version.rb
189
190
  - lib/tasks/phrasing_tasks.rake
@@ -195,7 +196,6 @@ files:
195
196
  - spec/dummy/app/controllers/site_controller.rb
196
197
  - spec/dummy/app/helpers/application_helper.rb
197
198
  - spec/dummy/app/helpers/phrasing_helper.rb
198
- - spec/dummy/app/mailers/.gitkeep
199
199
  - spec/dummy/app/models/.gitkeep
200
200
  - spec/dummy/app/views/layouts/application.html.erb
201
201
  - spec/dummy/app/views/site/index.html.erb
@@ -222,7 +222,6 @@ files:
222
222
  - spec/dummy/db/migrate/20131120000856_create_phrasing_phrase_versions.phrasing_rails_engine.rb
223
223
  - spec/dummy/db/schema.rb
224
224
  - spec/dummy/db/test.sqlite3
225
- - spec/dummy/lib/assets/.gitkeep
226
225
  - spec/dummy/log/.gitkeep
227
226
  - spec/dummy/log/development.log
228
227
  - spec/dummy/log/test.log
@@ -234,6 +233,7 @@ files:
234
233
  - spec/factories/phrasing_phrases.rb
235
234
  - spec/features/dummy_spec.rb
236
235
  - spec/features/phrasing_spec.rb
236
+ - spec/lib/phrasing_serializer_spec.rb
237
237
  - spec/lib/phrasing_spec.rb
238
238
  - spec/models/phrasing_phrase_spec.rb
239
239
  - spec/spec_helper.rb
File without changes
File without changes