Skip to content

Instantly share code, notes, and snippets.

@agarciadelrio
Created March 13, 2012 19:30
Show Gist options
  • Save agarciadelrio/2030985 to your computer and use it in GitHub Desktop.
Save agarciadelrio/2030985 to your computer and use it in GitHub Desktop.
activeadmin has_many new model label button and I18n
# config/initializer/active_admin.rb
ActiveAdmin.setup do |config|
# == Site Title
#
# Set the title that is displayed on the main layout
# for each of the active admin pages.
...
end
ActiveAdmin::FormBuilder.class_eval do
def has_many(association, options = {}, &block)
options = { :for => association }.merge(options)
options[:class] ||= ""
options[:class] << "inputs has_many_fields"
# Add Delete Links
form_block = proc do |has_many_form|
block.call(has_many_form) + if has_many_form.object.new_record?
template.content_tag :li do
template.link_to I18n.t('active_admin.has_many_delete'), "#", :onclick => "$(this).closest('.has_many_fields').remove(); return false;", :class => "button"
end
else
end
end
content = with_new_form_buffer do
template.content_tag :div, :class => "has_many #{association}" do
form_buffers.last << template.content_tag(:h3, association.to_s.titlecase)
inputs options, &form_block
# Capture the ADD JS
js = with_new_form_buffer do
inputs_for_nested_attributes :for => [association, object.class.reflect_on_association(association).klass.new],
:class => "inputs has_many_fields",
:for_options => {
:child_index => "NEW_RECORD"
}, &form_block
end
js = template.escape_javascript(js)
# replace next line
#js = template.link_to I18n.t('active_admin.has_many_new', :model => association.to_s.singularize.titlecase), "#", :onclick => "$(this).before('#{js}'.replace(/NEW_RECORD/g, new Date().getTime())); return false;", :class => "button"
# with this new line
js = template.link_to I18n.t('active_admin.has_many_new', :model => I18n.t(association.to_s.singularize)),
"#", :onclick => "$(this).before('#{js}'.replace(/NEW_RECORD/g, new Date().getTime())); return false;", :class => "button"
form_buffers.last << js.html_safe
end
end
form_buffers.last << content.html_safe
end
end
@agarciadelrio
Copy link
Author

insert this code in the very bottom of config/initializer/active_admin.rb
the trick is:
:model => I18n.t(association.to_s.singularize))

Then in your I18n file set the translation

Good luck!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment