How to not require "rails/all"? - ruby-on-rails

> CUT_OFF=10 bundle exec derailed bundle:mem
TOP: 129.6563 MiB
rails/all: 58.1406 MiB
action_mailbox/engine: 32.1719 MiB
I would like to not require action_mailbox/engine but it's required by rails/all
I'm not sure which gem it is.
I tried:
def require(file)
puts "#{file} => #{caller.first}"
super
end
But nothing shows up

It was weird that just by looking at Gemfile rails/all somehow was getting loaded. Sometimes just searching helps:
$ ag "rails/all" `bundle show --paths`
/home/alex/.rbenv/versions/3.1.2/lib/ruby/gems/3.1.0/gems/derailed_benchmarks-2.1.2/bin/derailed
90: require 'rails/all'
# ... 2 more results from `.tt` files
To answer your question, you can split rails gem and use what you need. I've answered that before:
https://stackoverflow.com/a/71993557/207090
Now in config/application.rb you can replace:
require "rails/all"
with requires in all.rb from railties gem and remove what you don't need:
https://github.com/rails/rails/blob/v7.0.4/railties/lib/rails/all.rb
you don't really have to, since missing files get rescued and app will boot with no issues. Any references/configs for mailbox have to be removed.
# config/application.rb
# require "rails/all"
require "rails"
%w(
active_record/railtie
active_storage/engine
action_controller/railtie
action_view/railtie
active_job/railtie
action_cable/engine
action_text/engine
rails/test_unit/railtie
).each do |railtie|
# action_mailer/railtie
# action_mailbox/engine
begin
require railtie
rescue LoadError
end
end

Related

How to prevent RSpec from running specs twice in Rails plugin with dummy app?

I'm writing a Rails extension. To test it, I use RSpec. In order to make models to test the plugin on, I use a pregenerated dummy app:
rails plugin new yaffle --dummy-path=spec/dummy --skip-test --full
I have a few tests. When I call them, they run twice.
> # I have 4 tests in app total
> rspec
> 8 examples, 0 failures
> rspec spec/yaffle/active_record/acts_as_yaffle_spec.rb
> 4 examples, 0 failures
> rspec spec/yaffle/active_record/acts_as_yaffle_spec.rb:4
> 2 examples, 0 failures
This is how my files look like:
# lib/yaffle.rb
Gem.find_files('yaffle/**/*.rb').each { |path| require path }
module Yaffle
# Your code goes here...
end
# spec/spec_helper.rb
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../dummy/config/environment", __FILE__)
RSpec.configure do |config|
config.example_status_persistence_file_path = '.rspec_status'
config.disable_monkey_patching!
end
# spec/dummy/config/environment.rb
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
# spec/yaffle/active_record/acts_as_yaffle_spec.rb
require "spec_helper"
RSpec.describe "Acts as yaffle" do
def test_a_hickwalls_yaffle_text_field_should_be_last_squawk
assert_equal "last_squawk", Hickwall.yaffle_text_field
end
def test_a_wickwalls_yaffle_text_field_should_be_last_tweet
assert_equal "last_tweet", Wickwall.yaffle_text_field
end
end
# spec/dummy/config/application.rb
require_relative 'boot'
require "rails/all"
Bundler.require(*Rails.groups)
require "yaffle"
module Dummy
class Application < Rails::Application
config.load_defaults 6.0
end
end
Also, I noticed that only files with require "spec_helper" are duplicated
So, what am I doing wrong? And, if it's a bug, how to work around it?
The cause
It was caused by this line:
Gem.find_files('yaffle/**/*.rb').each { |path| require path }
Apparently, Gem.find_files gets all files in gem directory that match that pattern - not only the files relative to gem root. So, yaffle/**/*.rb means both files in <GEM_ROOT>/lib/yaffle/... and <GEM_ROOT>/spec/lib/yaffle/....
https://apidock.com/ruby/v1_9_3_392/Gem/find_files/class
Fix
I fixed it by requiring all files explicitly:
require 'lib/yaffle/active_record/acts_as_yaffle'
require 'lib/yaffle/active_record/has_fleas'
It's also possible to just require all files from that directory:
Dir["lib/yaffle/active_record/**/*.rb"].each {|file| require file }

