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
Related
I'm new in Rails and now I'm trying to make a custom pluralization so I put this in my initializers/inflections.rb:
ActiveSupport::Inflector.inflections do |inflect|
inflect.clear
# Irregulares
inflect.irregular "país", "países"
end
But when I try on rails console I get:
Running via Spring preloader in process 3137
Loading development environment (Rails 5.0.2)
2.3.3 :001 > "país".pluralize
=> "país"
2.3.3 :002 >
I also tried to put this in my inflections.rb but with the same result:
ActiveSupport::Inflector.inflections ("pt-BR") do |inflect|
inflect.clear
# Irregulares
inflect.irregular "país", "países"
end
As I'm using rails-i18n my application.rb has this extra line:
config.i18n.default_locale = 'pt-BR'
I searched a lot and didn't find a solution.
Any suggestions?
Try running the console without Spring with DISABLE_SPRING=1
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?
Follow Installation in Rails 3 of Mongo Mapper i add to config/application.rb file :
config.generators do |g|
g.orm :mongo_mapper
end
when rails generate migration my_migration i got :
error mongo_mapper [not found]
but when rails generate model my_model, i got a fine model class with include MongoMapper::Document and this output :
invoke mongo_mapper
create app/models/user.rb
invoke test_unit
create test/unit/user_test.rb
create test/fixtures/users.yml
You don't have migrations while working with MongoDB.
Because MongoDB is a schema less database.
That's the reason you can't generate migrations with mongo mapper.
Does anyone know of a rake task or RSpec call that will generate a bunch of empty files relative to the existing controllers, models, helper files and views that already exist within your application?
You can generate an empty scaffold set of rspec tests against an existing controller using something like this:
rails generate rspec:scaffold recipe
You can improve on this by passing the attributes of the model you want to generate against, like this:
rails generate rspec:scaffold recipe title: string slug: string description: text
You'll still need to do some manual editing, but this should get you most of the way there.
The best solution for this is to add hooks in place within environment.rb to create the spec.rb files within the rails application each time a model or controller is created.
Here's the code for that (using RSpec and FactoryGirl):
module RailsApp
class Application < Rails::Application
config.generators do |g|
g.test_framework :rspec, :fixture_replacement => :factory_girl, :views => true, :helper => false
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
g.stylesheets false
g.javascripts false
g.helper false
end
end
end
This should work:
Install the rspec-rails gem by adding it to your development and test groups in your gemfile gem 'rspec-rails'
Run the rspec generator from inside your app rails generate rspec:install
Read over this doc quickly to see how it integrates with your rails app RSpec-rails doc