para-i18n 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: bffd454f68783dc4413fab9cff53b3c899b0f908
4
- data.tar.gz: 783928239cee302a6e963057cc8f68becd744c8c
2
+ SHA256:
3
+ metadata.gz: 90d12992580fc4a99c6c61436e3fda627f33c8064316d72d9dac154234bf7e58
4
+ data.tar.gz: 4592e7c3895bc9200f212879171f5b039d75884bd0dfa73f576a8624346fbd5d
5
5
  SHA512:
6
- metadata.gz: aa848897075a6d40a94d92451e3b660e1c18aade214e90ca0a394d066b58dd03f3d143f6457f29e469b805215f37e451f21a093443f67cc7298b2bd8a7a047de
7
- data.tar.gz: 3ce0e4a22d344090a9d98ba86fb505b778549e2a230d8e981b9288af8b0d16da180e8993d1b828c7d795d867092d01575c3a040e43e74f6f2c3c05ade9eaa975
6
+ metadata.gz: 2cf9a1a632377a4d9d4f0780401028e4fd44b6991427a96af28e9ea21414d804a3ad97d0ca552b051fe43848a0c98f5df703a999e104bd0a8eae9309a3b7d69c
7
+ data.tar.gz: f676eac78ac6492fc3a283a162a0e06874d55873ead0e5a0c429ed93cc2445e6dfa2f0491e51e08626eca37dbcc59c08eaba07a083248c843d9d84c67a9bd8af
@@ -0,0 +1,13 @@
1
+ module Para
2
+ module I18n
3
+ module FormHelper
4
+ def para_translations_form_for(resource, options = {}, &block)
5
+ extended_options = options.reverse_merge(
6
+ Para::I18n::FormBuilder::TranslationsForm::TRANSLATIONS_FORM_PARAM_KEY => true
7
+ )
8
+
9
+ para_form_for(resource, extended_options, &block)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ = form.fieldset do
2
+ - I18n.with_locale(@target_locale) do
3
+ = form.input :_disabled_for_locale, as: :boolean, label: t("para.i18n.disabled_for_locale")
4
+
5
+ - translated_model_fields_for(form.object.class).each do |field|
6
+ = form.input field.field_name, as: :i18n
7
+
8
+ - nested_relations_attributes_for(form.object.class).each do |field|
9
+ = form.input field.field_name, as: field.field_type
@@ -1,10 +1,14 @@
1
- = para_form_for(resource, url: @component.relation_path(resource, :translation)) do |form|
1
+ = para_translations_form_for(resource, url: @component.relation_path(resource, :translation), locale: @target_locale) do |form|
2
2
  = target_locale_select
3
3
 
4
4
  = form.fieldset do
5
- = I18n.with_locale(@target_locale) { form.input :_disabled_for_locale, as: :boolean, label: t("para.i18n.disabled_for_locale") }
5
+ - I18n.with_locale(@target_locale) do
6
+ = form.input :_disabled_for_locale, as: :boolean, label: t("para.i18n.disabled_for_locale")
6
7
 
7
8
  - translated_model_fields_for(resource.class).each do |field|
8
- = form.input field.field_name, as: :i18n, locale: @target_locale
9
+ = form.input field.field_name, as: :i18n
10
+
11
+ - nested_relations_attributes_for(resource.class).each do |field|
12
+ = form.input field.field_name, as: field.field_type
9
13
 
10
14
  = form.actions(except: :submit_and_add_another)
@@ -0,0 +1,28 @@
1
+ module Para
2
+ module I18n
3
+ module FormGeneratorConcern
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ include Para::Admin::BaseHelper
8
+ include Para::Generators::FieldHelpers
9
+ include Para::ModelHelper
10
+ include Para::I18n::Helpers::TranslationsHelper
11
+ end
12
+
13
+ private
14
+
15
+ def translated_attributes
16
+ translated_model_fields_for(model)
17
+ end
18
+
19
+ def nested_relations_attributes
20
+ nested_relations_attributes_for(model)
21
+ end
22
+
23
+ def model
24
+ @model ||= class_name.constantize
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,10 +1,9 @@
1
+ require "generators/para/i18n/base_form_generator"
2
+
1
3
  module Para
2
4
  module I18n
3
5
  class FormGenerator < Para::Generators::NamedBase
4
- include Para::Admin::BaseHelper
5
- include Para::Generators::FieldHelpers
6
- include Para::ModelHelper
7
- include Para::I18n::Helpers::TranslationsHelper
6
+ include Para::I18n::FormGeneratorConcern
8
7
 
9
8
  source_root File.expand_path("../templates", __FILE__)
10
9
 
