Skip to content

Commit

Permalink
Merge pull request #30 from cat-in-136/enhance_heart_index
Browse files Browse the repository at this point in the history
Introduce new activity-like heart#index
  • Loading branch information
cat-in-136 authored Jun 1, 2021
2 parents 3447abe + e1a8b1c commit 6faf494
Show file tree
Hide file tree
Showing 14 changed files with 254 additions and 286 deletions.
19 changes: 6 additions & 13 deletions .github/workflows/redmine_plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,12 @@ jobs:
fail-fast: false
matrix:
mat_env:
- '2.7 redmica/[email protected]'
- '2.7 redmine/[email protected]'
- '2.6 redmine/[email protected]'
- '2.6 redmine/[email protected]'
- '2.6 redmine/[email protected]'
- '2.5 redmine/[email protected]'
- '2.5 redmine/[email protected]'
- '2.4 redmine/[email protected]'
- '2.3 redmine/[email protected]'
- '2.7 redmica/[email protected]'
- '2.7 redmine/[email protected]'
- '2.6 redmine/[email protected]'
- '2.6 redmine/[email protected]'
- '2.6 redmine/[email protected]'
- '2.5 redmine/[email protected]'
experimental: [false]
include:
- mat_env: '2.7 redmica/redmica@master'
Expand Down Expand Up @@ -68,10 +65,6 @@ jobs:
restore-keys: |
${{ runner.os }}-gems-
- name: Use legacy bundler if ruby < 2.5 || redmine 3
if: ${{ env.RUBY_VERSION < 2.5 || startsWith(env.REDMINE_REF, '3.') }}
run: gem uninstall bundler && gem install bundler -v 1.17.3

- name: Before script
run: |
cd ./redmine
Expand Down
51 changes: 27 additions & 24 deletions app/controllers/hearts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ class HeartsController < ApplicationController
before_action :find_heartables, :only => [:heart, :unheart, :hearted_users]

def index
@offset, @limit = api_offset_and_limit

scope = Heart.of_projects(@project ? [@project] : Project.visible, User.current)
scope = scope.where.not(:user => User.current) unless params["including_myself"]
scope = scope.select(:heartable_type, :heartable_id).group(:heartable_type, :heartable_id)
scope = scope.order(Arel.sql("MAX(created_at) DESC"))
@scope_count = Heart.from(scope, :hearts).count
@hearts_pages = Paginator.new @scope_count, @limit, params["page"]
@offset ||= @hearts_pages.offset

@heartables = scope.
limit(@limit).
offset(@offset).
includes(:heartable).
map(&:heartable)
@days = Setting.activity_days_default.to_i
if params[:from]
begin; @date_to = params[:from].to_date + 1; rescue; end
end
@date_to ||= User.current.today + 1
@date_from = @date_to - @days

scope = scope.where(:created_at => @date_from...@date_to)

@heartables_with_hearts = scope.
order(:created_at => :desc).
includes(:heartable, :user => :email_address).
group_by(&:heartable)

respond_to do |format|
format.html
Expand All @@ -51,25 +52,27 @@ def index
end

def notifications
@offset, @limit = api_offset_and_limit
@user = User.current

scope = Heart.notifications_to(@user)
scope = scope.where.not(:user => User.current) unless params["including_myself"]
scope = scope.select(:heartable_type, :heartable_id).group(:heartable_type, :heartable_id)
scope = scope.order(Arel.sql("MAX(created_at) DESC"))
@scope_count = Heart.from(scope, :hearts).count
@hearts_pages = Paginator.new @scope_count, @limit, params["page"]
@offset ||= @hearts_pages.offset

@heartables = scope.
limit(@limit).
offset(@offset).
includes(:heartable).
map(&:heartable)
@days = Setting.activity_days_default.to_i
if params[:from]
begin; @date_to = params[:from].to_date + 1; rescue; end
end
@date_to ||= User.current.today + 1
@date_from = @date_to - @days

scope = scope.where(:created_at => @date_from...@date_to)

@heartables_with_hearts = scope.
order(:created_at => :desc).
includes(:heartable, :user => :email_address).
group_by(&:heartable)

respond_to do |format|
format.html
format.api
end
end

Expand Down
32 changes: 6 additions & 26 deletions app/models/heart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Heart < ActiveRecord::Base
validates_uniqueness_of :user_id, :scope => [:heartable_type, :heartable_id]
validate :validate_user

scope :of_projects, lambda { |*args|
def self.of_projects(*args)
projects = args.size > 0 ? args.shift : Project.none
user = args.size > 0 ? args.shift : nil
raise ArgumentError if args.size > 0
Expand All @@ -50,21 +50,11 @@ class Heart < ActiveRecord::Base
end
Heart.where(:heartable => heartables)
}.reduce { |scope1, scope2|
if ActiveRecord::VERSION::MAJOR >= 5 # Rails.version >= "5"
scope1.or(scope2)
else
Heart.where(
Heart.arel_table.grouping(scope1.where_values.reduce(:and)).or(
Heart.arel_table.grouping(scope2.where_values.reduce(:and))
)
).tap { |scope12|
scope12.bind_values = scope1.bind_values + scope2.bind_values
}
end
scope1.or(scope2)
}
}
end

