biovision-poll 0.1.180917.0 → 0.2.200324.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/admin/poll_answers_controller.rb +6 -5
  3. data/app/controllers/admin/poll_questions_controller.rb +6 -5
  4. data/app/controllers/admin/polls_controller.rb +7 -6
  5. data/app/controllers/poll_answers_controller.rb +15 -9
  6. data/app/controllers/poll_questions_controller.rb +15 -9
  7. data/app/controllers/polls_controller.rb +25 -16
  8. data/app/helpers/polls_helper.rb +19 -29
  9. data/app/models/poll.rb +50 -18
  10. data/app/models/poll_answer.rb +29 -36
  11. data/app/models/poll_question.rb +33 -39
  12. data/app/models/poll_user.rb +10 -1
  13. data/app/models/poll_vote.rb +20 -5
  14. data/app/services/biovision/components/polls_component.rb +21 -0
  15. data/app/views/admin/components/links/_polls.html.erb +1 -0
  16. data/app/views/admin/poll_answers/entity/_data.html.erb +0 -0
  17. data/app/views/admin/poll_answers/entity/_in_list.html.erb +0 -3
  18. data/app/views/admin/poll_answers/show.html.erb +25 -35
  19. data/app/views/admin/poll_questions/entity/_data.html.erb +0 -0
  20. data/app/views/admin/poll_questions/entity/_in_list.html.erb +6 -5
  21. data/app/views/admin/poll_questions/show.html.erb +29 -37
  22. data/app/views/admin/poll_votes/entity/_data.html.erb +0 -0
  23. data/app/views/admin/poll_votes/entity/_in_list.html.erb +6 -5
  24. data/app/views/admin/polls/entity/_data.html.erb +0 -0
  25. data/app/views/admin/polls/entity/_in_list.html.erb +1 -3
  26. data/app/views/admin/polls/index.html.erb +8 -2
  27. data/app/views/admin/polls/show.html.erb +32 -30
  28. data/app/views/admin/polls/users.html.erb +2 -1
  29. data/app/views/poll_answers/_form.html.erb +36 -30
  30. data/app/views/poll_answers/_poll_answer.html.erb +22 -2
  31. data/app/views/poll_answers/edit.html.erb +6 -5
  32. data/app/views/poll_answers/new.html.erb +8 -7
  33. data/app/views/poll_questions/_form.html.erb +53 -43
  34. data/app/views/poll_questions/edit.html.erb +6 -5
  35. data/app/views/poll_questions/new.html.erb +7 -6
  36. data/app/views/polls/_form.html.erb +64 -41
  37. data/app/views/polls/_poll.html.erb +11 -6
  38. data/app/views/polls/edit.html.erb +5 -4
  39. data/app/views/polls/new.html.erb +3 -2
  40. data/app/views/polls/results.html.erb +22 -0
  41. data/config/locales/polls-ru.yml +28 -26
  42. data/config/routes.rb +19 -15
  43. data/db/migrate/20200323000001_create_biovision_polls.rb +121 -0
  44. data/lib/biovision/poll/version.rb +3 -1
  45. metadata +10 -12
  46. data/app/uploaders/poll_image_uploader.rb +0 -46
  47. data/app/views/admin/index/dashboard/_biovision_poll.html.erb +0 -10
  48. data/db/migrate/20170906000001_create_polls.rb +0 -70
  49. data/db/migrate/20170906000002_create_poll_questions.rb +0 -22
  50. data/db/migrate/20170906000003_create_poll_answers.rb +0 -20
  51. data/db/migrate/20170906000004_create_poll_votes.rb +0 -20
  52. data/db/migrate/20180412000010_create_poll_users.rb +0 -21
  53. data/db/migrate/20180903111111_rename_vote_footprint.rb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e673d23a9adf4f6fd8b9a31e41d117bc29cd0821a687e4b5a650f83cbe400035
4
- data.tar.gz: dca9cfa8d1ab77d328e39ee1930b4a24112f0dca08695a45d8a22ad13239216a
3
+ metadata.gz: 3e28e8495e436c2f955aba432356fbea9112fec0496e039ec8098ac4ce892740
4
+ data.tar.gz: c3216e389c001b74779453256aaba57c0f8b9c8fa47a67af463f892fe7d2a11a
5
5
  SHA512:
