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

Enable Org Pro Dashboard View #2060

Merged
merged 11 commits into from Mar 18, 2019
18 changes: 1 addition & 17 deletions app/controllers/dashboards_controller.rb
Expand Up @@ -32,22 +32,6 @@ def show

def pro
authorize current_user, :pro_user?
@current_user_article_ids = current_user.articles.pluck(:id)
@this_week_reactions = ChartDecorator.decorate(Reaction.where(reactable_id: @current_user_article_ids, reactable_type: "Article").where("created_at > ?", 1.week.ago).order("created_at ASC"))
@this_week_reactions_count = @this_week_reactions.size
@last_week_reactions_count = Reaction.where(reactable_id: @current_user_article_ids, reactable_type: "Article").where("created_at > ? AND created_at < ?", 2.weeks.ago, 1.week.ago).size
@this_month_reactions_count = Reaction.where(reactable_id: @current_user_article_ids, reactable_type: "Article").where("created_at > ?", 1.month.ago).size
@last_month_reactions_count = Reaction.where(reactable_id: @current_user_article_ids, reactable_type: "Article").where("created_at > ? AND created_at < ?", 2.months.ago, 1.months.ago).size
@this_week_comments_count = Comment.where(commentable_id: @current_user_article_ids, commentable_type: "Article").where("created_at > ?", 1.week.ago).size
@this_week_comments = ChartDecorator.decorate(Comment.where(commentable_id: @current_user_article_ids, commentable_type: "Article").where("created_at > ?", 1.week.ago))
@last_week_comments_count = @this_week_comments.size
@this_month_comments_count = Comment.where(commentable_id: @current_user_article_ids, commentable_type: "Article").where("created_at > ?", 1.month.ago).size
@last_month_comments_count = Comment.where(commentable_id: @current_user_article_ids, commentable_type: "Article").where("created_at > ? AND created_at < ?", 2.months.ago, 1.months.ago).size
@this_week_followers_count = Follow.where(followable_id: current_user.id, followable_type: "User").where("created_at > ?", 1.week.ago).size
@last_week_followers_count = Follow.where(followable_id: current_user.id, followable_type: "User").where("created_at > ? AND created_at < ?", 2.weeks.ago, 1.week.ago).size
@this_month_followers_count = Follow.where(followable_id: current_user.id, followable_type: "User").where("created_at > ?", 1.month.ago).size
@last_month_followers_count = Follow.where(followable_id: current_user.id, followable_type: "User").where("created_at > ? AND created_at < ?", 2.months.ago, 1.months.ago).size
@reactors = User.
where(id: Reaction.where(reactable_id: @current_user_article_ids, reactable_type: "Article").order("created_at DESC").limit(100).pluck(:user_id))
@dashboard = Dashboard::Pro.new(current_user)
end
end
7 changes: 7 additions & 0 deletions app/facades/dashboard.rb
@@ -0,0 +1,7 @@
class Dashboard
attr_accessor :user
Zhao-Andy marked this conversation as resolved.
Show resolved Hide resolved

def initialize(user)
@user = user
end
end
Empty file added app/facades/dashboards/pro.rb
Empty file.
70 changes: 70 additions & 0 deletions app/facades/pro.rb
@@ -0,0 +1,70 @@
class Pro < Dashboard
def initialize(user)
super
end

def user_article_ids
user.articles.pluck(:id)
Zhao-Andy marked this conversation as resolved.
Show resolved Hide resolved
end

def this_week_reactions
ChartDecorator.decorate(Reaction.where(reactable_id: user_article_ids, reactable_type: "Article").where("created_at > ?", 1.week.ago).order("created_at ASC"))
end

def this_week_reactions_count
this_week_reactions.size
end

def last_week_reactions_count
Reaction.where(reactable_id: user_article_ids, reactable_type: "Article").where("created_at > ? AND created_at < ?", 2.weeks.ago, 1.week.ago).size
Copy link
Contributor

Choose a reason for hiding this comment

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

It's tempting to create private methods like reactions_scope and comments_scope for the repeating parts, just a suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I think I'll do that. There's definitely a better way to organize the mass amount of data.

end

def this_month_reactions_count
Reaction.where(reactable_id: user_article_ids, reactable_type: "Article").where("created_at > ?", 1.month.ago).size
end

def last_month_reactions_count
Reaction.where(reactable_id: user_article_ids, reactable_type: "Article").where("created_at > ? AND created_at < ?", 2.months.ago, 1.months.ago).size
end

