Ruby on Rails Thursday, July 2, 2015

On 2 July 2015 at 12:04, Federicko <fedomain@gmail.com> wrote:
> Oh yeah, like this?
>
> In controller:
>
> def rank
> @this_article = Article.find(params[:id])
>
> if (params[:rank] == 'up')
> @this_article.rankup
> else
> @this_article.rankdown
> end
>
> redirect_to articles_path()
> end
>
> In the model:
>
> def rankup()
>
> @affected_article = Article.find_by(ranking: ranking.to_i-1)
> self.ranking = self.ranking - 1
> @affected_article.ranking = @affected_article.ranking + 1
>
> Article.transaction do
> self.save
> @affected_article.save
> end
> end
>
> def rankdown()
>
> @affected_article = Article.find_by(ranking: ranking.to_i+1)
>
> self.ranking = self.ranking + 1
> @affected_article.ranking = @affected_article.ranking - 1
>
> Article.transaction do
> self.save
> @affected_article.save
> end
> end

Too much repeated code
How about
def rankup
@affected_article = Article.find_by(ranking: ranking.to_i-1)
adjust_ranks this, @affected_article
end

etc.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLuUF8k71m4Ju7uykn4T9JGTPFX0BsoCt6vC2CpzEPzK6g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment