I'm writing a plugin that needs some inflections of it's own. Using rails 2.3 which has the engines imbedded, where I should place my inflections?
I'd recommend adding a separate file (inflections.rb) in your plugins lib directory plugin. You should be able to load the inflections.rb file from the plugin by adding the following at the beginning of the plugin Ruby file.
require 'inflections"
Your inflections.rb file should follow the format provided as an example in new Rails projects:
# Sample Inflections
# ActiveSupport::Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
UPDATE: for anyone who is having the same issue but uses Rails 5, here is the right answer.
Don't place inflections.rb file in plugin/lib folder, but in plugin/config/initializers folder.
Then simply write your inflections (eg cliche/cliches):
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'cliche', 'cliches'
end
Related
i have created an engine with
rails plugin new myengine --mountable
when searching for 'inflections' in the project folder, i find the
/test/dummy/config/initializers/inflections.rb file
in this file i put
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'singular_model', 'plural_model'
end
when i try to use the model generator (from project root)
bin/rails generate model singular_model
then i get the a migration with singular_models as the table name and migration name
when i run the same command from myengine/test/dummy
bin/rails generate model singular_model
i get the spected result: plural_model as table name and migration name
so, how can i load the inflector into the rails generator?
At your engine, you must think on your test/dummy/ folder as the root of the app that is using this engine. This folder only exists for testing purpose, is not a setting for the engine.
Let say your engine is at folder myengine and your app is at folder myapp.
1) If you want a custom inflection, which is defined at engine level and it be used at the engine and at the app level. Then it must be defined in:
# myengine/config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.irregular 'singular_model', 'plural_model'
end
Yes, you must create the folder initializers and the file inflections.rb in it. And this inflection will be valid for each app that use the engine.
2) If you want your custom inflections works only for your app, but not for others apps that use the same engine. You must use the same code at yours myapp/config/initializers/inflections.rb, this file does exists by default in a Rails app.
In this last case the scope of the custom inflections is only the current app, just like at the test/dummy folder.
Depending on what behavior you need, is where you must put the code of your custom inflections.
You can actually create a file named inflections.rb in project _root/config/initializers/ and write your rules in that file for e.g.,
# Be sure to restart your server when you modify this file.
# Add new inflection rules using the following format. Inflections
# are locale specific, and you may define rules for as many different
# locales as you wish. All of these examples are active by default:
ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
inflect.irregular 'cloth', 'clothes'
end
# These inflection rules are supported but not enabled by default:
# ActiveSupport::Inflector.inflections(:en) do |inflect|
# inflect.acronym 'RESTful'
# end
And it gets applicable to the engine as well.
Hope it helps!
In Rails 4.2, rails engines do not load the initializers when running generators, which is discussed in this issue: https://github.com/rails/rails/issues/14472
Workaround
Define inflections in my_engine/config/initializers/inflections.rb:
# my_engine/config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'regatta', 'regattas'
end
Require the inflections initializer in the engine.rb:
# my_engine/lib/my_engine/engine.rb
require_relative '../../config/initializers/inflections'
module MyEngine
class Engine < ::Rails::Engine
# ...
end
end
Use the generator as expected: bin/rails g model Regatta
i'm developing a rails 4.1.1 application that uses custom inflections:
inflections.rb:
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural /([taeiou])([A-Z]|_|\$)/, '\1s\2'
inflect.plural /([rlnd])([A-Z]|_|$)/, '\1es\2'
inflect.singular /([taeiou])s([A-Z]|_|$)/, '\1\2'
inflect.singular /([rlnd])es([A-Z]|_|$)/, '\1\2'
end
and in the application.rb:
...
config.i18n.default_locale = :es
config.i18n.locale = :es
...
but when I try to generate a model it doesn't use the inflectors, I tryed opening a rails console and test using this command:
"direccion".pluralize
The answer was "direcions" wich is incorrect, the right answer is is "direcciones". What should I change so it uses my custom inflector at command line or in rails console so it can generate correctly my models?
I'm trying to generate a model called ClassAttendance, but Rails keeps naming the migrations class_attendances. I've tried correcting this problem by placing the following code the following code in \config\initializers\inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable "attendance"
end
This seems to work fine in the rails console:
$ rails console
Loading development environment (Rails 3.2.6)
irb(main):001:0> "attendance".pluralize
=> "attendance"
Unfortunately, the rails model generator seems to be unaffected:
$ rails generate model ClassAttendance
invoke active_record
create db/migrate/20120806201910_create_class_attendances.rb
create app/models/class_attendance.rb
invoke rspec
create spec/models/class_attendance_spec.rb
Does it have something to do with this?
irb(main):002:0> "class_attendance".pluralize
=> "class_attendances"
Or is there some other problem I'm not seeing?
That is the workaround, you need to place it in the inflections.rb file in the config/initializers/. So your config/initializers/inflections.rb would be
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w( attendance class_attendance ClassAttendance)
end
sorry for my english...
I have a rails application developed to spain, therefore, all content is in spanish, so, I have a search box to search in a mysql database, all rows are in spanish, I'd like to improve my search to allow to users to search keywords in singular or plural form, for example:
keyword: patatas
found: patata
keyword: veces
found: vez
keyword: vez
found: veces
keyword: actividades
found: actividad
In english, this could be relatively easy with help of singularize and pluralize methods ...
where `searching_field` like '%singularized_keyword%' or `searching_field` like '%pluralized_keyword%'
But, for spanish....
Some help?
Thanks!
You can define your own inflections now.
look in config/initializers/inflections.rb
an example based on your question
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'patata', 'patatas'
end
Thus
"patata".pluralize # => "patatas"
"patatas".singularize #=> "patata"
Of course you need to know the list of keywords in advance to use the irregular method in config/inflections.rb. Have a look at the commented out examples in that file. There are other methods that allow one to define rules using regular expressions and you could devise pattern matches to affect inflections for arbitrary keywords that match known patterns.
You have to clear all default inflections in English and create new ones in Spanish.
Add in config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear :all
inflect.plural /([^djlnrs])([A-Z]|_|$)/, '\1s\2'
inflect.plural /([djlnrs])([A-Z]|_|$)/, '\1es\2'
inflect.plural /(.*)z([A-Z]|_|$)$/i, '\1ces\2'
inflect.singular /([^djlnrs])s([A-Z]|_|$)/, '\1\2'
inflect.singular /([djlnrs])es([A-Z]|_|$)/, '\1\2'
inflect.singular /(.*)ces([A-Z]|_|$)$/i, '\1z\2'
end
I found this great way:
http://www.slideshare.net/crnixon/advanced-internationalization-with-rails
Regards.
It seems that it is now possible to use localized inflections now:
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:es) do |inflect|
inflect.plural /([^djlnrs])([A-Z]|_|$)/, '\1s\2'
inflect.plural /([djlnrs])([A-Z]|_|$)/, '\1es\2'
inflect.plural /(.*)z([A-Z]|_|$)$/i, '\1ces\2'
inflect.singular /([^djlnrs])s([A-Z]|_|$)/, '\1\2'
inflect.singular /([djlnrs])es([A-Z]|_|$)/, '\1\2'
inflect.singular /(.*)ces([A-Z]|_|$)$/i, '\1z\2'
end
With that (and after restarting the server) you can use:
"trebol".pluralize(:es) #=> "treboles"
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I override rails naming conventions?
My app is gonna be in spanish.
Say I want to scaffold generate actividad. the plural would be actividades so I want the table and the controller to be named that .... how can I do this?
Put the following code in config/environment.rb:
Inflector.inflections do |inflect|
inflect.irregular 'actividad', 'actividades'
end
Test the code in the console (script/console):
'actividad'.pluralize
'actividades'.singularize
More details can be found here:
http://codeidol.com/other/rubyckbk/Web-Development-Ruby-on-Rails/Understanding-Pluralization-Rules/
You can add your own pluralizations to Rails by adding inflections. Rails should have a file called inflections.rb under /config/initializers. You can add it there if it isn't. My file as an example (comments come from Rails):
# Add new inflection rules using the following format
# (all these examples are active by default):
# Inflector.inflections do |inflect|
# inflect.plural /^(ox)$/i, '\1en'
# inflect.singular /^(ox)en/i, '\1'
# inflect.irregular 'person', 'people'
# inflect.uncountable %w( fish sheep )
# end
ActiveSupport::Inflector.inflections do |inflect|
inflect.plural(/rion$/ ,'ria') # criterion => criteria
inflect.singular(/ria$/, 'rion') # criteria => criterion
end
I think your best bet is to write your code in English, and use the i18n api to present the content in Spanish and other languages if/when necessary.