activeadmin-globalize-2 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 74fe94f8cc464bd0d7b2162e29fbd14ab9751528eb0d99e8122887b6146e267f
4
+ data.tar.gz: f6460a081f728312a787d109c0988408a9ce8bb7b96423c10d5c2ecdd78439cb
5
+ SHA512:
6
+ metadata.gz: 74cdc6af3c0fcf1a58c4540f11cf284655727e2748e3a719f7357cc6ab17345cc9290be1aac1bb916048e293c444566cc0893a4324b917172ffd3cce0788411c
7
+ data.tar.gz: f53e505998d7222ea350779503f28c98358169f77437c858c4e54b3f56f7cdaaaffacb0b97cbb5f3321b544975547fae97fc9b7f104787062d0e0fa848d53c9a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Andrea Pavoni http://andreapavoni.com
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # ActiveAdmin::Globalize
2
+
3
+ Makes it easy to translate your resource fields.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/activeadmin-globalize-2.svg)](http://badge.fury.io/rb/activeadmin-globalize-2)
6
+
7
+ ## Installation
8
+
9
+ Current version targets Rails 4.2-5.2 and ActiveAdmin 1.x.
10
+
11
+ ```ruby
12
+ gem 'activeadmin-globalize-2', '~> 1.0.0'
13
+ ```
14
+
15
+ ## Require Assets
16
+
17
+ - active_admin.js: `//= require active_admin/active_admin_globalize.js`
18
+ - active_admin.css: `*= require active_admin/active_admin_globalize`
19
+
20
+ ## Your model
21
+
22
+ ```ruby
23
+ active_admin_translates :title, :description do
24
+ validates_presence_of :title
25
+ end
26
+ ```
27
+ ## In your Active Admin resource definition
28
+
29
+ ```ruby
30
+
31
+ # For usage with strong parameters you'll need to permit them
32
+ permit_params translations_attributes: [:id, :locale, :title, :description, :_destroy]
33
+
34
+ index do
35
+ # textual translation status
36
+ translation_status
37
+ # or with flag icons
38
+ translation_status_flags
39
+ # ...
40
+ actions
41
+ end
42
+
43
+ form do |f|
44
+ # ...
45
+ f.translated_inputs "Translated fields", switch_locale: false do |t|
46
+ t.input :title
47
+ t.input :description
48
+ end
49
+ # ...
50
+ end
51
+
52
+ # You can also set locales to show in tabs
53
+ # For example we want to show English translation fields without tab, and want to show other languages within tabs
54
+ form do |f|
55
+ # ...
56
+ f.inputs do
57
+ Globalize.with_locale(:en) do
58
+ f.input :title
59
+ end
60
+ end
61
+ f.inputs "Translated fields" do
62
+ f.translated_inputs 'ignored title', switch_locale: false, available_locales: (I18n.available_locales - [:en]) do |t|
63
+ t.input :title
64
+ t.input :description
65
+ end
66
+ end
67
+ # ...
68
+ end
69
+
70
+ # You can also set default language tab
71
+ # For example we want to make Bengali translation tab as default
72
+ form do |f|
73
+ # ...
74
+ f.inputs "Translated fields" do
75
+ f.translated_inputs 'ignored title', switch_locale: false, default_locale: :bn do |t|
76
+ t.input :title
77
+ t.input :description
78
+ end
79
+ end
80
+ # ...
81
+ end
82
+
83
+ ```
84
+ If `switch_locale` is set, each tab will be rendered switching locale.
85
+
86
+
87
+ ## Hints
88
+
89
+ To use the dashed locale keys as 'pt-BR' or 'pt-PT' you need to convert a string
90
+ to symbol (in application.rb)
91
+
92
+ ```ruby
93
+ config.i18n.available_locales = [:en, :it, :de, :es, :"pt-BR"]
94
+ ```
95
+
96
+ ## Credits
97
+
98
+ This is a fork of https://github.com/fabn/activeadmin-globalize and PRs have been opened to upstream the changes.
@@ -0,0 +1,159 @@
1
+ $ ->
2
+
3
+ translations = ->
4
+
5
+ # Hides or shows the + button and the remove button.
6
+ updateLocaleButtonsStatus = ($dom) ->
7
+ $localeList = $dom.find('.add-locale ul li:not(.hidden)')
8
+ if $localeList.length == 0
9
+ $dom.find('.add-locale').hide()
10
+ else
11
+ $dom.find('.add-locale').show()
12
+
13
+
14
+ # Hides or shows the locale tab and its corresponding element in the add menu.
15
+ toggleTab = ($tab, active) ->
16
+ $addButton = $tab.parents('ul').find('.add-locale li:has(a[href="' + $tab.attr('href') + '"])')
17
+ if active
18
+ $tab.addClass('hidden').show().removeClass('hidden')
19
+ $addButton.hide().addClass('hidden')
20
+ else
21
+ $tab.addClass('hidden').hide().addClass('hidden')
22
+ $addButton.show().removeClass('hidden')
23
+
24
+ $(".activeadmin-translations > ul").each ->
25
+ $dom = $(this)
26
+ # true when tabs are used in show action, false in form
27
+ showAction = $dom.hasClass('locale-selector')
28
+
29
+ if !$dom.data("ready")
30
+ $dom.data("ready", true)
31
+ $tabs = $("li > a", this)
32
+ # content to toggle is different according to current action
33
+ $contents = if showAction then $(this).siblings("div.field-translation") else $(this).siblings("fieldset")
34
+
35
+ $tabs.click (e) ->
36
+ $tab = $(this)
37
+ $tabs.not($tab).removeClass("active")
38
+ $tab.addClass("active")
39
+ $contents.hide()
40
+ $contents.filter($tab.attr("href")).show()
41
+ e.preventDefault()
42
+
43
+ $tabs.eq(0).click()
44
+
45
+ # Add button and other behavior is not needed in show action
46
+ return if showAction
47
+
48
+ # Collect tha available locales.
49
+ availableLocales = []
50
+ $tabs.not('.default').each ->
51
+ availableLocales.push($('<li></li>').append($(this).clone().removeClass('active')))
52
+
53
+ # Create a new tab as the root of the drop down menu.
54
+ $addLocaleButton = $('<li class="add-locale"><a href="#">+</a></li>')
55
+ $addLocaleButton.append($('<ul></ul>').append(availableLocales))
56
+
57
+ # Handle locale addition
58
+ $addLocaleButton.find('ul a').click (e) ->
59
+ href = $(this).attr('href')
60
+ $tab = $tabs.filter('[href="' + href + '"]')
61
+ toggleTab($tab, true)
62
+ $tab.click()
63
+ updateLocaleButtonsStatus($dom)
64
+ e.preventDefault()
65
+
66
+ # Remove a locale from the tab.
67
+ $removeButton = $('<span class="remove">x</span>').click (e) ->
68
+ e.stopImmediatePropagation()
69
+ e.preventDefault()
70
+ $tab = $(this).parent()
71
+ toggleTab($tab, false)
72
+ if $tab.hasClass('active')
73
+ $tabs.not('.hidden').eq(0).click()
74
+
75
+ updateLocaleButtonsStatus($dom)
76
+
77
+ # Add the remove button to every tab.
78
+ $tabs.not('.default').append($removeButton)
79
+
80
+ # Add the new button at the end of the locale list.
81
+ $dom.append($addLocaleButton)
82
+
83
+ $tabs.each ->
84
+ $tab = $(@)
85
+ $content = $contents.filter($tab.attr("href"))
86
+ containsErrors = $content.find(".input.error").length > 0
87
+ $tab.toggleClass("error", containsErrors)
88
+ # Find those tabs that are in use.
89
+ hide = true
90
+ # We will not hide the tabs that have any error.
91
+ if $tab.hasClass('error') || $tab.hasClass('default')
92
+ hide = false
93
+ else
94
+ # Check whether the input fields are empty or not.
95
+ $content.find('[name]').not('[type="hidden"]').each ->
96
+ if $(this).val()
97
+ # We will not hide the tab because it has some data.
98
+ hide = false
99
+ return false
100
+
101
+ if hide
102
+ toggleTab($tab, false)
103
+ else
104
+ toggleTab($tab, true)
105
+
106
+ # Remove the fields of hidden locales before form submission.
107
+ $form = $dom.parents('form')
108
+ if !$form.data('ready')
109
+ $form.data('ready')
110
+ $form.submit ->
111
+ # Get all translations (the nested ones too).
112
+ $('.activeadmin-translations > ul').each ->
113
+ # Get the corresponding fieldsets.
114
+ $fieldsets = $(this).siblings('fieldset')
115
+ $("li:not(.add-locale) > a", this).each ->
116
+ # Remove them if the locale is hidden.
117
+ if $(this).hasClass('hidden')
118
+ # check if it's an existing translation otherwise remove it
119
+ $currentFieldset = $("fieldset#{$(this).attr('href')}")
120
+ $translationId = $('input[id$=_id]', $currentFieldset)
121
+ if $translationId.val()
122
+ # mark it for database removal appending a _destroy element
123
+ $destroy = $('<input/>').attr(
124
+ type: 'hidden',
125
+ name: $translationId.attr('name').replace('[id]', '[_destroy]'),
126
+ id: $translationId.attr('id').replace('_id', '_destroy'),
127
+ value: '1'
128
+ )
129
+ $destroy.appendTo($currentFieldset)
130
+ else
131
+ # remove the fieldset from dom so it won't be submitted
132
+ $fieldsets.filter($(this).attr('href')).remove()
133
+
134
+ #Initially update the buttons' status
135
+ updateLocaleButtonsStatus($dom)
136
+ $tabs.filter('.default').click()
137
+
138
+ $a = $("a")
139
+ # jQuery 1.7 introduced "on" and deprecated "bind"
140
+ # AA 1.2 bumps jquery-rails to >= 4.2, so we can drop "bind" once AA req is bumped
141
+ bindingMethod = if ("on" of $a && typeof $a.on == 'function') then "on" else "bind"
142
+ # this is to handle elements created with has_many
143
+ $a[bindingMethod] "click", ->
144
+ setTimeout(
145
+ -> translations()
146
+ 50
147
+ )
148
+
149
+ # Used to toggle translations values for inline fields
150
+ $('a.ui-translation-trigger').click (e) ->
151
+ $locale = $(this).data('locale')
152
+ $td = $(this).closest('td')
153
+ $('.field-translation', $td).hide()
154
+ $(".locale-#{$locale}", $td).show()
155
+ $(this).parent().children('a.ui-translation-trigger').removeClass('active')
156
+ $(this).addClass('active')
157
+ e.preventDefault()
158
+
159
+ translations()
@@ -0,0 +1,114 @@
1
+ @import "active_admin/mixins"
2
+ @import "active_admin_globalize_mixins"
3
+ @import "active_admin_globalize_flags"
4
+
5
+ .active_admin
6
+ .activeadmin-translations
7
+ margin-bottom: 20px
8
+
9
+ &> ul
10
+ position: relative
11
+ top: 4px
12
+ padding: 0 10px
13
+
14
+ &> li
15
+ display: inline-block
16
+
17
+ &> a
18
+ display: inline-block
19
+ color: #666
20
+ margin-right: 3px
21
+ text-decoration: none
22
+ font-size: 17px
23
+ padding: 8px 15px
24
+ padding-bottom: 8px + 4px
25
+ margin-bottom: 0
26
+ position: relative
27
+
28
+ &.error
29
+ color: #932419
30
+
31
+ &.active
32
+ background: #f4f4f4
33
+ +inset-shadow(0, 4px, 4px, #ddd)
34
+ +border-top-radius(4px)
35
+ margin-bottom: 0
36
+ +gradient($secondary-gradient-stop, #f4f4f4)
37
+ text-shadow: 0 1px 0 white
38
+ color: #666 !important
39
+
40
+ &> span
41
+ font-size: 0.75em
42
+ font-weight: bold
43
+ position: absolute
44
+ top: 2px
45
+ right: 4px
46
+ display: none
47
+
48
+ &:hover
49
+ +text-shadow(red, 1px, 1px, 2px)
50
+
51
+ &:hover
52
+ span
53
+ display: block
54
+
55
+ span.hidden
56
+ display: none
57
+
58
+ &> li.add-locale
59
+ font-weight: bold
60
+ position: relative
61
+ z-index: 100
62
+
63
+ &> ul
64
+ display: none
65
+ font-size: 17px
66
+ font-weight: normal
67
+ padding: 0
68
+ +border-bottom-radius(4px)
69
+ position: absolute
70
+ +shadow(3px, 3px, 5px, #aaa)
71
+ background: #f4f4f4
72
+ z-index: 100
73
+
74
+ &> li
75
+ font-size: 17px
76
+ padding: 8px 15px
77
+ padding-bottom: 8px + 4px
78
+ margin-bottom: 0
79
+
80
+ &:hover
81
+ background: #ddd
82
+ background: -webkit-linear-gradient(left, $secondary-gradient-stop, #f4f4f4)
83
+ background: -moz-linear-gradient(left, $secondary-gradient-stop, #f4f4f4)
84
+ background: linear-gradient(left, $secondary-gradient-stop, #f4f4f4)
85
+
86
+ &> a
87
+ text-decoration: none
88
+ color: #666
89
+
90
+ &> li:first-child
91
+ border-top-right-radius: 4px
92
+
93
+ &:hover
94
+ background: #f4f4f4
95
+ +inset-shadow(0, 4px, 4px, #ddd)
96
+ +border-top-radius(4px)
97
+ margin-bottom: 0
98
+ +gradient($secondary-gradient-stop, #f4f4f4)
99
+ text-shadow: 0 1px 0 white
100
+ color: #666 !important
101
+
102
+ &> ul
103
+ display: block
104
+
105
+
106
+ &> fieldset.inputs
107
+ margin-bottom: 0
108
+
109
+ ol
110
+ padding-left: 0
111
+ width: 100%
112
+
113
+ li.hidden
114
+ padding: 0 !important
@@ -0,0 +1,65 @@
1
+ // This file (and the sprite image) is kindly generated by http://flag-sprites.com/
2
+ // and converted to SASS using this converter http://css2sass.heroku.com/
3
+
4
+ // Override flag positions in your stylesheet if you want to change default flags
5
+ .flag
6
+ display: inline-block
7
+ width: 16px
8
+ height: 11px
9
+ vertical-align: middle
10
+ background: image-url('active_admin/flags.png') no-repeat
11
+ &.flag-ar // originally flag-ae
12
+ background-position: -16px 0
13
+ &.flag-br
14
+ background-position: -32px 0
15
+ &.flag-pt-BR
16
+ background-position: -32px 0
17
+ &.flag-de
18
+ background-position: -48px 0
19
+ &.flag-es
20
+ background-position: 0 -11px
21
+ &.flag-fr
22
+ background-position: -16px -11px
23
+ &.flag-gb
24
+ background-position: -32px -11px
25
+ &.flag-hu
26
+ background-position: -48px -11px
27
+ &.flag-it
28
+ background-position: 0 -22px
29
+ &.flag-pt
30
+ background-position: -16px -22px
31
+ &.flag-pt-PT
32
+ background-position: -16px -22px
33
+ &.flag-tr
34
+ background-position: -32px -22px
35
+ &.flag-en // originally flag-us
36
+ background-position: -48px -22px
37
+ &.flag-he // originally flag-il
38
+ background-position: 0 -33px
39
+
40
+ // Used to distantiate inline locale selector
41
+ span.inline-locale-selector
42
+ margin-right: 10px
43
+ > .ui-translation-trigger
44
+ opacity: .4
45
+ &.empty
46
+ filter: grayscale(100%)
47
+ &.active
48
+ opacity: 1
49
+
50
+ .field-translation.hidden
51
+ display: none
52
+
53
+ ul.locale-selector
54
+ margin-bottom: 0 !important
55
+
56
+ div.field-translation
57
+ padding: 10px 10px
58
+ +inset-shadow(0, -40px, 40px, #ddd)
59
+ +border-top-radius(4px)
60
+ +border-bottom-radius(6px)
61
+ min-height: 80px
62
+
63
+ // prevent tr height flickering
64
+ span.field-translation.empty
65
+ vertical-align: bottom
@@ -0,0 +1,27 @@
1
+ @mixin border-top-radius($radius)
2
+ -webkit-border-top-right-radius: $radius
3
+ border-top-right-radius: $radius
4
+ -webkit-border-top-left-radius: $radius
5
+ border-top-left-radius: $radius
6
+ background-clip: padding-box
7
+
8
+ @mixin border-right-radius($radius)
9
+ -webkit-border-bottom-right-radius: $radius
10
+ border-bottom-right-radius: $radius
11
+ -webkit-border-top-right-radius: $radius
12
+ border-top-right-radius: $radius
13
+ background-clip: padding-box
14
+
15
+ @mixin border-bottom-radius($radius)
16
+ -webkit-border-bottom-right-radius: $radius
17
+ border-bottom-right-radius: $radius
18
+ -webkit-border-bottom-left-radius: $radius
19
+ border-bottom-left-radius: $radius
20
+ background-clip: padding-box
21
+
22
+ @mixin border-left-radius($radius)
23
+ -webkit-border-bottom-left-radius: $radius
24
+ border-bottom-left-radius: $radius
25
+ -webkit-border-top-left-radius: $radius
26
+ border-top-left-radius: $radius
27
+ background-clip: padding-box
@@ -0,0 +1,16 @@
1
+ ar:
2
+ active_admin:
3
+ globalize:
4
+ translations: "ترجمة"
5
+ language:
6
+ de: "ألماني"
7
+ en: "الإنجليزية"
8
+ es: "الأسبانية"
9
+ fr: "اللغة الفرنسية"
10
+ hu: "الهنغارية"
11
+ it: "الإيطالي"
12
+ pt-BR: "البرتغالية"
13
+ pt-PT: "البرتغالية (البرتغال)"
14
+ tr: "اللغة التركية"
15
+ he: "العبرية"
16
+
@@ -0,0 +1,17 @@
1
+ de:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Übersetzungen"
5
+ language:
6
+ de: "Deutsch"
7
+ en: "Englisch"
8
+ es: "Spanisch"
9
+ fr: "Fränzösisch"
10
+ hu: "Ungarisch"
11
+ it: "Italienisch"
12
+ pt-BR: "Portugiesisch"
13
+ pt-PT: "Portugiesisch (Portugal)"
14
+ tr: "Türkisch"
15
+ he: "Hebräisch"
16
+ ar: "Arabisch"
17
+
@@ -0,0 +1,17 @@
1
+ en:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Translations"
5
+ language:
6
+ de: "German"
7
+ en: "English"
8
+ es: "Spanish"
9
+ fr: "French"
10
+ hu: "Hungarian"
11
+ it: "Italian"
12
+ pt-BR: "Portuguese"
13
+ pt-PT: "Portuguese (Portugal)"
14
+ tr: "Turkish"
15
+ he: "Hebrew"
16
+ ar: "Arabic"
17
+
@@ -0,0 +1,17 @@
1
+ es:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Traducciones"
5
+ language:
6
+ de: "Alemán"
7
+ en: "Inglés"
8
+ es: "Español"
9
+ fr: "Francés"
10
+ hu: "Húngaro"
11
+ it: "Italiano"
12
+ pt-BR: "Portugués"
13
+ pt-PT: "Portugués (Portugal)"
14
+ tr: "Turco"
15
+ he: "Hebreo"
16
+ ar: "Arábica"
17
+
@@ -0,0 +1,17 @@
1
+ he:
2
+ active_admin:
3
+ globalize:
4
+ translations: "תרגומים"
5
+ language:
6
+ de: "גרמנית"
7
+ en: "אנגלית"
8
+ es: "ספרדית"
9
+ fr: "צרפתית"
10
+ hu: "הונגרית"
11
+ it: "איטלקית"
12
+ pt-BR: "פורטוגזית"
13
+ pt-PT: "פורטוגזית (פורטוגל)"
14
+ tr: "טורקית"
15
+ he: "עברית"
16
+ ar: "ערבית"
17
+
@@ -0,0 +1,17 @@
1
+ hu:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Fordítások"
5
+ language:
6
+ de: "Német"
7
+ en: "Angol"
8
+ es: "Spanyol"
9
+ fr: "Francia"
10
+ hu: "Magyar"
11
+ it: "Olasz"
12
+ pt-BR: "Portuguese"
13
+ pt-PT: "Portuguese (Portugal)"
14
+ tr: "Török"
15
+ he: "Héber"
16
+ ar: "Arab"
17
+
@@ -0,0 +1,17 @@
1
+ it:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Traduzioni"
5
+ language:
6
+ de: "Tedesco"
7
+ en: "Inglese"
8
+ es: "Spagnolo"
9
+ fr: "Francese"
10
+ hu: "Ungherese"
11
+ it: "Italiano"
12
+ pt-BR: "Portoghese"
13
+ pt-PT: "Portoghese (Portogallo)"
14
+ tr: "Turco"
15
+ he: "Ebraico"
16
+ ar: "Arabo"
17
+
@@ -0,0 +1,17 @@
1
+ pt-BR:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Traduções"
5
+ language:
6
+ de: "Alemão"
7
+ en: "Inglês"
8
+ es: "Espanhol"
9
+ fr: "Francês"
10
+ hu: "Húngaro"
11
+ it: "Italiano"
12
+ pt-BR: "Português"
13
+ pt-PT: "Português (Portugal)"
14
+ tr: "Turco"
15
+ he: "Hebraico"
16
+ ar: "Árabe"
17
+
@@ -0,0 +1,17 @@
1
+ pt-PT:
2
+ active_admin:
3
+ globalize:
4
+ translations: "Traduções"
5
+ language:
6
+ de: "Alemão"
7
+ en: "Inglês"
8
+ es: "Espanhol"
9
+ fr: "Francês"
10
+ hu: "Húngaro"
11
+ it: "Italiano"
12
+ pt-BR: "Português"
13
+ pt-PT: "Português (Portugal)"
14
+ tr: "Turco"
15
+ he: "Hebraico"
16
+ ar: "Árabe"
17
+
data/config/spring.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Tell spring where the app is
2
+ Spring.application_root = './spec/dummy'
@@ -0,0 +1,27 @@
1
+ module ActiveAdmin::Globalize
2
+ module ActiveRecordExtension
3
+
4
+ module Methods
5
+ def translation_names
6
+ self.translations.map(&:locale).map do |locale|
7
+ I18n.t("active_admin.globalize.language.#{locale}")
8
+ end.uniq.sort
9
+ end
10
+ end
11
+
12
+ def active_admin_translates(*args, &block)
13
+ translates(*args.dup)
14
+ args.extract_options!
15
+
16
+ if block
17
+ translation_class.instance_eval &block
18
+ end
19
+
20
+ accepts_nested_attributes_for :translations, allow_destroy: true
21
+
22
+ include Methods
23
+ end
24
+
25
+ end
26
+ end
27
+
@@ -0,0 +1,149 @@
1
+ require 'active_admin/views/components/attributes_table.rb'
2
+
3
+ module ActiveAdmin
4
+ module Views
5
+
6
+ # Provide additional method #translated_row to attribute table
7
+ class AttributesTable < ActiveAdmin::Component
8
+
9
+ # Show a row with their translations and selectors for choosing locale to show.
10
+ #
11
+ # If a block is given it will be used to format the translation,
12
+ # otherwise field taken from translation object is used as translation.
13
+ #
14
+ # Additional options are forwarded to the original row call.
15
+ #
16
+ # @overload translated_row(field, opts)
17
+ # @param [String] field row label, also used as field name if none given in options
18
+ # @param [Hash] opts the options to create a message with.
19
+ # @option opts [String] :field field to retrieve from model if different from first argument of args
20
+ # @option opts [String] :locale (I18n.locale) initial locale to show in the ui
21
+ # @option opts [Boolean] :inline (true) if true locale selectors (and values) are shown inline,
22
+ # otherwise as block content and tabs
23
+ #
24
+ # @yield if given will be used to build single translations
25
+ # @yieldparam [*::Translation] Globalize translation model
26
+ # @yieldreturn [String] content to show as translation
27
+ #
28
+ # @example Show inlined translation values for title field
29
+ # show do |p|
30
+ # attributes_table do
31
+ # translated_row(:title)
32
+ # end
33
+ # end
34
+ #
35
+ # @example Show block translation for body field with selection of initial locale
36
+ # show do |p|
37
+ # attributes_table do
38
+ # translated_row(:body, inline: false, locale: :es)
39
+ # end
40
+ # end
41
+ #
42
+ def translated_row(*args, &block)
43
+ options = args.extract_options!
44
+ options.reverse_merge!(inline: true, locale: I18n.locale)
45
+ field = options[:field] || args.first
46
+ raise ArgumentError, "Field '#{field}' is not translatable" unless translatable?(field)
47
+ # Remove my options from passed options
48
+ row_options = options.symbolize_keys.except(:field, :locale, :inline)
49
+ # Append remaining options to original args
50
+ args.push(row_options) unless row_options.empty?
51
+ # Render the table row with translations
52
+ row(*args) do
53
+ if options[:inline]
54
+ ''.html_safe.tap do |value|
55
+ # Add selectors for inline locale
56
+ value << inline_locale_selectors(field, options[:locale], &block)
57
+ # Build translations spans
58
+ value << field_translations(field, :span, options[:locale], &block)
59
+ end
60
+ else
61
+ content_tag(:div, class: 'activeadmin-translations') do
62
+ ''.html_safe.tap do |value|
63
+ # Render selectors as in translation ui
64
+ value << block_locale_selectors(field, options[:locale], &block)
65
+ # Build translations divs for actual translations
66
+ value << field_translations(field, :div, options[:locale], &block)
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ # @return [Boolean] true iff the field is translatable
76
+ def translatable?(field)
77
+ @resource_class.translates? &&
78
+ @resource_class.translated_attribute_names.include?(field.to_sym)
79
+ end
80
+
81
+ # Build a tag for each field translation with appropriate css classes to make javascript working
82
+ # @param [String] field field name to render
83
+ # @param [Symbol] tag tag to enclose field translation
84
+ # @param [Symbol] initial_locale locale to set as not hidden
85
+ def field_translations(field, tag, initial_locale, &block)
86
+ available_translations.map do |translation|
87
+ # Classes for translation span only first element is visible
88
+ css_classes = ['field-translation', "locale-#{translation.locale}"]
89
+ # Initially only element for selected locale is visible
90
+ css_classes.push 'hidden' unless translation.locale == initial_locale.to_sym
91
+ # Build content for cell or div using translation locale and given block
92
+ content = field_translation_value(translation, field, &block)
93
+ # return element
94
+ if tag == :span # inline element
95
+ # attach class to span if inline
96
+ css_classes.push('empty') if content.blank?
97
+ content_tag(tag, content.presence || 'Empty', class: css_classes)
98
+ else
99
+ # block content
100
+ content_tag(tag, class: css_classes) do
101
+ # Return content or empty span
102
+ content.presence || content_tag(:span, 'Empty', class: 'empty')
103
+ end
104
+ end
105
+ end.join(' ').html_safe
106
+ end
107
+
108
+ def block_locale_selectors(field, initial_locale, &block)
109
+ content_tag(:ul, class: 'available-locales locale-selector') do
110
+ available_translations.map do |translation|
111
+ css_classes = ['translation-tab']
112
+ css_classes << 'active' if translation.locale == initial_locale.to_sym
113
+ css_classes << 'empty' unless content.presence
114
+ content_tag(:li, class: css_classes) do
115
+ I18n.with_locale(translation.locale) do
116
+ default = 'default' if translation.locale == initial_locale.to_sym
117
+ content_tag(:a, I18n.t(:"active_admin.globalize.language.#{translation.locale}"), href: ".locale-#{translation.locale}", class: default)
118
+ end
119
+ end
120
+ end.join.html_safe
121
+ end
122
+ end
123
+
124
+ # Return flag elements to show the given locale using javascript
125
+ def inline_locale_selectors(field, initial_locale, &block)
126
+ content_tag(:span, class: 'inline-locale-selector') do
127
+ available_translations.map do |translation|
128
+ content = field_translation_value(translation, field, &block)
129
+ css_classes = ['ui-translation-trigger']
130
+ css_classes << 'active' if translation.locale == initial_locale.to_sym
131
+ css_classes << 'empty' unless content.presence
132
+ # Build a link to show the given translation
133
+ link_to(flag_icon(translation.locale), '#', class: css_classes, data: {locale: translation.locale})
134
+ end.join(' ').html_safe
135
+ end
136
+ end
137
+
138
+ def available_translations
139
+ @record_translations ||= @collection.first.translations.order(:locale)
140
+ end
141
+
142
+ def field_translation_value(translation, field)
143
+ I18n.with_locale(translation.locale) do
144
+ block_given? ? yield(translation) : translation.send(field)
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,12 @@
1
+ module ActiveAdmin
2
+ module Globalize
3
+ class Engine < ::Rails::Engine
4
+ initializer "Active Admin precompile hook", group: :all do |app|
5
+ app.config.assets.precompile += [
6
+ "active_admin/active_admin_globalize.css",
7
+ "active_admin/active_admin_globalize.js"
8
+ ]
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveAdmin
2
+ module Globalize
3
+ module FormBuilderExtension
4
+ extend ActiveSupport::Concern
5
+
6
+ def translated_inputs(name = "Translations", options = {}, &block)
7
+ options.symbolize_keys!
8
+ available_locales = options.fetch(:available_locales, I18n.available_locales)
9
+ switch_locale = options.fetch(:switch_locale, false)
10
+ default_locale = options.fetch(:default_locale, I18n.default_locale)
11
+ if block_given?
12
+ # If this translated_inputs is nested inside an inputs, AA has already set this
13
+ template.assigns[:has_many_block] = true
14
+ end
15
+ template.content_tag(:div, class: "activeadmin-translations") do
16
+ template.content_tag(:ul, class: "available-locales") do
17
+ available_locales.map do |locale|
18
+ default = 'default' if locale == default_locale
19
+ template.content_tag(:li) do
20
+ I18n.with_locale(switch_locale ? locale : I18n.locale) do
21
+ template.content_tag(:a, I18n.t(:"active_admin.globalize.language.#{locale}"), href:".locale-#{locale}", :class => default)
22
+ end
23
+ end
24
+ end.join.html_safe
25
+ end <<
26
+ available_locales.map do |locale|
27
+ translation = object.translations.find { |t| t.locale.to_s == locale.to_s }
28
+ translation ||= object.translations.build(locale: locale)
29
+ fields = proc do |form|
30
+ form.input(:locale, as: :hidden)
31
+ form.input(:id, as: :hidden)
32
+ I18n.with_locale(switch_locale ? locale : I18n.locale) do
33
+ block.call(form)
34
+ end
35
+ end
36
+ inputs_for_nested_attributes(
37
+ for: [:translations, translation ],
38
+ class: "inputs locale locale-#{translation.locale}",
39
+ &fields
40
+ )
41
+ end.join.html_safe
42
+ end
43
+ end
44
+
45
+ module ClassMethods
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ require 'active_admin/views/components/status_tag'
2
+
3
+ module ActiveAdmin
4
+ module Globalize
5
+ module IndexTableFor
6
+ def translation_status
7
+ column I18n.t("active_admin.globalize.translations") do |obj|
8
+ obj.translation_names.map do |t|
9
+ '<span class="status_tag">%s</span>' % t
10
+ end.join(" ").html_safe
11
+ end
12
+ end
13
+ def translation_status_flags
14
+ column I18n.t("active_admin.globalize.translations") do |obj|
15
+ obj.translations.map(&:locale).sort.map { |l| flag_icon(l) }.join(' ').html_safe
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveAdmin
2
+ module Globalize
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,16 @@
1
+ require 'globalize'
2
+ require 'activeadmin'
3
+
4
+ require 'active_admin/views/index_as_table'
5
+ require 'active_admin/globalize/engine'
6
+ require 'active_admin/globalize/form_builder_extension'
7
+ require 'active_admin/globalize/active_record_extension'
8
+ require 'active_admin/globalize/index_table_for_extension'
9
+ require 'active_admin/globalize/attributes_table_extension'
10
+ require 'active_admin/view_helpers/flag_helper'
11
+
12
+ ActiveRecord::Base.send :extend, ActiveAdmin::Globalize::ActiveRecordExtension
13
+
14
+ ActiveAdmin::FormBuilder.send :include, ActiveAdmin::Globalize::FormBuilderExtension
15
+ ActiveAdmin::Views::IndexAsTable::IndexTableFor.send :include, ActiveAdmin::Globalize::IndexTableFor
16
+
@@ -0,0 +1,17 @@
1
+ require 'active_admin/view_helpers'
2
+
3
+ module ActiveAdmin
4
+ module ViewHelpers
5
+ module FlagHelper
6
+
7
+ # Return an image tag with background of given locale
8
+ def flag_icon(locale)
9
+ content_tag :i, '', class: "flag flag-#{locale}", title: I18n.t("active_admin.globalize.language.#{locale}")
10
+ end
11
+
12
+ end
13
+
14
+ # Register as ActiveAdmin view helper
15
+ include FlagHelper
16
+ end
17
+ end
@@ -0,0 +1 @@
1
+ require 'active_admin/globalize'
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeadmin-globalize-2
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Stefano Verna
8
+ - Fabio Napoleoni
9
+ - tkalliom
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2022-06-06 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activeadmin
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ">="
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ - - "<"
23
+ - !ruby/object:Gem::Version
24
+ version: '2.0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ version: '1.0'
32
+ - - "<"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.0'
35
+ - !ruby/object:Gem::Dependency
36
+ name: globalize
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 4.0.0
42
+ - - "<"
43
+ - !ruby/object:Gem::Version
44
+ version: '6.0'
45
+ type: :runtime
46
+ prerelease: false
47
+ version_requirements: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 4.0.0
52
+ - - "<"
53
+ - !ruby/object:Gem::Version
54
+ version: '6.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 1.6.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 1.6.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Handles globalize translations in ActiveAdmin 1.x and Rails 4.x-5.x
84
+ email:
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - MIT-LICENSE
90
+ - README.md
91
+ - app/assets/images/active_admin/flags.png
92
+ - app/assets/javascripts/active_admin/active_admin_globalize.js.coffee
93
+ - app/assets/stylesheets/active_admin/active_admin_globalize.sass
94
+ - app/assets/stylesheets/active_admin/active_admin_globalize_flags.sass
95
+ - app/assets/stylesheets/active_admin/active_admin_globalize_mixins.sass
96
+ - config/locales/ar.yml
97
+ - config/locales/de.yml
98
+ - config/locales/en.yml
99
+ - config/locales/es.yml
100
+ - config/locales/he.yml
101
+ - config/locales/hu.yml
102
+ - config/locales/it.yml
103
+ - config/locales/pt-BR.yml
104
+ - config/locales/pt-PT.yml
105
+ - config/spring.rb
106
+ - lib/active_admin/globalize.rb
107
+ - lib/active_admin/globalize/active_record_extension.rb
108
+ - lib/active_admin/globalize/attributes_table_extension.rb
109
+ - lib/active_admin/globalize/engine.rb
110
+ - lib/active_admin/globalize/form_builder_extension.rb
111
+ - lib/active_admin/globalize/index_table_for_extension.rb
112
+ - lib/active_admin/globalize/version.rb
113
+ - lib/active_admin/view_helpers/flag_helper.rb
114
+ - lib/activeadmin-globalize-2.rb
115
+ homepage: http://github.com/Celkee/activeadmin-globalize-2
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubygems_version: 3.2.5
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Handles globalize translations
138
+ test_files: []