Ruby NoMethodError in Redmine from script/runner - ruby

I have some Ruby code I need to execute in the Redmine Ruby app in order to enable a module in all projects at once.
Surprisingly, ruby does access the Projects, but raises a NoMethodError when accessing a few particular methods inside each of the "Project" objects.
Here is the code:
Project.find(:all).each do |project|
print "Enabling modules for project '#{project.identifier}' ... "
puts project.methods.sort # this does print "enabled_module_names"
puts project.enabled_module_names
end
This fails with:
hostname:/srv/apps/redmine# script/runner vendor/plugins/customplugin/lib/enable_modules.rb
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in `method_missing': undefined method `enabled_module_names' for #<Project:0x7f28985c1cb0> (NoMethodError)
from vendor/plugins/customplugin/lib/enable_modules.rb:14
from vendor/plugins/customplugin/lib/enable_modules.rb:7:in `each'
from vendor/plugins/customplugin/lib/enable_modules.rb:7
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:46
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/runner:3
I have scratched my head a lot, but I can't grasp why would the code find the "Project" symbol but not the methods within, especially as "project.methods" does indeed list "enabled_module_names".
Any help most welcome.

Are you sure that enable_module_names is an instance method?
Is it within Project.instance_methods?
Edit (summary of the comments below):
In earlier versions, you have to use following:
enabled_module_names = project.enabled_modules.collect(&:name)
The getter is present in later versions only (see rev.4460 for details of this change)

Related

Ruby load does not see static methods in the class

I am experimenting with ruby (2.4.1p111) and with the load method and it does have some strange behavior:
I have two files:
mytest.rb:
class MyClass
def self.greet(param)
puts "Got called: #{param}"
end
greet 'Called locally'
load "./testld.rb"
end
and the loaded file:
testld.rb:
greet 'Called by load'
I understood from documentation that the loaded code from testld.rb and the method call right in the MyClass should behave the same. Instead, I am getting:
-bash-4.2$ ruby mytest.rb
Got called: Called locally
/Blacksmith/RB/testld.rb:1:in `<top (required)>': undefined method `greet'
for main:Object (NoMethodError)
from mytest.rb:9:in `load'
from mytest.rb:9:in `<class:MyClass>'
from mytest.rb:1:in `<main>'
Any idea what I do wrong?
You need to call MyClass.greet in testld.rb, that is because greet is a class method defined in the class MyClass.
Here is documentation for class methods.
Wherever you load a file, the loaded file is always evaluated within the main environment. The value of self in the main environment of testld.rb is the main environment of the entire script. Hence, your
greet 'Called by load'
is not equivalent to
MyClass.greet 'Called by load'
as you expected.

Using Rspec should receive

I am following a tutorial and have been getting some errors, I believe some of the language is outdated with Rspec and have messed around alot to try and fix this but have not been able to.
The error i get is
NoMethodError:
undefined method `should_recieve' for #<Panda:0x007f9cd45c6458>
# ./spec/zookeeper_spec.rb:9:in `block (2 levels) in <top (required)>'
The code in question is
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
panda.should_recieve(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I have realised that should_recieve should actually be should_receive and this has resolved the problem. I then replaced should_receive with and expectation and to receive
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
expect(panda).to receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end

`<top (required)>': undefined method `“raf_ebooks' for main:Object (NoMethodError) -

When trying to run my twitter_ebooks bot using ruby run.rb I get this error:
Faraday::Builder is now Faraday::RackBuilder.
/Users/Rafi/Desktop/my_ebooks/bots.rb:14:in `<top (required)>': undefined method `“raf_ebooks' for main:Object (NoMethodError)
from run.rb:33:in `require_relative'
from run.rb:33:in `<main>'
Here is the git repo, of the bot I'm using, if that helps: https://github.com/mispy/twitter_ebooks
Here is what is inside the bots.rb file (I have removed the auth tokens and consumer key etc for security): http://pastebin.com/gYqcMYhM
What does this error mean and how can I fix it? Thanks.
EDIT: I tried running it again after restarting got pretty much the same error except instead of my_ebooks/bots.rb:14 it says my_ebooks/bots.rb:13
Faraday::Builder is now Faraday::RackBuilder.
/Users/Rafi/Desktop/my_ebooks/bots.rb:13:in `<top (required)>': undefined local variable or method `“raf_ebooks”' for main:Object (NameError)
from run.rb:33:in `require_relative'
from run.rb:33:in `<main>'
You have curly quotation marks that Ruby doesn't pick up. This happens when you use word processors that try to get fancy.
TWITTER_USERNAME = “raf_ebooks" # Ebooks account username
TEXT_MODEL_NAME = “raf_ebooks" # This should be the name of the text model
Notice the broken syntax highlighting. They should be "normal" straight quotes:
TWITTER_USERNAME = "raf_ebooks" # Ebooks account username
TEXT_MODEL_NAME = "raf_ebooks" # This should be the name of the text model
Much better.
Get yourself a decent editor. TextEdit is not suitable for coding. TextMate and Sublime Text are popular commercial editors. GitHub's Atom is a free editor, pretty nice too.
The opening quote of the TWITTER_USERNAME value isn't a "regular" quote, it's some sort of smart quote. Hence Ruby isn't recognising it as a string, and is getting rather confused.

rspec Cannot load such file after working the first time

When running through the rspec course in codeschool I keep running into the same problem. I will set up as requested and after creating zombie_spec.rb and running rspec I get the proper output listed below:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
Run options: include {:focus=>true}
All examples were filtered out; ignoring {:focus=>true}
*
Pending:
A Zombie is named Ash
# Not yet implemented
# ./spec/lib/zombie_spec.rb:3
Finished in 0.00929 seconds
1 example, 0 failures, 1 pending
Randomized with seed 7259
As I continue on with the first video and create the class Zombie as mentioned I receive this error when running rspec again:
Justins-MacBook-Pro:rubyproject Justin$ rspec spec/lib/zombie_spec.rb
/usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- zombie (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p392/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/Justin/rubyproject/spec/lib/zombie_spec.rb:2:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/configuration.rb:819:in `load_spec_files'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:22:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/rvm/gems/ruby-1.9.3-p392/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
After this I removed everything and uninstalled and reinstalled rspec. Retried it again and returned the same results.
Any clue what is going on?
Thank you in advance for your help!
require_relative will load the file from the same directory as the rspec file
I had pretty much the same issue, though mine never worked even once at first. Updating the zombie_spec.rb file to show the full path of my zombie.rb file seemed to get it working properly.
Eg:require "/home/me/ruby/spec/lib/zombie"
I use this and it worked
require_relative "zombie.rb"
Well if this still a problem I got into the same issue so I created a folder under spec/lib where I put all the codes and another spec/test where goes all the tests then it worked.
and I also added require_relative here is the code snippet
zombie_spec.rb
require 'spec_helper'
require_relative '../lib/zombie'
describe Zombie do
it "has a name called'Jack'" do
zb = Zombie.new
zb.name.should == "Jack"
end
it "has no brains" do
zb = Zombie.new
zb.should be_intelligent == false
end
end
zombie.rb
class Zombie
attr_accessor :name
def initialize
#name = "Jack"
end
def intelligent?
false
end
end

How to set the path to the classes being tested with Cucumber?

I've tried following two written guides and one youtube video (http://www.youtube.com/watch?v=8ZmawOCDeyo) and they either skip over an important step (or it just works for them) but not for me.
I have created the following folder structure:
RubyBots\
features\
step_definitions\
creating_a_vehicle_for_an_ai_bot_steps.rb
support\
env.rb
creating_a_vehicle_for_an_ai_bot.feature
vehicle.rb
vehicle_factory.rb
Contents of creating_a_vehicle_for_an_ai_bot_steps.rb:
Given /^I have vehicle factory$/ do
#vehicle_factory = VehicleFactory.new
end
When /^I use it to build a basic vehicle$/ do
#new_vehicle = #vehicle_factory.build
end
Then /^I should have receive a basic vehicle$/ do
#test #new_vechile
end
Contents of vehicle_factory.rb:
class VehicleFactory
def initialize(name)
p name
end
end
Contents of env.rb file:
require 'vehicle_factory'
require 'vehicle'
I execute Cucumber (with no parameters) from inside a CMD window inside the RubyBots directory and I receive the following error:
no such file to load -- vehicle_factory (LoadError)
<internal:lib/rubygems/custom_require>:29:in `require'
<internal:lib/rubygems/custom_require>:29:in `require'
D:/Code/RubyBots/features/support/env.rb:1:in `<top (required)>'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/rb_support/rb_language.rb:143:in `load'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/rb_support/rb_language.rb:143:in `load_code_file'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:176:in `load_file'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:78:in `block in load_files!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:77:in `each'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime/support_code.rb:77:in `load_files!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime.rb:137:in `load_step_definitions'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/runtime.rb:39:in `run!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/cli/main.rb:43:in `execute!'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/lib/cucumber/cli/main.rb:20:in `execute'
C:/Ruby192/lib/ruby/gems/1.9.1/gems/cucumber-1.0.1/bin/cucumber:14:in `<top (required)>'
C:/Ruby192/bin/cucumber:19:in `load'
C:/Ruby192/bin/cucumber:19:in `<main>'
If I delete the env.rb file from the support directory I get the follow error when running Cucumber:
Scenario: Using a vehicle factory to build a basic vehicle # Features\creating_a_vehicle_for_an_ai_bot.feature:6
Given I have vehicle factory # Features/creating_a_vehicle_for_an_ai_bot_steps.rb:1
uninitialized constant Object::VehicleFactory (NameError)
./Features/creating_a_vehicle_for_an_ai_bot_steps.rb:2:in `/^I have vehicle factory$/'
Features\creating_a_vehicle_for_an_ai_bot.feature:7:in `Given I have vehicle factory'
When I use it to build a basic vehicle # Features/creating_a_vehicle_for_an_ai_bot_steps.rb:5
Then I should have receive a basic vehicle # Features/creating_a_vehicle_for_an_ai_bot_steps.rb:9
Failing Scenarios:
cucumber Features\creating_a_vehicle_for_an_ai_bot.feature:6 # Scenario: Using a vehicle factory to build a basic vehicle
I've tried everything I can think of and my google-fu is failing me - I hope it's something non-trivial so I'm not embarrassed, but easy to fix, so that I can move on! :)
Thanks for the help!
Your class is called VehicleFactory but you're trying to call vehicle_factory.new in creating_a_vehicle_for_an_ai_bot_steps.rb.
Also, I believe Cucumber expects a particular directory structure:
RubyBots/
features/
step_definitions/
creating_a_vehicle_for_an_ai_bot_steps.rb
support/
env.rb
creating_a_vehicle_for_an_ai_bot.feature
You'll need to require your own files from env.rb appropriately. Ruby doesn't work out any paths for you. One way of auto-requiring files based on symbol name (the way Rails does it) is described in my blog post (it relies on ActiveSupport 2.3 but should be similar for the current version).

Resources