@@ -14,16 +13,6 @@ module Para
14
13
  "app/views/admin/#{ plural_namespaced_path }/_translations_form.html.haml"
15
14
  )
16
15
  end
17
-
18
- private
19
-
20
- def translated_attributes
21
- translated_model_fields_for(model)
22
- end
23
-
24
- def model
25
- @model ||= class_name.constantize
26
- end
27
16
  end
28
17
  end
29
18
  end
@@ -1,9 +1,15 @@
1
- = para_form_for(resource, url: @component.relation_path(resource, :translation)) do |form|
1
+ = para_translations_form_for(resource, url: @component.relation_path(resource, :translation), locale: @target_locale) do |form|
2
2
  = target_locale_select
3
3
 
4
4
  = form.fieldset do
5
+ - I18n.with_locale(@target_locale) do
6
+ = form.input :_disabled_for_locale, as: :boolean, label: t("para.i18n.disabled_for_locale")
7
+
5
8
  <%- translated_attributes.each do |field| -%>
6
- = form.input :<%= field.field_name %>, as: :i18n, locale: @target_locale
9
+ = form.input :<%= field.field_name %>, as: :i18n
10
+ <%- end -%>
11
+ <%- nested_relations_attributes.each do |field| -%>
12
+ = form.input :<%= field.field_name %>, as: :<%= field.field_type %>
7
13
  <%- end -%>
8
14
 
9
15
  = form.actions(except: :submit_and_add_another)
@@ -0,0 +1,18 @@
1
+ require "generators/para/i18n/base_form_generator"
2
+
3
+ module Para
4
+ module I18n
5
+ class NestedFieldsGenerator < Para::Generators::NamedBase
6
+ include Para::I18n::FormGeneratorConcern
7
+
8
+ source_root File.expand_path("../templates", __FILE__)
9
+
10
+ def generate_nested_fields
11
+ template(
12
+ "_translations_fields.html.haml",
13
+ "app/views/admin/#{ plural_namespaced_path }/_translations_fields.html.haml"
14
+ )
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,11 @@
1
+ = form.fieldset do
2
+ - I18n.with_locale(@target_locale) do
3
+ = form.input :_disabled_for_locale, as: :boolean, label: t("para.i18n.disabled_for_locale")
4
+
5
+ <%- translated_attributes.each do |field| -%>
6
+ = form.input :<%= field.field_name %>, as: :i18n
7
+ <%- end -%>
8
+ <%- nested_relations_attributes.each do |field| -%>
9
+ = form.input :<%= field.field_name %>, as: :<%= field.field_type %>
10
+ <%- end -%>
11
+
@@ -1,4 +1,4 @@
1
- class Translate<%= table_name.camelize %> < ActiveRecord::Migration
1
+ class Translate<%= table_name.camelize %> < ActiveRecord::Migration<%= "[#{Rails.version.to_f}]" if Rails.version.to_i >= 5 %>
2
2
  def change
3
3
  add_column :<%= table_name %>, :_translations, :jsonb
4
4
  end
data/lib/para/i18n.rb CHANGED
@@ -13,6 +13,9 @@ require 'para/i18n/attribute_translation/base'
13
13
  require 'para/i18n/attribute_translation/simple_attribute'
14
14
  require 'para/i18n/attribute_translation/attachment'
15
15
 
16
+ require 'para/i18n/form_builder'
17
+ require 'para/i18n/inputs'
18
+
16
19
  require 'para/i18n/friendly_id'
17
20
 
18
21
  require 'para/i18n/i18n_input'
