Skip to content

Commit

Permalink
renamed article controllers. #190
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Dec 11, 2014
1 parent 3723181 commit e197941
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 69 deletions.
@@ -1,66 +1,66 @@
class Api::V3::ArticlesController < Api::V3::BaseController
before_filter :load_article, only: [:update, :destroy]
class Api::V3::WorksController < Api::V3::BaseController
before_filter :load_work, only: [:update, :destroy]

def index
# Filter by source parameter, filter out private sources unless admin
# Load articles from ids listed in query string, use type parameter if present
# Load works from ids listed in query string, use type parameter if present
# Translate type query parameter into column name
# Limit number of ids to 50
source_ids = get_source_ids(params[:source])

type = { "doi" => :doi, "pmid" => :pmid, "pmcid" => :pmcid, "mendeley" => :mendeley_uuid }.values_at(params[:type]).first || Article.uid_as_sym
type = { "doi" => :doi, "pmid" => :pmid, "pmcid" => :pmcid, "mendeley" => :mendeley_uuid }.values_at(params[:type]).first || Work.uid_as_sym

ids = params[:ids].nil? ? nil : params[:ids].split(",")[0...50].map { |id| Article.clean_id(id) }
id_hash = { :articles => { type => ids }, :retrieval_statuses => { :source_id => source_ids }}
@articles = ArticleDecorator.includes(:retrieval_statuses).references(:retrieval_statuses)
ids = params[:ids].nil? ? nil : params[:ids].split(",")[0...50].map { |id| Work.clean_id(id) }
id_hash = { :works => { type => ids }, :retrieval_statuses => { :source_id => source_ids }}
@works = WorkDecorator.includes(:retrieval_statuses).references(:retrieval_statuses)
.where(id_hash)
.order("articles.updated_at DESC")
.order("works.updated_at DESC")

# Return 404 HTTP status code and error message if article wasn't found, or no valid source specified
if @articles.blank?
# Return 404 HTTP status code and error message if work wasn't found, or no valid source specified
if @works.blank?
if params[:source].blank?
@error = "Article not found."
else
@error = "Source not found."
end
render "error", :status => :not_found
else
@articles = @articles.decorate(context: { days: params[:days], months: params[:months], year: params[:year], info: params[:info], source: params[:source] })
@works = @works.decorate(context: { days: params[:days], months: params[:months], year: params[:year], info: params[:info], source: params[:source] })
end
end

def show
# Load one article given query params
# Load one work given query params
source_ids = get_source_ids(params[:source])

id_hash = { :articles => Article.from_uri(params[:id]), :retrieval_statuses => { :source_id => source_ids }}
@article = Article.includes(:retrieval_statuses).references(:retrieval_statuses)
id_hash = { :works => Work.from_uri(params[:id]), :retrieval_statuses => { :source_id => source_ids }}
@work = Work.includes(:retrieval_statuses).references(:retrieval_statuses)
.where(id_hash).first


# Return 404 HTTP status code and error message if article wasn't found, or no valid source specified
if @article.blank?
# Return 404 HTTP status code and error message if work wasn't found, or no valid source specified
if @work.blank?
if params[:source].blank?
@error = "Article not found."
else
@error = "Source not found."
end
render "error", :status => :not_found
else
@article = @article.decorate(context: { days: params[:days], months: params[:months], year: params[:year], info: params[:info], source: params[:source] })
@work = @work.decorate(context: { days: params[:days], months: params[:months], year: params[:year], info: params[:info], source: params[:source] })
end
end

protected

def load_article
# Load one article given query params
id_hash = Article.from_uri(params[:id])
def load_work
# Load one work given query params
id_hash = Work.from_uri(params[:id])
if id_hash.respond_to?("key")
key, value = id_hash.first
@article = Article.where(key => value).first
@work = Work.where(key => value).first
else
@article = nil
@work = nil
end
end

Expand Down
@@ -1,44 +1,44 @@
class Api::V4::ArticlesController < Api::V4::BaseController
# include article controller methods
include Articable
class Api::V4::WorksController < Api::V4::BaseController
# include works controller methods
include Workable

before_filter :load_article, only: [:update, :destroy]
before_filter :load_work, only: [:update, :destroy]

def create
@article = Article.new(safe_params)
authorize! :create, @article
@work = Article.new(safe_params)
authorize! :create, @work

if @article.save
if @work.save
@success = "Article created."
render "success", :status => :created
else
@error = @article.errors
@error = @work.errors
render "error", :status => :bad_request
end
end

def update
authorize! :update, @article
authorize! :update, @work

if @article.blank?
if @work.blank?
@error = "No article found."
render "error", :status => :not_found
elsif @article.update_attributes(safe_params)
elsif @work.update_attributes(safe_params)
@success = "Article updated."
render "success", :status => :ok
else
@error = @article.errors
@error = @work.errors
render "error", :status => :bad_request
end
end