6
- metadata.gz: 2b2e12f2d66ab1451ec17c5b5190a85a5f993b59bede884793e5cff0b37435dae42fa28844e5fc23c288e41ab0b22b1adb14f953b94bfbb77b5be06852b0e338
7
- data.tar.gz: acde6ad3f720aedec127251e1a0e78824240220ff70fefcd554c57beb990eab5a1b98e0f5b6e541f98be3a0c5985d8a1621738e9690c8f518da3e4a344441208
6
+ metadata.gz: ba371cfdaaacdb94fe3c5816e6cda59ce155b3ee35570d8da7b4a5163330c8a81424c5dc037c434ef7f4aa6df4b1c51ab2f7e4a1713f0c354e346d936f97422d
7
+ data.tar.gz: f1c16c57a3bc5b7ed105725125244064f13946b8622264194116d571d1912f7b972d4bb420e3d96dc9e085e76d54e810d197bb1d5019cbfb47dfd7277bdbddd7
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Administrative part of hanlding poll answers
1
4
  class Admin::PollAnswersController < AdminController
2
5
  include ToggleableEntity
3
6
  include EntityPriority
@@ -11,14 +14,12 @@ class Admin::PollAnswersController < AdminController
11
14
 
12
15
  private
13
16
 
14
- def restrict_access
15
- require_privilege_group :poll_managers
17
+ def component_class
18
+ Biovision::Components::PollsComponent
16
19
  end
17
20
 
18
21
  def set_entity
19
22
  @entity = PollAnswer.find_by(id: params[:id])
20
- if @entity.nil?
21
- handle_http_404('Cannot find poll answer')
22
- end
23
+ handle_http_404('Cannot find poll answer') if @entity.nil?
23
24
  end
24
25
  end
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Administrative part for hanlding poll questions
1
4
  class Admin::PollQuestionsController < AdminController
2
5
  include ToggleableEntity
3
6
  include EntityPriority
@@ -10,14 +13,12 @@ class Admin::PollQuestionsController < AdminController
10
13
 
11
14
  private
12
15
 
13
- def restrict_access
14
- require_privilege_group :poll_managers
16
+ def component_class
17
+ Biovision::Components::PollsComponent
15
18
  end
16
19
 
17
20
  def set_entity
18
21
  @entity = PollQuestion.find_by(id: params[:id])
19
- if @entity.nil?
20
- handle_http_404('Cannot find poll question')
21
- end
22
+ handle_http_404('Cannot find poll question') if @entity.nil?
22
23
  end
23
24
  end
@@ -1,7 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Administrative part of handling polls
1
4
  class Admin::PollsController < AdminController
2
5
  include ToggleableEntity
3
6
 
4
- before_action :set_entity, except: [:index]
7
+ before_action :set_entity, except: :index
5
8
 
6
9
  # get /admin/polls
7
10
  def index
@@ -37,14 +40,12 @@ class Admin::PollsController < AdminController
37
40
 
38
41
  private
39
42
 
40
- def restrict_access
41
- require_privilege_group :poll_managers
43
+ def component_class
44
+ Biovision::Components::PollsComponent
42
45
  end
43
46
 
44
47
  def set_entity
45
48
  @entity = Poll.find_by(id: params[:id])
46
- if @entity.nil?
47
- handle_http_404('Cannot find poll')
48
- end
49
+ handle_http_404('Cannot find poll') if @entity.nil?
49
50
  end
50
51
  end
@@ -1,5 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Editing poll answers
1
4
  class PollAnswersController < AdminController
2
- before_action :set_entity, only: [:edit, :update, :destroy]
5
+ before_action :set_entity, only: %i[edit update destroy]
6
+
7
+ # post /poll_answers/check
8
+ def check
9
+ @entity = PollAnswer.instance_for_check(params[:entity_id], entity_parameters)
10
+
11
+ render 'shared/forms/check'
12
+ end
3
13
 
4
14
  # post /poll_answers
5
15
  def create
@@ -26,23 +36,19 @@ class PollAnswersController < AdminController
26
36
 
27
37
  # delete /poll_answers/:id
28
38
  def destroy
29
- if @entity.destroy
30
- flash[:notice] = t('poll_answers.destroy.success')
31
- end
39
+ flash[:notice] = t('.success') if @entity.destroy
32
40
  redirect_to(admin_poll_question_path(id: @entity.poll_question_id))
