Refactoring module

Dear all

Please see the following module, In module SX3 SX4 and SX5 have similar class Tasklist, but inherit from different class.

I will use the following code to connect to different data sources RemoteSX3Model.establish_connection sx3_hash RemoteSX4Model.establish_connection sx4_hash RemoteSX5Model.establish_connection sx5_hash

How can I refactor my code in module to look simpler? Thank you very much

you could create an abstract model RemoteModel from which RemoteSX3/4/5Model inherit. then you can move the methods self.find_incompete_tasklist and readonly? to that newly created model. that would shorten your code a lot. and copy/paste-errors like self.find_incompete_tasklist instead of self.find_incomplete_tasklist won't happen.

class GenericModel < ActiveRecord::Base   # GenericModel implements many methods that a specific model class can then   # override in its own definition if needed.   self.abstract_class = true

  def some_method(some_param = nil)     # this defined the "default" behavior   end end

class Project < GenericModel   # if this doesnt declare some_method, it uses the method in   # GenericModel end

class Scenario < GenericModel   def some_method(some_param = 'sam')     # scenario uses this method, not the one from GenericModel   end end

class Testcase < GenericModel   # uses the GenericModel version of some_method end

NOTE: You can also do this with controllers... I have many in my app that are little more than:

class ProjectsController < GenericController   def action1_specific_to_this_model   end

  def action2_specific_to_this_model   end end

GenericController does all the standard stuff (index, edit, show, save, update, delete) by looking at the params[:controller] and doing things like

@object = params[:controller].singularize.camelcase.constantize.find(params[:id])

Thanks for your reply

Refer to my module. Each module inherit from difference class.

if I use A general class, GeneralTasklist. i.e. class GeneralTasklist < ActiveRecord::Base   def method_1   end   def method_2   end end

How can I preserve the super class as RemoteSX3Model...and so on? Because I have to use the following code to establish connection to difference data servers. RemoteSX3Model.establish_connection sx3_hash RemoteSX4Model.establish_connection sx4_hash RemoteSX5Model.establish_connection sx5_hash

Any other better solution??

module SX3   class Tasklist < RemoteSX3Model   end end

module SX4   class Tasklist < RemoteSX4Model   end end

module SX5   class Tasklist < RemoteSX5Model   end end

Thanks again Valentino

Ar Chron wrote:

RemoteModel is declared an abstract_class. When RemoteSX3Model (or 4 or 5 - your 'concrete' classes) inherits from RemoteModel, those classes don't need to define the methods already present in RemoteModel (unless they are overriding the definition), and you get a single source for your method code that is shared among the concrete models.

Thanks for your help.

Sorry that I am a green programmer and not really familiar with OO, I am confused now. My understanding is that

class GenericModel < ActiveRecord::Base   self.abstract_class = true   def method_1   end   def method_2(a=10)     a + 10   end   def method_3   end end

class RemoteSX3Model < GenericModel end

class RemoteSX4Model < GenericModel end

class RemoteSX5Model < GenericModel end

For module SX3, is the class Tasklist, Request and Dictionary can call method_1-3?

module SX3   class Tasklist < RemoteSX3Model   end

  class Request < RemoteSX3Model   end

  class Dictionary < RemoteSX3Model   end end

For module SX4, is the class Tasklist can only call method_2 defined in GenericModel, but not method_1 and method_2?

module SX4   class Tasklist < RemoteSX4Model      def method_2      end   end end

For module SX5, is the class Tasklist can only call method_2, and the return value is 110?

module SX5   class Tasklist < RemoteSX5Model      def method_2(a=100)      end   end end

Million thanks Valentino

(Without repeating the example definitions from the previous post)

For module SX3, is the class Tasklist, Request and Dictionary can call method_1-3?

The inheritance hierarchy for Tasklist is:

Tasklist < RemoteSX3Model < GenericModel < ActiveRecord::Base

This class can use methods defined in Tasklist (its self), RemoteSX3Model, GenericModel, and ActiveRecord::Base. If the same method is defined in more than one of these classes, Tasklist will use the "closest" definition it finds, tracing from Tasklist to ActiveRecord::Base. The same logic holds for Request, and Dictionary.

For module SX4, is the class Tasklist can only call method_2 defined in GenericModel, but not method_1 and method_2?

Nope, Tasklist from SX4 will use its "method_2" (which takes no parameters), but method_1 and method_3 are still available via the Tasklist < RemoteSX4 < GenericModel path.

For module SX5, is the class Tasklist can only call method_2, and the return value is 110?

Tasklist from SX5 can still employ method_1 and method_3, but will use its definition of method_2, with a different default for a.

you can implement as many classes as you like. if you have something that is common to all of your Tasklist-classes you can of course create class GenericTasklist that holds the common code and form which Tasklist inherits.

you should take a look into a book that explains OOP though. without that knowledge, you will get stuck over and over again.