def destroy
authorize! :destroy, @article
authorize! :destroy, @work

if @article.blank?
if @work.blank?
@error = "No article found."
render "error", :status => :not_found
elsif @article.destroy
elsif @work.destroy
@success = "Article deleted."
render "success", :status => :ok
else
Expand All @@ -50,6 +50,6 @@ def destroy
private

def safe_params
params.require(:article).permit(:doi, :title, :pmid, :pmcid, :mendeley_uuid, :canonical_url, :year, :month, :day)
params.require(:work).permit(:doi, :title, :pmid, :pmcid, :mendeley_uuid, :canonical_url, :year, :month, :day)
end
end
4 changes: 0 additions & 4 deletions app/controllers/api/v5/articles_controller.rb

This file was deleted.

4 changes: 4 additions & 0 deletions app/controllers/api/v5/works_controller.rb
@@ -0,0 +1,4 @@
class Api::V5::WorksController < Api::V5::BaseController
# include work controller methods
include Workable
end
@@ -1,44 +1,44 @@
module Articable
module Workable
extend ActiveSupport::Concern

included do
def show
source_id = Source.where(name: params[:source]).pluck(:id).first

# Load one article given query params
id_hash = { :articles => Article.from_uri(params[:id]) }
@article = ArticleDecorator.includes(:retrieval_statuses)
# Load one work given query params
id_hash = { :works => Work.from_uri(params[:id]) }
@work = WorkDecorator.includes(:retrieval_statuses)
.references(:retrieval_statuses)
.where(id_hash).first
.decorate(context: { info: params[:info], source_id: source_id })

# Return 404 HTTP status code and error message if article wasn't found
if @article.blank?
# Return 404 HTTP status code and error message if work wasn't found
if @work.blank?
@error = "Article not found."
render "error", :status => :not_found
else
fresh_when last_modified: @article.updated_at
fresh_when last_modified: @work.updated_at
@success = "Article found."
end
end

def index
# Load articles from ids listed in query string, use type parameter if present
# Load works from ids listed in query string, use type parameter if present
# Translate type query parameter into column name
# Paginate query results, default is 50 articles per page
# Paginate query results, default is 50 works per page

if params[:ids]
type = ["doi", "pmid", "pmcid", "mendeley_uuid"].find { |t| t == params[:type] } || Article.uid
ids = params[:ids].nil? ? nil : params[:ids].split(",").map { |id| Article.clean_id(id) }
collection = Article.where(:articles => { type.to_sym => ids })
type = ["doi", "pmid", "pmcid", "mendeley_uuid"].find { |t| t == params[:type] } || Work.uid
ids = params[:ids].nil? ? nil : params[:ids].split(",").map { |id| Work.clean_id(id) }
collection = Work.where(:works => { type.to_sym => ids })
elsif params[:q]
collection = Article.query(params[:q])
collection = Work.query(params[:q])
elsif params[:source] && source = Source.where(name: params[:source]).first
collection = Article.joins(:retrieval_statuses)
collection = Work.joins(:retrieval_statuses)
.where("retrieval_statuses.source_id = ?", source.id)
.where("retrieval_statuses.event_count > 0")
else
collection = Article
collection = Work
end

if params[:class_name]
Expand Down Expand Up @@ -72,32 +72,32 @@ def index
# use cached counts for total number of results
total_entries = case
when params[:ids] || params[:q] || params[:class_name] then nil # can't be cached
when source && publisher then publisher.article_count_by_source(source.id)
when source then source.article_count
when publisher then publisher.article_count
else Article.count_all
when source && publisher then publisher.work_count_by_source(source.id)
when source then source.work_count
when publisher then publisher.work_count
else Work.count_all
end

collection = collection.paginate(per_page: per_page,
page: params[:page],
total_entries: total_entries)

fresh_when last_modified: collection.maximum(:updated_at)
@articles = collection.decorate(context: { info: params[:info],
@works = collection.decorate(context: { info: params[:info],
source: params[:source],
user: current_user.cache_key })
end

protected

def load_article
# Load one article given query params
id_hash = Article.from_uri(params[:id])
def load_work
# Load one work given query params
id_hash = Work.from_uri(params[:id])
if id_hash.respond_to?("key")
key, value = id_hash.first
@article = Article.where(key => value).first.decorate
@work = Work.where(key => value).first.decorate
else
@article = nil
@work = nil
end
end
end
Expand Down
@@ -1,4 +1,4 @@
class ArticleDecorator < Draper::Decorator
class WorkDecorator < Draper::Decorator
delegate_all
decorates_finders

Expand Down Expand Up @@ -32,7 +32,7 @@ def mendeley
end

def cache_key
{ article_id: id,
{ work_id: id,
update_date: update_date,
source_ids: source_ids,
info: context[:info] }
Expand Down

0 comments on commit e197941

Please sign in to comment.