From ba753f364974f89638bab64d7524fca809642b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Salvador=20P=C3=A9rez=20Garc=C3=ADa?= Date: Fri, 13 Jul 2018 18:06:10 +0200 Subject: [PATCH] Fixes #222 Community view shows the polls for the given proposal. --- app/models/community.rb | 11 ------ app/models/poll.rb | 15 ++++++++ app/models/proposal.rb | 1 + app/views/communities/_participants.html.erb | 20 ----------- app/views/communities/_poll.html.erb | 5 +++ app/views/communities/_subnav.html.erb | 37 ++++++++++++++++++++ app/views/communities/show.html.erb | 8 ++++- config/locales/en/activerecord.yml | 1 + config/locales/en/general.yml | 6 ++++ config/locales/es/activerecord.yml | 1 + config/locales/es/general.yml | 6 ++++ 11 files changed, 79 insertions(+), 32 deletions(-) delete mode 100644 app/views/communities/_participants.html.erb create mode 100644 app/views/communities/_poll.html.erb create mode 100644 app/views/communities/_subnav.html.erb diff --git a/app/models/community.rb b/app/models/community.rb index de1ba9e87..9a18f3510 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -6,7 +6,6 @@ class Community < ActiveRecord::Base def participants users_participants = users_who_commented + users_who_topics_author + - response_author_from_polls_for_proposal + author_from_community users_participants.uniq end @@ -55,14 +54,4 @@ class Community < ActiveRecord::Base def author_from_community from_proposal? ? User.where(id: proposal&.author_id) : User.where(id: investment&.author_id) end - - def response_author_from_polls_for_proposal - author_ids = [] - - Poll.where(related: proposal).each do |poll| - author_ids += poll.questions.pluck(:author_id) - end - - User.by_authors(author_ids.uniq) - end end diff --git a/app/models/poll.rb b/app/models/poll.rb index 74400a9fe..756c01eec 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -23,6 +23,7 @@ class Poll < ActiveRecord::Base validates :name, presence: true validate :date_range + validate :only_one_active, unless: :public? accepts_nested_attributes_for :questions, reject_if: :all_blank, allow_destroy: true @@ -35,6 +36,10 @@ class Poll < ActiveRecord::Base scope :published, -> { where('published = ?', true) } scope :by_geozone_id, ->(geozone_id) { where(geozones: {id: geozone_id}.joins(:geozones)) } scope :public_for_api, -> { all } + scope :overlaping_with, lambda { |poll| + where('? < ends_at and ? >= starts_at', poll.starts_at.beginning_of_day, poll.ends_at.end_of_day) + .where.not(id: poll.id) + } scope :sort_for_list, -> { order(:geozone_restricted, :starts_at, :name) } @@ -97,4 +102,14 @@ class Poll < ActiveRecord::Base end end + def only_one_active + if Poll.overlaping_with(self).any? + errors.add(:starts_at, I18n.t('activerecord.errors.messages.another_poll_active')) + end + end + + def public? + related.nil? + end + end diff --git a/app/models/proposal.rb b/app/models/proposal.rb index 53b97bfea..9593e0d82 100644 --- a/app/models/proposal.rb +++ b/app/models/proposal.rb @@ -33,6 +33,7 @@ class Proposal < ActiveRecord::Base has_many :proposal_notifications, dependent: :destroy has_many :proposal_executed_dashboard_actions, dependent: :destroy has_many :proposal_dashboard_actions, through: :proposal_executed_dashboard_actions + has_many :polls, as: :related validates :title, presence: true validates :question, presence: true diff --git a/app/views/communities/_participants.html.erb b/app/views/communities/_participants.html.erb deleted file mode 100644 index a153ec624..000000000 --- a/app/views/communities/_participants.html.erb +++ /dev/null @@ -1,20 +0,0 @@ -
- - - - <% @participants.each do |participant| %> - <%= render 'participant', participant: participant %> - <% end %> - -
diff --git a/app/views/communities/_poll.html.erb b/app/views/communities/_poll.html.erb new file mode 100644 index 000000000..b55a85b85 --- /dev/null +++ b/app/views/communities/_poll.html.erb @@ -0,0 +1,5 @@ +<%= link_to poll do %> + <%= poll.title %> +<% end %> +

<%= t('.take_part', from: l(poll.starts_at.to_date), to:(poll.ends_at.to_date)) %>

+
diff --git a/app/views/communities/_subnav.html.erb b/app/views/communities/_subnav.html.erb new file mode 100644 index 000000000..fbfa73f2b --- /dev/null +++ b/app/views/communities/_subnav.html.erb @@ -0,0 +1,37 @@ +
+ + + +
+
+ <%= render partial: 'participant', collection: @participants %> +
+ + <% if @community.proposal.present? %> +
+ <%= render partial: 'poll', collection: @community.proposal.polls %> +
+ <% end %> +
+
diff --git a/app/views/communities/show.html.erb b/app/views/communities/show.html.erb index a1593df50..0e7456049 100644 --- a/app/views/communities/show.html.erb +++ b/app/views/communities/show.html.erb @@ -39,8 +39,14 @@

<%= t("community.show.sidebar.participate") %>

<%= link_to t("community.show.sidebar.new_topic"), create_topic_link(@community) , class: "button expanded" %> + + <% if @community.proposal&.polls&.current&.any? %> + +

<%= t('.surveys') %>

+ <%= link_to t('.complete_survey'), @community.proposal.polls.current.first, class: 'button expanded hollow' %> + <% end %> -<%= render 'participants' %> +<%= render 'subnav' %> diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 6e0838c8b..47f36602e 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -358,6 +358,7 @@ en: cannot_comment_valuation: 'You cannot comment a valuation' messages: record_invalid: "Validation failed: %{errors}" + another_poll_active: There is another poll active for the given period restrict_dependent_destroy: has_one: "Cannot delete record because a dependent %{record} exists" has_many: "Cannot delete record because dependent %{record} exist" diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index ac8fdfccd..2a5a566f4 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -1002,3 +1002,9 @@ en: show: send_notification: Send message to the community author: Author + surveys: Surveys + complete_survey: Complete the survey + participants: + surveys: Surveys + poll: + take_part: Take part from %{from} to %{to} diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index 976ad6847..df874e78d 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -360,6 +360,7 @@ es: cannot_comment_valuation: 'No puedes comentar una evaluación' messages: record_invalid: 'Error de validación: %{errors}' + another_poll_active: Hay otra encuesta activa para este periodo. restrict_dependent_destroy: has_one: "No se puede eliminar el registro porque existe una %{record} dependiente" has_many: "No se puede eliminar el registro porque existe %{record} dependientes" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 38cbbc50c..01fd02cbb 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -1001,3 +1001,9 @@ es: show: send_notification: Enviar mensaje a la Comunidad author: Autor + surveys: Encuestas + complete_survey: Completa la encuesta + participants: + surveys: Encuestas + poll: + take_part: Participa del %{from} al %{to}