Class: Api::V1::ArticlesController
- Inherits:
-
BaseController
- Object
- BaseController
- Api::V1::ArticlesController
- Defined in:
- app/controllers/api/v1/articles_controller.rb
Instance Method Summary collapse
- #get_article_version_data(article) ⇒ Object protected
- #get_latest_article_update(supplier) ⇒ Object protected
- #index ⇒ Object
- #index_params ⇒ Object protected
- #pagination_response ⇒ Object protected
Instance Method Details
#get_article_version_data(article) ⇒ Object (protected)
25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/controllers/api/v1/articles_controller.rb', line 25 def get_article_version_data(article) version_attributes = article.latest_article_version.attributes version_attributes.delete_if { |key| key == 'id' || key.end_with?('_id') } version_attributes['article_unit_ratios'] = article.latest_article_version.article_unit_ratios.map do |ratio| ratio_attributes = ratio.attributes ratio_attributes.delete_if { |key| key == 'id' || key.end_with?('_id') } end version_attributes end |
#get_latest_article_update(supplier) ⇒ Object (protected)
37 38 39 40 41 42 43 44 45 46 |
# File 'app/controllers/api/v1/articles_controller.rb', line 37 def get_latest_article_update(supplier) latest_update = Article .with_latest_versions .undeleted .where(supplier_id: supplier, type: nil) .order('article_versions.updated_at DESC') .limit(1) .first&.updated_at latest_update&.utc end |
#index ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/controllers/api/v1/articles_controller.rb', line 4 def index supplier = Supplier.find_by_external_uuid!(index_params.fetch(:shared_supplier_uuid)) @articles = Article.with_latest_versions_and_categories.undeleted.where(supplier_id: supplier, type: nil) @articles = @articles.where('article_versions.updated_at > ?', index_params[:updated_after].to_datetime) if index_params.include?(:updated_after) @articles = @articles.where('article_versions.name LIKE ?', "%#{index_params[:name]}%") if index_params.include?(:name) @articles = @articles.where(article_versions: { origin: index_params[:origin] }) if index_params.include?(:origin) @articles = @articles.where(article_versions: { order_number: index_params[:order_numbers] }) if index_params.include?(:order_numbers) @articles = @articles.page(index_params[:page]).per(index_params.fetch(:per_page)) if index_params.include?(:page) data = @articles.map { |article| get_article_version_data(article) } render json: { articles: data, pagination: pagination_response, latest_update: get_latest_article_update(supplier) } end |
#index_params ⇒ Object (protected)
21 22 23 |
# File 'app/controllers/api/v1/articles_controller.rb', line 21 def index_params params.permit(:shared_supplier_uuid, :updated_after, :name, :origin, :page, :per_page, order_numbers: []) end |
#pagination_response ⇒ Object (protected)
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'app/controllers/api/v1/articles_controller.rb', line 48 def pagination_response return nil unless index_params.include?(:page) current = @articles.current_page total = @articles.total_pages { current_page: current, previous_page: (current > 1 ? (current - 1) : nil), next_page: (current == total ? nil : (current + 1)), per_page: @articles.limit_value, total_pages: total, number: @articles.total_count } end |