promethee 0.1.8 → 1.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 33e5e071efd89e21816ec9dd45caa7ad47962d25
4
- data.tar.gz: fabaf62dc44e7ea200fe22658246cb49d41b1598
3
+ metadata.gz: 2b518d1f83474f06c099c7714b4696d270cde07b
4
+ data.tar.gz: 5401b785dce334c87e2c3de4d005b91ee2ec9bb0
5
5
  SHA512:
6
- metadata.gz: 9a9096871a8d6eedf9114aacb463f7975d1c920b3e3e6ef0276f7469a9587a4152f5f90ab46e0d30172fcb40cc330393753fec91ac4f9bc4a9f6752b736a9cd8
7
- data.tar.gz: b4a7010c2a638d203ce65167ab85c82fc0b4f9d2092558b44d56b16b140825dce1eae2d08082a1edf8bd08579f488ddf66c12323a8752a9e13db15eef61800e7
6
+ metadata.gz: 117e2b926c3eca318925429f13c9013abf32d8f0345774747fbba7a7db084a3e2ef8f3d25b74a82839b9f3d61b85d43cbdceb48de7ebb0c35fa5a7dc8312d270
7
+ data.tar.gz: 4047603f08cc82901a66db5bd2a13d1f20f9f583668f995e8695d7b8e79fbab65ec7a256828c1fc1d86b8fd168ef7ad2eb53bc91ea7dba4893c4011e50a56d30
data/README.md CHANGED
@@ -213,7 +213,7 @@ The page can be localized.
213
213
  The localization data looks like:
