Skip to content

Commit

Permalink
Starting implementing a better translation system for Resource::Namin…
Browse files Browse the repository at this point in the history
…g. For instance it translates the models on demand not when ActiveAdmin loads.
  • Loading branch information
ysbaddaden committed Sep 20, 2011
1 parent 03ba8c0 commit fb68871
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 38 deletions.
16 changes: 11 additions & 5 deletions lib/active_admin/resource/action_items.rb
Expand Up @@ -49,23 +49,29 @@ def add_default_action_items
# New Link on all actions except :new and :show
add_action_item :except => [:new, :show] do
if controller.action_methods.include?('new')
link_to(I18n.t('active_admin.new_model', :model => active_admin_config.resource_name), new_resource_path)
txt = I18n.t('activeadmin.resources.#{active_admin_config.resource_name}.links.new_model',
:default => :"active_admin.new_model", :model => active_admin_config.human_name)
link_to(txt, new_resource_path)
end
end

# Edit link on show
add_action_item :only => :show do
if controller.action_methods.include?('edit')
link_to(I18n.t('active_admin.edit_model', :model => active_admin_config.resource_name), edit_resource_path(resource))
txt = I18n.t('activeadmin.resources.#{active_admin_config.resource_name}.links.edit_model',
:default => :"active_admin.edit_model", :model => active_admin_config.human_name)
link_to(txt, edit_resource_path(resource))
end
end

# Destroy link on show
add_action_item :only => :show do
if controller.action_methods.include?("destroy")
link_to(I18n.t('active_admin.delete_model', :model => active_admin_config.resource_name),
resource_path(resource),
:method => :delete, :confirm => I18n.t('active_admin.delete_confirmation'))
txt = I18n.t('activeadmin.resources.#{active_admin_config.resource_name}.links.delete_model',
:default => :"active_admin.delete_model", :model => active_admin_config.human_name)
confirm_txt = I18n.t('activeadmin.resources.#{active_admin_config.resource_name}.links.delete_confirmation',
:default => :"active_admin.delete_confirmation", :model => active_admin_config.human_name)
link_to(txt, resource_path(resource), :method => :delete, :confirm => confirm_txt)
end
end
end
Expand Down
52 changes: 37 additions & 15 deletions lib/active_admin/resource/naming.rb
Expand Up @@ -13,30 +13,52 @@ def underscored_resource_name

# A camelized safe representation for this resource
def camelized_resource_name
underscored_resource_name.camelize
@camelized_resource_name = underscored_resource_name.camelize
end

# Returns the name to call this resource.
# By default will use resource.model_name.human
def resource_name
@resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
underscored_resource_name.titleize
else
resource.model_name.human.titleize
end
@resource_name ||= underscored_resource_name.titleize

# @resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
# underscored_resource_name.titleize
# else
# resource.model_name.human.titleize
# end
end

# Returns the plural version of this resource
def plural_resource_name
@plural_resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
resource_name.pluralize
else
# Check if we have a translation available otherwise pluralize
begin
I18n.translate!("activerecord.models.#{resource.model_name.downcase}")
resource.model_name.human(:count => 3)
rescue I18n::MissingTranslationData
resource_name.pluralize
@plural_resource_name ||= resource_name.pluralize

# @plural_resource_name ||= if @options[:as] || !resource.respond_to?(:model_name)
# resource_name.pluralize
# else
# # Check if we have a translation available otherwise pluralize
# begin
# I18n.translate!("activerecord.models.#{resource.model_name.downcase}")
# resource.model_name.human(:count => 3)
# rescue I18n::MissingTranslationData
# resource_name.pluralize
# end
# end
end

def i18n_scope
resource_name.underscore
end

# Returns the human translation for this model.
def human_name(options = {})
begin
options[:count] ||= 3
I18n.t!("activeadmin.resources.#{resource_name}", options)
rescue
if resource.respond_to?(:model_name)
resource.model_name.human
else
resource_name
end
end
end
Expand Down
6 changes: 1 addition & 5 deletions lib/active_admin/sidebar_section.rb
Expand Up @@ -25,11 +25,7 @@ def icon

