Skip to content

Commit

Permalink
Creates a new Article#show endpoint
Browse files Browse the repository at this point in the history
Articles API gets a new #show endpoint to query for single articles.

ANZPL-43
  • Loading branch information
5minpause committed Jul 8, 2015
1 parent a66310d commit 439e3be
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 2 deletions.
32 changes: 30 additions & 2 deletions app/controllers/goldencobra/api/v2/articles_controller.rb
Expand Up @@ -5,6 +5,7 @@ module Api
module V2
class ArticlesController < ActionController::Base
skip_before_filter :verify_authenticity_token
before_filter :get_article, only: [:show]

respond_to :json

Expand Down Expand Up @@ -53,6 +54,26 @@ def index
end
end

# /api/v2/articles/:url[.json]
#
# @param methods [String] "beliebe Attribute des Artikels im JSON einfuegen"
#
# @return [json] Liefert Artikel mit einer bestimmten URL
# Im JSON Response befinden sich entweder alle Attribute, oder nur die mit
# params[:methods] definierten
def show
respond_to do |format|
format.json {
if params[:methods].present?
render json: @article,
serializer: Goldencobra::ArticleCustomSerializer,
scope: params[:methods]
else
render json: @article
end
}
end
end

# /api/v2/articles/create[.json]
# ---------------------------------------------------------------------------------------
Expand Down Expand Up @@ -89,7 +110,6 @@ def create
else
render status: 500, json: { :status => 500, :error => response.errors, :id => nil }
end

end


Expand Down Expand Up @@ -222,10 +242,18 @@ def update_article(article_param)

# Try to save the article
return article

end

private

def get_article
url = params[:url].present? ? "/#{params[:url]}" : "/"

@article = Goldencobra::Article.where(url_path: url).first
unless @article
raise "Article not found with url: #{url}"
end
end
end
end
end
Expand Down
38 changes: 38 additions & 0 deletions app/serializers/goldencobra/article_custom_serializer.rb
@@ -0,0 +1,38 @@
module Goldencobra
class ArticleCustomSerializer < ActiveModel::Serializer
# root "article" # would otherwise be "article_custom"
root false

# Includes all defined methods
def attributes
data = super
if scope
scope.split(",").each do |field|
data[field.to_sym] = object.send(field.to_sym)
end
end
data
end

has_many :metatags
has_many :images
has_many :widgets
has_many :children

def metatags
object.metatags.select([:id, :name, :value])
end

def images
object.images.pluck(:id)
end

def widgets
object.widgets.pluck(:id)
end

def children
object.children.active.pluck(:id)
end
end
end
28 changes: 28 additions & 0 deletions app/serializers/goldencobra/article_serializer.rb
@@ -0,0 +1,28 @@
module Goldencobra
class ArticleSerializer < ActiveModel::Serializer
root false
attributes(*Goldencobra::Article.attribute_names.map(&:to_sym))

has_many :metatags
has_many :images
has_many :widgets
has_many :children

def metatags
object.metatags.select([:id, :name, :value])
end

def images
object.images.pluck(:id)
end

def widgets
object.widgets.pluck(:id)
end

def children
object.children.active.pluck(:id)
end

end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -26,6 +26,7 @@

namespace "v2" do
get '/articles' => 'articles#index'
get '/articles/*url' => 'articles#show'
get '/articles/search' => 'articles#search'
get '/locale_string' => 'locales#get_string'
get '/setting_string' => 'settings#get_string'
Expand Down
1 change: 1 addition & 0 deletions goldencobra.gemspec
Expand Up @@ -76,6 +76,7 @@ Gem::Specification.new do |s|
s.add_dependency 'simple_enum'
s.add_dependency 'react-rails', '~> 1.0'
s.add_dependency 'addressable'
s.add_dependency 'active_model_serializers'
# s.add_dependency "wicked_pdf"
s.add_development_dependency "mysql2"
s.add_development_dependency 'annotate'
Expand Down
1 change: 1 addition & 0 deletions lib/goldencobra/engine.rb
Expand Up @@ -25,6 +25,7 @@
require 'geokit'
require "rack/utf8_sanitizer"
require 'simple_enum'
require 'active_model_serializers'

module Goldencobra
class Engine < ::Rails::Engine
Expand Down

0 comments on commit 439e3be

Please sign in to comment.