Skip to content

Commit

Permalink
API v1 article_categories endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
wvengen committed Oct 13, 2018
1 parent 7bf0c85 commit 9ebe42b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
26 changes: 26 additions & 0 deletions app/controllers/api/v1/article_categories_controller.rb
@@ -0,0 +1,26 @@
class Api::V1::ArticleCategoriesController < Api::V1::BaseController
include Concerns::CollectionScope

def index
render json: search_scope
end

def show
render json: scope.find(params.require(:id))
end

private

def max_per_page
nil
end

def default_per_page
nil
end

def scope
ArticleCategory.all
end

end
15 changes: 14 additions & 1 deletion app/models/article_category.rb
Expand Up @@ -9,13 +9,27 @@ class ArticleCategory < ActiveRecord::Base
# @!attribute articles
# @return [Array<Article>] Articles with this category.
has_many :articles
# @!attribute order_articles
# @return [Array<OrderArticle>] Order articles with this category.
has_many :order_articles, through: :articles
# @!attribute orders
# @return [Array<Order>] Orders with articles in this category.
has_many :orders, through: :order_articles

normalize_attributes :name, :description

validates :name, :presence => true, :uniqueness => true, :length => { :minimum => 2 }

before_destroy :check_for_associated_articles

def self.ransackable_attributes(auth_object = nil)
%w(id name)
end

def self.ransackable_associations(auth_object = nil)
%w(articles order_articles orders)
end

# Find a category that matches a category name; may return nil.
# TODO more intelligence like remembering earlier associations (global and/or per-supplier)
def self.find_match(category)
Expand All @@ -40,4 +54,3 @@ def check_for_associated_articles
end

end

3 changes: 3 additions & 0 deletions app/serializers/article_category_serializer.rb
@@ -0,0 +1,3 @@
class ArticleCategorySerializer < ActiveModel::Serializer
attributes :id, :name
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -252,6 +252,7 @@
resources :orders, only: [:index, :show]
resources :order_articles, only: [:index, :show]
resources :group_order_articles
resources :article_categories, only: [:index, :show]
end
end

Expand Down

0 comments on commit 9ebe42b

Please sign in to comment.