Trying override the default rspec-rails scaffold generator template for controllers but for some reason the new template isn't getting picked up
/lib/templates/rspec/scaffold/controller_spec.rb
Apparently putting it there should just work but it still doesn't show when running rails g rspec:scaffold.
Did the format change? I'm using Rails 4.2.2 and rspec-rails 3.3.3
A quick search on google revealed this one: https://github.com/rspec/rspec-rails/blob/master/lib/generators/rspec/scaffold/templates/controller_spec.rb
Which lives under lib/generators/spec/scaffold/templates/controller_spec.rb. Perhaps your source for the location is wrong?
Related
I need to duplicate a product in my spree application. So
def my_duplicate_product(product)
product.dup.tap do |new_product|
new_product.slug = "#{product.slug}-#{rand(1000)}"
...
This code causes the original product#slug to be changed.
What am I supposed to do to get a copy of that particular product and leave the original unchanged?
Rails version: 4.0.3
Update:
The problem is not in Ruby, nor Rails — it's all about globalize gem (v. 4.0.0).
This error was fixed in 4.0.3.
It realy broke #dup so some values were "shared" between the original model and the duplicated one.
See GitHub issue tracker for more information: https://github.com/globalize/globalize/pull/352
Maybe clone works:
def my_duplicate_product(product)
product.clone.tap do |new_product|
new_product.slug = "#{product.slug}-#{rand(1000)}"
Clone method on ruby doc
Update globalize gem to 4.0.3
See GitHub issue tracker for more information: https://github.com/globalize/globalize/pull/352
In our existing Rails 3 applications we use an overridden migration.rb file to customise the table creation behaviour.
This was done in Rails 3 by placing our custom file in lib/templates/active_record/model/migration.rb, however it appears Rails 4 has changed the location used for these templates, and this override isn't picked up anymore (it uses the default ActiveRecord migration when creating tables).
I've had a look through the 4.1 ActiveRecord code but can't get the override to work again.
Does anyone know the correct location to place our custom migration.rb in a Rails 4 codebase?
EDIT FOR CLARIFICATION
When you create a new model in Rails, the migration that is generated for you is based off the template found in (> 4.1.x) activerecord/lib/rails/generators/active_record/migration/templates/create_table_migration.rb in the Rails gem.
This has changed since Rails 3.2. In Rails 3.2 the template that was used was called migration.rb and was in the activerecord/lib/rails/generators/active_record/model/templates directory of the Rails gem.
In order to customise the generated template (add a custom SQL block that would be executed in the change method), we override this file by placing a modified copy of it in in our local code base under the lib/templates/active_record/model directory.
We customise it to add some application specific SQL to the end of the migration. In rails 3 this meant that any time you generated a new model, the resulting migration would auto-magically include our custom SQL at the end of the migration.
In our Rails 4 upgrade this custom migration isn't being used anymore, so we're getting vanilla migration files generated by rails, and are having to manually add the SQL each time.
I have tried following the same convention and placing the file in lib/templates/active_record/migration/migration.rb (and a variety of other locations) but the custom template is not being used by Rails when generating a migration.
In Rails 4.2.6 those paths are like this:
lib/templates/active_record/migration/migration.rb
lib/templates/migration/templates/create_table_migration.rb
And the ultimate answer to this problem is to look into source code of Thor library, because all rails generators are based on it. This is how I found correct paths.
Go to lib/thor/actions.rb file and look for find_in_source_paths method and just add puts statement there. Whenever you run any generator you can see a list of all paths that are searched for templates. There are also other ways, but this should give you an idea.
In Rails 4.1.4 this should go here:
activerecord/lib/active_record/migration.rb
Source:
http://api.rubyonrails.org/files/activerecord/lib/active_record/migration_rb.html
How does this translate to Rails 3.1?
#template.instance_variable_get(:#_first_render).name
It's supposed to output the name of the view being rendered. Note: it's not always the same as params[:action]
Thanks!
You can find one great answer for Rails 3.0.X here.
There is another for Rails 3.1.x but I tried it without success.
Is there a way to generate a subset of methods in the controller using scaffold instead of the regular scaffold?
For example: Only create new and show?
I am using rails 3, ruby 1.9.2
Thanks
I do not think the default rails scaffold generator supports this in Rails 3.
However, this can be accomplished using the nifty-generators gem:
https://github.com/ryanb/nifty-generators
In plain Rails3, you can do this when you generate the controller:
rails generate controller Artists new create
So, you could just generate the model, and then the controller, and get the overall functionality you were looking for.
My setup: Rails 3.0.9, Ruby 1.9.2
I wish to generate a scaffold only for the create action, what's the syntax for that? I'm guessing it's something I append to
rails g scaffold Project name:string ...
I don't believe you can. I'm looking at the generator and I see nothing related to using an option to limit the template.
Generator
Template
Rails scaffolding is a very basic tool, it's not meant to be relied on, once you get a handle on the way rails works. However, you can use Ryan Bates' nifty-generator gem to generate scaffolds with greater control. Example:
rails g nifty:scaffold post name:string index new edit
Learn more here. https://github.com/ryanb/nifty-generators