Carrierwave & sideliq & file size validator. Error: already initialized constant FileSizeValidator - carrierwave

I'm using carrierwave and file_size_validator
and I'm using sidekiq.
When I run "bundle exec sidekiq " in console
I see this errors
/lib/file_size_validator.rb:5: warning: already initialized constant FileSizeValidator::MESSAGES
lib/file_size_validator.rb:5: warning: previous definition of MESSAGES was here
lib/file_size_validator.rb:6: warning: already initialized constant FileSizeValidator::CHECKS
lib/file_size_validator.rb:6: warning: previous definition of CHECKS was here
lib/file_size_validator.rb:8: warning: already initialized constant FileSizeValidator::DEFAULT_TOKENIZER
lib/file_size_validator.rb:8: warning: previous definition of DEFAULT_TOKENIZER was here
lib/file_size_validator.rb:9: warning: already initialized constant FileSizeValidator::RESERVED_OPTIONS
/lib/file_size_validator.rb:9: warning: previous definition of RESERVED_OPTIONS was here

problem solved
I remove string
require 'file_size_validator'
from models
because in my application.rb file
config.autoload_paths += %W(#{config.root}/lib)

Related

How to configure datadog for Rails to avoid "The called method `initialize' is defined her" deprecation warning?

I’m using Ruby 2.7, Rail 6.2 and data dog (ddtrace) gem v 0.54.1. I have this config set up
# config/initializers/datadog-tracer.rb
Datadog.configure do |c|
c.tracer enabled: APP_CONFIG[:datadog][Rails.env][:enabled] && !Rails.env.test?
c.service = ‘myapp’
c.env = APP_CONFIG[:datadog][Rails.env][:env]
c.use :rails, log_injection: true
end
When I run rspec tests, I notice these deprecation warnings …
/Users/my.user/.rvm/gems/ruby-2.7.1/gems/ddtrace-0.54.1/lib/ddtrace/configuration/agent_settings_resolver.rb:84: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/Users/my.user/.rvm/gems/ruby-2.7.1/gems/ddtrace-0.54.1/lib/ddtrace/configuration/agent_settings_resolver.rb:32: warning: The called method `initialize' is defined here
I’m not clear what these refer to or if its anything I can control or configure but eager to fix them.

Rspec uninitialized constant for class nested inside module - Ruby on Rails

In a Rails project, I have created a new class inside the lib directory, this class is namespaced inside a module. When creating a spec file for it, I'm seeing NameError: uninitialized constant MyNamespace.
Here is my folder structure
app/
...
lib
my_namespace
my_new_class.rb
another_namespace
another_old_class.rb
spec
lib
my_namespace
my_new_class_spec.rb
another_namespace
another_old_class_spec.rb
Here the (abbreviated) contents of:
lib/my_namespace/my_new_class.rb
module MyNamespace
class MyNewClass
end
end
spec/lib/my_namespace/my_new_class_spec.rb
RSpec.describe MyNamespace::MyNewClass do
it "is true"
expect(true).to eq(true) # irrelevant at this point
end
end
The reason I included another_old_class_spec.rb is that its tests run without issue and I can't find anywhere that it's explicitly loaded or required in the test setup (in case that might be a potential issue).
When running the test with bundle exec rspec spec/lib/my_namespace/my_new_class_spec.rb or even bundle exec rspec spec/lib/my_namespace/ I get
An error occurred while loading ./spec/lib/my_namespace/my_new_class_spec.rb
Failure/Error:
RSpec.describe MyNamespace::MyNewClass do
NameError:
uninitialized constant MyNamespace
Like Georgiy Melnikov implied in his comment, by default the /lib directory is not in autoload paths, so the constant MyNamespace is not automatically resolved.
You basically have two options to fix this:
explicitly require the file with require lib/my_namespace/my_new_class at the top of the spec file
add lib/ to autoload paths (nowadays this is discouraged)

Why am I getting a WARNING: Skipping key error?

I use figaro and have a config/application.yml file. In it, I specify the development as such:
development:
FACEBOOK_SECRET: '***'
FACEBOOK_KEY: '***'
PARSE_APP_ID: '***'
PARSE_API_KEY: '***'
I use the parse-ruby-client gem to make push notifications to parse. It's configured in config/initializers/parse.rb
require 'parse-ruby-client'
Parse.init :application_id => ENV['PARSE_APP_ID'],
:api_key => ENV['PARSE_API_KEY'],
:quiet => false
When I start my server or my sidekiq worker, I get this warning:
WARNING: Skipping key "PARSE_APP_ID". Already set in ENV.
WARNING: Skipping key "PARSE_API_KEY". Already set in ENV.
But I don't get that warning for the FACEBOOK_SECRET or FACEBOOK_KEY. Also, I read about the error and still don't quite understand.
How is PARSE_APP_ID and PARSE_API_KEY already set in ENV? When and where is this set?
Googling for "warning skipping key already set in env" does not tell me much. The first github link talks about how to remove the warning. I feel that if I set things up correctly, there should not be a warning
I know what my problem is. I was setting the variables in my bash_profile. After I removed it and move it to application.yml, source ~/.bash_profile does not reset the ENV variable. You have to start a new shell.

Can't localize line error when compiling assets

I'm getting an error when pushing to heroku when compiling assets...
I've revised every single CSS in order to localize any error but nothing...
The thing is
it's telling me the line of the compiled file, but I need to know the line number of the error before compiling... Any ideas of how can I fix this? Thanks!
The error when pushing:
Running: rake assets:precompile
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
I, [2014-06-27T11:43:08.802691 #1716] INFO -- : Writing /tmp/build_6b3b977d-af90-4ecf-9f85-03999c81089c/public/assets/application-ccf6af846f884c868c81901ada8cf44b.js
rake aborted!
Invalid CSS after " color: #333;}": expected "}", was ""
(in /tmp/build_6b3b977d-af90-4ecf-9f85-03999c81089c/app/assets/stylesheets/application.css)
(sass):6836

Rails APP_PATH's Test dependency

I have the following test:
def test_to_show_warnings_if_app_path_is_defined
require File.dirname(__FILE__) + '/../lib/macros.rb'
warnings = $stderr.string.split(/\n/)
warnings.each { |w| assert_no_match(/warning: already initialized constant/, w) }
end
It should fail if there are warnings about already initialized constants. On the other side inside the required file I have the following problem:
MYPATH = ""
MYPATH = File.expand_path(File.split(APP_PATH)[0] + "/macros", __FILE__) if defined?(APP_PATH)
It checks if the APP_PATH is defined and saves on MYPATH. Clearly if APP_PATH is defined it will overwrite the constant. I can't correct the code above unless the test capture this warning. What should I do?
Remember that APP_PATH is defined when the rails server is running as you can see here in section 1.3 script/rails

Resources