Hy. I'm new to Ruby on Rails and OOP.
I work on a little Scheduler, and want to DRY my Model Methods.
I red some about usage of Module and Class in Rails, but can't find out what is the best way.
difference-between-a-class-and-a-module
ruby-class-module-mixins
Example:
Supposed i have 2 Models ( Datum and Person ).
Each Model has an Attribute which store's a Date, but with different Attribute Name's.
I wrote the same Method for Date Validation in both Module's.
My Models:
class Datum < ActiveRecord::Base
attr :start_date
def validate_date
# same validation stuff with self.start_at
end
end
class Person < ActiveRecord::Base
attr :birth_date
def validate_date
# same validation stuff with self.birth_date
end
end
Here is my attempt with a lib/ModelHelper and Datum Model:
class Datum < ActiveRecord::Base
include ModelHelper
attr_accessible :start_at
# Validations
before_validation :validate_date, :start_at
end
module ModelHelper
private
def validate_date *var
# validation stuff with birth_date and start_at
end
end
Question:
In my case, i think i need to assign a Parameter ( for each Model attribute, :start_at and :bith_date ).
But i can't discover how.
What is the best way to DRY my Models, with Module or Class ?
Why and How?