Move and refactor method from follow to user model to get user interests. Add specification to check the discard of duplicate interests.
This commit is contained in:
@@ -7,7 +7,7 @@ class UsersController < ApplicationController
|
|||||||
|
|
||||||
def show
|
def show
|
||||||
load_filtered_activity if valid_access?
|
load_filtered_activity if valid_access?
|
||||||
load_interest if valid_interest_access?
|
load_interests if valid_interests_access?
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@@ -63,15 +63,15 @@ class UsersController < ApplicationController
|
|||||||
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
|
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
|
||||||
end
|
end
|
||||||
|
|
||||||
def load_interest
|
def load_interests
|
||||||
@interests = Follow.interests_by(@user)
|
@user.interests
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_access?
|
def valid_access?
|
||||||
@user.public_activity || authorized_current_user?
|
@user.public_activity || authorized_current_user?
|
||||||
end
|
end
|
||||||
|
|
||||||
def valid_interest_access?
|
def valid_interests_access?
|
||||||
@user.public_interests || authorized_current_user?
|
@user.public_interests || authorized_current_user?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -18,14 +18,4 @@ class Follow < ActiveRecord::Base
|
|||||||
!! by_user_and_followable(user, followable).try(:first)
|
!! by_user_and_followable(user, followable).try(:first)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.interests_by(user)
|
|
||||||
interests = []
|
|
||||||
user.follows.each do |follow|
|
|
||||||
follow.followable.tags.each do |tag|
|
|
||||||
interests << tag.name
|
|
||||||
end
|
|
||||||
end
|
|
||||||
interests.uniq
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -309,6 +309,10 @@ class User < ActiveRecord::Base
|
|||||||
where(conditions.to_hash).where(["username = ?", login]).first
|
where(conditions.to_hash).where(["username = ?", login]).first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def interests
|
||||||
|
follows.map{|follow| follow.followable.tags.map(&:name)}.flatten.compact.uniq
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def clean_document_number
|
def clean_document_number
|
||||||
|
|||||||
@@ -49,7 +49,7 @@
|
|||||||
<% if @user.public_interests || @authorized_current_user %>
|
<% if @user.public_interests || @authorized_current_user %>
|
||||||
<div id="public_interests" class="public-interests">
|
<div id="public_interests" class="public-interests">
|
||||||
<h4><%= t('account.show.public_interests_title_list') %></h4>
|
<h4><%= t('account.show.public_interests_title_list') %></h4>
|
||||||
<% @interests.in_groups_of(10, false) do |interests_group| %>
|
<% @user.interests.in_groups_of(10, false) do |interests_group| %>
|
||||||
<div class="small-4 column end">
|
<div class="small-4 column end">
|
||||||
<ul class="no-bullet">
|
<ul class="no-bullet">
|
||||||
<% interests_group.each do |interest| %>
|
<% interests_group.each do |interest| %>
|
||||||
|
|||||||
@@ -242,7 +242,7 @@ feature 'Users' do
|
|||||||
login_as(@user)
|
login_as(@user)
|
||||||
visit account_path
|
visit account_path
|
||||||
|
|
||||||
check 'account_public_interest'
|
check 'account_public_interests'
|
||||||
click_button 'Save changes'
|
click_button 'Save changes'
|
||||||
|
|
||||||
logout
|
logout
|
||||||
|
|||||||
@@ -23,17 +23,4 @@ describe Follow do
|
|||||||
expect(follow).to_not be_valid
|
expect(follow).to_not be_valid
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "interests" do
|
|
||||||
|
|
||||||
let(:user) { create(:user) }
|
|
||||||
|
|
||||||
it "interests_by user" do
|
|
||||||
proposal = create(:proposal, tag_list: "Sport")
|
|
||||||
create(:follow, :followed_proposal, followable: proposal, user: user)
|
|
||||||
|
|
||||||
expect(Follow.interests_by(user)).to eq ["Sport"]
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -657,4 +657,27 @@ describe User do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#interests" do
|
||||||
|
let(:user) { create(:user) }
|
||||||
|
|
||||||
|
it "should return followed object tags" do
|
||||||
|
proposal = create(:proposal, tag_list: "Sport")
|
||||||
|
create(:follow, followable: proposal, user: user)
|
||||||
|
|
||||||
|
expect(user.interests).to eq ["Sport"]
|
||||||
|
end
|
||||||
|
|
||||||
|
it "should discard followed objects duplicated tags" do
|
||||||
|
proposal1 = create(:proposal, tag_list: "Sport")
|
||||||
|
proposal2 = create(:proposal, tag_list: "Sport")
|
||||||
|
budget_investment = create(:budget_investment, tag_list: "Sport")
|
||||||
|
|
||||||
|
create(:follow, followable: proposal1, user: user)
|
||||||
|
create(:follow, followable: proposal2, user: user)
|
||||||
|
create(:follow, followable: budget_investment, user: user)
|
||||||
|
|
||||||
|
expect(user.interests).to eq ["Sport"]
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user