vendredi 3 février 2017

Multiple controllers with redundant action logic in rails

I have 3 controllers. Two returning specialized items (Article, Announcement), and one returning either.

GET api/announcements/1 -- produces Announcement json GET api/articles/2 -- produces Article json GET api/posts/1 -- produces Announcement json GET api/posts/2 -- produces Article json

the show method for the ArticlesController contains logic that would have to be duplicated in the PostsController. More specifically:

def show
  deal_with_params(...)
  authorize!(...)
  render json: @resource
end

After I fetch the resource by unique_id, I know it's type and can branch out from there, but I only want to authorize and do some other operations on resources of type Article. Anyone have recommendations, patterns, or ideas that would help to make sure that changes to that ArticlesController#show would not have to be repeated in PostsController?

Aucun commentaire:

Enregistrer un commentaire