Jump to content

The ultimate community for Ruby on Rails developers.


Photo

DRYing up my controllers

DRY

  • Please log in to reply
3 replies to this topic

#1 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 09 June 2014 - 05:32 PM

My two controllers
https://github.com/N...e_controller.rb
https://github.com/N...e_controller.rb

Look almost identical, what's the best way to DRY this up, or is it best I leave things as they are?



#2 Ohm

Ohm

    Driver

  • Moderators
  • 416 posts
  • LocationCopenhagen

Posted 09 June 2014 - 07:54 PM   Best Answer

Those two links are the same, of course they look identical  :P

 

I think you mean the people and the thing controllers, right? Those look pretty similar, but I wouldn't do any DRYing it up on those two. I believe that you need to be able to read the files individually as well, and the only thing to do here would be to create a third controller, that the two first could inherit from.


Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 NerdcoreSteve

NerdcoreSteve

    Signalman

  • Members
  • 25 posts

Posted 09 June 2014 - 11:27 PM

Whoops. :) Yes I meant the person and thing controllers. :)

 

That advice seems reasonable. Readability aside, it would also mean more work! I have other stuff I want to do to get this thing usable. I might still revisit this at a later date after I've learned more.



#4 ccmcbeck

ccmcbeck

    Passenger

  • Members
  • 1 posts

Posted 10 February 2015 - 06:56 PM

What if you are using transactions in your controller actions?  When I delete a Person, I want to log an Activity so I wrap both of those in a transaction:

person.transaction do
  person.destroy!
  user.track person, :destroy
end

I believe it's a best practice to user the bang! methods in a transaction.  But what if an exception is raised like ActiveRecord::RecordNotDestroyed or in my case a custom Person::DeleteLastAdmin < StandardError exception?

 

Should the controller use a begin..rescue construct like:

begin
  # do transaction
  # report success for each format
rescue StandardError
  # report failure for each format
end





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users