My situation is a follows:
I'm running application X
X uses the gem "core"
core extends the models of X with models A, B, C, D
In development this works perfectly. However when I run
(bundle exec) rake RAILS_ENV=staging RAILS_GROUPS=assets assets:precompile
it fails on
** Execute environment
rake aborted!
uninitialized constant A
I tried to fix this issue by placing Rails.application.eager_load! before the Application.initialize! in environments.rb, but I'm afraid that only led to other errors.
Is there a way to include models from an engine in a gem BEFORE the assets:precompile?
I read something about requiring them one by one instead of eager_load all, but the path to the gem differs for every system.
engine.rb in "core":
require 'paperclip'
module Core
class Engine < ::Rails::Engine
config.time_zone = 'Amsterdam'
config.encoding = "utf-8"
config.autoload_paths += %W(#{config.root}/lib/**)
config.generators do |g|
g.test_framework :rspec, :views => false, :fixture => true
g.fixture_replacement :factory_girl, :dir => 'spec/factories'
end
initializer "core.load_app_instance_data" do |app|
Core.setup do |config|
config.app_root = app.root
end
app.class.configure do
#Pull in all the migrations from Commons to the application
config.paths['db/migrate'] += Core::Engine.paths['db/migrate'].existent
end
end
initializer "core.load_static_assets" do |app|
app.middleware.use ::ActionDispatch::Static, "#{root}/public"
end
end
end
I prefer to put any fix in the core gem instead of application X. However if it's not possible X is fine :)
Are you sure the "core" gem is loaded in the staging environment?
Related
When running rake spec on the command line for a large Rails project, I get a giant list of every rspec file that will be run.
Is there a way to hide that by default?
ruby-1.9.3-p448/bin/ruby -S rspec ./spec/acceptance/replicators/activity_replicator_spec.rb ./spec/acceptance/replicators/template_replicator_spec.rb ./spec/authorization_rules/admin_authorization_rules_spec.rb ...
When I run just rspec (no rake call) I don't get this console output.
EDIT 1
Working from phoet's answer, I tried
RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
t.warning = false
t.rcov = false
end
task :default => :spec
This did not solve the issue.
The last time I did this, I had to clear the rake task first.
if defined? RSpec
task(:spec).clear
RSpec::Core::RakeTask.new(:spec) do |t|
t.verbose = false
end
end
http://singlebrook.com/blog/disable-rspec-verbosity-to-hide-spec-list
You don't get such an output when calling rspec because the output comes from the RSpec::Core::RakeTask.
It's possible to configure this class and set the verbose flag:
RSpec::Core::RakeTask.new do |t|
t.verbose = false
end
I followed the railscasts about using carrierwave with fog with aws s3 to the T but it doesnt seem to work.
The Gemfile is as follows:
source 'https://rubygems.org'
ruby '1.9.3'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'pg'
gem 'pg_search'
gem 'devise'
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails", :git => 'git://github.com/seyhunak/twitter-bootstrap-rails.git'
gem "simple_form"
gem 'aws-sdk', '~> 1.8.1.2'
gem 'activerecord-reputation-system', require: 'reputation_system'
gem 'bootstrap-will_paginate'
gem 'carrierwave'
gem "rmagick"
gem "fog", "~> 1.3.1"
gem 'carrierwave-aws'
config/initializers/carrierwave.rb
require 'carrierwave'
CarrierWave.configure do |config|
config.fog_credentials = {
provider: "AWS",
aws_access_key_id: ENV["xxx"],
aws_secret_access_key: ENV["yyy"],
region: ENV['ap-southeast-1']
}
config.cache_dir = "#{Rails.root}/tmp/uploads"
config.fog_directory = ENV["ABC"]
end
The uploader:
class ImageUploader < CarrierWave::Uploader::Base
require 'carrierwave/processing/mime_types'
before :store, :remember_cache_id
after :store, :delete_tmp_dir
# Include RMagick or MiniMagick support:
include CarrierWave::RMagick
# include CarrierWave::MiniMagick
# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
include Sprockets::Helpers::RailsHelper
include Sprockets::Helpers::IsolatedHelper
# Choose what kind of storage to use for this uploader:
# storage :file
storage :fog
include CarrierWave::MimeTypes
process :set_content_type
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
version :thumb do
process :resize_to_limit => [200, 200]
end
# store! nil's the cache_id after it finishes so we need to remember it for deletion
def remember_cache_id(new_file)
#cache_id_was = cache_id
end
def delete_tmp_dir(new_file)
# make sure we don't delete other things accidentally by checking the name pattern
if #cache_id_was.present? && #cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/
FileUtils.rm_rf(File.join(root, cache_dir, #cache_id_was))
end
end
end
the controller:
class NotesController < ApplicationController
before_filter :authenticate_user!, only: [:create, :destroy]
before_filter :correct_user, only: :destroy
def index
end
def create
#note = current_user.notes.build(params[:note])
#notes = Note.paginate(page: params[:page])
if #note.save
flash[:success] = "Note uploaded!"
redirect_to root_url
else
flash.now[:error] = "Note failed to upload."
render 'static_pages/home'
end
end
def destroy
#note.destroy
flash[:success] = "Note deleted."
redirect_to root_url
end
private
def correct_user
#note = current_user.notes.find_by_id(params[:id])
redirect_to root_url if #note.nil?
end
end
I don't really know if the problem lies in the gem or the controller. Even the controller is based on the microposts controller in Michael Hartl's book "The Ruby on Rails Tutorial" so I don't know where I have gone wrong.
That's an awfully early version of fog. The latest is 1.12.1. Try updating to the latest release using the following entry in your Gemfile
gem "fog", "~> 1.12.1"
We create rails 3.2.9 engine with the command below:
rails plugin new my_eng --mountable --dummy-path=spec/dummy
in my_eng.gemspec, rspec was added:
s.add_development_dependency "rspec-rails", ">= 2.0.0"
Run bundle install. Then in engine's root directory:
rails g rspec:install
A spec_helper.rb file is created under spec/.
The problem is that when a model or controller is created,ex, rails g model my_model..., under spec/, there is no models directory and my_model_spec.rb were created. We tried to run rails g rspec:install under spec/dummy/ and the problem remains. What's wrong with our code? Thanks for the help.
You gotta insert in config/application.rb something like:
config.generators do |g|
g.template_engine :erb
g.test_framework :rspec, :fixture => true, :views => false
g.integration_tool :rspec, :fixture => true, :views => true
g.fixture_replacement :factory_girl, :dir => "spec/support/factories"
end
I added some confirmation dialog boxes for my Rails 3.1 application and, prior to that, their corresponding tests. Following the model of Railscast #257, I added ':js => true' to the test, added database_cleaner and modified the spec_helper.rb file accordingly.
When I run the test, Firefox launches, Capybara-Selenium fills in the fields the the appropriate username and a password, but log-in fails (i.e., "invalid username/password".) Other tests that do not have ':js => true' and also login, do still pass.
I would like to add more javascript to my application in the future and I am avoiding solutions that would hack Capybara to get this to work (e.g., click 'OK' on all dialogs.)
Any ideas what I might be missing? Fail that, any suggestions on how to debug this problem?
Thank you.
You should set use_transactional_fixtures = false for your seleniumtests.
This can be done in the spec_helper.rb with
config.use_transactional_fixtures = false
for all your tests.
Or do this for a single testcase:
describe 'testcase' , :type => :request do
self.use_transactional_fixtures = false
it 'your test', :js => :true do
testing...
end
end
This happens because selenium-tests access the database in a different way. With transactional fixtures enabled, selenium will work on an empty database -> your user does not exist and you cannot login.
For normal tests you should use transactional fixtures because your tests run much faster.
As per Avdi Grimm's post (features a detailed explanation):
Gemfile:
group :test do
gem 'database_cleaner'
end
spec_helper.rb:
config.use_transactional_fixtures = false
spec/support/database_cleaner.rb:
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
I had transactional fixtures disabled but adding the :js => true block to database_cleaner.rb did it for me.
I'm using the Paperclip gem with a Rails 3.1.1 app. It's working as advertised and expected. I would like to use the imagemagick -liquid-rescale delegate, however. According to the imagemagick documentation (which I tried in terminal.app), this works:
convert logo_trimmed.jpg -liquid-rescale 75x100%\! logo_lqr.jpg
I tried a variation...
convert my_pic.jpg -liquid-rescale 60x60\! my_new_pic.jpg
That worked as expected, too. I've tried several permutations in my Image model in my rails app, but I cannot get Paperclip to invoke liquid-rescale. My latest attempt was:
has_attached_file :pic, :styles => {:square => "-liquid-rescale 60x60\!" }
This fails without an error message, merely duplicating the original image with a new name.
How do I instruct paperclip to invoke liquid-rescale?
Here's what I did. I had to write a custom processor, which I named liquid.In the model:
has_attached_file :pic, :styles => {:square => {:processors =>[:liquid],:geometry => "60x60>"} }
I'm not sure whether the :geometry option is necessary, but I added it because thumbnail.rb in the paperclip gem says that it's not optional.
I then added a file: /my_app/lib/paperclip_processors/liquid.rb with contents:
module Paperclip
class Liquid < Thumbnail
def transformation_command
"-resize '60x60>' -liquid-rescale '60x60!'"
end
end
end
Finally, I ran the following in the console:
Image.all.each {|i| i.pic.reprocess!}
That did the trick.