`require': cannot load such file -- ./controllers/<controller> (LoadError) with Rspec

I'm trying to run a test file for a controller, but I'm running into this error:
/home/mariana/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require': cannot load such file -- ./controllers/users_controller
(LoadError)
I think rspec can't find the actual files it's supposed to test.
The test file, users_controller_spec.rb:
require 'rails_helper'
require './controllers/users_controller'
describe UsersController do
describe '#create' do
...
end
end
The header/preamble of rails_helper.rb, that deals with file paths (I think):
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
The section of the Gemfile that deals with rspec and development/test-specific stuff:
group :development, :test do
gem 'spring'
gem 'rspec', '~>3.5'
gem 'rspec-rails', '~>3.5'
end
The project tree:
->project
->app
->controllers
->users_controller.rb
->spec
->controllers
->users_controller_spec.rb
->rails_helper.rb
->spec_helper.rb
I've tried messing around with the require File.expand_path(...) line on rails_helper.rb. I also tried adding the rspec gem to the Gemfile alongside rspec-rails (the problem also existed when it was just rspec-rails).
Any suggestions?
I solved it by changing the path of the controller in the test from this:
require './controllers/users_controller.rb'
To this:
require './app/controllers/users_controller.rb'.
I already suspected it was a file path error, so I just tried different relative paths until one of them worked.

Getting started with Nokogiri in Rails? No need for gem 'nokogiri'?

I'm just looking into Nokogiri and was thinking about using it in my app, but apparently when I do bundle install (without gem 'nokogiri') it's already "Using nokogiri 1.6.7.1".
When I add gem 'nokogiri' in my Gemfile, there's no "installing..." So, is nokogiri already pre installed in Rails? If so, do I still have to require these:
require 'nokogiri'
require 'open-uri'
Where do I put this? Within my controller? or application.rb?
This is my application.rb looks like
require File.expand_path('../boot', __FILE__)
require 'rails/all'
Bundler.require(*Rails.groups)
if Rails.env.test? || Rails.env.development?
CONFIG = YAML.load(File.read(File.expand_path('../aws.yml', __FILE__)))
CONFIG.merge! CONFIG.fetch(Rails.env, {})
CONFIG.symbolize_keys!
end
module App
class Application < Rails::Application
config.middleware.use Rack::Pjax
config.active_record.raise_in_transactional_callbacks = true
config.active_job.queue_adapter = :sidekiq
end
end
Nokogiri is required by another gem (rails-dom-testing). So it's already installed.
And you don't need to write require 'nokogiri' statement. Because Rails uses Bundler to manage dependencies and load gems. Nokogiri will be already loaded.

How is this an un-initialized constant. (Mixins in rails)

under lib/ I have 'aisis_writer/loader.rb' which, inside that file, looks like:
module AisisWriter
module Loader
end
end
I then have, in my application.rb the following set up:
require File.expand_path('../boot', __FILE__)
# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)
module AisisWriter
class Application < Rails::Application
# Load Lib
config.autoload_paths += %W(#{config.root}/lib)
# Use Rack Attack For Throttling
config.middleware.use Rack::Attack
end
end
From there I did, in the ApplicationController.rb: include AisisWriter::Loader and then I ran my tests and got:
'<class:ApplicationController>': uninitialized constant AisisWriter::Loader (NameError)
Either I cannot do what I am doing because of naming conflicts or I am doing something wrong. Any one care to tell me what I might be doing wrong?
I don't think your config.autoload_paths is broad enough -- it's not including subfiles of the lib directory.
This should do the trick:
config.autoload_paths += Dir[Rails.root.join('lib', '{**/}')]
Try defining like this within 'aisis_writer/loader.rb'
module AisisWriter::Loader
end

Fedena Error in Boot.rb

/home/palpandi/.rvm/gems/ruby-1.8.7-p374#fedena_zip/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning:
Gem::Dependency#version_requirements is deprecated and will be removed
on or after August 2010. Use #requirement
/home/palpandi/.rvm/gems/ruby-1.8.7-p374#fedena_zip/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:105:in
`const_missing': uninitialized constant Rails::Boot::Bundler
(NameError)
Using Rails 2.3.5
Ruby 1.8.7
ubuntu 12.04
I had a similar problem. The correct way to solve this is to go to your project folder, then in config/boot.rb go to the very bottom, and just before the Rails.boot! line add the following :
begin
require "rubygems"
require "bundler"
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." + "Did you run bundle install?"
end
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
#bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
This will solve the "uninitialized constant Authorization" error.
put this line in your boot.rb
begin
require "rubygems"
require "bundler"
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." + "Did you run bundle install?"
end
or
gem install bundler
and after adding:
gem 'bundler'
in line 2 in config/boot.rb (just after require 'rubygems')

Resources