scope :notifications_to, lambda { |user|
def self.notifications_to(user)
raise ArgumentError unless user

ActiveRecord::Base.subclasses.select { |klass|
Expand All @@ -80,19 +70,9 @@ class Heart < ActiveRecord::Base
Heart.none
end
}.reduce { |scope1, scope2|
if Rails.version >= "5"
scope1.or(scope2)
else
Heart.where(
Heart.arel_table.grouping(scope1.where_values.reduce(:and)).or(
Heart.arel_table.grouping(scope2.where_values.reduce(:and))
)
).tap { |scope12|
scope12.bind_values = scope1.bind_values + scope2.bind_values
}
end
scope1.or(scope2)
}
}
end

def self.any_hearted?(objects, user)
objects = objects.reject(&:new_record?)
Expand Down
17 changes: 5 additions & 12 deletions app/views/hearts/_recent_heart_list.html.erb
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
<%# frozen_string_literal: true %>
<% if @heartables.empty? %>

<% if @heartables_with_hearts.empty? %>
<p class="nodata"><%=l :label_no_data %></p>
<% else %>
<% hearts_grouped = Heart.hearts_to(@heartables).
order(:created_at => :desc).
preload(:user => :email_address).
group_by { |v| [v.heartable_type, v.heartable_id] } %>
<ul class="recent-heart-list">
<% @heartables.each do |heartable| %>
<% hearts = hearts_grouped[[heartable.class.base_class.name, heartable.id]] || [] %>
<% heart_links = multiple_heart_links_with_counters(@heartables_with_hearts.keys, User.current) %>
<% @heartables_with_hearts.each do |heartable,hearts| %>
<li>
<div>
<span class="heartable-link">
<%= link_to_project(heartable.project) + ": " %>
<%= link_to_heartable heartable %>
</span>
<% heart_bool = hearts.any? { |v| v.user_id == User.current.id } %>
<% hearted_users_count = hearts.length %>
<%= heart_link_with_counter_manual heartable, heart_bool, hearted_users_count, User.current %>
<%= heart_links.shift %>
</div>
<ul>
<% hearts = hearts.reject { |v| v[:user_id] == User.current.id } unless params["including_myself"] %>
<% hearts.each do |heart| %>
<li class="author">
<%= avatar(heart.user, :size => "24") %>
Expand All @@ -31,5 +25,4 @@
</li>
<% end %>
</ul>
<span class="pagination"><%= pagination_links_full @hearts_pages %></span>
<% end %>
12 changes: 2 additions & 10 deletions app/views/hearts/index.api.rsb
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
# frozen_string_literal: true
api.array :heartables, api_meta(:total_count => @scope_count, :offset => @offset, :limit => @limit, :including_myself => !!params["including_myself"]) do
hearts_grouped = Heart.hearts_to(@heartables).
order(:created_at => :desc).
preload(:user => :email_address).
group_by { |v| [v.heartable_type, v.heartable_id] }
@heartables.each do |heartable|
hearts = hearts_grouped[[heartable.class.base_class.name, heartable.id]] || []
api.array :heartables, api_meta(:range_from => @date_from, :range_to => @date_to, :days => @days, :from => params[:from], :including_myself => !!params["including_myself"]) do
@heartables_with_hearts.each do |heartable,hearts|
api.heartable do
render_api_heartable_include(heartable, api)

hearted_users_count = hearts.length
hearts = hearts.reject { |v| v[:user_id] == User.current.id } unless params["including_myself"]
api.hearted_users_count hearted_users_count
api.array :hearts do
hearts.each do |heart|
api.heart do
Expand Down
41 changes: 32 additions & 9 deletions app/views/hearts/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
<%# frozen_string_literal: true %>

<div class="contextual">
<%= form_tag({}, :method => :get) do %>
<label for="including_myself">
<%= check_box_tag 'including_myself', 1, params[:including_myself], :onchange => "this.form.submit();" %>
<%=l :including_hearted_myself %>
</label>
<% end %>
</div>

<h2><%= l(:recently_hearted_objects) %></h2>

<p class="subtitle"><%= l(:label_date_from_to, :start => format_date(@date_to - @days), :end => format_date(@date_to-1)) %></p>

<%= render :partial => "hearts/recent_heart_list" %>

<span class="pagination">
<ul class="pages">
<li class="previous page">
<%= link_to("\xc2\xab " + l(:label_previous),
{:params => request.query_parameters.merge(:from => @date_to - @days - 1)},
:title => l(:label_date_from_to, :start => format_date(@date_to - 2*@days), :end => format_date(@date_to - @days - 1)),
:accesskey => accesskey(:previous)) %>
</li><% unless @date_to > User.current.today %><li class="next page">
<%= link_to(l(:label_next) + " \xc2\xbb",
{:params => request.query_parameters.merge(:from => @date_to + @days - 1)},
:title => l(:label_date_from_to, :start => format_date(@date_to), :end => format_date(@date_to + @days - 1)),
:accesskey => accesskey(:next)) %><% end %>
</li>
</ul>
</span>