214
214
  ```
215
215
  @localization.data = {
216
- locale: 'fr',
216
+ version: 1,
217
217
  components: [
218
218
  {
219
219
  id: '9lebjl4j6',
@@ -1,12 +1,11 @@
1
1
  <%
2
- data = data.to_json unless data.is_a? String
2
+ promethee_data = Promethee::Data.new master_data
3
3
  %>
4
4
 
5
-
6
5
  <script>
7
6
  var promethee = angular
8
7
  .module('Promethee', ['summernote', 'ngAnimate'])
9
- .value('data', <%= data.html_safe %>)
8
+ .value('data', <%= promethee_data.to_json.html_safe %>)
10
9
  .value('definitions', [])
11
10
  .filter('htmlSafe', ['$sce', function($sce) {
12
11
  return function(val) {
@@ -1,12 +1,16 @@
1
1
  <%
2
- data = data.to_json unless data.is_a? String
3
- master = master.to_json unless master.is_a? String
2
+ promethee_data = Promethee::Data.new master_data, localization_data: localization_data
4
3
  %>
5
4
 
6
5
  <script>
7
6
  var prometheeLocalizer = angular
8
7
  .module('PrometheeLocalizer', ['summernote'])
9
- .value('data', <%= data.html_safe %>);
8
+ .value('data', <%= promethee_data.localization_data_to_json.html_safe %>)
9
+ .filter('htmlSafe', ['$sce', function($sce) {
10
+ return function(val) {
11
+ return $sce.trustAsHtml(val);
12
+ };
13
+ }]);
10
14
 
11
15
  prometheeLocalizer.controller('PrometheeLocalizerController', ['$scope', 'data', function($scope, data) {
12
16
 
@@ -20,7 +24,7 @@ master = master.to_json unless master.is_a? String
20
24
  ['code', ['codeview']]
21
25
  ]
22
26
  };
23
-
27
+
24
28
  }]);
25
29
 
26
30
  angular.element(function() {
@@ -1,8 +1,7 @@
1
1
  <%
2
- data = Promethee::Data.prepare data
3
- l = Promethee::Data.prepare l
4
- Promethee::Localization.localize! data, l unless l.nil?
2
+ promethee_data = Promethee::Data.new master_data, localization_data: localization_data
5
3
  %>
4
+
6
5
  <div class="promethee">
7
- <%= render 'promethee/components/page', page: data %>
6
+ <%= render 'promethee/components/page', page: promethee_data.to_h %>
8
7
  </div>
@@ -19,7 +19,15 @@
19
19
  </script>
20
20
 
21
21
  <script type="text/ng-template" id="promethee/components/text/localize">
22
- <summernote config="options" ng-model="component.attributes.body"></summernote>
22
+ <div class="row">
23
+ <div class="col-md-6">
24
+ <summernote config="options" ng-model="component.attributes.body"></summernote>
25
+ </div>
26
+ <div class="col-md-6">
27
+ <b>Master</b>
28
+ <div class="promethee-editor__wrapper" ng-bind-html="component.master.attributes.body | htmlSafe"></div>
29
+ </div>
30
+ </div>
23
31
  </script>
24
32
 
25
33
  <script>
@@ -20,5 +20,4 @@ module Promethee
20
20
  require 'promethee/core_ext/form_builder'
21
21
 
22
22
  require 'promethee/data'
23
- require 'promethee/localization'
24
23
  end
@@ -6,18 +6,18 @@ module ActionView
6
6
  # https://github.com/rails/rails/blob/bdc581616b760d1e2be3795c6f0f3ab4b1e125a5/actionview/lib/action_view/helpers/tags/text_field.rb
7
7
  class PrometheeEditor < Base
8
8
  def render
9
- data = object.send @method_name unless object.nil?
10
- data = @options[:value] if @options.include? :value
11
- ApplicationController.renderer.render partial: 'promethee/edit', locals: { data: data }
9
+ master_data = object.send @method_name unless object.nil?
10
+ master_data = @options[:value] if @options.include? :value
11
+ ApplicationController.renderer.render partial: 'promethee/edit', locals: { master_data: master_data }
12
12
  end
13
13
  end
14
14
 
15
15
  class PrometheeLocalizer < Base
16
16
  def render
17
- data = object.send @method_name unless object.nil?
18
- data = @options[:value] if @options.include? :value
19
- master = @options[:master]
20
- ApplicationController.renderer.render partial: 'promethee/localize', locals: { data: data, master: master }
17
+ localization_data = object.send @method_name unless object.nil?
18
+ localization_data = @options[:value] if @options.include? :value
19
+ master_data = @options[:master]
20
+ ApplicationController.renderer.render partial: 'promethee/localize', locals: { localization_data: localization_data, master_data: master_data }
21
21
  end
22
22
  end
23
23
  end
@@ -1,9 +1,120 @@
1
- class Promethee::Data
2
- def self.prepare(data)
3
- if data.is_a? String
4
- JSON.parse data, symbolize_names: true
5
- else
6
- data
1
+ # The data class can be used for any level: the page, or one of the components
2
+ module Promethee
3
+ class Data
4
+ def initialize(data, options = {})
5
+ @master_data = convert_if_necessary data
6
+ @master_data_unlocalized = deep_clone @master_data
7
+ localization_data = options[:localization_data]
8
+ unless localization_data.nil?
9
+ @localization_data = convert_if_necessary localization_data
10
+ localize!
11
+ end
12
+ end
13
+
14
+ def localization_data
15
+ prepare_localization
16
+ @localization_data
17
+ end
18
+
19
+ def localization_data_to_json
20
+ localization_data.to_json
21
+ end
22
+
23
+ def to_json
24
+ @master_data.to_json
25
+ end
26
+
27
+ def to_h
28
+ @master_data
29
+ end
30
+
31
+ protected
32
+
33
+ def convert_if_necessary(string_or_hash)
34
+ string_or_hash.is_a?(String) ? JSON.parse(string_or_hash, symbolize_names: true) : string_or_hash
35
+ end
36
+
37
+ def deep_clone(hash)
38
+ JSON.parse(hash.to_json, symbolize_names: true)
39
+ end
40
+
41
+ # This will recursively merge the localization_data into the master_data
42
+ def localize!
43
+ merge!
44
+ localize_children!
45
+ end
46
+
47
+ def merge!
48
+ @master_data[:attributes].merge! localized_component[:attributes] if localized?
49
+ end
50
+
51
+ # Look for localized component with the same id
52
+ def localized_component
53
+ @localized_component ||= components.select { |component| component[:id] == @master_data[:id] }.first
54
+ end
55
+
56
+ def components
57
+ @components ||= @localization_data.include?(:components) ? @localization_data[:components] : []
58
+ end
59
+
60
+ def localized?
61
+ !localized_component.nil?
62
+ end
63
+
64
+ def localize_children!
65
+ children.each do |child_hash|
66
+ child_data = Promethee::Data.new child_hash, localization_data: @localization_data
67
+ child_data.localize!
68
+ end
69
+ end
70
+
71
+ def children
72
+ return [] if !@master_data.include? :children or @master_data[:children].nil?
73
+ @master_data[:children]
74
+ end
75
+
76
+ def prepare_localization
77
+ # TODO extract localizable components from master data, in the correct order, merged with existing ones
78
+ # TODO update master_version
79
+
80
+ # Flattens master data, and select only text components
81
+ flat_master_data = self.class.flatten_components(@master_data_unlocalized[:children]).select do |component|
82
+ component[:type].to_sym === :text
83
+ end
84
+
85
+ # We add master reference
86
+ flat_master_data.each do |data|
87
+ data[:master] = data.clone
88
+ end
89
+
90
+ if @localization_data
91
+ # If localization_data has been provided, we merge flat_master_data components with its components
92
+ @localization_data[:components] = flat_master_data.map do |component|
93
+ localized_component = @localization_data[:components].find do |localized_component|
94
+ localized_component[:id] == component[:id]
95
+ end
96
+
97
+ # Update master reference
98
+ localized_component[:master] = component[:master] unless localized_component.nil?
99
+
100
+ localized_component || component
101
+ end
102
+ else
103
+ # In the other case, localization_data components are flat_master_data components
104
+ @localization_data = {
105
+ version: @master_data[:version],
106
+ components: flat_master_data
107
+ }
108
+ end
109
+ end
110
+
111
+ # This method take an array of components and puts every components and their children into a unique flat array
112
+ def self.flatten_components components
113
+ components.reduce [] do |flat_components, component|
114
+ flat_components +
115
+ [component.except(:children)] +
116
+ flatten_components(component[:children] || [])
117
+ end
7
118
  end
8
119
  end
9
120
  end
@@ -1,6 +1,6 @@
1
1
  module Promethee::Rails::Helper
2
2
  def promethee(data, options = {})
3
- ApplicationController.renderer.render partial: 'promethee/show', locals: { data: data, l: options[:l] }
3
+ ApplicationController.renderer.render partial: 'promethee/show', locals: { master_data: data, localization_data: options[:l] }
4
4
  end
5
5
 
6
6
  def promethee_class_for component
@@ -1,5 +1,5 @@
1
1
  module Promethee
2
2
  module Rails
3
- VERSION = '0.1.8'
3
+ VERSION = '1.0.0'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: promethee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julien Dargelos
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-11-27 00:00:00.000000000 Z
14
+ date: 2017-11-29 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: rails
@@ -195,6 +195,20 @@ dependencies:
195
195
  - - ">="
196
196
  - !ruby/object:Gem::Version
197
197
  version: '0'
198
+ - !ruby/object:Gem::Dependency
199
+ name: annotate
200
+ requirement: !ruby/object:Gem::Requirement
201
+ requirements:
202
+ - - ">="
203
+ - !ruby/object:Gem::Version
204
+ version: '0'
205
+ type: :development
206
+ prerelease: false
207
+ version_requirements: !ruby/object:Gem::Requirement
208
+ requirements:
209
+ - - ">="
210
+ - !ruby/object:Gem::Version
211
+ version: '0'
198
212
  description:
199
213
  email:
200
214
  - contact@juliendargelos.com
@@ -249,7 +263,6 @@ files:
249
263
  - lib/promethee/core_ext/form_helper.rb
250
264
  - lib/promethee/core_ext/tags.rb
251
265
  - lib/promethee/data.rb
252
- - lib/promethee/localization.rb
253
266
  - lib/promethee/rails/engine.rb
254
267
  - lib/promethee/rails/helper.rb
255
268
  - lib/promethee/rails/version.rb
@@ -332,7 +345,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
332
345
  version: '0'
333
346
  requirements: []
334
347
  rubyforge_project:
335
- rubygems_version: 2.6.11
348
+ rubygems_version: 2.6.14
336
349
  signing_key:
337
350
  specification_version: 4
338
351
  summary: Bring fire to your page
@@ -1,7 +0,0 @@
1
- class Promethee::Localization
2
- def self.localize!(master_data, localization_data)
3
- localized_components = localization_data[:components].select { |component| component[:id] == master_data[:id] }.first
4
- master_data[:attributes].merge! localized_components[:attributes] unless localized_components.nil?
5
- master_data[:children].each { |d| localize! d, localization_data } unless master_data[:children].nil?
6
- end
7
- end