33
41
  end
34
42
 
35
43
  protected
36
44
 
37
- def restrict_access
38
- require_privilege_group :poll_managers
45
+ def component_class
46
+ Biovision::Components::PollsComponent
39
47
  end
40
48
 
41
49
  def set_entity
42
50
  @entity = PollAnswer.find_by(id: params[:id])
43
- if @entity.nil?
44
- handle_http_404('Cannot find poll answer')
45
- end
51
+ handle_http_404('Cannot find poll answer') if @entity.nil?
46
52
  end
47
53
 
48
54
  def entity_parameters
@@ -1,5 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Managing poll questions
1
4
  class PollQuestionsController < AdminController
2
- before_action :set_entity, only: [:edit, :update, :destroy]
5
+ before_action :set_entity, only: %i[edit update destroy]
6
+
7
+ # post /poll_questions/check
8
+ def check
9
+ @entity = PollQuestion.instance_for_check(params[:entity_id], entity_parameters)
10
+
11
+ render 'shared/forms/check'
12
+ end
3
13
 
4
14
  # post /poll_questions
5
15
  def create
@@ -26,23 +36,19 @@ class PollQuestionsController < AdminController
26
36
 
27
37
  # delete /poll_questions/:id
28
38
  def destroy
29
- if @entity.destroy
30
- flash[:notice] = t('poll_questions.destroy.success')
31
- end
39
+ flash[:notice] = t('.success') if @entity.destroy
32
40
  redirect_to(admin_poll_path(id: @entity.poll_id))
33
41
  end
34
42
 
35
43
  protected
36
44
 
37
- def restrict_access
38
- require_privilege_group :poll_managers
45
+ def component_class
46
+ Biovision::Components::PollsComponent
39
47
  end
40
48
 
41
49
  def set_entity
42
50
  @entity = PollQuestion.find_by(id: params[:id])
43
- if @entity.nil?
44
- handle_http_404('Cannot find poll question')
45
- end
51
+ handle_http_404('Cannot find poll question') if @entity.nil?
46
52
  end
47
53
 
48
54
  def entity_parameters
@@ -1,8 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Displaying and editing polls
1
4
  class PollsController < ApplicationController
2
- before_action :restrict_access, except: [:index, :show, :results, :answer]
3
- before_action :set_entity, except: [:index, :new, :create]
4
-
5
- layout 'admin', except: [:index, :show, :results]
5
+ before_action :restrict_access, except: %i[index show results answer]
6
+ before_action :set_entity, except: %i[check index new create]
7
+
8
+ layout 'admin', except: %i[check index show results]
9
+
10
+ # post /polls/check
11
+ def check
12
+ @entity = Poll.instance_for_check(params[:entity_id], entity_parameters)
13
+
14
+ render 'shared/forms/check'
15
+ end
6
16
 
7
17
  # get /polls
8
18
  def index
@@ -26,9 +36,7 @@ class PollsController < ApplicationController
26
36
 
27
37
  # get /polls/:id
28
38
  def show
29
- unless @entity.visible_to?(current_user)
30
- redirect_to polls_path
31
- end
39
+ redirect_to polls_path unless @entity.visible_to?(current_user)
32
40
  end
33
41
 
34
42
  # get /polls/:id/edit
@@ -46,17 +54,15 @@ class PollsController < ApplicationController
46
54
 
47
55
  # delete /polls/:id
48
56
  def destroy
49
- if @entity.destroy
50
- flash[:notice] = t('polls.destroy.success')
51
- end
57
+ flash[:notice] = t('.success') if @entity.destroy
52
58
  redirect_to(admin_polls_path)
53
59
  end
54
60
 
55
61
  # post /polls/:id/results
56
62
  def answer
57
- @entity.process_answers(current_user, params.require(:answer))
63
+ @entity.process_answers(current_user, params.require(:answer), owner_for_entity(true), visitor_slug)
58
64
 
59
- redirect_to root_path(id: @entity.id)
65
+ redirect_to results_poll_path(id: @entity.id)
60
66
  end
61
67
 
62
68
  # get /polls/:id/results
@@ -66,15 +72,18 @@ class PollsController < ApplicationController
66
72
 
67
73
  protected
68
74
 
