- The following order is suggested: extends, includes, associations, validations, enumerations, scopes, class and object methods. Everything in alphabetical order.
belongs_to
associations come first, followed byhas_many
associations. Any other kind of association comes afterhas_many
. Break line between kinds of associations, but not between associations of the same kind.- Sentences like
accepts_nested_attributes_for
come after associations. - Validate associations first and attributes last. Break line between associations and attributes validations, but not between validations of the same kind. Enumerations are validated on theirs declarations.
- Always use
datetime
for date attributes, even if in the MER it's defined asDATE
.
- Prefer use of hash validations, like
validates :name, presence: true
, for presence, inclusion, etc. - Please, do not validate multiple attributes in the same line. Declare a
validates
sentence for each association or attribute. - When use custom validation, create a class in
app/validations
with custom validation and callvalidates_with
in model, see our example. - Validate object under the
foreign_key
. - Never validate presence of boolean, nor it should be tested. Use
validates :boolean_attr, inclusion: [true, false]
instead.
- Prefer scopes under class methods.
- When query is much complex, create a query class into
app/queries
- Keep simple methods in model, like update an attribute (without condition) and simple queries.
- Define
subject
as object of class. - Test associations, validations and attributes.
- Test class methods in
describe '.method'
. - Test instance method in
describe '#method'
. - Use shoulda-matchers.