Rails: preventing spec files from being initialized during rake tasks - ruby-on-rails-3

I have a Rails app with a spec folder full of rspec tests. Many of them reference resources in my app, such as the 'User' resource. Everything is fine on my machine, but when I try to setup the app on a new machine and build the database with rake db:setup, it starts the whole Rails app, and for some reason, parts of the rspec tests get evaluated. Since one of them is referencing the 'User' resource, for which a database table has not been created yet, the rake task fails with relation "users" does not exist.
I can easily solve this my moving the spec folder out of the parent directory, running the setup, and then moving it back, but this is annoying. Obviously, I'd like for the spec files to be ignored during rake tasks like this. Any ideas?

This SO Answer helped me: FactoryGirl screws up rake db:migrate process.
This is the specific solution that worked for me.
In your gemfile, change factory_girls_rails so it isn't required
gem 'factory_girl_rails', :require => false
Then require it in your spec helper
require 'factory_girl_rails'

Related

How to make Graphviz's dot executable path available in my rails application

Please i am new to rails a i want to generate an ER diagram for my application, i found that using rails-erd gem was an option which depended on Graphviz i have install the Graphviz application but can't figure out a way to make it visible in my rails application. I get the following error each time i run rake erd in my rails application.
$ rails erd
rails aborted!
Unable to find GraphViz's "dot" executable. Please visit
https://voormedia.github.io/rails-erd/install.html for installation
instructions.
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => erd => erd:generate => erd:check_dependencies
(See full trace by running task with --trace)
I just figured it out!!!
the reason it is not working is because Graphviz is not been seen by rails.
To solve the above issue, i just included Graphviz to my class path, and restarted my system, after which it started working.
The following steps needs to be taking:
Download Graphviz for windows and install.
Add the path to dot.exe which is located at C:\Program Files (x86)\Graphviz2.38\bin to your class path.
Add the gem to your gem file.
Install rails install gem rails-erd.
Generate your er diagrams by simply typing rails erd and an er diagram would be generated in a pdf file (erd.pdf) would be generated in your project root directory.

Missing Gemfile.lock on creation of rails application

While creating a Rails application:
user$ rails new App
All the other files get created, i.e. app, config, db, doc, script, test, Gemfile, etc I find that the Gemfile.lock fails to be created. Any particular reason why this could be happening? I am facing errors in bundling gems, though I do not know whether the absence of the Gemfile.lock file is causing this. Please help!
you dont get a lock until after you do your bundle, and whenever there is a well form group of gems built (with all dependancies available..)
if you run:
bundle install
I think one should get created for you.
or
bundle update
if you have a lock file and have an updated gemset

Unpack refinery gems in rails project

I tried rake gem:unpack but I get task not found. I would like to have refinery gems in my /vendor directory to be able to see the never ending views and partials and may be modify them.
I'm still learning how to do things the "Rails way", but I feel your need to have the files in a directory that's easy to see. One command I found that will dump in vendor in the local project is bundle install --deployment. You'll need to run this after initially doing a bundle install.
run bundle package
more here: http://gembundler.com/bundle_package.html

Load a Railtie only for Server and Console

I am creating a gem for a Rails project and I got some troubles to
understand how generators and initializers work.
I would like to
initialize my module loading some stuff from the database from models
related to tables my gem should create with a migration file.
Problem is : if I create a Railtie and put it my gem lib directory,
when I try to run my generator (to create the migration template file
for example), it's already trying to run the Railtie even though
tables required don't exist yet (because the migration file has not been executed yet).
How to restrict the "scope" of the Railtie ? I would like it only to
run when booting Rails from a server (webrick, thin, ...) or from the
console, but not for any rake tasks (including generators). I think
that rake tasks (like generators) load the entire Rails environment
and my problem should come from that.
Is there a simpler way to do what I want ?
any help or advice appreciated.

Rails Engine - Gems dependencies, how to load them into the application?

I'm doing an engine here, it works alright in stand alone.
When I transform it into a gem, and load it inside another application, I get a lot of undefined errors, coming from my engine gem's dependecies.
Here is the gemspec:
s.add_dependency('paperclip')
s.add_dependency('jquery-rails')
s.add_dependency('rails3-jquery-autocomplete')
s.add_dependency('remotipart')
s.add_dependency('cancan')
In the application, when I do a bundle install, it lists all these dependencies, but as i run the application I receive a lot of undefined methods errors (has_attachment from paperclip for example). It seems that the application doesn't load the engines dependencies.
Is this the default behavior? Can I change it?
Same thing happened with a plugin inside the engine.
If I insert by hand those gems, in the application Gemfile, all works...
Include them in your gemfile and run bundle install. Then require them in your lib/<your_engine>/engine.rb file. Don't forget to require rubygems
require 'rubygems'
require 'paperclip'
require 'jquery-rails'
require 'rails3-jquery-autocomplete'
require 'remotipart'
require 'cancan'
Then in your host app (The app where you included your gem) run bundle install/ bundle update (bundle update did the trick for me) and then everything should work perfectly. You can also test this by starting the console in your host app and just type the module name e.g.
Loading development environment (Rails 3.0.3)
irb(main):001:0> Paperclip
=> Paperclip
Hope this helps
You can require them manually like Daniel posted, and you can also require them automatically. You need to add dependencies in 3 files:
yourengine.gemspec
s.add_dependency "rails", '4.1.0'
s.add_dependency "sqlite3"
Gemfile
# Imports dependencies from yourengine.gemspec
gemspec
lib/yourengine.rb
# requires all dependencies
Gem.loaded_specs['yourengine'].dependencies.each do |d|
require d.name
end
require 'yourengine/engine'
module Yourengine
end
Update: It's a simplistic demonstration of how to require the dependencies. You should test it and filter unwanted items, for example: require d.name unless d.type == :development (thx #imsinu9)
from paperclip's README :
For Non-Rails usage:
class ModuleName < ActiveRecord::Base
include Paperclip::Glue
...
end
I had the same issue and that fixed it for me.
You must add the gem file to both the .gemspec file, and your engine.rb file.
In the .gemspec file it would be like:
s.add_dependency "kaminari", "0.16.1"
In the engine.rb file at the top add:
require "kaminari"
I think you also need to add the gem to the rails engine Gemfile and bundle install, but I'm not certain if you need it there.
At the time being (Rails 3.1 and above I think), you shouldn't have do declare any gems in the test/dummy/Gemfile anymore:
Quote from test/dummy/Gemfile (generated using rails plugin new my_engine --full):
Declare your gem's dependencies in simple_view_helpers.gemspec.
Bundler will treat runtime dependencies like base dependencies, and
development dependencies will be added by default to the :development group.
Declare any dependencies that are still in development here instead of in
your gemspec. These might include edge Rails or gems from your path or
Git. Remember to move these dependencies to your gemspec before releasing
your gem to rubygems.org.
You really shouldn't need them on the Gemsec, and they should be loaded. When you say "here is the gemspec", you are surrounding it with Gem::Specification.new do |s| or something to that effect, right?
You can include all gems for the environment with a simple bundler command:
Bundler.require(*Rails.groups)
You could add this to an config/initializer.