75
+ def component_class
76
+ Biovision::Components::PollsComponent
77
+ end
78
+
69
79
  def restrict_access
70
- require_privilege_group :poll_managers
80
+ error = 'Managing polls is not allowed for current user'
81
+ handle_http_401(error) unless component_handler.allow?
71
82
  end
72
83
 
73
84
  def set_entity
74
85
  @entity = Poll.find_by(id: params[:id])
75
- if @entity.nil?
76
- handle_http_404('Cannot find poll')
77
- end
86
+ handle_http_404('Cannot find poll') if @entity.nil?
78
87
  end
79
88
 
80
89
  def entity_parameters
@@ -1,42 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Helper methods for polls component
1
4
  module PollsHelper
2
5
  # @param [Poll] entity
3
- def admin_poll_link(entity)
4
- link_to(entity.name, admin_poll_path(id: entity.id))
6
+ # @param [String] text
7
+ # @param [Hash] options
8
+ def admin_poll_link(entity, text = entity.name, options = {})
9
+ link_to(text, admin_poll_path(id: entity.id), options)
5
10
  end
6
11
 
7
12
  # @param [PollQuestion] entity
8
- def admin_poll_question_link(entity)
9
- link_to(entity.text, admin_poll_question_path(id: entity.id))
13
+ # @param [String] text
14
+ # @param [Hash] options
15
+ def admin_poll_question_link(entity, text = entity.text, options = {})
16
+ link_to(text, admin_poll_question_path(id: entity.id), options)
10
17
  end
11
18
 
12
19
  # @param [PollAnswer] entity
13
- def admin_poll_answer_link(entity)
14
- link_to(entity.text, admin_poll_answer_path(id: entity.id))
15
- end
16
-
17
- # @param [Poll] entity
18
- def poll_link(entity)
19
- link_to(entity.name, poll_path(id: entity.id))
20
- end
21
-
22
- # @param [Poll] entity
23
- def poll_image_medium(entity)
24
- return '' if entity.image.blank?
25
- versions = "#{entity.image.medium_2x.url} 2x"
26
- image_tag(entity.image.medium.url, alt: entity.name, srcset: versions)
27
- end
28
-
29
- # @param [Poll] entity
30
- def poll_image_small(entity)
31
- return '' if entity.image.blank?
32
- versions = "#{entity.image.medium.url} 2x"
33
- image_tag(entity.image.small.url, alt: entity.name, srcset: versions)
20
+ # @param [String] text
21
+ # @param [Hash] options
22
+ def admin_poll_answer_link(entity, text = entity.text, options = {})
23
+ link_to(text, admin_poll_answer_path(id: entity.id), options)
34
24
  end
35
25
 
36
26
  # @param [Poll] entity
37
- def poll_image_preview(entity)
38
- return '' if entity.image.blank?
39
- versions = "#{entity.image.preview_2x.url} 2x"
40
- image_tag(entity.image.preview.url, alt: entity.name, srcset: versions)
27
+ # @param [String] text
28
+ # @param [Hash] options
29
+ def poll_link(entity, text = entity.name, options = {})
30
+ link_to(text, poll_path(id: entity.id), options)
41
31
  end
42
32
  end
data/app/models/poll.rb CHANGED
@@ -1,49 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Poll
4
+ #
5
+ # Attributes:
6
+ # active [boolean]
7
+ # agent_id [Agent], optional
8
+ # allow_comments [Boolean]
9
+ # anonymous_votes [boolean]
10
+ # comments_count [integer]
11
+ # created_at [DateTime]
12
+ # data [jsonb]
13
+ # description [string], optional
14
+ # end_date [date], optional
15
+ # exclusive [boolean]
16
+ # ip [inet]
17
+ # language_id [Language], optional
18
+ # name [string]
19
+ # open_results [boolean]
20
+ # poll_questions_count [integer]
21
+ # pollable_id [integer], optional
22
+ # pollable_type [string], optional
23
+ # show_on_homepage [boolean]
24
+ # simple_image_id [SimpleImage], optional
25
+ # updated_at [DateTime]
26
+ # user_id [User]
27
+ # uuid [uuid]
28
+ # visible [boolean]
1
29
  class Poll < ApplicationRecord
30
+ include Checkable
2
31
  include HasOwner
32
+ include HasSimpleImage
33
+ include HasUuid
3
34
  include Toggleable
