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 methodhas_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:inblock 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:inload_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:inload_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:ineach’
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:inblock 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:innew_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:inload’
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:ineach’
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:inroutes’
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:inblock 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:indraw’
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:inload’
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:inblock 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:inload_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:inblock 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:inreload_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:innew’
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:inblock 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:ininject’
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:inapp’
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:ininstance_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:inblock 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:inrun_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:inmethod_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:inrequire’
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:inblock 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:inload_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:inrequire_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:inrequire’
from script/rails:6:in `’