Skip to content

Commit

Permalink
Tag autocompleters, Tag objects in API
Browse files Browse the repository at this point in the history
  • Loading branch information
thegcat committed Jun 14, 2017
1 parent 867c941 commit 01699ee
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
15 changes: 13 additions & 2 deletions app/controllers/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ProductsController < ApplicationController
property :quantity, :integer, :optional, 'Total quantity'
property :available_quantity, :integer, :optional, 'Available quantity'
property :tags, :array, :optional, 'Tags',
'items' => { 'type' => 'string' }
'items' => { '$ref' => 'readTag' }
property :pricings, :array, :optional, 'Pricings',
'items' => { '$ref' => 'readPricing' }
property :identifiers, :array, :optional, 'Identifiers',
Expand All @@ -63,10 +63,17 @@ class ProductsController < ApplicationController
property :code, :string, :required, 'Identifying code'
end

swagger_model :readTag do
description 'A Product\'s or User\'s Tag'
property :name, :string, :required, 'Tag name'
property :occurrences, :integer, :required,
'Number of objects with this tag'
end

swagger_model :writeProduct do
property :name, :string, :optional, 'Product name'
property :tags, :array, :optional, 'Tags',
'items' => { 'type' => 'string' }
'items' => { '$ref' => 'writeTag' }
property :pricings, :array, :optional, 'Pricings',
'items' => { '$ref' => 'writePricing' }
property :identifiers, :array, :optional, 'Identifiers',
Expand All @@ -81,6 +88,10 @@ class ProductsController < ApplicationController
swagger_model :writeIdentifier do
property :code, :string, :required, 'Identifying code'
end

swagger_model :writeTag do
property :name, :string, :required, 'Tag name'
end
# :nocov:

before_action :doorkeeper_authorize!, only: [:show, :index]
Expand Down
23 changes: 23 additions & 0 deletions app/controllers/tag_autocompletes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class TagAutocompletesController < ApplicationController
before_action -> { doorkeeper_authorize! :admin }

def index
tags = []

if key = params.keys.detect { |k| k.end_with?('_id') }
type = key.gsub(/_id\z/, '').classify
id = params[key].to_i

tags = ActsAsTaggableOn::Tag.
joins(:taggings).
distinct.
where(taggings: { taggable_type: type }).
where.not(taggings: { taggable_id: id }).
order(taggings_count: :desc).
limit(10).
named_like(params[:q] || '')
end

render json: tags, represent_with: TagsRepresenter
end
end
15 changes: 13 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class UsersController < ApplicationController
property :allow_negative_balance, :boolean, :optional,
'Whether the user can have a negative balance (pre/postpaid)'
property :tags, :array, :optional, 'Tags',
'items' => { 'type' => 'string' }
'items' => { '$ref' => 'readTag' }
property :identifiers, :array, :optional, 'Identifiers',
'items' => { '$ref' => 'readIdentifier' }
end
Expand All @@ -49,17 +49,28 @@ class UsersController < ApplicationController
property :code, :string, :required, 'Identifying code'
end

swagger_model :readTag do
description 'A Product\'s or User\'s Tag'
property :name, :string, :required, 'Tag name'
property :occurrences, :integer, :required,
'Number of objects with this tag'
end

swagger_model :writeUser do
property :name, :string, :optional, 'User name'
property :tags, :array, :optional, 'Tags',
'items' => { 'type' => 'string' }
'items' => { '$ref' => 'writeTag' }
property :identifiers, :array, :optional, 'Identifiers',
'items' => { '$ref' => 'writeIdentifier' }
end

swagger_model :writeIdentifier do
property :code, :string, :required, 'Identifying code'
end

swagger_model :writeTag do
property :name, :string, :required, 'Tag name'
end
# :nocov:

before_action :doorkeeper_authorize!, only: [:show]
Expand Down
11 changes: 10 additions & 1 deletion app/representers/product_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ class ProductRepresenter < ApplicationDecorator
property :name
property :quantity, writeable: false, type: Integer
property :available_quantity, writeable: false, type: Integer
collection :tag_list, as: :tags
collection(
:tags,
decorator: TagRepresenter,
skip_parse: lambda { |fragment:, **|
fragment['name'].blank?
},
instance: lambda { |fragment:, **|
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name(fragment['name'])
}
)

collection(
:pricings,
Expand Down
5 changes: 5 additions & 0 deletions app/representers/tag_representer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class TagRepresenter < ApplicationDecorator
property :type, getter: ->(_) { 'tag' }, writeable: false
property :name, writeable: false
property :taggings_count, as: :occurrences, writeable: false
end
3 changes: 3 additions & 0 deletions app/representers/tags_representer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class TagsRepresenter < CollectionDecorator
items decorator: TagRepresenter
end
11 changes: 10 additions & 1 deletion app/representers/user_representer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ class UserRepresenter < ApplicationDecorator
property :name
property :balance, writeable: false, type: Integer
property :allow_negative_balance, type: Virtus::Attribute::Boolean
collection :tag_list, as: :tags
collection(
:tags,
decorator: TagRepresenter,
skip_parse: lambda { |fragment:, **|
fragment['name'].blank?
},
instance: lambda { |fragment:, **|
ActsAsTaggableOn::Tag.find_or_create_with_like_by_name(fragment['name'])
}
)

collection(
:identifiers,
Expand Down
9 changes: 7 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
Rails.application.routes.draw do
use_doorkeeper
get 'heartbeat' => 'heartbeat#check'
concern :taggable do
resources :tag_autocompletes, only: :index
end
resources :identifiers, only: :show
resources :users, only: %i(show index create update) do
resources :users, only: %i(show index create update),
concerns: :taggable do
resource :user_deposit, only: :create, path: 'deposit'
end
resources :products, only: %i(index show create update)
resources :products, only: %i(index show create update),
concerns: :taggable
resources :carts, only: %i(create show update) do
resource :cart_payment, only: :create, path: 'pay'
end
Expand Down

0 comments on commit 01699ee

Please sign in to comment.