oop - Using an object oriented approach in Ruby -


i have 2 classes, class a , class b.

i've noticed share lot of same code. example:

def viewable_by?(user)   super || clinic.has_staff_member?(user) || user.system_admin? || self.person == user.person end 

i want minimize code duplicated between classes. in refactoring, i've found of doesn't fit neatly 1 class falls cleanly in single responsibility principle. want put single module, methods have time formatting, viewing permissions, , few other things.

as see it, have few choices. (and bet can suggest others.) object oriented point of view, approach should go , why?

  1. use 1 single module shared between both of classes. may not have specific single responsibility, clean code significantly, , keeps in 1 place.
  2. make tiny classes , mix in both classes modules. have single responsibility, there many of them, of may have 1 method. seems waste.
  3. perhaps use presenter things time formatting, , permissions module shared between both classes. perhaps "cleaner," methods going everywhere.
  4. another possibility haven't yet considered?

edit

this question had mentioned clinic::appointment , clinic::visit classes, rather , b. answers may refer appointments , visits.

this neat question because deals in great way overall strucuture of project. understand appointment , visit separated things, , visit don't need linked appointment.

for authorization methods, viewable_by?, recommend move authorizations other place - might want check cancan structure, have worked many rails projects, , work application, coding authorization system yourself. in part, answer use (3).

however, since not code shared 2 classes authorization purposes, try classify set of methods, , give answer each class of methods think of. method classes have similar behavior try encapsulate in module , include (so (1), in smaller parts). example 1 module hasvisitors methods got_on_time? , was_conclusive? (well, maybe not best examples, it). when model has broader scope, authorization, present in of classes, time go (3).

i suggest stop , think again if should have visit class apart appointment , relationship, not now. after got @ home, have fun, take off head, think again next day.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -