Skip to content

Commit

Permalink
HumanNameHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
stgeneral committed Jul 13, 2024
1 parent 7ac382c commit 386f39d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/auxiliary_rails/view_helpers/human_name_helper.rb
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
1 change: 1 addition & 0 deletions lib/auxiliary_rails_resourceable.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require_relative 'auxiliary_rails_resourceable/version'
require_relative 'auxiliary_rails_resourceable/railtie'

require 'pagy'
require 'pundit'
Expand Down
10 changes: 10 additions & 0 deletions lib/auxiliary_rails_resourceable/railtie.rb
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

0 comments on commit 386f39d

Please sign in to comment.