Any help with refactory this ugly code?

16 views
Skip to first unread message

Darek F.

unread,
Jan 20, 2012, 8:29:33 AM1/20/12
to rubyonra...@googlegroups.com
https://gist.github.com/1644085

#verifications/index.haml
- if Ad.unverified_ads.size == 0
- else
= link_to "Fast Verify",
fast_verify_info_verification_path(Ad.unverified_ads.last), :method =>
:put
= link_to "Normal Verify", verify_info_verification_path(ad), :method =>
:put

#verifications/verify_info.haml
= link_to "Verify", verify_verification_path(@ad)

#verifications/fast_verify_info.haml
= link_to "Verify",
fast_verify_verification_path(Ad.unverified_ads.last), :method => :put

#verification_controller.rb
def verify_info
@ad = Ad.find(params[:id])
end

def fast_verify_info
@ad = Ad.find(params[:id])
end

def fast_verify
@ad = Ad.find(params[:id])
@ad.verify!
if Ad.unverified_ads.size == 0
redirect_to verifications_path
else
redirect_to
fast_verify_info_verification_path(Ad.unverified_ads.last)
end
end

def verify
@ad = Ad.find(params[:id])
@ad.verify!
if params[:fast] == true
redirect_to Ad.unverified_ads.last
else
redirect_to verifications_path
end
end

I think there is too many actions, how to improve this?

--
Posted via http://www.ruby-forum.com/.

Michael Pavling

unread,
Jan 20, 2012, 9:22:18 AM1/20/12
to rubyonra...@googlegroups.com
Try replacing the redundant bits one at a time. For instance:

Ad.unverified_ads.size == 0
is equivalent to
Ad.unverified_ads.blank?

And you're repeating "if...else" a lot with just one line in each, so
turn it into a ternary:

if Ad.unverified_ads.size == 0
redirect_to verifications_path
else
redirect_to fast_verify_info_verification_path(Ad.unverified_ads.last)
end

becomes

redirect_to (Ad.unverified_ads.blank? ? verifications_path :
fast_verify_info_verification_path(Ad.unverified_ads.last))

regedarek

unread,
Jan 20, 2012, 4:21:31 PM1/20/12
to rubyonra...@googlegroups.com
Thanks a lot, this my amendments.
https://gist.github.com/1643261

Darek F.

unread,
Jan 20, 2012, 5:21:36 PM1/20/12
to rubyonra...@googlegroups.com
Thanks a lot.

I maked some improvments =>

--
Posted via http://www.ruby-forum.com/.

Reply all
Reply to author
Forward
0 new messages