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
|
||||
load_filtered_activity if valid_access?
|
||||
load_interest if valid_interest_access?
|
||||
load_interests if valid_interests_access?
|
||||
end
|
||||
|
||||
private
|
||||
@@ -63,15 +63,15 @@ class UsersController < ApplicationController
|
||||
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
|
||||
end
|
||||
|
||||
def load_interest
|
||||
@interests = Follow.interests_by(@user)
|
||||
def load_interests
|
||||
@user.interests
|
||||
end
|
||||
|
||||
def valid_access?
|
||||
@user.public_activity || authorized_current_user?
|
||||
end
|
||||
|
||||
def valid_interest_access?
|
||||
def valid_interests_access?
|
||||
@user.public_interests || authorized_current_user?
|
||||
end
|
||||
|
||||
|
||||
@@ -18,14 +18,4 @@ class Follow < ActiveRecord::Base
|
||||
!! by_user_and_followable(user, followable).try(:first)
|
||||
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
|
||||
|
||||
@@ -309,6 +309,10 @@ class User < ActiveRecord::Base
|
||||
where(conditions.to_hash).where(["username = ?", login]).first
|
||||
end
|
||||
|
||||
def interests
|
||||
follows.map{|follow| follow.followable.tags.map(&:name)}.flatten.compact.uniq
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def clean_document_number
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
<% if @user.public_interests || @authorized_current_user %>
|
||||
<div id="public_interests" class="public-interests">
|
||||
<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">
|
||||
<ul class="no-bullet">
|
||||
<% interests_group.each do |interest| %>
|
||||
|
||||
@@ -242,7 +242,7 @@ feature 'Users' do
|
||||
login_as(@user)
|
||||
visit account_path
|
||||
|
||||
check 'account_public_interest'
|
||||
check 'account_public_interests'
|
||||
click_button 'Save changes'
|
||||
|
||||
logout
|
||||
|
||||
@@ -23,17 +23,4 @@ describe Follow do
|
||||
expect(follow).to_not be_valid
|
||||
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
|
||||
|
||||
@@ -657,4 +657,27 @@ describe User do
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user