Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User page: new following section #1750

Merged
merged 17 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ var initialize_modules = function() {
App.LegislationAnnotatable.initialize();
App.WatchFormChanges.initialize();
App.TreeNavigator.initialize();
App.Followable.initialize();
};

$(function(){
Expand Down
12 changes: 5 additions & 7 deletions app/assets/javascripts/followable.js.coffee
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
App.Followable =

initialize: ->
$('.followable-content a[data-toggle]').on 'click', (event) ->
event.preventDefault()

update: (followable_id, button) ->
update: (followable_id, button, notice) ->
$("#" + followable_id + " .js-follow").html(button)
# Temporary line. Waiting for issue resolution: https://github.com/consul/consul/issues/1736
initialize_modules()
if ($('[data-alert]').length > 0)
$('[data-alert]').replaceWith(notice)
else
$("body").append(notice)
31 changes: 31 additions & 0 deletions app/assets/stylesheets/layout.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1932,8 +1932,39 @@ table {
.activity {
margin-bottom: $line-height * 2;

.accordion li {
margin-bottom: $line-height / 2;

.accordion-title {
border-bottom: 1px solid $border;
background: #f8f9fb;
font-size: $small-font-size;
padding: $line-height / 2;
}

.accordion-content {
padding: 0;
}
}

.accordion .title {
display: block;
line-height: $line-height;
}

.accordion .icon {
font-size: rem-calc(20);
float: left;
margin-right: $line-height / 3;

&.icon-debates {
margin-top: rem-calc(3);
}
}

table {
border: 0;
margin-bottom: 0;
}

td {
Expand Down
11 changes: 8 additions & 3 deletions app/controllers/follows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ class FollowsController < ApplicationController
load_and_authorize_resource

def create
@followable = find_followable
@follow = Follow.create(user: current_user, followable: @followable)
@follow = Follow.create(user: current_user, followable: find_followable)
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.create.notice_html")
render :refresh_follow_button
end

def destroy
@follow = Follow.find(params[:id])
@followable = @follow.followable
@follow.destroy
flash.now[:notice] = t("shared.followable.#{followable_translation_key(@follow.followable)}.destroy.notice_html")
render :refresh_follow_button
end

Expand All @@ -20,4 +20,9 @@ def destroy
def find_followable
params[:followable_type].constantize.find(params[:followable_id])
end

def followable_translation_key(followable)
followable.class.name.parameterize("_")
end

end
26 changes: 13 additions & 13 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
class UsersController < ApplicationController
has_filters %w{proposals debates budget_investments comments}, only: :show
has_filters %w{proposals debates budget_investments comments follows}, only: :show

load_and_authorize_resource
helper_method :author?
helper_method :author_or_admin?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't really understand the reason to remove author_or_admin? method and its usage, can you explain a bit further? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bertocq The main reason is that apparently it was not being used anywhere in the application.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 you're right, I did a project search and no use, although sometimes looking for the method string is not enough if you do compose de method name with a string interpolation like send("#{this}_or_#{that}") but that's not the case I guess based on the specs passing ^_^

Nice catch & cleanup 👏 😄

helper_method :valid_interests_access?

def show
load_filtered_activity if valid_access?
load_interests if valid_interests_access?
end

private
Expand All @@ -17,7 +16,8 @@ def set_activity_counts
proposals: Proposal.where(author_id: @user.id).count,
debates: (Setting['feature.debates'] ? Debate.where(author_id: @user.id).count : 0),
budget_investments: (Setting['feature.budgets'] ? Budget::Investment.where(author_id: @user.id).count : 0),
comments: only_active_commentables.count)
comments: only_active_commentables.count,
follows: @user.follows.count)
end

def load_filtered_activity
Expand All @@ -26,7 +26,8 @@ def load_filtered_activity
when "proposals" then load_proposals
when "debates" then load_debates
when "budget_investments" then load_budget_investments
when "comments" then load_comments
when "comments" then load_comments
when "follows" then load_follows
else load_available_activity
end
end
Expand All @@ -44,6 +45,9 @@ def load_available_activity
elsif @activity_counts[:comments] > 0
load_comments
@current_filter = "comments"
elsif @activity_counts[:follows] > 0
load_follows
@current_filter = "follows"
end
end

Expand All @@ -63,8 +67,8 @@ def load_budget_investments
@budget_investments = Budget::Investment.where(author_id: @user.id).order(created_at: :desc).page(params[:page])
end

def load_interests
@user.interests
def load_follows
@follows = @user.follows.group_by(&:followable_type)
end

def valid_access?
Expand All @@ -75,12 +79,8 @@ def valid_interests_access?
@user.public_interests || authorized_current_user?
end

def author?
@author ||= current_user && (current_user == @user)
end

def author_or_admin?
@author_or_admin ||= current_user && (author? || current_user.administrator?)
def author?(proposal)
proposal.author_id == current_user.id if current_user
end

def authorized_current_user?
Expand Down
30 changes: 30 additions & 0 deletions app/helpers/followables_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module FollowablesHelper

def followable_type_title(followable_type)
t("activerecord.models.#{followable_type.underscore}.other")
end

def followable_icon(followable)
{
proposals: 'Proposal',
budget: 'Budget::Investment'
}.invert[followable]
end

def render_follow(follow)
followable = follow.followable
partial = followable_class_name(followable)
locals = {followable_class_name(followable).to_sym => followable}

render partial, locals
end

def followable_class_name(followable)
followable.class.to_s.parameterize.gsub('-', '_')
end

def find_or_build_follow(user, followable)
Follow.find_or_initialize_by(user: user, followable: followable)
end

end
56 changes: 4 additions & 52 deletions app/helpers/follows_helper.rb
Original file line number Diff line number Diff line change
@@ -1,61 +1,13 @@
module FollowsHelper

def show_follow_action?(followable)
current_user && !followed?(followable)
end

def show_unfollow_action?(followable)
current_user && followed?(followable)
end

def follow_entity_text(followable)
entity = followable.class.name.gsub('::', '/').downcase
def follow_text(followable)
entity = followable.class.name.underscore
t('shared.follow_entity', entity: t("activerecord.models.#{entity}.one").downcase)
end

def follow_entity_title(followable)
entity = followable.class.name.gsub('::', '/').downcase
t('shared.follow_entity_title', entity: t("activerecord.models.#{entity}.one").downcase)
end

def unfollow_entity_text(followable)
entity = followable.class.name.gsub('::', '/').downcase
def unfollow_text(followable)
entity = followable.class.name.underscore
t('shared.unfollow_entity', entity: t("activerecord.models.#{entity}.one").downcase)
end

def entity_full_name(followable)
name = followable.class.name
name.downcase.gsub("::", "-")
end

def follow_link_wrapper_id(followable)
"follow-expand-#{entity_full_name(followable)}-#{followable.id}"
end

def unfollow_link_wrapper_id(followable)
"unfollow-expand-#{entity_full_name(followable)}-#{followable.id}"
end

def follow_link_id(followable)
"follow-#{entity_full_name(followable)}-#{followable.id}"
end

def unfollow_link_id(followable)
"unfollow-#{entity_full_name(followable)}-#{followable.id}"
end

def follow_drop_id(followable)
"follow-drop-#{entity_full_name(followable)}-#{followable.id}"
end

def unfollow_drop_id(followable)
"unfollow-drop-#{entity_full_name(followable)}-#{followable.id}"
end

private

def followed?(followable)
Follow.followed?(current_user, followable)
end

end
2 changes: 1 addition & 1 deletion app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,4 @@ def current_administrator?
current_user && current_user.administrator?
end

end
end
2 changes: 1 addition & 1 deletion app/models/abilities/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(user)
can :create, Budget::Investment, budget: { phase: "accepting" }
can :suggest, Budget::Investment, budget: { phase: "accepting" }
can :destroy, Budget::Investment, budget: { phase: ["accepting", "reviewing"] }, author_id: user.id
can :vote, Budget::Investment, budget: { phase: "selecting" }
can :vote, Budget::Investment, budget: { phase: "selecting" }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

^_^ /bertocq likes this

can [:show, :create], Budget::Ballot, budget: { phase: "balloting" }
can [:create, :destroy], Budget::Ballot::Line, budget: { phase: "balloting" }

Expand Down
4 changes: 4 additions & 0 deletions app/models/concerns/followable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ module Followable
has_many :followers, through: :follows, source: :user
end

def followed_by?(user)
followers.include?(user)
end

end
11 changes: 0 additions & 11 deletions app/models/follow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,4 @@ class Follow < ActiveRecord::Base
validates :followable_id, presence: true
validates :followable_type, presence: true

scope(:by_user_and_followable, lambda do |user, followable|
where(user_id: user.id,
followable_type: followable.class.to_s,
followable_id: followable.id)
end)

def self.followed?(user, followable)
return false unless user
!! by_user_and_followable(user, followable).try(:first)
end

end
4 changes: 3 additions & 1 deletion app/views/budgets/investments/_investment_show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@
url: budget_investment_url(budget_id: investment.budget_id, id: investment.id)
} %>

<%= render 'follows/followable_button', followable: investment if current_user %>
<% if current_user %>
<%= render 'follows/follow_button', follow: find_or_build_follow(current_user, investment) %>
<% end %>

</aside>
</div>
Expand Down
24 changes: 24 additions & 0 deletions app/views/follows/_follow_button.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<span class="js-follow">
<span class="followable-content">

<% if follow.followable.followed_by?(current_user) %>

<%= link_to t('shared.unfollow'),
follow_path(follow),
method: :delete, remote: true,
title: unfollow_text(follow.followable),
class: 'button hollow' %>

<% else %>

<%= link_to t('shared.follow'),
follows_path(followable_id: follow.followable.id,
followable_type: follow.followable.class.name),
method: :post, remote: true,
title: follow_text(follow.followable),
class: 'button hollow' %>

<% end %>

</span>
</span>
45 changes: 0 additions & 45 deletions app/views/follows/_followable_button.html.erb

This file was deleted.

5 changes: 3 additions & 2 deletions app/views/follows/refresh_follow_button.js.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
App.Followable.update("<%= dom_id(@followable) %>",
"<%= j render('followable_button', followable: @followable) %>")
App.Followable.update("<%= dom_id(@follow.followable) %>",
"<%= j render('follow_button', follow: @follow) %>",
"<%= j render('layouts/flash') %>")
Loading