Skip to content
Snippets Groups Projects
Commit e9b7bc35 authored by Mateus Rambo Strey's avatar Mateus Rambo Strey
Browse files

Merge branch 'master' into rspec_doc

parents 965a42d3 8be805f7
No related branches found
No related tags found
No related merge requests found
Showing
with 48 additions and 68 deletions
...@@ -8,7 +8,7 @@ class V1::ActivitiesController < ApplicationController ...@@ -8,7 +8,7 @@ class V1::ActivitiesController < ApplicationController
# Render all activities that logged user can see # Render all activities that logged user can see
def index def index
authorize :activity, :index? authorize :activity, :index?
activities = paginate ::ActivityPolicy::Scope.new(current_user, ::PublicActivity::Activity).resolve activities = paginate current_user.activities
render json: activities render json: activities
end end
......
...@@ -12,20 +12,12 @@ class V1::FeedController < ApplicationController ...@@ -12,20 +12,12 @@ class V1::FeedController < ApplicationController
private private
#TODO: Tests
def activities_followed def activities_followed
activities = [] activities = []
types.each do |type| current_user.watching.each do |watching|
model = type.classify.constantize activities.push(*watching.activities.to_a)
current_user.watching(type).each do |follow|
followed = model.find(follow.followable_id)
activities.push(*followed.activities.to_a)
end
end end
activities activities
end end
def types
['User', 'Collection']
end
end end
...@@ -4,8 +4,8 @@ class V1::UsersController < ApplicationController ...@@ -4,8 +4,8 @@ class V1::UsersController < ApplicationController
include ::Paginator include ::Paginator
before_action :set_user, only: [:show, :update, :destroy, :watching] before_action :set_user, only: [:show, :update, :destroy, :following]
before_action :authenticate_user!, only: [:create, :update, :destroy, :watching] before_action :authenticate_user!, only: [:create, :update, :destroy, :following]
# GET /v1/users # GET /v1/users
# GET /v1/users.json # GET /v1/users.json
...@@ -54,11 +54,11 @@ class V1::UsersController < ApplicationController ...@@ -54,11 +54,11 @@ class V1::UsersController < ApplicationController
end end
end end
def watching def following
type = params[:object_type] type = params[:object_type]
is_current = (@user.id == current_user.id) unless current_user.nil? is_current = (@user.id == current_user.id) unless current_user.nil?
return render status: :bad_request unless type.in? %w(User Collection) 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 render json: w, root: 'follows', status: :ok
end end
......
...@@ -20,4 +20,8 @@ class Bookmark < ApplicationRecord ...@@ -20,4 +20,8 @@ class Bookmark < ApplicationRecord
validates_presence_of :user, :bookmarkable validates_presence_of :user, :bookmarkable
validates :user_id, uniqueness: { scope: [:bookmarkable_id, :bookmarkable_type] } validates :user_id, uniqueness: { scope: [:bookmarkable_id, :bookmarkable_type] }
def recipient
bookmarkable
end
end end
...@@ -15,8 +15,8 @@ ...@@ -15,8 +15,8 @@
class Carousel < ApplicationRecord class Carousel < ApplicationRecord
has_attached_file :image, styles: { has_attached_file :image, styles: {
larger: "600x600>", larger: '600x600>',
thumbnail: "100x100>" thumbnail: '100x100>'
}, default_url: "/images/:style/missing.png" }
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
end end
...@@ -43,7 +43,7 @@ class Collection < ApplicationRecord ...@@ -43,7 +43,7 @@ class Collection < ApplicationRecord
belongs_to :owner, polymorphic: true belongs_to :owner, polymorphic: true
validates :name, :owner, presence: 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) } scope :from_user, ->(user) { where(owner: user) }
...@@ -71,7 +71,7 @@ class Collection < ApplicationRecord ...@@ -71,7 +71,7 @@ class Collection < ApplicationRecord
end end
def user_own?(user) 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 return false unless user.is_a? User
user.is_admin? || owner?(user) user.is_admin? || owner?(user)
end end
...@@ -81,10 +81,14 @@ class Collection < ApplicationRecord ...@@ -81,10 +81,14 @@ class Collection < ApplicationRecord
end end
def add_items(items) def add_items(items)
collection_items = items.map{ |item| CollectionItem.new(collection: self, collection_items = items.map do |item|
collectionable: item[:type].constantize.find(item[:id]), CollectionItem.new(
order: item[:order])} collection: self,
CollectionItem.import collection_items, on_duplicate_key_update: {conflict_target: [:collection_id, :collectionable_id, :collectionable_type], columns: [:order]} 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 end
def delete_items(items) def delete_items(items)
...@@ -93,7 +97,6 @@ class Collection < ApplicationRecord ...@@ -93,7 +97,6 @@ class Collection < ApplicationRecord
end end
end end
## score methods ## score methods
def user_category def user_category
owner.try('user_category') owner.try('user_category')
......
...@@ -20,4 +20,8 @@ class CollectionItem < ApplicationRecord ...@@ -20,4 +20,8 @@ class CollectionItem < ApplicationRecord
validates :collection, :collectionable, presence: true validates :collection, :collectionable, presence: true
validates :order, uniqueness: true, allow_blank: true validates :order, uniqueness: true, allow_blank: true
def recipient
collection
end
end end
...@@ -29,4 +29,8 @@ class Complaint < ApplicationRecord ...@@ -29,4 +29,8 @@ class Complaint < ApplicationRecord
def reason def reason
complaint_reason.reason complaint_reason.reason
end end
def recipient
complainable
end
end end
...@@ -2,7 +2,6 @@ module Complainable ...@@ -2,7 +2,6 @@ module Complainable
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
has_many :complaints, as: :complainable has_many :complaints, as: :complainable
end end
end end
...@@ -14,7 +14,6 @@ module Followable ...@@ -14,7 +14,6 @@ module Followable
# return an array of PublicActivity::Activity # return an array of PublicActivity::Activity
def activities def activities
condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)' 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
end end
...@@ -2,8 +2,7 @@ module Highlights ...@@ -2,8 +2,7 @@ module Highlights
extend ActiveSupport::Concern extend ActiveSupport::Concern
included do included do
scope :this_week, -> (limit = 1000) { where('created_at >= ?', 1.week.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) } scope :this_month, ->(limit = 1000) { where('created_at >= ?', 1.month.ago).order(score: :desc).limit(limit) }
end end
end
end
\ No newline at end of file
...@@ -14,7 +14,7 @@ module Reputationable ...@@ -14,7 +14,7 @@ module Reputationable
return 0.0 if scores.empty? 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 end
def learning_objects_in_best_collections def learning_objects_in_best_collections
...@@ -36,7 +36,7 @@ module Reputationable ...@@ -36,7 +36,7 @@ module Reputationable
return 0.0 if reviews.nil? || reviews.size < 5 return 0.0 if reviews.nil? || reviews.size < 5
array = reviews.map(&:rates_percentage) 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 end
def learning_objects_recently_submitted def learning_objects_recently_submitted
...@@ -46,7 +46,7 @@ module Reputationable ...@@ -46,7 +46,7 @@ module Reputationable
end end
def collections_score_average 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) collections.where(privacy: 'public').average(:score)
end end
......
...@@ -7,8 +7,7 @@ module Reviewable ...@@ -7,8 +7,7 @@ module Reviewable
def review_ratings_average def review_ratings_average
array = reviews.map(&:rating_average) array = reviews.map(&:rating_average)
return 0.0 if array.size == 0 return 0.0 if array.size.zero?
array.inject(0.0) { |a, e| a + e }.to_f / array.size.to_f array.inject(0.0) { |acc, elem| acc + elem }.to_f / array.size.to_f
end end
end end
...@@ -31,10 +31,9 @@ module Scoreable ...@@ -31,10 +31,9 @@ module Scoreable
def normalized_bookmarked def normalized_bookmarked
max = Bookmark.where(bookmarkable_type: self.class.name).group(:bookmarkable_id).order('count_all DESC').count 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? 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]) average_by_max(value, max.first[1])
end end
...@@ -44,5 +43,4 @@ module Scoreable ...@@ -44,5 +43,4 @@ module Scoreable
return 0.0 if max <= 0.0 return 0.0 if max <= 0.0
value.to_f / max.to_f value.to_f / max.to_f
end end
end end
...@@ -43,5 +43,4 @@ module Sociable ...@@ -43,5 +43,4 @@ module Sociable
def viewed?(user) def viewed?(user)
!View.where(user: user, viewable: self).blank? !View.where(user: user, viewable: self).blank?
end end
end end
...@@ -5,9 +5,7 @@ module Stageable ...@@ -5,9 +5,7 @@ module Stageable
has_many :educational_stages, through: :stage_relations, dependent: :destroy has_many :educational_stages, through: :stage_relations, dependent: :destroy
has_many :stage_relations, as: :stageable, dependent: :destroy has_many :stage_relations, as: :stageable, dependent: :destroy
end end
# ~~~~ stageable actions ~~~~ #
# A stageable can add educational stages # A stageable can add educational stages
# Examples: # Examples:
# stageable.add_educational_stages(ids: [1, 2, 3]) # stageable.add_educational_stages(ids: [1, 2, 3])
...@@ -28,9 +26,4 @@ module Stageable ...@@ -28,9 +26,4 @@ module Stageable
StageRelation.where(educational_stage: educational_stage, stageable: self).destroy_all StageRelation.where(educational_stage: educational_stage, stageable: self).destroy_all
end end
end end
# ~~~~ end stageable actions ~~~~ #
end end
...@@ -6,7 +6,6 @@ module Subjectable ...@@ -6,7 +6,6 @@ module Subjectable
has_many :subject_relations, as: :subjectable, dependent: :destroy has_many :subject_relations, as: :subjectable, dependent: :destroy
end end
# ~~~~ subjectable actions ~~~~ #
# A subjectable can add subjects # A subjectable can add subjects
# Examples: # Examples:
# subjectable.add_subjects(ids: [1, 2, 3]) # subjectable.add_subjects(ids: [1, 2, 3])
...@@ -27,10 +26,4 @@ module Subjectable ...@@ -27,10 +26,4 @@ module Subjectable
SubjectRelation.where(subject: subject, subjectable: self).destroy_all SubjectRelation.where(subject: subject, subjectable: self).destroy_all
end end
end end
# ~~~~ end subjectable actions ~~~~ #
end end
...@@ -5,7 +5,6 @@ module Tagger ...@@ -5,7 +5,6 @@ module Tagger
has_many :taggings, as: :tagger, dependent: :destroy has_many :taggings, as: :tagger, dependent: :destroy
end end
# ~~~~ taggable actions ~~~~ #
# An tagger can tagging learning object and collection # An tagger can tagging learning object and collection
# Examples: # Examples:
# tagger.tag(LearningObject.find(1), with: "Tag") # tagger.tag(LearningObject.find(1), with: "Tag")
...@@ -13,7 +12,7 @@ module Tagger ...@@ -13,7 +12,7 @@ module Tagger
def tag(taggable, with: []) def tag(taggable, with: [])
with.each do |tag_name| with.each do |tag_name|
tag = Tag.where(name: tag_name).first_or_create 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
end end
...@@ -22,11 +21,7 @@ module Tagger ...@@ -22,11 +21,7 @@ module Tagger
# tagger.untag(LearningObject.find(1), "Tag") # tagger.untag(LearningObject.find(1), "Tag")
# tagger.untag(Collection.find(1), "Tag") # tagger.untag(Collection.find(1), "Tag")
def untag(taggable, tag_name) 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 Tagging.where(tagger: self, tag: tag, taggable: taggable).destroy_all
end end
# ~~~~ end taggable actions ~~~~ #
end end
...@@ -5,5 +5,4 @@ module Thumbnailable ...@@ -5,5 +5,4 @@ module Thumbnailable
has_attached_file :thumbnail, styles: { medium: '530x300', small: '250x140' }, default_url: '' 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'] validates_attachment_content_type :thumbnail, content_type: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif']
end end
end end
...@@ -3,7 +3,7 @@ module Trackable ...@@ -3,7 +3,7 @@ module Trackable
include PublicActivity::Model include PublicActivity::Model
included do included do
tracked owner: proc { |controller, model| tracked owner: proc { |controller, model| model.try(:user) || model.try(:owner) || controller.try(:current_user) }
model.try(:user) || model.try(:owner) || controller.try(:current_user) } tracked recipient: proc { |_controller, model| model.try(:recipient) || model }
end end
end end
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