@@ -0,0 +1,10 @@
1
+ require 'para/form_builder'
2
+
3
+ require 'para/i18n/form_builder/nested_form'
4
+ require 'para/i18n/form_builder/translations_form'
5
+
6
+ # Extend the para extended simple_form form builder to included translations related
7
+ # methods.
8
+ #
9
+ SimpleForm::FormBuilder.prepend(Para::I18n::FormBuilder::NestedForm)
10
+ SimpleForm::FormBuilder.prepend(Para::I18n::FormBuilder::TranslationsForm)
@@ -0,0 +1,46 @@
1
+ # frozen_string_literals: true
2
+
3
+ module Para
4
+ module I18n
5
+ module FormBuilder
6
+ # Thid module provides methods to extend the para's nested form functionality by
7
+ # extending the SimpleForm::FormBuilder.
8
+ #
9
+ module NestedForm
10
+ # Traverses the parent_builder chain until the top of the builder chain returning
11
+ # the top-most form builder which represents the one created through the
12
+ # `form_for` like helper.
13
+ #
14
+ # Note that this could go into the Para::FormBuilder::NestedForm module, but it's
15
+ # not used inside Para, so we keep it here for now.
16
+ #
17
+ def top_level_form_builder
18
+ parent_builder = options[:parent_builder]
19
+
20
+ while parent_builder && parent_builder.options[:parent_builder]
21
+ parent_builder = parent_builder.options[:parent_builder]
22
+ end
23
+
24
+ parent_builder || self
25
+ end
26
+
27
+ # Don't allow nested fields destruction if we're in a translations form as this is
28
+ # the method used by the FormBuilder#remove_association_button method to check
29
+ # whether it's allowed to render the remove button or not.
30
+ #
31
+ def allow_destroy?
32
+ return false if translations_form?
33
+
34
+ super
35
+ end
36
+
37
+ # Overrides the Para::FormBuilder::NestedForm#fields_partial_name method to render
38
+ # a different nested fields partial when the builder is a translations form
39
+ #
40
+ def nested_fields_partial_name
41
+ translations_form? ? :translations_fields : :fields
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literals: true
2
+
3
+ module Para
4
+ module I18n
5
+ module FormBuilder
6
+ # Thid module extends the Para overriden SimpleForm::FormBuilder to include some
7
+ # trnaslations form related methods and overrides, allowing a simple use of
8
+ # translations forms with the para-i18n gem.
9
+ #
10
+ module TranslationsForm
11
+ TRANSLATIONS_FORM_PARAM_KEY = :_translations_form
12
+
13
+ # Allows storing a gobal reference to the target locale of the translations form,
14
+ # which allows input objects to fetch it directly, without having to provide
15
+ # manually the :locale parameter for each input
16
+ #
17
+ # Example :
18
+ #
19
+ # = para_translations_form_for(resource, locale: @target_Locale) do |form|
20
+ # = form.input :title, as: :i18n # No need to pass `:locale` option here.
21
+ #
22
+ def target_locale
23
+ top_level_form_builder.options.with_indifferent_access[:locale]
24
+ end
25
+
26
+ # Allows checking if the current form builder is a translations form, depending
27
+ # on the passed TRANSLATIONS_FORM_PARAM_KEY option. This is automatically passed
28
+ # to the form builder when using the `para_translations_form_for` helper.
29
+ #
30
+ # Example :
31
+ #
32
+ # para_translations_form_for(resource)
33
+ # # OR
34
+ # para_form_for(resource, _translations_form: true)
35
+ #
36
+ def translations_form?
37
+ !!top_level_form_builder.options.with_indifferent_access[TRANSLATIONS_FORM_PARAM_KEY]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -14,7 +14,7 @@ module FriendlyId
14
14
  # model. Example :
15
15
  model_class.instance_eval do
16
16
  relation.class.send(:include, ClassMethods)
17
- if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) || ActiveRecord::VERSION::MAJOR == 5
17
+ if (ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR == 2) || ActiveRecord::VERSION::MAJOR >= 5
18
18
  model_class.send(:extend, ClassMethods)
19
19
  end
20
20
  end
@@ -9,6 +9,15 @@ module Para
9
9
  model.translated_attribute_names.include?(field.name.to_sym)
10
10
  end
11
11
  end
12
+
13
+ def nested_relations_attributes_for(model)
14
+ model_field_mappings(model).fields.select do |field|
15
+ next unless field.field_type.in?(["nested_one", "nested_many"])
16
+
17
+ # This returns nil if the target model is not translated
18
+ model.reflect_on_association(field.name).klass.translatable?
19
+ end
20
+ end
12
21
  end
13
22
  end
14
23
  end
@@ -26,11 +26,11 @@ module Para
26
26
  private
27
27
 
28
28
  def locale
29
- if (locale = options[:locale])
30
- locale
31
- else
32
- raise 'Missing `:locale` option passed to :i18n input.'
33
- end
29
+ locale = options.fetch(:locale, @builder.target_locale)
30
+ return locale if locale.present?
31
+
32
+ raise 'No target locale for the current i18n input. Please provide a `:locale`' \
33
+ 'option to the :i18n input or the `para_translations_form_for` method'
34
34
  end
35
35
 
36
36
  def original_content
