diff --git a/app/controllers/v1/activities_controller.rb b/app/controllers/v1/activities_controller.rb index ab1d3fcb5440929fabfdb181ab1e4db516abd3e0..4df2c71f59a9a945317d8f01300d1be452312c41 100644 --- a/app/controllers/v1/activities_controller.rb +++ b/app/controllers/v1/activities_controller.rb @@ -8,7 +8,7 @@ class V1::ActivitiesController < ApplicationController # Render all activities that logged user can see def index authorize :activity, :index? - activities = paginate ::ActivityPolicy::Scope.new(current_user, ::PublicActivity::Activity).resolve + activities = paginate current_user.activities render json: activities end diff --git a/app/controllers/v1/feed_controller.rb b/app/controllers/v1/feed_controller.rb index 5e1b748e168605f5e99f7ff9e3766738d810dcf1..3a1d894494aa40c2ee300b2eecd9ffc4d1aba55a 100644 --- a/app/controllers/v1/feed_controller.rb +++ b/app/controllers/v1/feed_controller.rb @@ -12,20 +12,12 @@ class V1::FeedController < ApplicationController private -#TODO: Tests def activities_followed activities = [] - types.each do |type| - model = type.classify.constantize - current_user.watching(type).each do |follow| - followed = model.find(follow.followable_id) - activities.push(*followed.activities.to_a) - end + current_user.watching.each do |watching| + activities.push(*watching.activities.to_a) end activities end - def types - ['User', 'Collection'] - end end diff --git a/app/controllers/v1/users_controller.rb b/app/controllers/v1/users_controller.rb index 692dcd51c455a1a280d06d4230a1c29a8173151d..4f71bdb81dc07159e433bb954a1aa3c18bba94a3 100644 --- a/app/controllers/v1/users_controller.rb +++ b/app/controllers/v1/users_controller.rb @@ -4,8 +4,8 @@ class V1::UsersController < ApplicationController include ::Paginator - before_action :set_user, only: [:show, :update, :destroy, :watching] - before_action :authenticate_user!, only: [:create, :update, :destroy, :watching] + before_action :set_user, only: [:show, :update, :destroy, :following] + before_action :authenticate_user!, only: [:create, :update, :destroy, :following] # GET /v1/users # GET /v1/users.json @@ -54,11 +54,11 @@ class V1::UsersController < ApplicationController end end - def watching + def following type = params[:object_type] is_current = (@user.id == current_user.id) unless current_user.nil? return render status: :bad_request unless type.in? %w(User Collection) - w = @user.watching(type, is_current) + w = @user.following(type, is_current) render json: w, root: 'follows', status: :ok end diff --git a/app/models/bookmark.rb b/app/models/bookmark.rb index df1fc1901295b1397bc0f446464b9f0fa7e47ba9..c912a2dcb58aa35f436b2a28f39e239a76f20dcc 100644 --- a/app/models/bookmark.rb +++ b/app/models/bookmark.rb @@ -20,4 +20,8 @@ class Bookmark < ApplicationRecord validates_presence_of :user, :bookmarkable validates :user_id, uniqueness: { scope: [:bookmarkable_id, :bookmarkable_type] } + + def recipient + bookmarkable + end end diff --git a/app/models/carousel.rb b/app/models/carousel.rb index 5959c79b4323867755b23d3d64ba70a57a7a577b..066dc5ed10c67120748603f367d1ef9d1bf3bca6 100644 --- a/app/models/carousel.rb +++ b/app/models/carousel.rb @@ -15,8 +15,8 @@ class Carousel < ApplicationRecord has_attached_file :image, styles: { - larger: "600x600>", - thumbnail: "100x100>" - }, default_url: "/images/:style/missing.png" + larger: '600x600>', + thumbnail: '100x100>' + } validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ end diff --git a/app/models/collection.rb b/app/models/collection.rb index f7afa2de31e63f28fee5a581692909fc8b2b7ee3..772eef5b102c02f465cb43f8cabe5180599692ac 100644 --- a/app/models/collection.rb +++ b/app/models/collection.rb @@ -43,7 +43,7 @@ class Collection < ApplicationRecord belongs_to :owner, polymorphic: true validates :name, :owner, presence: true - validates_inclusion_of :privacy, in: %w(public private), message: "Privacy must be public or private" + validates_inclusion_of :privacy, in: %w(public private), message: 'Privacy must be public or private' scope :from_user, ->(user) { where(owner: user) } @@ -71,7 +71,7 @@ class Collection < ApplicationRecord end def user_own?(user) - return true if user.is_a? Institution and owner.users.include?(user) + return true if user.is_a?(Institution) && owner.users.include?(user) return false unless user.is_a? User user.is_admin? || owner?(user) end @@ -81,10 +81,14 @@ class Collection < ApplicationRecord end def add_items(items) - collection_items = items.map{ |item| CollectionItem.new(collection: self, - collectionable: item[:type].constantize.find(item[:id]), - order: item[:order])} - CollectionItem.import collection_items, on_duplicate_key_update: {conflict_target: [:collection_id, :collectionable_id, :collectionable_type], columns: [:order]} + collection_items = items.map do |item| + CollectionItem.new( + collection: self, + collectionable: item[:type].constantize.find(item[:id]), + order: item[:order] + ) + end + CollectionItem.import collection_items, on_duplicate_key_update: { conflict_target: [:collection_id, :collectionable_id, :collectionable_type], columns: [:order] } end def delete_items(items) @@ -93,7 +97,6 @@ class Collection < ApplicationRecord end end - ## score methods def user_category owner.try('user_category') diff --git a/app/models/collection_item.rb b/app/models/collection_item.rb index 507a75bad3a1233ab11f25bf272a50caa8e1839e..daa0525b2873446e890cfbbe6ce7f580284e7a08 100644 --- a/app/models/collection_item.rb +++ b/app/models/collection_item.rb @@ -20,4 +20,8 @@ class CollectionItem < ApplicationRecord validates :collection, :collectionable, presence: true validates :order, uniqueness: true, allow_blank: true + + def recipient + collection + end end diff --git a/app/models/complaint.rb b/app/models/complaint.rb index e8bdae3166a359daa9816217cae9a92afc2b1a92..f6ce9aa452a69d05a320b3a1ae51438f97bffcbc 100644 --- a/app/models/complaint.rb +++ b/app/models/complaint.rb @@ -29,4 +29,8 @@ class Complaint < ApplicationRecord def reason complaint_reason.reason end + + def recipient + complainable + end end diff --git a/app/models/concerns/complainable.rb b/app/models/concerns/complainable.rb index cc6e5cd103c92965c3346fab941265d9d2b1e525..72875e02eaaa82d859286be866950df189ee230c 100644 --- a/app/models/concerns/complainable.rb +++ b/app/models/concerns/complainable.rb @@ -2,7 +2,6 @@ module Complainable extend ActiveSupport::Concern included do - has_many :complaints, as: :complainable + has_many :complaints, as: :complainable end - end diff --git a/app/models/concerns/followable.rb b/app/models/concerns/followable.rb index 0aba750b7b44e1e3a01b3fc456ec770292e187c7..ef04ded79d40b282650d068ac622b6a903c0f8a6 100644 --- a/app/models/concerns/followable.rb +++ b/app/models/concerns/followable.rb @@ -14,7 +14,6 @@ module Followable # return an array of PublicActivity::Activity def activities condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)' - PublicActivity::Activity.order('created_at DESC').where(condition, {type: self.class.to_s, id: self.id}).all + PublicActivity::Activity.order('created_at DESC').where(condition, type: self.class.to_s, id: id).all end - end diff --git a/app/models/concerns/highlights.rb b/app/models/concerns/highlights.rb index 62a3d5480644ae26a88a5289f44140e583a80930..bb336eaf9b7ebaaff0b422d881d9936389bd20a3 100644 --- a/app/models/concerns/highlights.rb +++ b/app/models/concerns/highlights.rb @@ -2,8 +2,7 @@ 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) } + 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 +end diff --git a/app/models/concerns/reputationable.rb b/app/models/concerns/reputationable.rb index 13697eb0598d3d444a80c563fbda111c29bb1412..f452af019864aff73d7e7605067263079fe33b5c 100644 --- a/app/models/concerns/reputationable.rb +++ b/app/models/concerns/reputationable.rb @@ -14,7 +14,7 @@ module Reputationable return 0.0 if scores.empty? - scores.inject(0.0) { |a, e| a + e }.to_f / scores.size.to_f + scores.inject(0.0) { |acc, elem| acc + elem }.to_f / scores.size.to_f end def learning_objects_in_best_collections @@ -36,7 +36,7 @@ module Reputationable return 0.0 if reviews.nil? || reviews.size < 5 array = reviews.map(&:rates_percentage) - array.inject(0.0) { |a, e| a + e } / array.size + array.inject(0.0) { |acc, elem| acc + elem } / array.size end def learning_objects_recently_submitted @@ -46,7 +46,7 @@ module Reputationable end def collections_score_average - return 0.0 if collections.nil? || collections.where(privacy: 'public').count == 0 + return 0.0 if collections.nil? || collections.where(privacy: 'public').count.zero? collections.where(privacy: 'public').average(:score) end diff --git a/app/models/concerns/reviewable.rb b/app/models/concerns/reviewable.rb index 4e6cdbfc409abea03fb04d619446c1109f4012d5..a125c93e8c0eb3f0b46cc346ecc9efd6d3f76133 100644 --- a/app/models/concerns/reviewable.rb +++ b/app/models/concerns/reviewable.rb @@ -7,8 +7,7 @@ module Reviewable def review_ratings_average array = reviews.map(&:rating_average) - return 0.0 if array.size == 0 - array.inject(0.0) { |a, e| a + e }.to_f / array.size.to_f + return 0.0 if array.size.zero? + array.inject(0.0) { |acc, elem| acc + elem }.to_f / array.size.to_f end - end diff --git a/app/models/concerns/scoreable.rb b/app/models/concerns/scoreable.rb index bae2c9657837e531041af485604ac78b6628facb..0e101ecdb8ef197e0b15caa4912698eb04fa391c 100644 --- a/app/models/concerns/scoreable.rb +++ b/app/models/concerns/scoreable.rb @@ -31,10 +31,9 @@ module Scoreable def normalized_bookmarked max = Bookmark.where(bookmarkable_type: self.class.name).group(:bookmarkable_id).order('count_all DESC').count - value = Bookmark.where(bookmarkable_id: id, bookmarkable_type: self.class.name).count - return 0.0 if max.blank? + value = Bookmark.where(bookmarkable_id: id, bookmarkable_type: self.class.name).count average_by_max(value, max.first[1]) end @@ -44,5 +43,4 @@ module Scoreable return 0.0 if max <= 0.0 value.to_f / max.to_f end - end diff --git a/app/models/concerns/sociable.rb b/app/models/concerns/sociable.rb index 41b8b699fcfea5e2556049c0571d0195cd12c8cf..b3bf6ab3a954dc09bfb98784e35d25d668dd5cfd 100644 --- a/app/models/concerns/sociable.rb +++ b/app/models/concerns/sociable.rb @@ -43,5 +43,4 @@ module Sociable def viewed?(user) !View.where(user: user, viewable: self).blank? end - end diff --git a/app/models/concerns/stageable.rb b/app/models/concerns/stageable.rb index e601f7903727e740a8304bb3404362fc05819a41..fa63296638f14e78950fd78eff69047cebec6347 100644 --- a/app/models/concerns/stageable.rb +++ b/app/models/concerns/stageable.rb @@ -5,9 +5,7 @@ module Stageable has_many :educational_stages, through: :stage_relations, dependent: :destroy has_many :stage_relations, as: :stageable, dependent: :destroy end - - - # ~~~~ stageable actions ~~~~ # + # A stageable can add educational stages # Examples: # stageable.add_educational_stages(ids: [1, 2, 3]) @@ -28,9 +26,4 @@ module Stageable StageRelation.where(educational_stage: educational_stage, stageable: self).destroy_all end end - - # ~~~~ end stageable actions ~~~~ # - - - end diff --git a/app/models/concerns/subjectable.rb b/app/models/concerns/subjectable.rb index e171b90d021e2a9828da655319b749dfad8189cf..07a49f72ac2c03a60b45a69b79cfb08ed52e64a1 100644 --- a/app/models/concerns/subjectable.rb +++ b/app/models/concerns/subjectable.rb @@ -6,7 +6,6 @@ module Subjectable has_many :subject_relations, as: :subjectable, dependent: :destroy end - # ~~~~ subjectable actions ~~~~ # # A subjectable can add subjects # Examples: # subjectable.add_subjects(ids: [1, 2, 3]) @@ -27,10 +26,4 @@ module Subjectable SubjectRelation.where(subject: subject, subjectable: self).destroy_all end end - - # ~~~~ end subjectable actions ~~~~ # - - - - end diff --git a/app/models/concerns/tagger.rb b/app/models/concerns/tagger.rb index d1b2c3fe3af14836135c68be4ca19dc6edfaebbc..f1815b29bcdc09dcad4d1d2dd2cff9419547a4f2 100644 --- a/app/models/concerns/tagger.rb +++ b/app/models/concerns/tagger.rb @@ -5,7 +5,6 @@ module Tagger has_many :taggings, as: :tagger, dependent: :destroy end - # ~~~~ taggable actions ~~~~ # # An tagger can tagging learning object and collection # Examples: # tagger.tag(LearningObject.find(1), with: "Tag") @@ -13,7 +12,7 @@ module Tagger def tag(taggable, with: []) with.each do |tag_name| tag = Tag.where(name: tag_name).first_or_create - Tagging.where(tag: tag, tagger_id: self.id, tagger_type: self.class.name, taggable_id: taggable, taggable_type: taggable.class.name).first_or_create + Tagging.where(tag: tag, tagger_id: id, tagger_type: self.class.name, taggable_id: taggable, taggable_type: taggable.class.name).first_or_create end end @@ -22,11 +21,7 @@ module Tagger # tagger.untag(LearningObject.find(1), "Tag") # tagger.untag(Collection.find(1), "Tag") def untag(taggable, tag_name) - tag = Tag.find_by_name(tag_name) + tag = Tag.find_by(name: tag_name) Tagging.where(tagger: self, tag: tag, taggable: taggable).destroy_all end - - # ~~~~ end taggable actions ~~~~ # - - end diff --git a/app/models/concerns/thumbnailable.rb b/app/models/concerns/thumbnailable.rb index 123ffb7de4f048ba8e712fe8e04d1e6f39899613..dae374fa5008715ddd215561e9307b1ec5220519 100644 --- a/app/models/concerns/thumbnailable.rb +++ b/app/models/concerns/thumbnailable.rb @@ -5,5 +5,4 @@ module Thumbnailable has_attached_file :thumbnail, styles: { medium: '530x300', small: '250x140' }, default_url: '' validates_attachment_content_type :thumbnail, content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'] end - end diff --git a/app/models/concerns/trackable.rb b/app/models/concerns/trackable.rb index c0b2c747e084b47917e7cdbe140ad5829f25f807..3c7289986ac6cfcfc776cb183b86bd716db07771 100644 --- a/app/models/concerns/trackable.rb +++ b/app/models/concerns/trackable.rb @@ -3,7 +3,7 @@ module Trackable include PublicActivity::Model included do - tracked owner: proc { |controller, model| - model.try(:user) || model.try(:owner) || controller.try(:current_user) } + tracked owner: proc { |controller, model| model.try(:user) || model.try(:owner) || controller.try(:current_user) } + tracked recipient: proc { |_controller, model| model.try(:recipient) || model } end end diff --git a/app/models/download.rb b/app/models/download.rb index 4b64617e7f6353d90023ff4bbfc71fcd84386261..abebce69951491c8e64051b7841f3bd7995eda59 100644 --- a/app/models/download.rb +++ b/app/models/download.rb @@ -18,4 +18,8 @@ class Download < ApplicationRecord belongs_to :user validates_presence_of :user, :downloadable + + def recipient + downloadable + end end diff --git a/app/models/follow.rb b/app/models/follow.rb index cbdb5fcfdb46b63deff787fdb1470502ead2b716..602fb95dc4752f2105d30de2f7aab4ba0cb42ff9 100644 --- a/app/models/follow.rb +++ b/app/models/follow.rb @@ -19,4 +19,8 @@ class Follow < ApplicationRecord validates_presence_of :user, :followable validates :user_id, uniqueness: { scope: [:followable_id, :followable_type] } + + def recipient + followable + end end diff --git a/app/models/like.rb b/app/models/like.rb index 10ba05dd5a99971b41694bb9c98532c990234fdc..4b4b8cd75abd1234163f125568c2b2a0074412f6 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -20,4 +20,8 @@ class Like < ApplicationRecord validates_presence_of :user, :likeable validates :user_id, uniqueness: { scope: [:likeable_id, :likeable_type] } + + def recipient + likeable + end end diff --git a/app/models/rate.rb b/app/models/rate.rb index 7bf069868e9e8c04c60a51b015eabf6ec41fb9f2..32e0eb83ce9fd69c60296c248dd38b1e90cf6a17 100644 --- a/app/models/rate.rb +++ b/app/models/rate.rb @@ -20,9 +20,13 @@ class Rate < ApplicationRecord validates_uniqueness_of :user, scope: :review validate :cannot_rate_your_own, on: :create + def recipient + review + end + private def cannot_rate_your_own - errors.add(:user_id, "cannot rate your own review") if review.user == user + errors.add(:user_id, 'cannot rate your own review') if review.user == user end end diff --git a/app/models/review.rb b/app/models/review.rb index e0b4595bfb32dfd4fe579facb4dca4c944a0b6a8..089bc24cb32aadbc31cd4bd9e709199018dc2078 100644 --- a/app/models/review.rb +++ b/app/models/review.rb @@ -33,16 +33,15 @@ class Review < ApplicationRecord accepts_nested_attributes_for :review_ratings - default_scope { includes(:user) } - acts_as_paranoid has_paper_trail def rating_values - review_ratings.includes(:rating).map { |r| {rating: r.rating, value: r.value} } + review_ratings.includes(:rating).map { |r| { rating: r.rating, value: r.value } } end def rating_average + return 0.0 if review_ratings.empty? review_ratings.average(:value).floor(1).to_f end @@ -60,4 +59,8 @@ class Review < ApplicationRecord return Rate.find_by(user: user, review: self).approves if rated? user nil end + + def recipient + reviewable + end end diff --git a/app/models/review_rating.rb b/app/models/review_rating.rb index 65f4e9f674912e01b6f17ba49b82895d06fc2603..223d58ba5c9f85c2f643bd1a1a95805623ab8ece 100644 --- a/app/models/review_rating.rb +++ b/app/models/review_rating.rb @@ -14,7 +14,7 @@ class ReviewRating < ApplicationRecord belongs_to :review belongs_to :rating - validates_inclusion_of :value, in: Array(1..5), message: "Review Rating must be between 1 and 5." + validates_inclusion_of :value, in: Array(1..5), message: 'Review Rating must be between 1 and 5.' validates_presence_of :rating validates_uniqueness_of :rating, scope: :review diff --git a/app/models/role.rb b/app/models/role.rb index 890aa26750776b84e240a75301073d955d9224a9..9a9797ceaa545174bf97ded84a843df1854a9f80 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -12,22 +12,21 @@ class Role < ApplicationRecord has_and_belongs_to_many :users - validates_presence_of :name - validates_uniqueness_of :name + validates :name, presence: true, uniqueness: true def self.admin - self.find_by_name('admin') || self.create!(name: 'admin') + find_by(name: 'admin') || create!(name: 'admin') end def self.teacher - self.find_by_name('teacher') || self.create!(name: 'teacher') + find_by(name: 'teacher') || create!(name: 'teacher') end def self.student - self.find_by_name('student') || self.create!(name: 'student') + find_by(name: 'student') || create!(name: 'student') end def self.curator - self.find_by_name('curator') || self.create!(name: 'curator') + find_by(name: 'curator') || create!(name: 'curator') end end diff --git a/app/models/score.rb b/app/models/score.rb index c6d7ddea5d604ef4a7cd1f8a8cfb299caf95ddc1..64a3340a7f7acbaa6ef86b21cdb7e032dea266c7 100644 --- a/app/models/score.rb +++ b/app/models/score.rb @@ -28,6 +28,6 @@ class Score < ApplicationRecord end def category_values(category) - score_user_categories.find { |x| x.score_id == id && x.user_category_id == category.id }.map {|x| [x.code, x.value] }.to_h + score_user_categories.find { |x| x.score_id == id && x.user_category_id == category.id }.map { |x| [x.code, x.value] }.to_h end end diff --git a/app/models/share.rb b/app/models/share.rb index 94e3682eff7c55ec011f7547a6255fcb259fb747..c9f92a8d846db3ad70cceafdb915feae69d22327 100644 --- a/app/models/share.rb +++ b/app/models/share.rb @@ -18,4 +18,8 @@ class Share < ApplicationRecord belongs_to :user validates_presence_of :user, :shareable + + def recipient + shareable + end end diff --git a/app/models/tagging.rb b/app/models/tagging.rb index 613a8e9c29cca3346afa774f3f3f0c603a0b15e4..e42b96b0169237029d32da20972928cc3f2c8372 100644 --- a/app/models/tagging.rb +++ b/app/models/tagging.rb @@ -7,4 +7,8 @@ class Tagging < ApplicationRecord belongs_to :taggable, polymorphic: true validates_presence_of :tagger, :tag, :taggable + + def recipient + taggable + end end diff --git a/app/models/user.rb b/app/models/user.rb index 75f36df0bf6248435002f4aac6d51e2cd7d700c8..4a3ae3ff4899ed16653b7520b7eb1301ae753913 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -67,8 +67,8 @@ class User < ApplicationRecord after_create :default_role - has_attached_file :avatar, styles: {medium: "300x300>", thumb: "60x60>"}, default_url: '' - validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/ + has_attached_file :avatar, styles: { medium: '300x300>', thumb: '60x60>' }, default_url: '' + validates_attachment_content_type :avatar, content_type: %r{ \Aimage\/.*\Z } validates :terms_of_service, acceptance: true searchkick language: 'brazilian', match: :word_start, searchable: [:name], callbacks: :async @@ -80,21 +80,28 @@ class User < ApplicationRecord def search_data { - name: name, - score: score + name: name, + score: score } end def is_admin? roles.each do |role| - return true if role.name == "admin" + return true if role.name == 'admin' end false end def associated_collections - c = collections.blank? ? [] : collections.includes(:owner) - c + Collection.where(owner: institutions).includes(:owner) + c = collections.blank? ? [] : collections.to_a + institutions.each { |i| c.push(*i.collections.to_a) } + c + end + + def associated_learning_objects + lo = learning_objects.blank? ? [] : learning_objects.to_a + institutions.each { |i| lo.push(*i.learning_objects.to_a) } + lo end # ~~~~ followable actions ~~~~ # @@ -124,7 +131,7 @@ class User < ApplicationRecord # This function permits see what person is following. # For current user, list all users and all collections. But, # if, isn't current user, list only users, and public collections which this person follows. - def watching(followable_type, is_current_user = true) + def following(followable_type, is_current_user = true) f = Follow.where(user_id: id, followable_type: followable_type).all # If type collection, and isn't current_user, so should list only public collections @@ -140,35 +147,39 @@ class User < ApplicationRecord f end + # Returns everything that this user have followed or created + def watching + watching_array = [] + [User, Collection].each do |model| + following(model.to_s).each do |follow| + watching_array.push(*model.find(follow.followable_id)) + end + end + watching_array.push(*associated_collections) + watching_array.push(*associated_learning_objects) + watching_array.push(*reviews) + end + # ~~~~ end followable actions ~~~~ # private def default_role - roles << Role.find_by_name('teacher') + roles << Role.teacher end def self.from_omniauth(access_token) - data = access_token.info + user = User.find_by(email: user_email) + return nil if user.blank? - if access_token.provider == 'twitter' - user_email = "#{access_token.uid}@twitter.com" - else - user_email = data['email'] - end - - user = User.where(email: user_email).first - - unless user - user = User.create( - name: data['name'], - email: user_email, - provider: access_token.provider, - uid: access_token.uid, - password: Devise.friendly_token[0, 20] - ) - end - user + data = access_token.info + user_email = access_token.provider == 'twitter' ? "#{access_token.uid}@twitter.com" : data['email'] + User.create( + name: data['name'], + email: user_email, + provider: access_token.provider, + uid: access_token.uid, + password: Devise.friendly_token[0, 20] + ) end - end diff --git a/app/models/user_category.rb b/app/models/user_category.rb index bf0567cbb0cfd443d7a8c4f5b47e877c7ae23ca3..cef5ad9cd55315436573f8c6673884bbc3562f8c 100644 --- a/app/models/user_category.rb +++ b/app/models/user_category.rb @@ -24,7 +24,7 @@ class UserCategory < ApplicationRecord update(reference: ScoreCalculatorService.new(User.new).calculate(hash)) # defining maximum reference change every reference in UserCategory - UserCategory.where.not(id: id).each(&:calculate_reference) if reference == UserCategory.maximum(:reference) + UserCategory.where.not(id: id).find_each(&:calculate_reference) if reference == UserCategory.maximum(:reference) end private diff --git a/app/models/view.rb b/app/models/view.rb index 3f80ab25bce2eeb4a6e4d7936036aca8dc7733c8..c79ef6273e2656589215a767f335c5dfc5b45959 100644 --- a/app/models/view.rb +++ b/app/models/view.rb @@ -23,13 +23,17 @@ class View < ApplicationRecord scope :created_last, ->(user) { where(user: user).order('created_at DESC').limit(1) } + def recipient + viewable + end + private def current_time_greater_than_last last_view = viewable.views.created_last(user).first unless last_view.blank? - return false if Time.now < (last_view.created_at + 1.days) + return false if Time.current < (last_view.created_at + 1.day) end true diff --git a/app/serializers/public_activity/activity_serializer.rb b/app/serializers/public_activity/activity_serializer.rb index 7f5c9b8273c13a6c86cce598874cd8075691948d..c7fd51fdcf5855c55209df4f610ec4d4b5a1ed79 100644 --- a/app/serializers/public_activity/activity_serializer.rb +++ b/app/serializers/public_activity/activity_serializer.rb @@ -1,6 +1,16 @@ class PublicActivity::ActivitySerializer < ActiveModel::Serializer + def activity + object.key + end + + def recipient_type + object.recipient.class.name.demodulize + end + cache key: 'activity', expires_in: 24.hours - attributes :id, :trackable_type, :recipient, :parameters, :created_at + attributes :id, :trackable_type, :activity, :recipient_type, :parameters, :created_at + has_one :owner + has_one :recipient has_one :trackable end diff --git a/config/routes.rb b/config/routes.rb index eae43a932f55f600a335bb4247bbe0feee4ae5a2..82e59650383752625ef9e513ec5866bda75f7771 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -66,7 +66,7 @@ Rails.application.routes.draw do resources :users, concerns: [:followable, :deletable] do member do resources :bookmarks, module: 'users', only: [:index, :create, :destroy] - get 'watching/:object_type', to: 'users#watching' + get 'following/:object_type', to: 'users#following' get 'activities', to: 'activities#user_activities' end end diff --git a/test/controllers/v1/collections_controller_test.rb b/test/controllers/v1/collections_controller_test.rb index 4f0d2e19d2902be2a0b100bfe68ae6c1dbc50d51..69faa2d019d01db395ea916d7be572572c5be448 100644 --- a/test/controllers/v1/collections_controller_test.rb +++ b/test/controllers/v1/collections_controller_test.rb @@ -101,6 +101,20 @@ class V1::CollectionsControllerTest < ActionController::TestCase assert_response :unauthorized end + + test 'should anyone show public collection and return :ok' do + auth_request users(:jack) + get :show, params: { id: collections(:ufpr).id } + assert_response :ok + end + + test 'should anyone show private collection and return :unauthorized' do + auth_request users(:jack) + get :show, params: { id: collections(:private).id } + assert_response :unauthorized + end + + test 'should user add items to collection and return :ok code' do auth_request users(:john) post :add_object, params: { id: collections(:ufpr).id, collection: {items: [{id: learning_objects(:lo_metadata2).id, type: 'LearningObject'}] }} diff --git a/test/fixtures/collections.yml b/test/fixtures/collections.yml index 1482a161c91f19320a68d9e8cf8cda9c6f7fcc59..2b0c62981889bb5ca78f84a2c0ec6ead32d414ae 100644 --- a/test/fixtures/collections.yml +++ b/test/fixtures/collections.yml @@ -2,3 +2,8 @@ ufpr: name: 'UFPR collection' owner: john (User) privacy: 'public' + +private: + name: 'UFPR collection' + owner: john (User) + privacy: 'private'