Refinery CMS: generating migrations for existing models within an engine - ruby-on-rails

I want to add an attribute to a model I've created within a Refinery CMS engine. I know I could do the following:
rails generate migration AddPartNumberToProducts part_number:string
Manually move the migration file from db/migrate to vendor/extensions/products/db/migrate
But is there a command to generate the migration into the correct folder in the first place?
Thanks!

The method I have outlined above is correct, according to #parndt (the lead developer of the Refinery project).

Related

Does controllers get created whenever models are created?

I created model for user in ruby on rails using scaffold.
Then I got to know that when I saw controller's folder of the project I would find usercontroller.rb file created ? Does this means that whenever model is created controllers are created with it ?
It isn't the model creation that does it, it's the scaffolding.
http://guides.rubyonrails.org/v3.2.9/getting_started.html
Section 6 of that document describes what is generated during scaffolding. The scaffolding process creates a number of files, controller being one of them.
rails generate scaffold will generate a model, controller database migration, and views.
Here is a list of the generators that Rails provides:
assets
controller
generator
helper
integration_test
jbuilder
mailer
migration
model
resource
scaffold
scaffold_controller
task
The Ruby on Rails guides can provide you with additional information on the command line tools.

In what directory / folder do you install Rails Scaffold

I am following the Rails Tutorial. It is starting to generate a scaffold. I just don't know for sure where to pit it. In a subdirectory or right at the root. My hunch is the root. i.e. JimJones$ not JimJones$/work/newapp. The apps can keep[ changing so I would imagine I would install the scaffold at the root. Any help would be greatly appreciated. Thanks, Chris
Your hunch is wrong, you should be in your /newapp directory, assuming that's the name of your app.
If you're not sure where you're at, enter pwd at the command line, and this will
print out your current location. If it ends in /newapp, you're all good.
rails generate scaffold myscaffold creates a set of MVC scaffolds called myscaffold(s), INSIDE your current app. In fact, You shouldn't be able to run rails generate scaffold unless you are inside a rails project.
Scaffolding is specific to Rails applications. First create a new Rails application using rails new appname command. Then move to the appname directory.
Then use scaffolding
rails generate scaffold test
The above command will generate a set of model, database migration for that model, controller, views, and test suites for the resource test
More info on scaffolding : http://guides.rubyonrails.org/command_line.html
You need to be in the rails app directory to use scaffold and the code generated by scaffold will be put in appropriate locations in your rails app directory e.g. model in models directory controller in controllers

Is there a built in way to rename an entire scaffold?

I'm using Rails 3.2, and have started with a scaffolding and built out from it, but have realized I need to rename the entire scaffold (Model, View, Controller, db:migrate, etc). Is there a built in way to do this, or should I just do it manually?
I don't think there's anything rails provides to rename the name of models/controllers/views/tests etc. once they are created - whether as a part of a scaffold, or not.
You will have to change it manually.
If it is a brand new app that you have just started on, it might be easier to just delete the whole directory/drop the database, and start over again.
If not, you will have to go through the files created/modified by the scaffold generation, and modify them manually.
Make sure you either drop_and_recreate the relevant table, or add a migration to rename the table. See How do you write a migration to rename an ActiveRecord model and its table in Rails? for some relevant advice.
I think there's no out of the box method to rename files generated by scaffold.What you have to do is create a new scaffold and copy your codes from the old to the new.Copy contents from factories, model, controller and their respective spec file to the new scaffold. Then remove the old scaffold with the command
rspec d scaffold <Model Name>
and you have to create a new migration to drop that old table from the database. Then run your migrations.

Rails: renaming a controller and corresponding model

Is there an easy way to rename a controller and model in my app and all the instances in the corresponding code?
I'm using textmate, would this be as simple as using the replace function and replacing the word Post with Report?
You need to change the name of the Controller and the associated Model, Views, Helpers, Tests & Routes directories, file names, class names & the names in the class definitions.
I found two ways to do this but before you try anything I recommend you back-up your app, preferably with a Software Version Control system like Git & Github.com.
Your first option is to do it manually & there is a good explanation on how to do this here: How to rename rails controller and model in a project
Another way is to destroy your controller & model, and then generate a new one, this will remove all the files which were generated the first time round & replace them with new ones. Michael Hartl explains this solution well in his online guide to Ruby on Rails here: http://ruby.railstutorial.org/chapters/static-pages#sidebar-undoing_things
This is the solution I followed when I needed to make this change to my app, I needed to replace a MVC scaffold I generated called board with a new one called product.
1. First
I made a back-up of the work I did in the layout of the board view, app/views/boards/index.html.erb
2. Then
I ran the below rails commands in the terminal window.
$ rake db:rollback
$ rails destroy scaffold board name:string description:text image:string price:decimal
$ rails generate scaffold product product_type:string name:string description:text image:string price:decimal
$ rake db:migrate
3. Finally
I copied my backed-up boards/index.html.erb file into the newly generated app/views/products/index.html.erb & did a find & replace in my text editor on this file to replace board with product.
I think the second option is much more reliable & quicker but it’s important to make this change early on in your project before you make too many manual changes to the code. It would be better to just take a little more time planning your MVC names & database tables properly before you start your project.
You can also use rails_refactor gem to rename controller, model, etc
for more info check https://github.com/jcrisp/rails_refactor
To rename controller and model use this gem https://github.com/jcrisp/rails_refactor
If you are using textmate, use 'command-shift-f" to look for a string throughout your entire project.
Yes and no. You can rename it that way, but you'll also need to rename the files as well or Rails won't know where to look for the files corresponding to the new Report model/controller/etc.

Creating migration through generator in ROR

I am making an plugin in ROR and I want to make a migration file that will create a table. I am using generator for generating migration but not able to run it .
So please tell me the exact process to create a migration through generators.
Thanks!
http://izumi.plan99.net/manuals/creating_plugins-938a313a.html
go to this url

Resources