Skip to content
Snippets Groups Projects
Commit 7d98a31c authored by Clarissa's avatar Clarissa
Browse files

SCRUM#281: changing return on pages through paginator for portalmec-admin

parent b5493c32
No related branches found
No related tags found
No related merge requests found
......@@ -22,25 +22,49 @@ module Paginator
def paginate(model)
total_count model
return model.limit(limit).offset(offset) if model.respond_to?('limit')
@values = model.limit(limit).offset(offset) if model.respond_to?('limit')
return params_actions(@values) if ( @values )
return model[offset..limit]
end
private
def limit
return params[:limit].to_i if !params[:limit].blank?
return params[:results_per_page].to_i if !params[:results_per_page].blank?
return 12
end
def limit
return params[:limit].to_i if !params[:limit].blank?
return params[:results_per_page].to_i if !params[:results_per_page].blank?
return 10
end
def offset
return params[:offset].to_i if !params[:offset].blank?
return params[:page].to_i*params[:results_per_page].to_i if !params[:page].blank? && !params[:results_per_page].blank?
return 0
end
def total_count(model)
headers['X-Total-Count'] = model.count
end
def offset
return params[:offset].to_i if !params[:offset].blank?
return params[:page].to_i*params[:results_per_page].to_i if !params[:page].blank? && !params[:results_per_page].blank?
return 0
end
def params_actions(values)
# filtering
# name, author (LearningObject), city (Institution), privacy (Collections)
if ( !params[:filter].blank? )
hash_params = JSON.parse params[:filter]
hash_params.each do |key, value|
values = values.where("#{key} ILIKE ?", "%#{value}%")
end
end
# ordering
# name, id, author, score, email, city
sort_p = params[:sort] if !params[:sort].blank?
values = values.sort { |a,b| (a[sort_p].is_a? Integer)? a[sort_p] <=> b[sort_p] : a[sort_p].to_s.downcase <=> b[sort_p].to_s.downcase } if sort_p and params[:order] == 'ASC'
values = values.sort { |a,b| (a[sort_p].is_a? Integer)? -(a[sort_p] <=> b[sort_p]) : -(a[sort_p].to_s.downcase <=> b[sort_p].to_s.downcase) } if sort_p and params[:order] == 'DESC'
return values
end
def total_count(model)
headers['X-Total-Count'] = model.count
end
end
......@@ -20,7 +20,9 @@
class V1::ActivitiesController < ApplicationController
include ::ResourceModel
include ::Paginator
before_action :authenticate_user!
before_action :set_activity, only: [:show]
# GET v1/activities
# GET v1/activities.json
......@@ -31,6 +33,13 @@ class V1::ActivitiesController < ApplicationController
render json: activities
end
# GET v1/activities/1
# GET v1/activities/1.json
def show
authorize :activity, :show?
render json: @activity
end
# GET v1/users/1/activities
# GET v1/users/1/activities.json
# Render specific user activities
......@@ -46,4 +55,11 @@ class V1::ActivitiesController < ApplicationController
activities = paginate current_user.activities_filtered
render json: activities
end
private
def set_activity
@activity = ::ActivityPolicy::Scope.new(current_user, ::PublicActivity::Activity).resolve.find(params[:id])
end
end
......@@ -19,6 +19,7 @@
class V1::RatingsController < ApplicationController
include ::DeletedObjectsController
include ::Paginator
before_action :set_rating, only: [:show, :update, :destroy]
before_action :authenticate_user!, only: [:create, :update, :destroy]
......@@ -27,7 +28,8 @@ class V1::RatingsController < ApplicationController
# GET v1/ratings
# GET v1/ratings.json
def index
render json: Rating.all
ratings = paginate Rating
render json: ratings
end
# GET v1/ratings/1
......
......@@ -18,13 +18,18 @@
# along with portalmec. If not, see <http://www.gnu.org/licenses/>.
class V1::ScoresController < ApplicationController
include ::Paginator
before_action :set_score, only: [:show,:update]
before_action :authenticate_user!, only: [:update]
# GET v1/scores
# GET v1/scores.json
def index
render json: Score.order(:name).includes(:score_user_categories).sort{|score| score.score_user_categories.size}.reverse
params[:sort] = 'name' if params[:sort].blank?
params[:order] = 'ASC' if params[:order].blank?
scores = paginate Score
render json: scores
end
# GET /scores/1
......
......@@ -41,6 +41,10 @@ class ActivityPolicy < ApplicationPolicy
true
end
def show?
true
end
## only user followers can see your activities
def user_activities?
record if user.following? record
......
......@@ -36,6 +36,29 @@ class UserSerializer < ActiveModel::Serializer
object.learning_objects.where('state = ?', LearningObject.states[:published]).count
end
attributes :id, :email, :provider, :name, :description, :submitter_request, :education, :score, :cover, :role_ids, :institution_ids, :avatar, :likes_count, :followed, :complained, :follows_count, :learning_objects_count, :collections_count, :created_at, :updated_at
attributes \
:id,
:email,
:provider,
:name,
:description,
:submitter_request,
:education,
:score,
:cover,
:role_ids,
:institution_ids,
:avatar,
:likes_count,
:followed,
:complained,
:follows_count,
:learning_objects_count,
:collections_count,
:created_at,
:updated_at
has_many :subjects
has_many :roles
has_many :institutions
end
......@@ -123,7 +123,7 @@ Rails.application.routes.draw do
end
namespace :v1 do
resources :activities, only: :index
resources :activities, only: [:index, :show]
resources :feed, only: [:index]
resources :users, concerns: [:followable, :deletable, :publisher, :versionable] do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment