Working with modules to group certain methods. Currently I have this
set up and it works:
**# app/models/a.rb**
class A
has_history
end
**# lib/history.rb**
module History
module Model
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def has_history(options = {})
send :include, InstanceMethods
end
end
module InstanceMethods
# methods ...
end
end
end
**# config/application.rb**
config.autoload_paths += %W(#{config.root}/lib)
**# config/environment.rb**
ActiveSupport.on_load(:active_record) do
include History::Model
end
This works, the methods are mixed into the models.
In a similar way, I wanted to package another set of methods with a
different purpose in the same application and I followed the same
approach:
**# lib/history.rb**
module Fleet
module Model
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def has_fleet(options = {})
send :include, InstanceMethods
end
end
module InstanceMethods
# methods ...
end
end
end
**# config/environment.rb**
ActiveSupport.on_load(:active_record) do
include History::Model
include Fleet::Model
end
But, I don''t think the module Fleet::Model is loaded and throws up
errors when I do this in a model which is supposed to include this
module:
**# app/models/b.rb**
class B
has_fleet
end
I am unable to get around this and not sure what is going wrong here.
Throws up the following errors:
syed@rails:~/work/projects/mapunity/pinpoint$ rails c
/home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.7/lib/
active_record/base.rb:1009:in `method_missing'': undefined local
variable or method `has_fleet'' for #<Class:0x9c2f200> (NameError)
from /home/syed/work/projects/mapunity/pinpoint/app/models/
account.rb:2:in `<class:Account>''
from /home/syed/work/projects/mapunity/pinpoint/app/models/
account.rb:1:in `<top (required)>''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:454:in `load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:454:in `block in load_file''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:596:in `new_constants_in''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:453:in `load_file''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:340:in `require_or_load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:491:in `load_missing_constant''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:183:in `block in const_missing''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:181:in `each''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:181:in `const_missing''
from /home/syed/work/projects/mapunity/pinpoint/app/admin/
accounts.rb:1:in `<top (required)>''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:235:in `load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:235:in `block in load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:225:in `block in load_dependency''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:596:in `new_constants_in''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:225:in `load_dependency''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:235:in `load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activeadmin-0.3.2/
lib/active_admin/application.rb:132:in `block in load!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activeadmin-0.3.2/
lib/active_admin/application.rb:132:in `each''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activeadmin-0.3.2/
lib/active_admin/application.rb:132:in `load!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activeadmin-0.3.2/
lib/active_admin/application.rb:155:in `routes''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activeadmin-0.3.2/
lib/active_admin.rb:63:in `routes''
from /home/syed/work/projects/mapunity/pinpoint/config/routes.rb:
2:in `block in <top (required)>''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/routing/route_set.rb:233:in `instance_exec''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/routing/route_set.rb:233:in `draw''
from /home/syed/work/projects/mapunity/pinpoint/config/routes.rb:
1:in `<top (required)>''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:235:in `load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:235:in `block in load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:225:in `block in load_dependency''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:596:in `new_constants_in''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:225:in `load_dependency''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:235:in `load''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:127:in `block in reload_routes!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:127:in `each''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:127:in `reload_routes!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activeadmin-0.3.2/
lib/active_admin/reloader.rb:17:in `block in attach!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/callbacks.rb:420:in `_run_prepare_callbacks''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/callbacks.rb:40:in `initialize''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/stack.rb:33:in `new''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/stack.rb:33:in `build''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/stack.rb:79:in `block in build''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/stack.rb:79:in `each''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/stack.rb:79:in `inject''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/actionpack-3.0.7/lib/
action_dispatch/middleware/stack.rb:79:in `build''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:162:in `app''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application/finisher.rb:35:in `block in <module:Finisher>''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/initializable.rb:25:in `instance_exec''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/initializable.rb:25:in `run''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/initializable.rb:50:in `block in run_initializers''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/initializable.rb:49:in `each''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/initializable.rb:49:in `run_initializers''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:134:in `initialize!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:77:in `method_missing''
from /home/syed/work/projects/mapunity/pinpoint/config/
environment.rb:5:in `<top (required)>''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:239:in `require''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:239:in `block in require''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:225:in `block in load_dependency''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:596:in `new_constants_in''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:225:in `load_dependency''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.7/
lib/active_support/dependencies.rb:239:in `require''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/application.rb:103:in `require_environment!''
from /home/syed/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.7/lib/
rails/commands.rb:22:in `<top (required)>''
from script/rails:6:in `require''
from script/rails:6:in `<main>''
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.