* API Articles v0-v1 restructure * Remove unused helper * Bulk move API controllers into concerns + add V1 controllers * Extract API routes + some fixes * Fix v1 api_controller authenticate! + add more article_controller specs * Completed spec/requests/api/v1/articles_spec.rb * specs up to listings * All v1 specs except for 9 skips * mime_types cleanup + authenticate! relocation Co-authored-by: Fernando Valverde <fernando@visualcosita.com>
43 lines
1.3 KiB
Ruby
43 lines
1.3 KiB
Ruby
module Api
|
|
module ReadinglistController
|
|
extend ActiveSupport::Concern
|
|
|
|
INDEX_REACTIONS_ATTRIBUTES_FOR_SERIALIZATION = %i[id reactable_id created_at status].freeze
|
|
private_constant :INDEX_REACTIONS_ATTRIBUTES_FOR_SERIALIZATION
|
|
|
|
PER_PAGE_MAX = 100
|
|
private_constant :PER_PAGE_MAX
|
|
|
|
def index
|
|
per_page = (params[:per_page] || 30).to_i
|
|
num = [per_page, PER_PAGE_MAX].min
|
|
|
|
@readinglist = Reaction
|
|
.select(INDEX_REACTIONS_ATTRIBUTES_FOR_SERIALIZATION)
|
|
.readinglist
|
|
.where(user_id: @user.id)
|
|
.where.not(status: "archived")
|
|
.order(created_at: :desc)
|
|
.page(params[:page])
|
|
.per(num)
|
|
|
|
articles = Article
|
|
.includes(:organization)
|
|
.select(ArticlesController::INDEX_ATTRIBUTES_FOR_SERIALIZATION)
|
|
.where(id: @readinglist.map(&:reactable_id))
|
|
.decorate
|
|
|
|
@users_by_id = User
|
|
.joins(:profile)
|
|
.select(UsersController::SHOW_ATTRIBUTES_FOR_SERIALIZATION)
|
|
.find(articles.map(&:user_id))
|
|
.index_by(&:id)
|
|
|
|
articles_by_id = articles.index_by(&:id)
|
|
|
|
@articles_by_reaction_ids = @readinglist.each_with_object({}) do |reaction, result|
|
|
result[reaction.id] = articles_by_id[reaction.reactable_id]
|
|
end
|
|
end
|
|
end
|
|
end
|