def this_week_comments
ChartDecorator.decorate(Comment.where(commentable_id: user_article_ids, commentable_type: "Article").where("created_at > ?", 1.week.ago))
end

def this_week_comments_count
this_week_comments.size
end

def last_week_comments_count
Comment.where(commentable_id: user_article_ids, commentable_type: "Article").where("created_at > ? AND created_at < ?", 2.weeks.ago, 1.week.ago).size
end

def this_month_comments_count
Comment.where(commentable_id: user_article_ids, commentable_type: "Article").where("created_at > ?", 1.month.ago).size
end

def last_month_comments_count
Comment.where(commentable_id: user_article_ids, commentable_type: "Article").where("created_at > ? AND created_at < ?", 2.months.ago, 1.months.ago).size
end

def this_week_followers_count
Follow.where(followable_id: user.id, followable_type: "User").where("created_at > ?", 1.week.ago).size
end

def last_week_followers_count
Follow.where(followable_id: user.id, followable_type: "User").where("created_at > ? AND created_at < ?", 2.weeks.ago, 1.week.ago).size
end

def this_month_followers_count
Follow.where(followable_id: user.id, followable_type: "User").where("created_at > ?", 1.month.ago).size
end

def last_month_followers_count
Follow.where(followable_id: user.id, followable_type: "User").where("created_at > ? AND created_at < ?", 2.months.ago, 1.months.ago).size
end

