Yesterday, I opened a GitHub
Issuehttps://github.com/rails/rails/pull/7071 about
moving Rails inflections to an initializer. The idea wasn’t received
well,
understandably, as generating a large initializer with new Rails apps is
pretty unappealing. However, the core members seemed open to the
discussion
of alternatives continuing here.
For those of you not in the know, inflections (the defined rules for
singularization/pluralization) in Rails are currently frozen. What this
means is that most pull requests submitted that add, remove, or change
inflections are closed and not merged. The reasoning behind this is to
avoid breaking existing applications that depend on these inflections,
even
if they are errant. People are advised to fix (yes, fix) these
inflections
themselves in their inflections initializer.
In my opinion, with a major 4.0 release coming up, inflections should be
unfrozen and fixed/cleaned up. There is a lot that is bad in this file,
and
I don’t think fear of breaking existing apps is a good reason to freeze
mistakes. People should read the CHANGELOG when updating. They should
read
the upgrade guides. It’s not that hard to redefine the inflection you
need
in that initializer, and the current inflections are based mostly around
exceptions to grammatical rules rather than the rules themselves.
If people want specifics, I’ve written a post about this:
I’m curious as to what others think. I’d like to help make this a better
framework, and I’ve heard a lot of complaints from personal colleagues
about how messy the inflections are.
I personally don’t like them at all. I wish that all the names of
controllers/routes/models were unchanged so I wouldn’t have to keep
deciding whether I needed to pluralize. I remember spending too much
time trying to straighten out a scaffold I created named “series”. It
created a model named “Serie”! I shouldn’t have to spend energy
figuring that out. It should use the exact name I give it.
I disagree with this; I think the inflections are important. They’re
just
messy right now. I don’t think it’s an arduous task to fix them, and I
don’t think it’s unreasonable to take the stance of “Rails can’t
possibly
include every inflection by default.” My problem with the current
inflections is that it seems like they were trying to include every
inflection. Now we’ve got a long list of inflections based on exceptions
and irregularities, most of which rarely see the light of day in any
Rails
application. But they’re frozen, so it’s difficult to get Rails core to
accept fixes.
Looking over grammatical rules, the actual list of inflections could be
extremely short:
I could be missing something here, but those cover the regular plurals
and
a couple of irregularities that will show up in Rails apps enough to
warrant definition. More irregularities could be argued, but
personally I
think the stance should be that most irregularities should be defined in
the user’s initializer (which is what Rails core tells people now
anyways).
But telling people right in the documentation to fix errant
inflections in
their initializer? Yes, let’s admit we’re wrong. That’s noble. But let’s
fix it, too.
At least they should fix the ones that are incorrect…
“Octopus”.pluralize should return “Octopuses” and NOT “Octopi”…
But then all those applications that rely on the current pluralisation
of octopus would break when upgraded. Innumerable applications around
the internet would come to their knees.
For anybody who shares my sentiment, until Rails core decides it’s time
to
clean up their inflections, I’ve published a gem to provide more sane
default pluralization/singularization rules: https://github.com/davidcelis/infletions
Here’s the difference:
4 pluralization rules (down from 21)
5 singularization rules (down from 27)
3 irregularities (down from 7)
1 uncountable (down from 10)
At least they should fix the ones that are incorrect…
“Octopus”.pluralize should return “Octopuses” and NOT “Octopi”…
But then all those applications that rely on the current pluralisation
of octopus would break when upgraded. Innumerable applications around
the internet would come to their knees.
Surely all those sites’ developers could just override the inflector
back to what it used to be, and get on with all the other tasks they
need to complete to upgrade to Rails4?
Yep, the point I’ve been getting at. People shouldn’t blindly upgrade
their
Rails versions anyway, and when they see errors that a Controller can’t
be
found based on their Model name or vice versa, it should be pretty
obvious
as to what’s going on unless they’re clueless about the inflector.
One thing that have always bothered me about the inflector and this
pluralize business is that it only work in English. That make rails
newbies
from non-english speaking countries have a harder time learning the
technology than they should. I know ruby’s keywords are already in
English,
and so are method names and so on. But reading a foreing language is
easier; and so is using already defined names. However, a person who
doesn’t know English very well will not do a good job in naming his own
variables and models in English. A famous quote from computer science
field
goes: “There are only two hard things in Computer Science: cache
invalidation and naming things”. Imagine naming it in a language you
aren’t proficient.
Perhaps inflection should not be the default. Maybe it should be
optional.
If you want it, then you can load your language’s inflection gem, if
it’s
available.
On Thursday, July 19, 2012 6:53:43 AM UTC-5, paulie wrote:
I personally don’t like them at all. I wish that all the names of
controllers/routes/models were unchanged so I wouldn’t have to keep
deciding whether I needed to pluralize. I remember spending too much
time trying to straighten out a scaffold I created named “series”. It
created a model named “Serie”! I shouldn’t have to spend energy
figuring that out. It should use the exact name I give it.
I agree with this some what. There have been times where I needed a
singular table name or plural model name. I know I could override which
table it points to and do a little foot work on my part.
The current state of inflections isn’t too horrible, but it should at
least
be decoupled from Rails core.
Maybe I’m missing something, but couldn’t the default inflection just
don’t
do anything? That is, the plural of any word is the word itself. From
the
user point of view, it would be like there is no pluralization at all.
I don’t think that’s really a good option. Some Rails internals already
rely heavily upon the inflections; there have to be some pluralizations
loaded by default (whether from within the framework, or an inflections
gem). If they WERE to be extracted into a gem (which is what I did), it
would still need to be bundled by default. Inflections are used in
routing,
mapping between models and controllers, and a lot of other places within
Rails. They need to be there.
I just ran into this again. I attempted to call a model “Media”, and
the scaffold decided that I meant “Medium”, which is NOT a newspaper;
it’s someone who has a seance. As I often do in that situation, I
deleted those files and changed the name to “MediaOutlet” so it would
act predictably.
Agreed on all counts. I don’t think pluralization needs to go anywhere.
I
think the resourceful routes and controller names are more
representative
of what’s going on. I just think the pluralization rules need to be
cleaner
and that the backwards compatibility excuse doesn’t work so well for a
major 4.0 release.
index: GET /media
show: GET /media/1
edit: GET /media/1/edit
create: POST /media
update: PUT /media/1
new: GET /media/new
delete: DELETE /media
I wasn’t suggesting changing this retroactively, I was supporting the
idea of the inflections gem, which would allow an replacement that
didn’t require duplicating all the rules in javascript.
Guys, lets keep in mind that I meant for this discussion to be about how
the inflection rules themselves are bad. Not how they’re used internally
with Rails, but how the file that defines these rules is an utter mess.
On Saturday, 28 July 2012 10:21:23 UTC-4, paulie wrote:
I just ran into this again. I attempted to call a model “Media”, and
the scaffold decided that I meant “Medium”, which is NOT a newspaper;
it’s someone who has a seance.
As I often do in that situation, I
deleted those files and changed the name to “MediaOutlet” so it would
act predictably.
And as a side effect, got a model name that makes it way easier to
understand what you’re actually representing. Until you clarified it, my
first guess was going to be that ‘Media’ was a class representing an
audio/video/image file.
That beats having to call pluralize and singularize all the time and
try to keep it straight.
For bonus points, how would the new behavior distinguish between the
‘show’
and ‘index’ actions? (currently plural-with-no-args vs.
singular-with-one-arg)
I suspect you’re going to face a lot of pushback from all the developers
who have managed to “keep it straight”, since your suggestion involves
breaking every one of their applications.
–Matt J.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.