Use an object from outside your modules scope - ruby

I have code like this.
class User < ActiveRecord::Base
end
module Foo
class User
end
end
module Foo
class DoesSomethingWithActiveRecordUser
def initialize user_id
User.find(user_id)
end
end
end
If I call Foo::DoesSomethingWithActiveRecordUser.new(1) I get an error message that says something like undefined method 'find' for Foo::User.
How do I call the ActiveRecord User from within Foo?
Thanks.

Like this:
::User.find(user_id)

Related

Define instance method of a class after class already defined in ruby

I have some problem with extending class with instance method after separate module is included into separate class
module ActsAsCommentable
def self.included(commentable)
Thread.class_eval do
def commentable
p "disqusable is #{commentable}"
p "disqusable class is #{commentable}"
end
end
end
end
class Thread
#some code...
end
class Asset
include ActsAsCommentable
end
And now I want to call this method somelike this:
thread = Thread.new
thread.commentable
The problem is, of course is that there is no binding with include method for class eval, and I could save variables that I want to pass into class eval in ActsAsCommentable module, but I dont want to. Is there a better way?
I tried to do instead
module ActsAsCommentable
def self.included(commentable)
class << Thread
define_method :commentable do
p "disqusable is #{commentable}"
p "disqusable class is #{commentable}"
end
end
end
end
But As I guessed this creates instance method for singletone object of class and therefore I can call it only through
Thread.commentable
And again, no binding...
If I understand you correctly, you need to be able to access the commentable variable inside your Thread extension, right?
If so, just change this:
Thread.class_eval do
To this:
Thread.class_exec(commentable) do |commentable|
And it should work.

Can not add ActiveRecord callback by using class_eval method

I have a User model, which includes module Staff:
class User < ActiveRecord::Base
include Staff
...
end
I want to add an after_update callback to all the models that include this module:
module Staff
def self.included(model)
model.class_eval do
after_update :callback
end
end
private
def callback
...
end
end
I get a NoMethodError:
undefined method `after_update' for Object:Class
What am I doing wrong?
Solved!
The problem was that I had another class including my module Staff, which was not an ActiveRecord model.

Rails: class methods helpers from Lib

Sorry the title may not be very clear. Basically I have a wrapper for the Split gem called ABFeature in lib/ab_feature/ab_feature.rb
In my view I want to be able to call my helpers like this:
ABFeature.current_settings
But this is not working, here is the error I have:
undefined local variable or method `session' for ABFeature:Module
session is a method from ActionController and is seems I can't access it...
Here is my code:
require 'split'
module ABFeature
class << self
include Split::Helper
def current_settings
...
end
end
end
class ActionController::Base
ActionController::Base.send :extend, ABFeature
end
Any idea?,
Greg
I'm not sure what the result should be but if you want the current_settings method available in the controller I think you can do
module ABFeature
include Split::Helper
def current_settings
end
end
and then
class ApplicationController < ActionController::Base
include ABFeature
end
I think you usually call helpers as instance methods. Then they should share the context with the controller.

How to determine table name within a Rails 3 model class

I want to get table name in a model method. I found there should be method table_name but when I try to call it I get NameError Exception: undefined local variable or method `table_name'. It is obviously not there:
pp methods.grep(/^ta.*/)
["table_name_prefix?",
"table_name_suffix?",
"taint",
"taguri",
"taguri=",
"tainted?",
"table_name_prefix",
"table_name_suffix",
"tap"]
How to get a "real" table name (no lowecase - pluralize tricks)?
Thanks
But I need that information in the
model's instance method. How to get
it?
You can simply do this in your instance method:
class Model
def instance_method
puts Model.table_name
end
end
Found it.
It's a class method. Its not so obvious from the Rails 3 documentation.
self.class.table_name
If you are in a class method of the class you want the table name for, try:
class Model < ActiveRecord::Base
def self.class_method
puts self.table_name
end
end
If you try using
self.class.table_name
you'll run into a NoMethodError: undefined method 'table_name' for Class:Class

How do I access the string name of the parent class from a class method in a extended class or included module

My problem basically looks like this:
module Foo
class Bar
def self.who
self.class.to_s
end
end
end
class World < Foo::Bar
end
When I call World.who I don't get "World" as a result, I get "Class". Some quick Googling didn't yield anything useful, so hence I'm here hoping someone will know how to get the correct class name :)
If you're calling foo.bar then inside the bar method the value of self will be foo. So when you call World.who the value of self inside who is World. Since World is a class, World.class will return Class, so that's what you get.
To get back "World" just call self.to_s or self.name (or just to_s or name).
You get that because World is a Class. In ruby, AClass.class != AClass. So, you could use this:
module Foo
class Bar
def self.who
to_s
end
end
end
class World < Foo::Bar
end

Resources