ruby on rails - Refactoring Fat ActiveRecord Model, What Now? -


i've been using codeclimate improve code base , have model class falling down on 'definition outside of methods' , 'total overall complexity' don't use codeclimate definition outside of methods refers class definitions associations , validations. , total overall complexity says, ie non of individual methods complex overall class complex.

now appreciate false position, d class on score annoying , there may more can improve class, question is:

what if should simplify activerecord class?

my main problem i've exhausted written ways refactor fat model, using article http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ , similar, left with:

  • scopes x2
  • attr_accessible x2
  • carrierwave mount x1
  • validators x4
  • associations x16
  • accepts_nested_attributes_for x1
  • hooks eg after_save x3
  • methods calculations of various other attribute make views more semantic , easier understand
  • methods around associations, handling empty associations, merging associations

its closed source project, given lack of domain logic in class i've shared here. https://gist.github.com/msaspence/5317419

callbacks hell. mean, useful overload models.

do have action in controller handle unpublish thing?

if (which hope), moved / extracted service object:

after_save :email_author_if_first_published def email_author_if_first_published   if pending_was == true && pending == false   self.create_activity key: 'action.publish'   delay.send_action_published_email end end  def send_action_published_email   actionauthormailer.action_published(self, self.user).deliver end 

this should not belong model, presenter should better:

def mixpanel_data_points   {     campaign_id: campaign_id,     cause_id: cause_id   } end  def open_graph_type   'campaign_action' end  def facebook_invite_message   "please me raise awareness on patientscreate. it'll take 5 seconds , create genuine change. thanks" end  def percentage_completed   v = (participation_count.to_f/participation_target_count.to_f)*100   v > 100.0 ? 100.0 : v end 

this seems not good:

after_update :update_campaing_actions_count def update_campaing_actions_count   campaign.decrement_counter(:actions_count, self.campaign_id_was)   campaign.increment_counter(:actions_count, self.campaign_id) end 

because if campaign_id not changed


this looks bad:

def url   open_graph_url "/actions/#{id}" end 

can't tell more, don;t know sheer data or presentation in other methods


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -