Dex
Dex

Reputation: 12759

Rails 3 Admin Section Design Question About Inheritance and Namespacing

The way my site operates, some records are created by the user but are only partially filled out. An administrator has to complete some of the record fields. I was thinking about putting all the validations related to the administrative fields in a subclass.

For example, in /app/models/document.rb:

class Document < ActiveRecord::Base
  # minimal validations needed, etc
end

In /app/models/admin/document.rb (I'm not even sure if the syntax below is valid Ruby)

class Admin::Document < Document
  # Extra validations for the fields the admin
end

Would my approach be a bad idea? I also plan on having role based authentications using something like CanCan.

Upvotes: 0

Views: 436

Answers (2)

Andy Lindeman
Andy Lindeman

Reputation: 12165

I think it might be better to keep all of the validations in one class, and have a boolean database column that stores if the record has been completed by the administrator.

class Document < ActiveRecord::Base
  attr_protected :completed_by_administrator # do not allow this to be set via mass assignment
  validate ..., :if => :completed_by_administrator # only do these validates if completed_by_administrator is true
end

When the record is first created, completed_by_administrator is false and the admin-only validations do not run. When the record is completed by an administrator, set completed_by_administrator = true in your controller and the admin-only validations automatically run before the record is saved.

Upvotes: 1

karlfreeman
karlfreeman

Reputation: 588

Perhaps an alternative route would be to use CanCan to allow admins to complete a users document but instead of subclassing the record or splitting it over two. When the form is submitted add an administrated ( or approved ) field then if its one admin or several you can search by the unadministrated( or unapproved )

Surely at the end of the day the admin does the final submission so they can then set the administrated ( or approved ) to true. Thereby finalizing the document?

Upvotes: 2

Related Questions