diff --git a/Gemfile b/Gemfile index adc9b7c07..6b0586919 100644 --- a/Gemfile +++ b/Gemfile @@ -28,7 +28,6 @@ gem 'alto_guisso_rails', :git => "https://github.com/instedd/alto_guisso_rails", # libraries gem 'actionpack-action_caching' gem 'actionpack-page_caching' -gem 'active_model_serializers' # TODO: consider removing (one serializer) gem 'activerecord-import' gem "breadcrumbs_on_rails" gem 'carrierwave' @@ -36,7 +35,6 @@ gem 'decent_exposure' # NOTE: pattern used in 12 out of 29 contr gem 'gettext', '~> 3.1.2' gem 'gettext_i18n_rails_js', git: "https://github.com/juanboca/gettext_i18n_rails_js.git", branch: 'master' gem 'ice_cube' -# gem 'includes-count' # TODO: remove (only one use + breaking ActiveRecord with a frozen array) gem "instedd-rails" #, '~> 0.0.24' gem 'mini_magick' gem 'msgpack', '~> 0.7.5' diff --git a/Gemfile.lock b/Gemfile.lock index 86a7c34c2..69afeffd4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,11 +71,6 @@ GEM erubis (~> 2.7.0) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - active_model_serializers (0.10.14) - actionpack (>= 4.1) - activemodel (>= 4.1) - case_transform (>= 0.2) - jsonapi-renderer (>= 0.1.1.beta1, < 0.3) activejob (5.0.7.2) activesupport (= 5.0.7.2) globalid (>= 0.3.6) @@ -118,8 +113,6 @@ GEM marcel (~> 1.0.0) mini_mime (>= 0.1.3) ssrf_filter (~> 1.0) - case_transform (0.2) - activesupport childprocess (0.9.0) ffi (~> 1.0, >= 1.0.11) coffee-rails (4.1.1) @@ -236,7 +229,6 @@ GEM activesupport (>= 4.2) aes_key_wrap bindata - jsonapi-renderer (0.2.2) jwt (2.3.0) knockoutjs-rails (3.5.1.1) railties (>= 3.1) @@ -471,7 +463,6 @@ PLATFORMS DEPENDENCIES actionpack-action_caching actionpack-page_caching - active_model_serializers activerecord-import alto_guisso! alto_guisso_rails! diff --git a/app/controllers/api/collections_controller.rb b/app/controllers/api/collections_controller.rb index f92c0abb7..776abfc7e 100644 --- a/app/controllers/api/collections_controller.rb +++ b/app/controllers/api/collections_controller.rb @@ -9,7 +9,8 @@ class Api::CollectionsController < ApiController expose(:collection) { Collection.find(params[:collection_id] || params[:id]) } def index - render json: current_user.collections.includes_count(:sites).load, each_serializer: Api::CollectionSerializer + collections = current_user.collections + render_json collections_json(collections, collections.count_sites) end def create diff --git a/app/helpers/api/json_helper.rb b/app/helpers/api/json_helper.rb index ebb7fe2e5..82afa63af 100644 --- a/app/helpers/api/json_helper.rb +++ b/app/helpers/api/json_helper.rb @@ -10,6 +10,28 @@ def collection_json(collection, results) obj end + def collections_json(collections, sites_counts) + collections.each do |collection| + { + anonymous_location_permission: collection.anonymous_location_permission, + anonymous_name_permission: collection.anonymous_name_permission, + created_at: collection.created_at, + description: collection.description, + icon: collection.icon, + id: collection.id, + lat: collection.lat, + lng: collection.lng, + max_lat: collection.max_lat, + max_lng: collection.max_lng, + min_lat: collection.min_lat, + min_lng: collection.min_lng, + name: collection.name, + updated_at:collection.updated_at, + count: sites_counts[collection.id].to_i, + } + end + end + def site_item_json(result) source = result['_source'] diff --git a/app/models/collection.rb b/app/models/collection.rb index a06823877..5de2f2fad 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -36,6 +36,10 @@ class Collection < ApplicationRecord after_save :touch_lifespan after_destroy :touch_lifespan + def self.count_sites + Site.where(collection_id: select(:id)).group(:collection_id).count + end + def max_value_of_property(es_code) client = Elasticsearch::Client.new results = client.search index: index_name, type: 'site', body: { diff --git a/app/serializers/api/collection_serializer.rb b/app/serializers/api/collection_serializer.rb deleted file mode 100644 index 1f9b1b07d..000000000 --- a/app/serializers/api/collection_serializer.rb +++ /dev/null @@ -1,11 +0,0 @@ -class Api::CollectionSerializer < ActiveModel::Serializer - attributes :anonymous_location_permission, :anonymous_name_permission, :created_at, :description, :icon, :id, :lat, :lng, :max_lat, :max_lng, :min_lat, :min_lng, :name, :updated_at, :count - - def count - if object.respond_to? :sites_count - object.sites_count - else - object.sites.count - end - end -end