How do I properly arrange my Gemfile for Rails 3? - ruby-on-rails-3

I am trying to install vote-fu into my rails 3 project.
The documentation ( for rails 2 ) says to install it into my environments.rb file as so..
config.gem "peteonrails-vote_fu", :lib => 'vote_fu', :source => 'http://gems.github.com'
How could I convert that to rails 3 for the Gemfile?

In Gemfile:
gem 'vote_fu'
http://gems.github.com is no more. The standard gem host is http://gemcutter.org which Bundler is setup to use by default, so you don't need to specify source. Vote_fu is hosted on gemcutter (see: http://rubygems.org/gems/vote_fu).
For reference, you would convert the options in config.gem like this:
:source => 'example.com' changes to (on its own line in Gemfile):
source 'example.com'
:lib => 'mylib' changes to:
gem 'libkey_mylib', :require => 'mylib'

Related

Paperclip Mongoid S3 error

GETTING THIS ERROR WHEN UPLOADING A FILE :
LoadError (no such file to load -- aws-sdk (You may need to install the aws-sdk gem)):
app/controllers/uploaded_files_controller.rb:19:in `create'
I am using Mongo and Paperclip. I can upload files fine without using s3. However, our production server is on Heroku and so I have to use Amazon to store the files.
I've read other Stack Overflow posts about this but none address my specific issue.
I have restarted my server several times. that's not it.
I am indeed requiring the Amazon gem in my Gemfile
I have done a bundle install after putting the amazon gem in ( I know its obvious, but still I had to state this )
I am NOT using ImageMagick. These uploads are simple text file uploads.
I know that my Amazon bucket name and auth stuff is correct because I use this app to connect to other Amazon resources in a different capacity.
Can anyone help with this ? Here is my code:
class UploadedFile
include Mongoid::Document
include Mongoid::Paperclip
require "aws/s3"
has_mongoid_attached_file :file,
:storage => :s3,
:bucket_name => 'my-uploads',
:path => ':attachment/:id/:style.:extension',
:s3_credentials => File.join(Rails.root, 'config', 'amazon_s3.yml')
end
OK, I've found the answer: The gem needs to be updated.
Paperclip now requires Amazon SDK gem instead of the s3 gem.
gem 'aws-s3', :require => "aws/s3"
should be instead
gem 'aws-sdk', :require => "aws-sdk"

Disable cookies for a cucumber test

I would like to be able do something like the following:
#javascript #disallow_cookies
Scenario: Test Something that depends on cookies
How would I write the #disallow_cookies cucumber hook to achieve this?
I am using the following:
gem 'thin', '1.2.11'
gem 'cucumber-rails' , '1.0.4'
gem 'database_cleaner', '0.6.7'
gem 'capybara', '1.1.2'
gem 'capybara-firebug','0.0.10'
gem 'selenium-webdriver', '2.14.0'
Thanks!
This is an old question but here goes. Create a before hook for the tag #disallow_cookies.
Before('#disallow_cookies') do
profile = Selenium::WebDriver::Firefox::Profile.new
profile['network.cookie.cookieBehavior'] = 2 # disables all kind of cookies
Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile)
end
Assuming that you are creating the driver in the general before hook you should be fine. I haven't run this code but it is where I would start the process.

ruby resque without loading rails environment

I have a resque worker which works great but is just too slow. The main reason for this is I'm using activerecord and having to load the entire environment which takes at least 10-20 seconds just to load up (I don't keep a running worker at all times as I'm using Heroku and pay for the time the worker runs). I'm using a resque worker to grab & parse data from an external website and then dumping the data into my database.
My question is whether I should rewrite the method to not use Rails and instead use DataMapper? Or something else which would load faster than activerecord.
Or If I should extract the code (using ActiveRecord) which figures out what to do with the external data and move it out of the worker and back into the app?
Hope that makes sense.
I have the same problem.
you could setup your environment on the rake resque:setup rake task
I tried this. assuming my rake resque task is on lib/tasks/resque.rake
require "resque/tasks"
task "resque:setup" do
root_path = "#{File.dirname(__FILE__)}/../.."
db_config = YAML::load(File.open(File.join(root_path,'config','database.yml')))["development"]
ActiveRecord::Base.establish_connection(db_config)
require "#{root_path}/app/workers/photo_downloader.rb" #workers
#Dir.glob("#{root_path}/app/models/*").each { |r| puts r; require r } #require all model
require "#{root_path}/app/models/photo.rb" # require model individually
end
I haven't completely success beacuse I use the Paperclip gem which require rails environment
Rails’ bootstrap is really slow; it is intended to be kept running, until certain time for restart (to eliminate some memory leaks there most likely is, any software is not bug-free), and is not intended to be used as a site that is launched for one request and then shut down.
That kind of usage more resembles a script. If you need to launch it with browser, you can easily use something like Erubis to write the page and use ActiveRecord in the script (I think it was useable outside of rails) or similar abstraction layer. Myself, for small tasks, I just use Mysql2.
Use bundler to get active_record and other gem to you without rails application .
require 'rubygems'
require 'logger'
require 'active_record'
require 'bundler'
require "active_support"
require "spreadsheet"
require 'net/ping'
require 'net/http'
Bundler.setup
Bundler.require(:default) if defined?(Bundler)
$config_logger = Logger.new("./log/dev.log")
class Dbconnect
def initialize
#settings = YAML.load_file('./config/database.yml')["development"]
#adapter = #settings["adapter"] if #settings["adapter"]
#database = #settings["database"] if #settings["database"]
#pool = #settings["pool"] if #settings["pool"]
#timeout = #settings["timeout"] if #settings["timeout"]
end
def connect_to_db
ActiveRecord::Base.establish_connection(
:adapter => #adapter,
:database => #database,
:reconnect => #reconnect,
:pool => #pool,
:timeout => #timeout)
$config_logger.info "\n db Connected: to => #{#database} "
end
end
end
}
Example Gemfile :
source "http://rubygems.org"
gem 'mail'
gem "escape_utils"
gem 'json',:require => "json"
gem 'json_pure'
gem 'resque'
gem 'resque-scheduler'
gem 'redis-namespace'
gem 'resque-status'
gem 'rake'
gem 'em-udns'
gem 'sqlite3'
gem 'spreadsheet'
gem 'activerecord', '3.2.1', :require => "active_record"
gem 'net-scp', :require => 'net/scp'
gem 'net-sftp', :require => 'net/sftp'
gem 'net-ssh', :require => 'net/ssh'
gem 'dir'
gem 'amatch'
gem 'haml'
gem 'net-ping'
gem install bundler
rest of the thing : bundle install .

Rails 3: Devise: No route matches "/"

I'm running into some issues when trying to add Devise to my Rails 3 app. I started by creating a new Rails 3 (rc2) app with a "Home" controller and "index" action and verified that "/" would render "#home/index". Next I set devise 1.1.1 in my Gemfile, installed Devise, created a User model, and migrated the database. Now "/" returns No route matches "/" and none of the Devise routes will work.
What is the fix for this?
Apparently the latest gem version (1.1.1) of Devise does not work with Rails 3.0.0rc2. You must use the latest version from github.
Modify your Gemfile from:
gem 'devise', '1.1.1'
To:
gem "devise", :git => "git://github.com/plataformatec/devise.git"

Rails3, Cucumber, Capybara, File upload => bad content body (EOFError)?

I'm having a hard time getting a simple file upload test working. I'm using Rails 3.0.0 on ruby 1.9.2 with Cucumber and Capybara.
View:
<%= form_tag "/upload/create", :multipart => true do %>
<label for="file">File to Upload:</label>
<%= file_field_tag "file" %>
<%= submit_tag "Upload" %>
<% end %>
Cucumber Step:
When /^I upload the basic file$/ do
visit path_to("upload")
path = File.join(::Rails.root, "somefile")
attach_file("file", path)
click_button("Upload")
end
In my controller, i have commented out everything except for:
def create
file = params[:file]
end
Gemfile snippet:
group :development, :test do
# testing with specs
gem "ZenTest", ">= 4.3.3"
gem "autotest"
gem "rspec-rails", ">= 2.0.0.beta.19", :git => "git://github.com/rspec/rspec-rails.git"
gem "rspec", :git => "git://github.com/rspec/rspec.git"
gem "rspec-core", :git => "git://github.com/rspec/rspec-core.git"
gem "rspec-expectations", :git => "git://github.com/rspec/rspec-expectations.git"
gem "rspec-mocks", :git => "git://github.com/rspec/rspec-mocks.git"
# cucumber stuff
gem 'capybara'
gem 'database_cleaner'
gem 'cucumber-rails'
gem 'cucumber'
gem 'spork'
gem 'launchy' # So you can do Then show me the page
gem 'escape_utils' # needed to fix Cucumber - http://crimpycode.brennonbortz.com/?p=42
end
When I try to run the test, I receive:
(::) failed steps (::)
bad content body (EOFError)
<internal:prelude>:10:in `synchronize'
I appreciate any help or insight. Thanks.
This turned out to be an issue with rack-test and it probably won't be a problem for most until more people adopt Rails3 and Ruby 1.9.x.
Upgrading rack-test to the current master branch fixed the problem.
I'm not sure when rack-test will include these changes in the gem.
See also:
groups.google.com/group/cukes/browse_thread/thread/5028306893c2c54a
I don't have an answer but working on the same problem in the same environ - cukes, capybara, rails 3, 1.9.2.... if I figure this out will let you know. Have you thought of posting on the cucumber google group or the Rails google group? If you don't once I get my act together and cant figure out will post to one of these.
Also, it seems that webrat has the method for attach_file() and thus when I generated cucumber without capybara it had a corollary method in web_steps.rb, but after I added capybara and regenerated cucumber it was gone....