biovision-vote 0.2.180610 → 0.4.190905.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/assets/javascripts/biovision/vote/biovision-vote.js +1 -1
- data/app/controllers/admin/votes_controller.rb +10 -2
- data/app/controllers/votes_controller.rb +10 -5
- data/app/models/concerns/votable_item.rb +4 -1
- data/app/models/vote.rb +17 -10
- data/app/services/biovision/components/vote_component.rb +24 -0
- data/app/views/admin/components/links/_vote.html.erb +3 -0
- data/app/views/admin/votes/index.html.erb +2 -1
- data/app/views/votes/_vote_block.html.erb +7 -8
- data/config/locales/votes-en.yml +4 -0
- data/config/locales/votes-ru.yml +6 -2
- data/config/locales/votes-sv.yml +41 -0
- data/config/routes.rb +4 -4
- data/db/migrate/20170330000001_create_votes.rb +25 -15
- data/lib/biovision/vote/version.rb +3 -1
- metadata +8 -22
- data/app/views/admin/index/dashboard/_biovision_vote.html.erb +0 -10
- data/db/migrate/20180610110015_add_slug_to_votes.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b9622a08b05afd38c9d1c3114c2abc19e83f12b64729060216cfd5f7acf8df19
|
4
|
+
data.tar.gz: 5c561a9230de49435f88ecec4e7aab3d04bc60973aa942af44d17268ac9bc2ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9fdc9b015b3cac71815c8a0c340b7130338b777c001f645abd758da328b8b762519bc742d32db0f35a882d240b2415ea9ee236d353ab59d6712b2e1473f1ea6
|
7
|
+
data.tar.gz: 39e630aeb2517455a084bdcdda72fb51bb43b6ad45e9041de6792a7df8cdb9fad052845713a336156d07546ad22806ae9ad6191ae4f308b2bc14c549f52faaf9
|
@@ -32,7 +32,7 @@ document.addEventListener('DOMContentLoaded', function () {
|
|
32
32
|
handle_ajax_failure(result);
|
33
33
|
};
|
34
34
|
|
35
|
-
const request = Biovision.
|
35
|
+
const request = Biovision.jsonAjaxRequest('POST', $form.getAttribute('action'), on_success, on_failure);
|
36
36
|
|
37
37
|
$button.classList.add('switch');
|
38
38
|
|
@@ -1,12 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Administrative part of vote handling
|
1
4
|
class Admin::VotesController < AdminController
|
2
5
|
# get /admin/votes
|
3
6
|
def index
|
4
|
-
@collection = Vote.
|
7
|
+
@collection = Vote.list_for_administration.page(current_page)
|
5
8
|
end
|
6
9
|
|
7
10
|
private
|
8
11
|
|
12
|
+
def component_slug
|
13
|
+
Biovision::Components::VoteComponent::SLUG
|
14
|
+
end
|
15
|
+
|
9
16
|
def restrict_access
|
10
|
-
|
17
|
+
error = 'Managing votes is not allowed'
|
18
|
+
handle_http_401(error) unless component_handler.allow?('moderator')
|
11
19
|
end
|
12
20
|
end
|
@@ -1,5 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Handling votes
|
1
4
|
class VotesController < ApplicationController
|
2
|
-
before_action :set_entity, only:
|
5
|
+
before_action :set_entity, only: :destroy
|
3
6
|
|
4
7
|
# post /votes
|
5
8
|
def create
|
@@ -19,6 +22,10 @@ class VotesController < ApplicationController
|
|
19
22
|
|
20
23
|
private
|
21
24
|
|
25
|
+
def component_slug
|
26
|
+
Biovision::Components::VoteComponent::SLUG
|
27
|
+
end
|
28
|
+
|
22
29
|
def set_entity
|
23
30
|
@entity = Vote.find_by(id: params[:id])
|
24
31
|
if @entity.nil? || !@entity.editable_by?(current_user)
|
@@ -28,7 +35,7 @@ class VotesController < ApplicationController
|
|
28
35
|
|
29
36
|
def creation_parameters
|
30
37
|
parameters = params.require(:vote).permit(Vote.creation_parameters)
|
31
|
-
parameters.merge(owner_for_entity(true))
|
38
|
+
parameters.merge(owner_for_entity(true)).merge(slug: visitor_slug)
|
32
39
|
end
|
33
40
|
|
34
41
|
def process_vote
|
@@ -41,9 +48,7 @@ class VotesController < ApplicationController
|
|
41
48
|
|
42
49
|
def count_vote
|
43
50
|
@entity.save
|
44
|
-
|
45
|
-
Metric.register(name)
|
46
|
-
Metric.register(Vote::METRIC_VOTE_HIT)
|
51
|
+
component_handler.count_vote(@entity)
|
47
52
|
|
48
53
|
render :result, status: :created
|
49
54
|
end
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module VotableItem
|
2
4
|
extend ActiveSupport::Concern
|
3
5
|
|
4
6
|
included do
|
5
|
-
has_many :votes, as: :votable, dependent: :
|
7
|
+
has_many :votes, as: :votable, dependent: :delete_all
|
6
8
|
end
|
7
9
|
|
8
10
|
# @param [Vote|String] vote_or_slug
|
@@ -25,6 +27,7 @@ module VotableItem
|
|
25
27
|
def voted(vote_or_slug)
|
26
28
|
vote = vote_or_slug.is_a?(String) ? votes.find_by(slug: vote_or_slug) : vote_or_slug
|
27
29
|
return :none if vote&.id.nil?
|
30
|
+
|
28
31
|
vote.upvote? ? :upvote : :downvote
|
29
32
|
end
|
30
33
|
end
|
data/app/models/vote.rb
CHANGED
@@ -1,16 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Vote
|
4
|
+
#
|
5
|
+
# Attributes:
|
6
|
+
# agent_id [Agent], optional
|
7
|
+
# created_at [DateTime]
|
8
|
+
# delta [Integer]
|
9
|
+
# ip [Inet], optional
|
10
|
+
# slug [String]
|
11
|
+
# updated_at [DateTime]
|
12
|
+
# user_id [User], optional
|
13
|
+
# votable_id [Integer]
|
14
|
+
# votable_type [String]
|
1
15
|
class Vote < ApplicationRecord
|
2
16
|
include HasOwner
|
3
17
|
|
4
|
-
METRIC_VOTE_HIT = 'votes.any.hit'
|
5
|
-
METRIC_UPVOTE_HIT = 'votes.upvote.hit'
|
6
|
-
METRIC_DOWNVOTE_HIT = 'votes.downvote.hit'
|
7
|
-
|
8
18
|
belongs_to :user, optional: true
|
9
19
|
belongs_to :agent, optional: true
|
10
20
|
belongs_to :votable, polymorphic: true
|
11
21
|
|
12
22
|
before_validation { self.delta = (delta.to_i > 0 ? 1 : -1) }
|
13
|
-
before_validation
|
23
|
+
before_validation { self.slug = current_slug if slug.blank? }
|
14
24
|
validates_uniqueness_of :votable_id, scope: [:slug, :votable_type]
|
15
25
|
|
16
26
|
after_create :add_vote_result
|
@@ -51,7 +61,7 @@ class Vote < ApplicationRecord
|
|
51
61
|
end
|
52
62
|
|
53
63
|
def self.creation_parameters
|
54
|
-
%i
|
64
|
+
%i[delta votable_id votable_type]
|
55
65
|
end
|
56
66
|
|
57
67
|
def upvote?
|
@@ -73,14 +83,11 @@ class Vote < ApplicationRecord
|
|
73
83
|
|
74
84
|
private
|
75
85
|
|
76
|
-
def generate_slug
|
77
|
-
self.slug = current_slug
|
78
|
-
end
|
79
|
-
|
80
86
|
def add_vote_result
|
81
87
|
votable.vote_result = votable.vote_result + delta
|
82
88
|
votable.upvote_count = votable.upvote_count + 1 if upvote?
|
83
89
|
votable.downvote_count = votable.downvote_count + 1 if downvote?
|
90
|
+
votable.vote_impact(delta) if votable.respond_to?(:vote_impact)
|
84
91
|
votable.save
|
85
92
|
end
|
86
93
|
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Biovision
|
4
|
+
module Components
|
5
|
+
# Component for votes
|
6
|
+
class VoteComponent < BaseComponent
|
7
|
+
METRIC_VOTE_HIT = 'votes.any.hit'
|
8
|
+
METRIC_UPVOTE_HIT = 'votes.upvote.hit'
|
9
|
+
METRIC_DOWNVOTE_HIT = 'votes.downvote.hit'
|
10
|
+
SLUG = 'vote'
|
11
|
+
|
12
|
+
def self.privilege_names
|
13
|
+
%w[moderator]
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param [Vote] entity
|
17
|
+
def count_vote(entity)
|
18
|
+
name = entity.upvote? ? METRIC_UPVOTE_HIT : METRIC_DOWNVOTE_HIT
|
19
|
+
register_metric(name)
|
20
|
+
register_metric(METRIC_VOTE_HIT)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
<% content_for :meta_title, t('.title', page: current_page) %>
|
2
2
|
<% content_for :breadcrumbs do %>
|
3
|
-
|
3
|
+
<%= admin_biovision_component_link(component_handler.component) %>
|
4
|
+
<span><%= t('admin.votes.nav_item.text') %></span>
|
4
5
|
<% end %>
|
5
6
|
|
6
7
|
<article>
|
@@ -1,17 +1,16 @@
|
|
1
1
|
<%
|
2
|
-
|
3
|
-
block_class = votable.vote_applicable?(slug) ? 'active' : ''
|
2
|
+
block_class = votable.vote_applicable?(visitor_slug) ? 'active' : ''
|
4
3
|
%>
|
5
|
-
<div class="vote-block <%= block_class %> voted-<%= votable.voted(
|
4
|
+
<div class="vote-block <%= block_class %> voted-<%= votable.voted(visitor_slug) %>" data-id="<%= votable.id %>">
|
6
5
|
<div class="vote upvote"></div>
|
7
6
|
<div class="result"><%= votable.vote_result %></div>
|
8
7
|
<div class="vote downvote"></div>
|
9
8
|
<% unless block_class.blank? %>
|
10
|
-
<% entity = Vote.new(votable: votable) %>
|
11
|
-
<%=
|
12
|
-
<%=
|
13
|
-
<%=
|
14
|
-
<%=
|
9
|
+
<% entity = Vote.new(votable: votable, delta: 0) %>
|
10
|
+
<%= form_with(model: entity, local: true) do %>
|
11
|
+
<%= f.hidden_field :votable_id, id: "vote_votable_id_#{votable.id}" %>
|
12
|
+
<%= f.hidden_field :votable_type, id: "vote_votable_type_#{votable.id}" %>
|
13
|
+
<%= f.hidden_field :delta, id: "vote_delta_#{votable.id}" %>
|
15
14
|
<% end %>
|
16
15
|
<% end %>
|
17
16
|
</div>
|
data/config/locales/votes-en.yml
CHANGED
data/config/locales/votes-ru.yml
CHANGED
@@ -16,9 +16,7 @@ ru:
|
|
16
16
|
vote: "Голос"
|
17
17
|
attributes:
|
18
18
|
vote:
|
19
|
-
agent: "Агент пользователя"
|
20
19
|
delta: "Изменение"
|
21
|
-
ip: "IP-адрес"
|
22
20
|
slug: "Идентификатор"
|
23
21
|
user: "Пользователь"
|
24
22
|
votable: "Объект голосования"
|
@@ -35,3 +33,9 @@ ru:
|
|
35
33
|
dashboard:
|
36
34
|
biovision_vote:
|
37
35
|
heading: "Голоса пользователей"
|
36
|
+
biovision:
|
37
|
+
components:
|
38
|
+
vote:
|
39
|
+
name: "Голосование"
|
40
|
+
privileges:
|
41
|
+
moderator: "Модератор"
|
@@ -0,0 +1,41 @@
|
|
1
|
+
sv:
|
2
|
+
upvote_count:
|
3
|
+
zero: "Inga röster upp"
|
4
|
+
one: "%{count} rösta upp"
|
5
|
+
few: "%{count} röster upp"
|
6
|
+
many: "%{count} röster upp"
|
7
|
+
other: "%{count} röster upp"
|
8
|
+
downvote_count:
|
9
|
+
zero: "Inga röster nere"
|
10
|
+
one: "%{count} rösta ner"
|
11
|
+
few: "%{count} röster ner"
|
12
|
+
many: "%{count} röster ner"
|
13
|
+
other: "%{count} röster ner"
|
14
|
+
activerecord:
|
15
|
+
models:
|
16
|
+
vote: "Omröstning"
|
17
|
+
attributes:
|
18
|
+
vote:
|
19
|
+
agent: "Användaragent"
|
20
|
+
delta: "Delta"
|
21
|
+
ip: "IP-adress"
|
22
|
+
slug: "Pollett"
|
23
|
+
user: "Användare"
|
24
|
+
votable: "Röstade-entitet"
|
25
|
+
votable_id: "Röstade-entitet"
|
26
|
+
admin:
|
27
|
+
votes:
|
28
|
+
nav_item:
|
29
|
+
description: "Hantera användarnas röster"
|
30
|
+
text: "Röster av användare"
|
31
|
+
index:
|
32
|
+
heading: "Röster av användare"
|
33
|
+
title: "Röster av användare, sida %{page}"
|
34
|
+
index:
|
35
|
+
dashboard:
|
36
|
+
biovision_vote:
|
37
|
+
heading: "Röster av användare"
|
38
|
+
biovision:
|
39
|
+
components:
|
40
|
+
vote:
|
41
|
+
name: "Röster"
|
data/config/routes.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Rails.application.routes.draw do
|
2
|
-
resources :votes, only:
|
2
|
+
resources :votes, only: :destroy, defaults: { format: :json }
|
3
3
|
|
4
|
-
scope '(:locale)', constraints: { locale: /ru|en/ } do
|
5
|
-
resources :votes, only:
|
4
|
+
scope '(:locale)', constraints: { locale: /ru|en|sv/ } do
|
5
|
+
resources :votes, only: :create, defaults: { format: :json }
|
6
6
|
|
7
7
|
namespace :admin do
|
8
|
-
resources :votes, only:
|
8
|
+
resources :votes, only: :index
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
@@ -1,22 +1,32 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Create table and component for votes
|
4
|
+
class CreateVotes < ActiveRecord::Migration[5.2]
|
2
5
|
def up
|
3
|
-
|
4
|
-
|
5
|
-
t.timestamps
|
6
|
-
t.references :user, foreign_key: true, on_update: :cascade, on_delete: :cascade
|
7
|
-
t.references :agent, foreign_key: true, on_update: :cascade, on_delete: :nullify
|
8
|
-
t.inet :ip
|
9
|
-
t.integer :delta, limit: 2, default: 0, null: false
|
10
|
-
t.integer :votable_id, null: false
|
11
|
-
t.string :votable_type, null: false
|
12
|
-
t.string :slug, index: true
|
13
|
-
end
|
14
|
-
end
|
6
|
+
create_component
|
7
|
+
create_votes unless Vote.table_exists?
|
15
8
|
end
|
16
9
|
|
17
10
|
def down
|
18
|
-
if Vote.table_exists?
|
19
|
-
|
11
|
+
drop_table :votes if Vote.table_exists?
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def create_votes
|
17
|
+
create_table :votes, comment: 'Vote' do |t|
|
18
|
+
t.references :user, foreign_key: { on_update: :cascade, on_delete: :cascade }
|
19
|
+
t.references :agent, foreign_key: { on_update: :cascade, on_delete: :nullify }
|
20
|
+
t.inet :ip
|
21
|
+
t.timestamps
|
22
|
+
t.integer :delta, limit: 2, default: 0, null: false
|
23
|
+
t.integer :votable_id, null: false
|
24
|
+
t.string :votable_type, null: false
|
25
|
+
t.string :slug, index: true
|
20
26
|
end
|
21
27
|
end
|
28
|
+
|
29
|
+
def create_component
|
30
|
+
BiovisionComponent.create(slug: Biovision::Components::VoteComponent::SLUG)
|
31
|
+
end
|
22
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: biovision-vote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.190905.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxim Khan-Magomedov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2019-09-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rails-i18n
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -52,20 +52,6 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: kaminari
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - ">="
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '0'
|
62
|
-
type: :runtime
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - ">="
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '0'
|
69
55
|
- !ruby/object:Gem::Dependency
|
70
56
|
name: pg
|
71
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -154,7 +140,8 @@ files:
|
|
154
140
|
- app/models/biovision/vote/application_record.rb
|
155
141
|
- app/models/concerns/votable_item.rb
|
156
142
|
- app/models/vote.rb
|
157
|
-
- app/
|
143
|
+
- app/services/biovision/components/vote_component.rb
|
144
|
+
- app/views/admin/components/links/_vote.html.erb
|
158
145
|
- app/views/admin/votes/_nav_item.html.erb
|
159
146
|
- app/views/admin/votes/entity/_in_list.html.erb
|
160
147
|
- app/views/admin/votes/index.html.erb
|
@@ -163,9 +150,9 @@ files:
|
|
163
150
|
- app/views/votes/result.jbuilder
|
164
151
|
- config/locales/votes-en.yml
|
165
152
|
- config/locales/votes-ru.yml
|
153
|
+
- config/locales/votes-sv.yml
|
166
154
|
- config/routes.rb
|
167
155
|
- db/migrate/20170330000001_create_votes.rb
|
168
|
-
- db/migrate/20180610110015_add_slug_to_votes.rb
|
169
156
|
- lib/biovision/vote.rb
|
170
157
|
- lib/biovision/vote/engine.rb
|
171
158
|
- lib/biovision/vote/version.rb
|
@@ -189,8 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
176
|
- !ruby/object:Gem::Version
|
190
177
|
version: '0'
|
191
178
|
requirements: []
|
192
|
-
|
193
|
-
rubygems_version: 2.7.6
|
179
|
+
rubygems_version: 3.0.4
|
194
180
|
signing_key:
|
195
181
|
specification_version: 4
|
196
182
|
summary: Voting functionality for biovision-based applications
|
@@ -1,10 +0,0 @@
|
|
1
|
-
<% if current_user_has_privilege?(:moderator) %>
|
2
|
-
<nav>
|
3
|
-
<div class="heading" id="biovision-vote-nav-heading">
|
4
|
-
<%= t('.heading') %>
|
5
|
-
</div>
|
6
|
-
<ul aria-labelledby="biovision-vote-nav-heading">
|
7
|
-
<li><%= render 'admin/votes/nav_item' %></li>
|
8
|
-
</ul>
|
9
|
-
</nav>
|
10
|
-
<% end %>
|
@@ -1,15 +0,0 @@
|
|
1
|
-
class AddSlugToVotes < ActiveRecord::Migration[5.2]
|
2
|
-
def up
|
3
|
-
unless column_exists?(:votes, :slug)
|
4
|
-
add_column :votes, :slug, :string, index: true
|
5
|
-
|
6
|
-
Vote.order('id asc').each do |vote|
|
7
|
-
vote.update! slug: vote.current_slug
|
8
|
-
end
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def down
|
13
|
-
# No need to rollback
|
14
|
-
end
|
15
|
-
end
|