Rails public_activity gem rspec setting error - ruby-on-rails

I am integrating public_activity (version 1.5) gem to my Rails 4 app.
Trying to set up tests as specified in their wiki, I write the following:
#spec_helper.rb
require 'public_activity/testing'
PublicActivity.enabled = false
However, trying to run my specs I get the following error:
/my_app/spec/spec_helper.rb:24:in <top (required)>': undefined methodenabled=' for PublicActivity:Module (NoMethodError)
Looking at Public Activity module source code I can clearly see enable= method there.
Can you please advise me what am I doing wrong here?

Looking at the source, testing.rb does not require PublicActivity where enabled= is defined, so I believe you'll need to do
require 'public_activity'
require 'public_activity/testing'
like it's done in their test_helper.rb.

The documentation seems to be incorrect.
I was able to get it to work like this:
PublicActivity::Config.instance.enabled = false
Update: Jan Klimo's answer is the correct way.

Related

ApiAuth gem + ActiveResource

I'm trying to get ApiAuth working with ActiveResource and having no luck. The documentation suggests this as the way to use the gem:
class Foo < ActiveResource::Base
with_api_auth("foo", "bar")
end
This results in the following error:
NoMethodError: undefined method `with_api_auth' for Foo:Class
I know that the api_auth library is available because when I do
require 'api_auth'
i get "false", which I believe means that the library/gem was already loaded.
Additionally, I picked out the module/class where with_api_auth is defined and don't get an error:
2.3.8 :004 >
ApiAuth::Rails::ActiveResourceExtension::ActiveResourceApiAuth
=> ApiAuth::Rails::ActiveResourceExtension::ActiveResourceApiAuth
2.3.8 :005 >
I found a couple issues for this exact error on the api_auth github project, but neither had solutions that worked for me.
Anyone else see this error or know how to eliminate it?
So in the end it was an ordering of gems in my Gemfile that made the difference. It ended up being an ordering issue in my Gemfile. I found an issue (113) on the gem github issues list that said to make sure the order was right by doing:
gem 'activeresource'
gem 'api-auth'
Originally this hadn't worked and it ended up being because you no longer have to explicitly put activeresource in your Gemfile. So I moved gem 'api-auth' the last line in my Gemfile and everything worked.
I don't know for sure, but I think it has to do with how mixins are loaded on bundle install. "think" being the most important word in that statement.

How to use rails api find_or_create_by in ruby?

I tried to run test a gem for rails.
In the test, I am in ruby not rails.
The purpose of this code is to create rails modules.
But this kind of codes will be a template supposed to run in rails.
Problem come when test encountered something like
my_module= Myprogram::Module.find_or_create_by
:module_code=>my_module.code,
:code=> scode,
:name=>sname
then
error: Undefined method find_or_create_by
My first though is to require (what) gem that has this method.
Can anyone help me out.?
Myprogram::Module is mongoid class using namespace to that class.
I fixed the error by.
require proper gem which is mongoid (thanks #marmeladze and mikwat)
require_relative to the class (without this will show NameError:
uninitialized constant Myprogram::Module::Mongoid
In my case here the code:
.
.
require 'mongoid'
require_relative '../lib/generators/myprogram/templates/app/models/myprogram/module.rb'

Can't create users in rails when using authlogic because of cookies

So, I have a standard rails 3 app with authlogic. If I'm not in the browser (in the console or in the test environment), I can't create user models.
For example:
I have this code either in a rspec test or in my console:
user = User.create(...user attributes...)
And I get this error:
NoMethodError: undefined method `cookies' for main:Object
I've looked all over the internet and can't figure this out. Any help would be greatly appreciated.
EDIT:
So, I looked around some more and found in the documentation to do this:
include Authlogic::TestCase
but then I get this error:
uninitialized constant Authlogic::TestCase
I had this same exact problem. Where in your application did you put the following line:
Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)
I ran into this error when I placed the line in either the environment.rb file or within a file within my initializer folder.
I eliminated the problem when I placed the line in my ApplicationController instead.

"undefined method `fixtures' for ActiveSupport::TestCase:Class" while testing with Rails 3.1

I'm writing tests for a Gem and after a couple of hours trying to get rid of this, decided to look around - and to my surprise, there's not a single reference to it on Google (apart from one on mongoid, where people simply ignored it).
So, the problem is simple: I have this block on my test initialization:
class ActiveSupport::TestCase
fixtures :all
end
but the tests fail to execute:
`<class:TestCase>': undefined method `fixtures' for ActiveSupport::TestCase:Class (NoMethodError)
The gem depends on Rails 3 and every dependency is checked & double-checked. The code is on github, in case anyone wants to check (https://github.com/herval/acts_as_recommendable)
I'm out of ideas. Anyone share a light?
I'm not sure, but it seems that fixtures cannot be used with mongoid.
Take a look at this for further details:
https://groups.google.com/forum/?fromgroups=#!topic/mongoid/tqlx3j88Lqw
It's been a long time since I used Rails' built in testing, so take this with a grain of salt. I'm guessing that the actual ActiveSupport::TestCase class hasn't actually been loaded before the initializer is being reached.
It might be enough to just add require 'test_help' at the top of the initializer.

uninitialized constant ActiveSupport::SecureRandom

I'm having this strange error for the devise_invitable extension:
uninitialized constant ActiveSupport::SecureRandom
But the strange thing is that I don't know how to load that module anyway, like if in my console I execute ActiveSupport, thats fine and responds with true but not that SecureRandom class, or ActiveSupport::SecureRandom, and like I know its part of ActiveRecord, it's in the docs here: http://api.rubyonrails.org/classes/ActiveSupport/SecureRandom.html
How would you start troubleshooting an issue like this?
More Details
So it seems the class SecureRandom works as is, but not when called as part of ActiveSupport like ActiveSupport::SecureRandom, why would this be?
I fixed this by switching to the master branch of Devise on my 3-1-stable Rails application.
gem 'devise', :git => "git://github.com/plataformatec/devise"
I ran into this issue with the activeadmin gem and solved it with a hack at the top of devise.rb
ActiveSupport::SecureRandom = SecureRandom
Source: http://coderwall.com/p/fttpra

Resources