def reactors
User.where(id: Reaction.where(reactable_id: user_article_ids, reactable_type: "Article").
order("created_at DESC").limit(100).pluck(:user_id))
end
end
52 changes: 26 additions & 26 deletions app/views/dashboards/pro.html.erb
Expand Up @@ -9,61 +9,61 @@
<div class="card">
<h4>Reactions this week</h4>
<div class="featured-stat">
<%= @this_week_reactions_count %>
<%= @dashboard.this_week_reactions_count %>
</div>
<% week_reaction_rate = @this_week_reactions_count.to_f / @last_week_reactions_count.to_f %>
<% week_reaction_rate = @dashboard.this_week_reactions_count.to_f / @dashboard.last_week_reactions_count.to_f %>
<div class="stat-percentage" style="color: <%= week_reaction_rate > 1 ? "green" : "red" %>">
<%= week_reaction_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @last_week_reactions_count.positive? ? (week_reaction_rate * 100).round(2) : "infinity " %>%
<%= week_reaction_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @dashboard.last_week_reactions_count.positive? ? (week_reaction_rate * 100).round(2) : "infinity " %>%
</div>
</div>
<div class="card">
<h4>Reactions this month</h4>
<div class="featured-stat">
<%= @this_month_reactions_count %>
<%= @dashboard.this_month_reactions_count %>
</div>
<% month_reaction_rate = @this_month_reactions_count.to_f / @last_month_reactions_count.to_f %>
<% month_reaction_rate = @dashboard.this_month_reactions_count.to_f / @dashboard.last_month_reactions_count.to_f %>
<div class="stat-percentage" style="color: <%= month_reaction_rate > 1 ? "green" : "red" %>">
<%= month_reaction_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @last_month_reactions_count.positive? ? (month_reaction_rate * 100).round(2) : "infinity " %>%
<%= month_reaction_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @dashboard.last_month_reactions_count.positive? ? (month_reaction_rate * 100).round(2) : "infinity " %>%
</div>
</div>
<div class="card">
<h4>New followers this week</h4>
<div class="featured-stat">
<%= @this_week_followers_count %>
<%= @dashboard.this_week_followers_count %>
</div>
<% week_followers_rate = @this_week_followers_count.to_f / @last_week_followers_count.to_f %>
<% week_followers_rate = @dashboard.this_week_followers_count.to_f / @dashboard.last_week_followers_count.to_f %>
<div class="stat-percentage" style="color: <%= week_followers_rate > 1 ? "green" : "red" %>">
<%= week_followers_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @last_week_followers_count.positive? ? (week_followers_rate * 100).round(2) : "infinity " %>%
<%= week_followers_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @dashboard.last_week_followers_count.positive? ? (week_followers_rate * 100).round(2) : "infinity " %>%
</div>
</div>
<div class="card">
<h4>New followers this month</h4>
<div class="featured-stat">
<%= @this_month_followers_count %>
<%= @dashboard.this_month_followers_count %>
</div>
<% month_followers_rate = @this_month_followers_count.to_f / @last_month_followers_count.to_f %>
<% month_followers_rate = @dashboard.this_month_followers_count.to_f / @dashboard.last_month_followers_count.to_f %>
<div class="stat-percentage" style="color: <%= month_followers_rate > 1 ? "green" : "red" %>">
<%= month_followers_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @last_month_followers_count.positive? ? (month_followers_rate * 100).round(2) : "infinity " %>%
<%= month_followers_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @dashboard.last_month_followers_count.positive? ? (month_followers_rate * 100).round(2) : "infinity " %>%
</div>
</div>
<div class="card">
<h4>New comments this week</h4>
<div class="featured-stat">
<%= @this_week_comments_count %>
<%= @dashboard.this_week_comments_count %>
</div>
<% week_comments_rate = @this_week_comments_count.to_f / @last_week_comments_count.to_f %>
<% week_comments_rate = @dashboard.this_week_comments_count.to_f / @dashboard.last_week_comments_count.to_f %>
<div class="stat-percentage" style="color: <%= week_comments_rate > 1 ? "green" : "red" %>">
<%= week_comments_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @last_week_followers_count.positive? ? (week_comments_rate * 100).round(2) : "infinity " %>%
<%= week_comments_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @dashboard.last_week_followers_count.positive? ? (week_comments_rate * 100).round(2) : "infinity " %>%
</div>
</div>
<div class="card">
<h4>New comments this month</h4>
<div class="featured-stat">
<%= @this_month_comments_count %>
<%= @dashboard.this_month_comments_count %>
</div>
<% month_comments_rate = @this_month_comments_count.to_f / @last_month_comments_count.to_f %>
<% month_comments_rate = @dashboard.this_month_comments_count.to_f / @dashboard.last_month_comments_count.to_f %>
<div class="stat-percentage" style="color: <%= week_comments_rate > 1 ? "green" : "red" %>">
<%= month_comments_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @last_month_comments_count.positive? ? (month_comments_rate * 100).round(2) : "infinity " %>%
<%= month_comments_rate > 1 ? "\u25b2" : "\u25BC" %> <%= @dashboard.last_month_comments_count.positive? ? (month_comments_rate * 100).round(2) : "infinity " %>%
</div>
</div>
</div>
Expand All @@ -73,11 +73,11 @@
<div class="charts-container">
<canvas
id="reactionsChart"
data-labels="<%= @this_week_reactions.formatted_dates %>"
data-total-count="<%= @this_week_reactions.total_per_day %>"
data-total-likes="<%= @this_week_reactions.total_by_type_per_day(type: "Reaction", category: "like") %>"
data-total-unicorns="<%= @this_week_reactions.total_by_type_per_day(type: "Reaction", category: "unicorn") %>"
data-total-readinglist="<%= @this_week_reactions.total_by_type_per_day(type: "Reaction", category: "readinglist") %>">
data-labels="<%= @dashboard.this_week_reactions.formatted_dates %>"
data-total-count="<%= @dashboard.this_week_reactions.total_per_day %>"
data-total-likes="<%= @dashboard.this_week_reactions.total_by_type_per_day(type: "Reaction", category: "like") %>"
data-total-unicorns="<%= @dashboard.this_week_reactions.total_by_type_per_day(type: "Reaction", category: "unicorn") %>"
data-total-readinglist="<%= @dashboard.this_week_reactions.total_by_type_per_day(type: "Reaction", category: "readinglist") %>">
</canvas>
</div>
<%= javascript_pack_tag "proCharts", defer: true %>
Expand All @@ -87,16 +87,16 @@
<div class="charts-container">
<canvas
id="commentsChart"
data-labels="<%= @this_week_comments.formatted_dates %>"
data-total-count="<%= @this_week_comments.total_per_day %>">
data-labels="<%= @dashboard.this_week_comments.formatted_dates %>"
data-total-count="<%= @dashboard.this_week_comments.total_per_day %>">
</canvas>
</div>
</div>
</div>
<h2>Activity</h2>
<h3>People who recently reacted ❤🦄🔖 to your content:</h3>
<div class="recent-reactors-container">
<% @reactors.each do |user| %>
<% @dashboard.reactors.each do |user| %>
<div class="single-article">
<a href="<%= user.path %>" class="block-link">
<h2>
Expand Down