1
require 'active_support/inflector' unless defined?(ActiveSupport::Inflector)
3
module ActiveSupport #:nodoc:
4
module CoreExtensions #:nodoc:
6
# String inflections define new methods on the String class to transform names for different purposes.
7
# For instance, you can figure out the name of a database from the name of a class.
9
# "ScaleScore".tableize # => "scale_scores"
11
# Returns the plural form of the word in the string.
13
# "post".pluralize # => "posts"
14
# "octopus".pluralize # => "octopi"
15
# "sheep".pluralize # => "sheep"
16
# "words".pluralize # => "words"
17
# "the blue mailman".pluralize # => "the blue mailmen"
18
# "CamelOctopus".pluralize # => "CamelOctopi"
20
Inflector.pluralize(self)
23
# The reverse of +pluralize+, returns the singular form of a word in a string.
25
# "posts".singularize # => "post"
26
# "octopi".singularize # => "octopus"
27
# "sheep".singularize # => "sheep"
28
# "word".singularize # => "word"
29
# "the blue mailmen".singularize # => "the blue mailman"
30
# "CamelOctopi".singularize # => "CamelOctopus"
32
Inflector.singularize(self)
35
# By default, +camelize+ converts strings to UpperCamelCase. If the argument to camelize
36
# is set to <tt>:lower</tt> then camelize produces lowerCamelCase.
38
# +camelize+ will also convert '/' to '::' which is useful for converting paths to namespaces.
40
# "active_record".camelize # => "ActiveRecord"
41
# "active_record".camelize(:lower) # => "activeRecord"
42
# "active_record/errors".camelize # => "ActiveRecord::Errors"
43
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
44
def camelize(first_letter = :upper)
46
when :upper then Inflector.camelize(self, true)
47
when :lower then Inflector.camelize(self, false)
50
alias_method :camelcase, :camelize
52
# Capitalizes all the words and replaces some characters in the string to create
53
# a nicer looking title. +titleize+ is meant for creating pretty output. It is not
54
# used in the Rails internals.
56
# +titleize+ is also aliased as +titlecase+.
58
# "man from the boondocks".titleize # => "Man From The Boondocks"
59
# "x-men: the last stand".titleize # => "X Men: The Last Stand"
61
Inflector.titleize(self)
63
alias_method :titlecase, :titleize
65
# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string.
67
# +underscore+ will also change '::' to '/' to convert namespaces to paths.
69
# "ActiveRecord".underscore # => "active_record"
70
# "ActiveRecord::Errors".underscore # => active_record/errors
72
Inflector.underscore(self)
75
# Replaces underscores with dashes in the string.
77
# "puni_puni" # => "puni-puni"
79
Inflector.dasherize(self)
82
# Removes the module part from the constant expression in the string.
84
# "ActiveRecord::CoreExtensions::String::Inflections".demodulize # => "Inflections"
85
# "Inflections".demodulize # => "Inflections"
87
Inflector.demodulize(self)
90
# Replaces special characters in a string so that it may be used as part of a 'pretty' URL.
96
# "#{id}-#{name.parameterize}"
100
# @person = Person.find(1)
101
# # => #<Person id: 1, name: "Donald E. Knuth">
103
# <%= link_to(@person.name, person_path %>
104
# # => <a href="/person/1-donald-e-knuth">Donald E. Knuth</a>
105
def parameterize(sep = '-')
106
Inflector.parameterize(self, sep)
109
# Creates the name of a table like Rails does for models to table names. This method
110
# uses the +pluralize+ method on the last word in the string.
112
# "RawScaledScorer".tableize # => "raw_scaled_scorers"
113
# "egg_and_ham".tableize # => "egg_and_hams"
114
# "fancyCategory".tableize # => "fancy_categories"
116
Inflector.tableize(self)
119
# Create a class name from a plural table name like Rails does for table names to models.
120
# Note that this returns a string and not a class. (To convert to an actual class
121
# follow +classify+ with +constantize+.)
123
# "egg_and_hams".classify # => "EggAndHam"
124
# "posts".classify # => "Post"
126
# Singular names are not handled correctly.
128
# "business".classify # => "Busines"
130
Inflector.classify(self)
133
# Capitalizes the first word, turns underscores into spaces, and strips '_id'.
134
# Like +titleize+, this is meant for creating pretty output.
136
# "employee_salary" # => "Employee salary"
137
# "author_id" # => "Author"
139
Inflector.humanize(self)
142
# Creates a foreign key name from a class name.
143
# +separate_class_name_and_id_with_underscore+ sets whether
144
# the method should put '_' between the name and 'id'.
147
# "Message".foreign_key # => "message_id"
148
# "Message".foreign_key(false) # => "messageid"
149
# "Admin::Post".foreign_key # => "post_id"
150
def foreign_key(separate_class_name_and_id_with_underscore = true)
151
Inflector.foreign_key(self, separate_class_name_and_id_with_underscore)
154
# +constantize+ tries to find a declared constant with the name specified
155
# in the string. It raises a NameError when the name is not in CamelCase
156
# or is not initialized.
159
# "Module".constantize # => Module
160
# "Class".constantize # => Class
162
Inflector.constantize(self)