diff --git a/app/controllers/concerns/highlights_controller.rb b/app/controllers/concerns/highlights_controller.rb new file mode 100644 index 0000000000000000000000000000000000000000..8ff30cc9708b94f44b0708e49596fc406a3d6a37 --- /dev/null +++ b/app/controllers/concerns/highlights_controller.rb @@ -0,0 +1,16 @@ +module HighlightsController + extend ActiveSupport::Concern + + # GET /v1/learning_objects/this_week + # GET /v1/learning_objects/this_week.json + def this_week + render json: highlights_resource.this_week + end + + # GET /v1/learning_objects/this_month + # GET /v1/learning_objects/this_month.json + def this_month + render json: highlights_resource.this_month + end + +end diff --git a/app/controllers/concerns/sociable_controller.rb b/app/controllers/concerns/sociable_controller.rb index 7ef79fed13f8681a39c0641f45a7d8070c8e1324..f32b44a7c40414018b8de6b9bd2f76c953c0d8de 100644 --- a/app/controllers/concerns/sociable_controller.rb +++ b/app/controllers/concerns/sociable_controller.rb @@ -35,6 +35,10 @@ module SociableController protected + def sociable + raise NotImplementedError + end + def view_object! sociable.view current_user if user_signed_in? end diff --git a/app/controllers/v1/collections_controller.rb b/app/controllers/v1/collections_controller.rb index bb711ea9675a330f9b12f27e65d8a6a8c7edb71e..195f6023552e7864fb1c165b0444b3558646cf3a 100644 --- a/app/controllers/v1/collections_controller.rb +++ b/app/controllers/v1/collections_controller.rb @@ -3,6 +3,7 @@ class V1::CollectionsController < ApplicationController include ::FollowableController include ::TaggableController include ::DeletedObjectsController + include ::HighlightsController before_action :set_collection, only: [:show, :update, :destroy] before_action :authenticate_user!, only: [:create, :update, :destroy] @@ -50,22 +51,13 @@ class V1::CollectionsController < ApplicationController private - def deleted_resource - Collection - end + def deleted_resource; Collection; end + def highlights_resource; Collection; end # social concerns methods - def followable - set_collection - end - - def taggable - set_collection - end - - def sociable - set_collection - end + def followable; set_collection; end + def taggable; set_collection; end + def sociable; set_collection; end def set_collection @collection ||= Collection.find(params[:id]) diff --git a/app/controllers/v1/learning_objects_controller.rb b/app/controllers/v1/learning_objects_controller.rb index 8a33dfb97f870edc710462ec86894fbb6e049609..fd1a803da84b6aa752922a8ddbea0cc823ead5a6 100644 --- a/app/controllers/v1/learning_objects_controller.rb +++ b/app/controllers/v1/learning_objects_controller.rb @@ -5,6 +5,7 @@ class V1::LearningObjectsController < ApplicationController include ::TaggableController include ::Paginator include ::DeletedObjectsController + include ::HighlightsController before_action :authenticate_user!, except: [:index, :show] before_action :set_learning_object, only: [:show, :update, :destroy] @@ -53,17 +54,10 @@ class V1::LearningObjectsController < ApplicationController private - def deleted_resource - LearningObject - end - - def sociable - set_learning_object - end - - def taggable - set_learning_object - end + def deleted_resource; LearningObject; end + def highlights_resource; LearningObject; end + def sociable; set_learning_object; end + def taggable; set_learning_object; end # Use callbacks to share common setup or constraints between actions. def set_learning_object diff --git a/app/controllers/v1/reviews_controller.rb b/app/controllers/v1/reviews_controller.rb index 117e330597400f3847b8e3028f81aed9546ea9ed..992a5e308ee09eaea10b828e1c56ea6dae7d1b08 100644 --- a/app/controllers/v1/reviews_controller.rb +++ b/app/controllers/v1/reviews_controller.rb @@ -1,5 +1,6 @@ class V1::ReviewsController < ApplicationController include ::DeletedObjectsController + include ::ResourceModel before_action :set_review, only: [:show, :destroy, :rate] before_action :authenticate_user!, only: [:create, :rate, :destroy] @@ -79,9 +80,7 @@ class V1::ReviewsController < ApplicationController end def reviewable - resource, id = request.path.split('/')[2, 3] - reviewable_model = resource.singularize.classify.constantize - @reviewable = reviewable_model.find(id) + @reviewable = resource_model end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 9c15b6be117503dad5e5e76923d10f2af01e80c6..d24e649cb34111b2afca7e2030268af7df9992f0 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -54,11 +54,8 @@ class V1::UsersController < ApplicationController def watching type = params[:object_type] is_current = (@user.id == current_user.id) unless current_user.nil? - - render nothing: true, status: :bad_request unless type.in? %w(User Collection) - + return render nothing: true, status: :bad_request unless type.in? %w(User Collection) w = @user.watching(type, is_current) - render json: w, root: 'follows', status: :ok end diff --git a/app/models/collection.rb b/app/models/collection.rb index 49f552c1401bbee0739e80d5f5e4afd45d0be299..ae5893b540b25d6c94757bf55d2290c92ff0f126 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -30,6 +30,7 @@ class Collection < ActiveRecord::Base include Thumbnailable include Taggable include Trackable + include Highlights has_many :collection_items, as: :collectionable, dependent: :destroy has_many :collections, through: :collection_items, source: :collectionable, source_type: 'Collection' diff --git a/app/models/concerns/highlights.rb b/app/models/concerns/highlights.rb new file mode 100644 index 0000000000000000000000000000000000000000..62a3d5480644ae26a88a5289f44140e583a80930 --- /dev/null +++ b/app/models/concerns/highlights.rb @@ -0,0 +1,9 @@ +module Highlights + extend ActiveSupport::Concern + + included do + scope :this_week, -> (limit = 1000) { where('created_at >= ?', 1.week.ago).order(score: :desc).limit(limit) } + scope :this_month, -> (limit = 1000) { where('created_at >= ?', 1.month.ago).order(score: :desc).limit(limit) } + end + +end \ No newline at end of file diff --git a/app/models/learning_object.rb b/app/models/learning_object.rb index f7530aab370428ebe006b5e5ae25f37bd5e3e2ef..985dd42465d49e8ac33373d92cb465ffe1d844c9 100644 --- a/app/models/learning_object.rb +++ b/app/models/learning_object.rb @@ -1,4 +1,4 @@ -# == Schema Information + # == Schema Information # # Table name: learning_objects # @@ -38,6 +38,7 @@ class LearningObject < ActiveRecord::Base include Scoreable include Thumbnailable include Taggable + include Highlights # *current_user* create learning object # *current_user* update learning object @@ -136,4 +137,5 @@ class LearningObject < ActiveRecord::Base def user_category publisher.try('user_category') end + end diff --git a/config/routes.rb b/config/routes.rb index 3eb9386fef16c3b71676d6d066d62dd737b61610..1aaea82a5262fc5385f86f59922219680b2c1a33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -37,6 +37,13 @@ Rails.application.routes.draw do end end + concern :highlights do + collection do + get :this_week + get :this_month + end + end + # GET /learning_objects/1/versions/123 # GET /learning_objects/1/versions # POST /learning_objects/1/versions/234/checkout @@ -67,8 +74,8 @@ Rails.application.routes.draw do get :search, to: 'search#index' get 'search/autocomplete', to: 'search#autocomplete' - resources :collections, concerns: [:followable, :sociable, :reviewable, :taggable, :versionable, :deletable] - resources :learning_objects, concerns: [:sociable, :reviewable, :taggable, :versionable, :deletable] do + resources :collections, concerns: [:followable, :sociable, :reviewable, :taggable, :versionable, :deletable, :highlights] + resources :learning_objects, concerns: [:sociable, :reviewable, :taggable, :versionable, :deletable, :highlights] do member do resource :chunk, module: 'learning_objects', only: [:create, :show] resource :upload, module: 'learning_objects', only: :create diff --git a/test/models/collection_test.rb b/test/models/collection_test.rb index 758411c545ad260d9420ec262d722139922ee98b..242f37dbf6ab6b9861e98ca460fce83feb480b72 100644 --- a/test/models/collection_test.rb +++ b/test/models/collection_test.rb @@ -47,6 +47,14 @@ class CollectionTest < ActiveSupport::TestCase include ::Portalmec::SociableTests + test 'should get all collections created this week' do + Collection.this_week.each {|o| assert_instance_of Collection, o} + end + + test 'should get all collections created this month' do + Collection.this_month.each {|o| assert_instance_of Collection, o} + end + protected def sociable_object diff --git a/test/models/learning_object_test.rb b/test/models/learning_object_test.rb index abbf05d0474e49485602b83634260826630ed21e..f2924dc9b678914b0cae67e95fa510379aaef648 100644 --- a/test/models/learning_object_test.rb +++ b/test/models/learning_object_test.rb @@ -56,6 +56,29 @@ class LearningObjectTest < ActiveSupport::TestCase include ::Portalmec::SociableTests + test 'should search data return a hash' do + learning_object = learning_objects(:lo_complete) + assert_equal learning_object.search_data, { + name: 'Institution Object 1', + description: 'Testing', + author: 'Mauricio', + object_type: 'Imagem', + score: learning_object.score, + published_at: learning_object.published_at, + tags: learning_object.tags.map(&:name), + source: nil, + state: learning_object.state + } + end + + test 'should get all learning objects created this week' do + LearningObject.this_week.each {|o| assert_instance_of LearningObject, o} + end + + test 'should get all learning objects created this month' do + LearningObject.this_month.each {|o| assert_instance_of LearningObject, o} + end + protected def sociable_object