-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module AuxiliaryRails | ||
module ViewHelpers | ||
module HumanNameHelper | ||
# Human-readable name for a model or a model's attribute | ||
# | ||
# @param model [Class, Object] The model class or an instance of the model | ||
# @param attribute [Symbol, String, nil] The attribute name (optional) | ||
# @return [String] The human-readable name | ||
def human_name(model, attribute = nil, count: 1) | ||
if attribute.nil? | ||
model.model_name.human(count: count) | ||
else | ||
model = model.class unless model.is_a?(Class) | ||
if count > 1 | ||
model.human_attribute_name(attribute).pluralize(count) | ||
else | ||
model.human_attribute_name(attribute) | ||
end | ||
end | ||
end | ||
|
||
# Human-readable name for a model or a model's attribute plural form | ||
# | ||
# @param model [Class, Object] The model class or an instance of the model | ||
# @param attribute [Symbol, String, nil] The attribute name (optional) | ||
# @return [String] The human-readable name in plural form | ||
def human_name_plural(model, attribute = nil, count: 2) | ||
human_name(model, attribute, count: count) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
require 'rails/railtie' | ||
require 'auxiliary_rails/view_helpers/human_name_helper' | ||
|
||
module AuxiliaryRails | ||
class Railtie < Rails::Railtie | ||
initializer 'auxiliary_rails.view_helpers' do | ||
ActionView::Base.include AuxiliaryRails::ViewHelpers::HumanNameHelper | ||
end | ||
end | ||
end |