<%= content_for :sidebar do %>
<%= form_tag({}, :method => :get, :id => 'hearts_index_scope_form') do %>
<h3><%= l(:recently_hearted_objects) %></h3>
<p>
<%= t(:label_hearted_days_to_html, :days => @days, :date => date_field_tag('from', '', :value => (@date_to - 1), :size => 10)) %>
<%= calendar_for('from') %>
</p>
<p>
<label for="including_myself">
<%= check_box_tag 'including_myself', 1, params[:including_myself] %>
<%=l :including_hearted_myself %>
</label>
</p>
<p><%= submit_tag l(:button_apply), :class => 'button-small', :name => 'submit' %></p>
<% end %>
<%= render :partial => "hearts/sidebar" %>
<% end %>

Expand Down
17 changes: 17 additions & 0 deletions app/views/hearts/notifications.api.rsb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true
api.array :heartables, api_meta(:range_from => @date_from, :range_to => @date_to, :days => @days, :from => params[:from], :including_myself => !!params["including_myself"]) do
@heartables_with_hearts.each do |heartable,hearts|
api.heartable do
render_api_heartable_include(heartable, api)

api.array :hearts do
hearts.each do |heart|
api.heart do
api.user(:id => heart.user_id, :name => heart.user.name) unless heart.user.nil?
api.created_at heart.created_at
end
end
end
end
end
end
26 changes: 26 additions & 0 deletions app/views/hearts/notifications.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,35 @@

<h2><%= l(:hearts_notification_label) %></h2>

<p class="subtitle"><%= l(:label_date_from_to, :start => format_date(@date_to - @days), :end => format_date(@date_to-1)) %></p>

<%= render :partial => "hearts/recent_heart_list" %>

<span class="pagination">
<ul class="pages">
<li class="previous page">
<%= link_to("\xc2\xab " + l(:label_previous),
{:params => request.query_parameters.merge(:from => @date_to - @days - 1)},
:title => l(:label_date_from_to, :start => format_date(@date_to - 2*@days), :end => format_date(@date_to - @days - 1)),
:accesskey => accesskey(:previous)) %>
</li><% unless @date_to > User.current.today %><li class="next page">
<%= link_to(l(:label_next) + " \xc2\xbb",
{:params => request.query_parameters.merge(:from => @date_to + @days - 1)},
:title => l(:label_date_from_to, :start => format_date(@date_to), :end => format_date(@date_to + @days - 1)),
:accesskey => accesskey(:next)) %><% end %>
</li>
</ul>
</span>

<%= content_for :sidebar do %>
<%= form_tag({}, :method => :get, :id => 'hearts_index_scope_form') do %>
<h3><%= l(:hearts_notification_label) %></h3>
<p>
<%= t(:label_hearted_days_to_html, :days => @days, :date => date_field_tag('from', '', :value => (@date_to - 1), :size => 10)) %>
<%= calendar_for('from') %>
</p>
<p><%= submit_tag l(:button_apply), :class => 'button-small', :name => 'submit' %></p>
<% end %>
<%= render :partial => "hearts/sidebar" %>
<% end %>

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ en:
recently_hearted_by: "Recently Liked by %{author}"
recently_hearted_by_me: "Recently Liked by me"
including_hearted_myself: Include liked by myself
label_hearted_days_to_html: "%{days} days up to %{date}"
label_hearted_time: "Liked %{age} ago"
label_hearted_time_by: "Liked by %{author} %{age} ago"
hearts_link_label: Like
Expand Down
1 change: 1 addition & 0 deletions config/locales/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ja:
recently_hearted_by: "%{author}が最近いいね!した"
recently_hearted_by_me: "私が最近いいね!した"
including_hearted_myself: 自分自身のいいね!も含める
label_hearted_days_to_html: "%{date} までの%{days}日間"
label_hearted_time: "%{age}前にいいね!しました"
label_hearted_time_by: "%{author}が%{age}前にいいね!しました"
hearts_link_label: いいね!
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/001_create_hearts.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateHearts < ((Rails.version > "5")? ActiveRecord::Migration[4.2] : ActiveRecord::Migration)
class CreateHearts < ActiveRecord::Migration[4.2]
def change
create_table :hearts do |t|
t.references :heartable, :polymorphic => true, :index => true
Expand Down
2 changes: 1 addition & 1 deletion init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
name 'Redmine Hearts plugin'
author '@cat_in_136'
description 'provide intra-Redmine Like/Fav reactions'
version '1.0.5'
version '2.0.0'
url 'https://github.com/cat-in-136/redmine_hearts'
author_url 'https://github.com/cat-in-136/'

Expand Down
Loading

0 comments on commit 6faf494

Please sign in to comment.