I want to use ActiveModel::Validations in a small Roda app, and I really want to keep it small and I would like to avoid loading the entirety of ActiveModel in it. Is there a way to require only ActiveModel::Validations ?
At least with activemodel-4.2.6. an attempt to require 'active_model/validations' results with
/home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations/format.rb:4:in `<module:Validations>': uninitialized constant ActiveModel::Validations::EachValidator (NameError)
from /home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations/format.rb:3:in `<module:ActiveModel>'
from /home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations/format.rb:1:in `<top (required)>'
from /home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations.rb:405:in `require'
from /home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations.rb:405:in `block in <top (required)>'
from /home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations.rb:405:in `each'
from /home/bbozo/.rvm/gems/ruby-2.2.4/gems/activemodel-4.2.6/lib/active_model/validations.rb:405:in `<top (required)>'
Hmff, OK, writing this question I found the solution ^_^
I managed to load "only" ActiveModel validations and it's dependencies, I ended up requiring
require 'active_support/concern'
require 'active_model/validator'
require 'active_model/validations'
require 'active_model/naming'
require 'active_model/callbacks'
require 'active_support/callbacks'
require 'active_model/translation
just to make this pass:
class Foo
include ActiveModel::Validations
end
so it seems I'll be requiring the entire active model or find an alternative validations framework.
require 'active_model'
class Account
include ActiveModel::Validations
end
Related
Background - in a Rails app, I'm seeing a bug in production where a file cannot find a class from the standard library; but in development, everything works fine. Presumably, some gem that I have in the development group but not the production group is requiring the necessary library, so the symbol is define for development but not for production.
Is there any way I can get Ruby to tell me where require was called for a given file?
(I'm deliberately not naming the offending library, because I don't want suggestions as to what might be requiring it; I want to know how I can find out myself)
To answer your direct question: patch/override the require method and get access to all the info you need.
app/config/environment.rb
# set up intrumentation
module RequireSpy
def require(*args)
puts "requiring #{args.join(', ')} from #{caller.first}"
super
end
end
Object.include(RequireSpy)
# init your rails app as usual
require File.expand_path('../application', __FILE__)
Rails.application.initialize!
Upon booting the rails app, it'll print a LOT of output:
requiring tilt from /Users/sergio/.gem/ruby/2.4.2/gems/sinatra-1.4.8/lib/sinatra/base.rb:3:in `<top (required)>'
requiring rack/protection from /Users/sergio/.gem/ruby/2.4.2/gems/sinatra-1.4.8/lib/sinatra/base.rb:4:in `<top (required)>'
requiring thread from /Users/sergio/.gem/ruby/2.4.2/gems/sinatra-1.4.8/lib/sinatra/base.rb:7:in `<top (required)>'
requiring time from /Users/sergio/.gem/ruby/2.4.2/gems/sinatra-1.4.8/lib/sinatra/base.rb:8:in `<top (required)>'
requiring uri from /Users/sergio/.gem/ruby/2.4.2/gems/sinatra-1.4.8/lib/sinatra/base.rb:9:in `<top (required)>'
You probably want to filter that.
I am creating some automated tests using Cucumber and Capybara. I want to add the Touch Action gem (https://github.com/Ricardonacif/touch_action). I added the gem and then in my env file required it but i am getting the following error. I am really confused as to whether i should be creating a separate helper file but i tried something along these lines and still got the same error. Could anyone offer any advice as to how to resolve this?
uninitialized constant RSpec (NameError)
/Users/em/.rvm/gems/ruby-2.1.1/gems/touch_action-1.3.0/lib/touch_action/capybara_rspec_helper.rb:17:in `<top (required)>'
/Users/em/.rvm/gems/ruby-2.1.1/gems/touch_action-1.3.0/lib/touch_action.rb:12:in `require'
/Users/em/.rvm/gems/ruby-2.1.1/gems/touch_action-1.3.0/lib/touch_action.rb:12:in `<top (required)>'
/Users/em/reallyenglish/learning_specs/learning-platform-specs/features/support/env.rb:3:in `require'
/Users/em/reallyenglish/learning_specs/learning-platform-specs/features/support/env.rb:3:in `<top (required)>'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/rb_support/rb_language.rb:94:in `load'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/rb_support/rb_language.rb:94:in `load_code_file'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/runtime/support_code.rb:237:in `load_file'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/runtime/support_code.rb:97:in `block in load_files!'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/runtime/support_code.rb:96:in `each'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/runtime/support_code.rb:96:in `load_files!'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/runtime.rb:242:in `load_step_definitions'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/runtime.rb:65:in `run!'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/lib/cucumber/cli/main.rb:38:in `execute!'
/Users/em/.rvm/gems/ruby-2.1.1/gems/cucumber-2.0.0/bin/cucumber:9:in `<top (required)>'
/Users/em/.rvm/gems/ruby-2.1.1/bin/cucumber:23:in `load'
/Users/em/.rvm/gems/ruby-2.1.1/bin/cucumber:23:in `<main>'
/Users/em/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `eval'
/Users/em/.rvm/gems/ruby-2.1.1/bin/ruby_executable_hooks:15:in `<main>'
Gemfile
source "https://rubygems.org"
gem 'rake'
gem 'touch_action'
group(:test) do
gem 'cucumber'
gem 'capybara'
gem 'rspec'
gem 'selenium-webdriver'
end
env.rb
require 'capybara'
require 'capybara/cucumber'
require 'touch_action'
Capybara.configure do |config|
config.default_selector = :css
config.default_driver = :selenium
config.app_host = 'http://testem.co.uk
# Capybara.ignore_hidden_elements = false --video testing
config.include Capybara::DSL
end
After do
page.execute_script("window.localStorage.clear()")
end
After our chat discussion, it seems that touch-action needs rspec to be explicitly required. Your env.rb file should look like this:
require 'rspec'
require 'capybara'
require 'capybara/cucumber'
require 'touch_action'
This is the error I get when I run any rake command: undefined method 'desc' for Sinatra::Application:Class
# app.rb
require 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/contrib'
get '/' do
puts "Hello World"
end
# config.ru
require "./app"
run Sinatra::Application
# Rakefile
require './app'
require 'sinatra/activerecord/rake'
# Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
gem 'activerecord', '~> 4.0.2'
gem 'sinatra', '~> 1.4.4'
gem 'sinatra-activerecord', '~> 1.2.3'
gem 'sinatra-contrib', '~> 1.4.2'
Full trace:
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:269:in `method_missing'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-activerecord-1.2.3/lib/sinatra/activerecord/tasks.rake:4:in `block in <top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:126:in `class_eval'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:126:in `block in new'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:118:in `initialize'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:118:in `new'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:118:in `new'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-contrib-1.4.2/lib/sinatra/namespace.rb:142:in `namespace'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-1.4.4/lib/sinatra/base.rb:1972:in `block (2 levels) in delegate'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-activerecord-1.2.3/lib/sinatra/activerecord/tasks.rake:3:in `<top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `block in load'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:223:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/sinatra-activerecord-1.2.3/lib/sinatra/activerecord/rake.rb:77:in `<top (required)>'
/Users/j/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
/Users/j/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_require.rb:51:in `require'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/backports-3.3.5/lib/backports/tools.rb:328:in `require_with_backports'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `block in require'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:214:in `load_dependency'
/Users/j/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.2/lib/active_support/dependencies.rb:229:in `require'
/Users/j/Desktop/app/Rakefile:2:in `<top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/rake_module.rb:25:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/rake_module.rb:25:in `load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:637:in `raw_load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:94:in `block in load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:93:in `load_rakefile'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:77:in `block in run'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:165:in `standard_exception_handling'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/lib/rake/application.rb:75:in `run'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/gems/rake-10.1.0/bin/rake:33:in `<top (required)>'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/bin/rake:23:in `load'
/Users/j/.rvm/gems/ruby-2.0.0-p247#global/bin/rake:23:in `<main>'
/Users/j/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `eval'
/Users/j/.rvm/gems/ruby-2.0.0-p247/bin/ruby_executable_hooks:15:in `<main>'
Found an easiest solution:
Juste add the require:false attribute to sinatra contrib in your gemfile:
gem "sinatra-contrib",require: false
Found this here:
http://aaronlerch.github.io/blog/sinatra-bundler-and-the-global-namespace/
The Sinatra namespace extension from Sinatra contrib is interfering with Rake’s own namespace support. They both define a namespace method, and the Sinatra contrib version is being called (incorrectly) from the Sinatra-ActiveRecord Rake tasks.
If you’re not using the namespaces from Sinatra-contrib, then the easiest solution would be to only require those extensions that you need; e.g. change
require 'sinatra/contrib'
to
require 'sinatra/whatever'
require 'sinatra/anotherextension'
If you are using Sinatra namespaces then I think you may be able to get round this by moving to a modular style app. Change your app.rb to something like
require 'sinatra/base' # note this has changed from just 'sinatra'
require 'sinatra/activerecord'
require 'sinatra/contrib'
class MyApp < Sinatra::Base
register Sinatra::Contrib
get '/' do
"Hello World"
end
# other routes etc. as needed
end
Then in your config.ru you need run MyApp rather then run Sinatra::Application (of course you can – and should – give your class a better name). This avoids the collision of the two namespace methods, since the Sinatra version is only available in your application class, not the top level.
This is my Rakefile
require 'bundler'
Bundler.setup
require 'active_record'
require 'sqlite3'
require 'yaml'
require 'logger'
task :migrate => :environment do
ActiveRecord::Migrator.migrate('db/migrate', ENV["VERSION"] ? ENV["VERSION"].to_i : nil )
end
task :environment do
ActiveRecord::Base.establish_connection(YAML::load(File.open('config/database.yaml'))['development'])
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
When I execute the task I got this error:
rake aborted!
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:2449: warning: already initialized constant Class::VALID_FIND_OPTIONS
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:2449: warning: previous definition of VALID_FIND_OPTIONS was here
undefined method `alias_method_chain' for #<Class:0x00000001606340>
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:2002:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/validations.rb:387:in `block in included'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/validations.rb:386:in `class_eval'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/validations.rb:386:in `included'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3210:in `include'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3210:in `block in <module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3208:in `class_eval'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:3208:in `<module:ActiveRecord>'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/activerecord-2.3.18/lib/active_record/base.rb:5:in `<top (required)>'
/home/marco/desenv/technical_analysis/Rakefile:14:in `block in <top (required)>'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/rake-10.0.4/lib/rake/task.rb:246:in `call'
/usr/local/rvm/gems/ruby-2.0.0-p0#ta/gems/rake-10.0.4/lib/rake/task.rb:246:in `block in execute'
I had some debug and some test... Then i moved to 3.2.13 version of active record, and all worked as expected. I didn't find any docs for 3.2.18 version...
I don't mind to use 3.2.13 version, but I got curious about that.
First of all, you were using ActiveRecord 2.3.18, not 3.2.18 (the latter of which does not exist, to my knowledge). ActiveRecord 2.3 is getting to be quite old, and I don't believe it's compatible with Ruby 2.0, which you're using.
But the central issue, I think, is that in Rails 2.2-2.3, alias_method_chain was moved to the ActiveSupport module, before being moved back to Module in 3.0. You're not including ActiveSupport in your task, and I think that's what was causing the problem.
So I think a quick fix would be just to require "active_support".
I'm trying to create Ruby script to run with my Rails 3 environment.
However, whenever I run this rubyscript with Rails Runner, I get this error:
require 'rubygems'
require 'json'
#payload = {
"message" => "helloworld",
"station" => {"id"=>12345}
}.to_json
puts #payload
ERROR:
/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require': no such file to load -- json (LoadError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in `load_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:554:in `new_constants_in'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in `load_dependency'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require'
from createflags.rb:2
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39:in `eval'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands/runner.rb:47
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39:in `require'
from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39
from script/rails:6:in `require'
from script/rails:6
Can someone please tell me what I can do to fix this?
I'm not totally familiar with the internals of Bundler, but it may be that it doesn't allow requireing other gems not in the Gemfile. Maybe adding gem 'json' would take care of this.
Of course, ActiveSupport has its own JSON module, so there should be no need to pull in another gem.
remove require "json" and then try. I believe ActiveSupport already has to_json in built.