# The title gets displayed within the section in the view
def title
begin
I18n.t!("active_admin.sidebars.#{name.to_s}")
rescue I18n::MissingTranslationData
name.to_s.titlecase
end
I18n.t!("active_admin.sidebars.#{name.to_s}", :default => name.to_s.titleize)
end

# If a block is not passed in, the name of the partial to render
Expand Down
7 changes: 3 additions & 4 deletions lib/active_admin/views/components/scopes.rb
Expand Up @@ -21,10 +21,9 @@ def build_scope(scope)
else
scoping_class.name.underscore
end

scope_name = I18n.t("resources.#{i18n_scope}.scopes.#{scope.id}",
:scope => "active_admin",
:default => [ "scopes.#{scope.id}".to_sym, scope.name ]

scope_name = I18n.t("activeadmin.resources.#{i18n_scope}.scopes.#{scope.id}",
:default => [ "activeadmin.scopes.#{scope.id}".to_sym, scope.name ]
)

if current_scope?(scope)
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/views/pages/edit.rb
Expand Up @@ -4,7 +4,7 @@ module Pages
class Edit < Base

def title
I18n.t('active_admin.edit_model', :model => active_admin_config.resource_name)
I18n.t('active_admin.edit_model', :model => active_admin_config.human_name)
end

def main_content
Expand Down
18 changes: 13 additions & 5 deletions lib/active_admin/views/pages/index.rb
Expand Up @@ -5,7 +5,7 @@ module Pages
class Index < Base

def title
active_admin_config.plural_resource_name
active_admin_config.human_name.pluralize
end

def config
Expand Down Expand Up @@ -62,22 +62,30 @@ def find_index_renderer_class(symbol_or_class)
end

def render_blank_slate
blank_slate_content = I18n.t("active_admin.blank_slate.content", :resource_name => active_admin_config.resource_name.pluralize)
blank_slate_content = I18n.t("activeadmin.resources.#{active_admin_config.i18n_scope}.blank_slate.content",
:default => :"active_admin.blank_slate.content",
:resource_name => active_admin_config.human_name
)
if controller.action_methods.include?('new')
blank_slate_content += " " + link_to(I18n.t("active_admin.blank_slate.link"), new_resource_path)
txt = I18n.t("activeadmin.resources.#{active_admin_config.i18n_scope}.blank_slate.link",
:default => :"active_admin.blank_slate.link")
blank_slate_content += " " + link_to(txt, new_resource_path)
end
insert_tag(view_factory.blank_slate, blank_slate_content)
end

def render_empty_results
empty_results_content = I18n.t("active_admin.pagination.empty", :model => active_admin_config.resource_name.pluralize)
empty_results_content = I18n.t("activeadmin.resources.#{active_admin_config.i18n_scope}.pagination.empty",
:default => :"active_admin.pagination.empty",
:resource_name => active_admin_config.human_name
)
insert_tag(view_factory.blank_slate, empty_results_content)
end

def render_index
renderer_class = find_index_renderer_class(config[:as])

paginated_collection(collection, :entry_name => active_admin_config.resource_name) do
paginated_collection(collection, :entry_name => active_admin_config.human_name) do
div :class => 'index_content' do
insert_tag(renderer_class, config, collection)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/views/pages/new.rb
Expand Up @@ -4,7 +4,7 @@ module Pages
class New < Base

def title
I18n.t('active_admin.new_model', :model => active_admin_config.resource_name)
I18n.t('active_admin.new_model', :model => active_admin_config.human_name)
end

def main_content
Expand Down
8 changes: 6 additions & 2 deletions lib/active_admin/views/pages/show.rb
Expand Up @@ -28,15 +28,19 @@ def main_content
end

def attributes_table(*args, &block)
panel(I18n.t('active_admin.details', :model => active_admin_config.resource_name)) do
heading = I18n.t('active_admin.resources.#{active_admin_config.resource_name}.details',
:defaults => :'active_admin.details',
:model => active_admin_config.resource_name)
# panel(I18n.t('active_admin.details', :model => active_admin_config.resource_name)) do
panel(heading) do
attributes_table_for resource, *args, &block
end
end

protected

def default_title
"#{active_admin_config.resource_name} ##{resource.id}"
"#{active_admin_config.human_name} ##{resource.id}"
end

module DefaultMainContent
Expand Down

0 comments on commit fb68871

Please sign in to comment.