how to generate rspec model in rails3 [duplicate] - ruby-on-rails

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Could not find generator rspec_model issue
is there any command to generate the model for Rspec without generating any migration and fixture for rails 3, and is it possible to generate the rspec_model after building the project
with same app/model?

The only generator in the rspec-rails gem is rspec:install.
Reading: https://github.com/rspec/rspec-rails#generators
When running rspec:install, it's not going to create spec/model/... files for your existing models, but there is no proble with you running rspec:install on an existing application.
Reading: Could not find generator rspec_model issue
It's really very easy to create your own spec/model/... files; all they need is
require 'rspec_helpers'
at the top.

Related

Ruby on Rails 4 adding CRUD [duplicate]

This question already has answers here:
Rails: How to run `rails generate scaffold` when the model already exists?
(7 answers)
Closed 7 years ago.
This is my current situation:
ShoeDeal/
app/controller/
shoe_controller.rb
app/model/
customer.rb
item.rb
order.rb
app/views/shoe/
index.html.erb
db/migrate/
create_customers.rb
create_items.rb
create_orders.rb
I’m making a simple ShoeDeals App where your can select a pair of shoes that you want to purchase and add to a shopping cart. i just kinda found out that ruby has a CRUD terminal command ( rails generate scaffold ) but i already started manually creating the files list. Can i still run this code to finish where i left off?
Generating scaffold files for an existing model/controller etc will create the standard files where they don't exists and skip the ones that do. You can temporarily rename your existing files and inspect/merge the generated code into your existing classes.

Ruby Gem Development - How to use ActiveRecord?

I'm currently trying to develop my first ruby gem and I'm already stuck. I used the "bundle gem" command to create the basic structure and read some tutorials but what I can't find is how to integrate ActiveRecord.
Where do I create my migrations?
Do I create the "db/migrations" folder within the lib folder or at the root?
And do I have to do anything in the Rakefile (I found some questions where the answer was something like "you have to create your own [my_gem]:db:migrate" or something like that.)
All I need is a way to create a gem, which defines ActiveRecord models (including the migrations of course), which can then be used by a rails app.
Any help on that one would be greatly appreciated!
Greetings, Flo
When building a gem to integrate with a rails project, you want to build a railtie engine. If you are using rails 3.0.x, use enginex, if you are using rails 3.1 you should be use the new generator:
rails g plugin new your-plugin-name
Then, inside your gem, you can just define models, inside the app/models/ folder and they will automatically be picked up.
Migrations is somewhat harder: for rails 3.1 it is ok if you define them in the correct folder, in rails 3.0 you will have to manually generate a task to copy the migrations to your code-base. Check this link where I answered that very question.
For more information on rails engines check this and this article.
getting the functionality of ActiveRecord can be done by:
require "rubygems"
require "active_record"
class User < ActiveRecord::Base
end
This should work.

Ruby Error: "No such file or directory -- script/generate (LoadError)"

I know that this error has been discussed elsewhere on the web, and this may seem like a stupid question, but I've got a very strange situation on my hands here.
I'm running on Snow Leopard, with fully updated Ruby and Rails gems. I created a new Rails project using ruby new testing, then navigated into that folder using cd ~/testing, and tried to create a basic scaffolding using ruby script/generate scaffold newtest name:string, and I got this error back:
ruby: No such file or directory -- script/generate (LoadError)
I have searched Google thoroughly and tried to implement every solution that I could, but nothing has been working. I don't understand why I have this error or how to fix it.
If you are on rails 3 then the command is:
rails generate scaffold newtest name:string
Or the slightly shorter:
rails g scaffold newtest name:string
Notice rails not ruby.
If you're on Rails 3, you need to use the rails command instead, which now does much of the scripting.
(This is according to another StackOverflow question.)
If you're using the latest version of rails then you no longer use script/generate.
In Rails 3 try using something like this instead:
cd ~/testing
rails generate scaffold Post name:string title:string content:text
You can find more info on the difference between rails 2 and rails 3 here if you like:
http://www.viget.com/extend/rails-3-generators-scaffolding/

Generate only tests from existing model / controllers

I have a Rails3 app that is based on someone else's work. For some reason they decided not to supply the tests with the app, which I am finding frustrating.
What I want to be able to do is scaffold the tests for all the existing controllers and models so I can get a head start on creating the tests myself in test::unit. I don't want to recreate the models or controllers, just create the tests.
I am new to Rails, and have hunted about for the rake command that might do this, but all with no luck so far. Any advice / direction most appreciated.
I know it's a little old, but you can do this:
rails g scaffold Post -s
The -s makes it skip the files already created. Also, if you don't use the flag it just asks you if you want to override the file, so, no worries.
To only generate the associated test files for an existing Rails 3 app, I use "generate resource" but skip everything that I don't want:
rails g resource Post --skip --no-resource-route --no-migration --no-helper --no-assets
Other options can be found using rails generate resource --help
-s, [--skip] # Skip files that already exist
--resource-route # Indicates when to generate resource route
[--helper] # Indicates when to generate helper
[--assets] # Indicates when to generate assets
[--migration] # Indicates when to generate migration
Why not use generate scaffold? Because it might generate views that I'm not using.
There's no way to do this that I'm aware of. It would be pretty easy though to just create a temporary rails project and generate scaffolds for all of your models then copy the resulting test directory into the real project.
I.e.
rails new temporary
cd temporary
rails g scaffold Post title:string body:text
rails g scaffold Comment post:references author:string body:text
cp -r test ../real_rails_app/
etc.
This answer is now out of date. Up to date rails versions allow you to generate only the missing files with the skip option.

Switching test::unit with rspec under rails [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to remove unit test and replace it with rspec?
Kinda odd question here: when you go with rpsec instead of test::unit on rails, what do you do with the test dir? Keep it (for any compatibility issues maybe?) or remove it?
I have always kept it, in case some plugin or generator expects to find it there.
If you don't like having the test directory, I suggest you try to remove it and see if it works. You can always recreate it later, if you notice that you need it.

Resources