4
35
 
5
- PER_PAGE = 10
6
36
  NAME_LIMIT = 140
7
37
  DESCRIPTION_LIMIT = 255
8
38
 
9
- toggleable %i[active anonymous_votes open_results show_on_homepage visible exclusive]
10
-
11
- mount_uploader :image, PollImageUploader
39
+ toggleable %i[active anonymous_votes open_results show_on_homepage exclusive visible]
12
40
 
13
41
  belongs_to :user
14
42
  belongs_to :agent, optional: true
15
- belongs_to :region, optional: true if Gem.loaded_specs.key?('biovision-regions')
16
43
  belongs_to :pollable, polymorphic: true, optional: true
17
44
  has_many :poll_questions, dependent: :delete_all
18
45
  has_many :poll_users, dependent: :delete_all
19
46
 
20
47
  validates_presence_of :name
21
- validates_length_of :name, maximum: NAME_LIMIT
22
48
  validates_length_of :description, maximum: DESCRIPTION_LIMIT
49
+ validates_length_of :name, maximum: NAME_LIMIT
23
50
 
24
51
  scope :recent, -> { order('id desc') }
25
52
  scope :visible, -> { where(visible: true) }
26
- scope :active, -> { visible.where('active = true and (end_date is null or (date(end_date) >= date(now())))') }
53
+ scope :active, -> { where('active = true and (end_date is null or (date(end_date) >= date(now())))') }
54
+ scope :list_for_visitors, -> { active.visible.recent }
55
+ scope :list_for_administration, -> { recent }
27
56
 
28
57
  # @param [Integer] page
29
58
  def self.page_for_administration(page = 1)
30
- recent.page(page).per(PER_PAGE)
59
+ list_for_administration.page(page)
31
60
  end
32
61
 
33
62
  # @param [Integer] page
34
63
  def self.page_for_visitors(page = 1)
35
- visible.recent.page(page).per(PER_PAGE)
64
+ list_for_visitors.page(page)
36
65
  end
37
66
 
38
67
  def self.entity_parameters
39
- Poll.toggleable_attributes + %i(image name description end_date)
68
+ Poll.toggleable_attributes + %i[description end_date name simple_image_id]
40
69
  end
41
70
 
42
71
  # @param [User] user
43
72
  def editable_by?(user)
44
73
  return false if user.nil?
45
- privilege = :chief_poll_manager
46
- owned_by?(user) || UserPrivilege.user_has_privilege?(user, privilege)
74
+
75
+ owned_by?(user)
47
76
  end
48
77
 
49
78
  # @param [User] user
@@ -54,6 +83,7 @@ class Poll < ApplicationRecord
54
83
  # @param [User] user
55
84
  def votable_by?(user)
56
85
  return false if user.nil? || voted?(user)
86
+
57
87
  if exclusive?
58
88
  includes?(user)
59
89
  # elsif anonymous_votes?
@@ -72,10 +102,6 @@ class Poll < ApplicationRecord
72
102
  open_results? || editable_by?(user)
73
103
  end
74
104
 
75
- def regional?
76
- !region_id.nil?
77
- end
78
-
79
105
  # @param [User] user
80
106
  def includes?(user)
81
107
  poll_users.owned_by(user).exists?
@@ -97,18 +123,24 @@ class Poll < ApplicationRecord
97
123
 
98
124
  # @param [User] user
99
125
  # @param [Hash] answers
100
- def process_answers(user, answers)
126
+ # @param [Hash] tracking
127
+ # @param [String] slug
128
+ def process_answers(user, answers, tracking, slug)
101
129
  return unless votable_by?(user)
130
+
102
131
  answers.each do |question_id, answer_ids|
103
132
  question = PollQuestion.find_by(id: question_id)
104
133
  next if question.nil? || question.poll_id != id
134
+
105
135
  if question.multiple_choice?
106
136
  answers = question.poll_answers.where(id: answer_ids)
107
137
  else
108
138
  answers = question.poll_answers.where(id: answer_ids.first)
109
139
  end
140
+
110
141
  answers.each do |answer|
111
- PollVote.create(poll_answer: answer, user: user)
142
+ tracking[:slug] = slug
143
+ answer.poll_votes.create(tracking)
112
144
  end
113
145
  end
114
146
  end