@@ -0,0 +1,4 @@
1
+ require "para/inputs/nested_many_input"
2
+ require 'para/i18n/inputs/nested_many_input'
3
+
4
+ Para::Inputs::NestedManyInput.prepend(Para::I18n::Inputs::NestedManyInput)
@@ -0,0 +1,41 @@
1
+ module Para
2
+ module I18n
3
+ module Inputs
4
+ module NestedManyInput
5
+ def input(_wrapper_options = nil)
6
+ # Disable the add button and orderable features of the input.
7
+ # Note : The remove button disabling is done by the
8
+ # Para::I18n::FormBuilder::TranslationsForm extension
9
+ if within_translations_form?
10
+ options[:add_button] = false
11
+ options[:orderable] = false
12
+ end
13
+
14
+ super
15
+ end
16
+ private
17
+
18
+ def within_translations_form?
19
+ @builder.translations_form?
20
+ end
21
+
22
+ # Extend remote partial params with the :_translations_form key if we're in the
23
+ # context of a translations form builder.
24
+ #
25
+ def remote_partial_params
26
+ super.tap do |remote_partial_params|
27
+ if within_translations_form?
28
+ remote_partial_params.deep_merge!(
29
+ builder_options: {
30
+ Para::I18n::FormBuilder::TranslationsForm::TRANSLATIONS_FORM_PARAM_KEY => true,
31
+ locale: @builder.target_locale
32
+ }
33
+ )
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+
@@ -1,5 +1,5 @@
1
1
  module Para
2
2
  module I18n
3
- VERSION = "0.4.1"
3
+ VERSION = "0.5.0"
4
4
  end
5
5
  end
data/para-i18n.gemspec CHANGED
@@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ["lib"]
21
21
 
22
- spec.add_dependency "para", "~> 0.8"
22
+ spec.add_dependency "para", ">= 0.9.2", "<= 1.0"
23
23
 
24
24
  spec.add_development_dependency "bundler", "~> 1.11"
25
25
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,29 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: para-i18n
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valentin Ballestrino
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-22 00:00:00.000000000 Z
11
+ date: 2021-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: para
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.9.2
20
+ - - "<="
18
21
  - !ruby/object:Gem::Version
19
- version: '0.8'
22
+ version: '1.0'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
- - - "~>"
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: 0.9.2
30
+ - - "<="
25
31
  - !ruby/object:Gem::Version
26
- version: '0.8'
32
+ version: '1.0'
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: bundler
29
35
  requirement: !ruby/object:Gem::Requirement
@@ -83,14 +89,19 @@ files:
83
89
  - app/assets/javascripts/para/admin/translation.coffee
84
90
  - app/assets/javascripts/para/i18n.coffee
85
91
  - app/controllers/para/admin/translations_controller.rb
92
+ - app/helpers/para/i18n/form_helper.rb
93
+ - app/views/para/admin/resources/_translations_fields.html.haml
86
94
  - app/views/para/admin/resources/_translations_form.html.haml
87
95
  - app/views/para/admin/translations/_target_locale_select.html.haml
88
96
  - app/views/para/admin/translations/edit.html.haml
89
97
  - bin/console
90
98
  - bin/setup
91
99
  - config/locales/para-i18n.fr.yml
100
+ - lib/generators/para/i18n/base_form_generator.rb
92
101
  - lib/generators/para/i18n/form/form_generator.rb
93
102
  - lib/generators/para/i18n/form/templates/_translations_form.html.haml
103
+ - lib/generators/para/i18n/nested_fields/nested_fields_generator.rb
104
+ - lib/generators/para/i18n/nested_fields/templates/_translations_fields.html.haml
94
105
  - lib/generators/para/i18n/translate/templates/model_migration.rb.erb
95
106
  - lib/generators/para/i18n/translate/translate_generator.rb
96
107
  - lib/para/i18n.rb
@@ -100,11 +111,16 @@ files:
100
111
  - lib/para/i18n/attribute_translation/simple_attribute.rb
101
112
  - lib/para/i18n/engine.rb
102
113
  - lib/para/i18n/fallbacks.rb
114
+ - lib/para/i18n/form_builder.rb
115
+ - lib/para/i18n/form_builder/nested_form.rb
116
+ - lib/para/i18n/form_builder/translations_form.rb
103
117
  - lib/para/i18n/friendly_id.rb
104
118
  - lib/para/i18n/helpers.rb
105
119
  - lib/para/i18n/helpers/translations_form_helper.rb
106
120
  - lib/para/i18n/helpers/translations_helper.rb
107
121
  - lib/para/i18n/i18n_input.rb
122
+ - lib/para/i18n/inputs.rb
123
+ - lib/para/i18n/inputs/nested_many_input.rb
108
124
  - lib/para/i18n/model.rb
109
125
  - lib/para/i18n/resources_buttons.rb
110
126
  - lib/para/i18n/resources_table.rb
@@ -129,8 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
129
145
  - !ruby/object:Gem::Version
130
146
  version: '0'
131
147
  requirements: []
132
- rubyforge_project:
133
- rubygems_version: 2.6.11
148
+ rubygems_version: 3.1.4
134
149
  signing_key:
135
150
  specification_version: 4
136
151
  summary: Para CMS I18n admin module for models