Skip to content

Commit

Permalink
add locale specific title and body support to UI
Browse files Browse the repository at this point in the history
  • Loading branch information
weilu committed Mar 4, 2014
1 parent b4b0f0b commit 71c4809
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 34 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/admin.less
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ span.detail-section {
}
}

#editor {
.editor {
min-height: 300px;
padding: 15px;
margin-top: 8px;
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/documents_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def create
render :new
end
end

def show
render inline: @document.body.html_safe
end
Expand All @@ -44,7 +44,7 @@ def find_document
end

def document_params
params.required(:document).permit(:body, :title, :key, :is_auth)
params.required(:document).permit(:key, :is_auth, *Document.locale_params)
end

def create_document
Expand Down
10 changes: 10 additions & 0 deletions app/models/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def to_param
end
end

def self.locale_params
params = []
TRANSLATABLE_ATTR.each do |attr|
Rails.configuration.i18n.available_locales.each do |locale|
params << "#{locale.underscore}_#{attr}".to_sym
end
end
params
end

private

def with_locale locale
Expand Down
60 changes: 33 additions & 27 deletions app/views/admin/documents/_form.html.slim
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
= simple_vertical_form_for [:admin, document] do |f|
= f.input :title
= f.input :key
= f.input :body, as: :hidden
- Rails.configuration.i18n.available_locales.each do |locale|
- locale = locale.underscore
div.locale_container
= f.input "#{locale}_title"
= f.input "#{locale}_body", as: :hidden, input_html: { class: 'document_body' }

label class="string optional control-label" for="editor" = t('activerecord.attributes.document.body')
= render 'wysiwyg_toolbar', target: '#editor'
#editor.panel
- editor_id = "#{locale}_editor"
label class="string optional control-label" = t("activerecord.attributes.document.#{locale}_body")
= render 'wysiwyg_toolbar', target: "##{editor_id}"
.editor.panel id="#{editor_id}"

= f.input :key
= f.input :is_auth, as: :radio_buttons
= f.button :submit

coffee:
$ ->
$('#editor').html($('#document_body').val())
javascript:
$(function() {
$('.document_body').each(function(i, el){
$(el).closest('.locale_container').find('.editor').html($(el).val())
})
$("a[title]").tooltip container: "body"
$(".dropdown-menu input").click(->
false
).change(->
$(this).parent(".dropdown-menu").siblings(".dropdown-toggle").dropdown "toggle"
return
).keydown "esc", ->
@value = ""
$(this).change()
return
$("a[title]").tooltip({
container: "body"
});
$("#editor").wysiwyg
hotKeys:
"ctrl+b meta+b": "bold"
"ctrl+i meta+i": "italic"
"ctrl+u meta+u": "underline"
"ctrl+z meta+z": "undo"
$(".editor").wysiwyg({
hotKeys: {
"ctrl+b meta+b": "bold",
"ctrl+i meta+i": "italic",
"ctrl+u meta+u": "underline",
"ctrl+z meta+z": "undo",
"ctrl+y meta+y meta+shift+z": "redo"
},
dragAndDropImages: false
});
$('input[type=submit]').on 'click', (event) ->
$('#document_body').val($('#editor').cleanHtml())
$('input[type=submit]').on('click', function(event) {
$('.editor').each(function(i, el){
var value = $(el).cleanHtml();
$(el).closest('.locale_container').find('.document_body').val(value)
})
});
})
6 changes: 4 additions & 2 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,11 @@ en:
payment_transaction: Payment
attributes:
document:
body: Body
zh_cn_body: Chinese content
zh_cn_title: Chinese title
en_body: English content
en_title: English title
key: Key
title: Title
is_auth: Is Auth
withdraw_address:
address: Address
Expand Down
6 changes: 4 additions & 2 deletions config/locales/zh-CN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ zh-CN:
password: 登录密码
password_confirmation: 重复登录密码
document:
body: 内容
zh_cn_body: 中文内容
zh_cn_title: 中文标题
en_body: 英文内容
en_title: 英文标题
key: 索引键
title: 标题
is_auth: 是否登录查阅
withdraw_address:
address: 提现地址
Expand Down

0 comments on commit 71c4809

Please sign in to comment.