Bugfixing and several enhancements

* Added missing specs
* Removed models that were refactored previously
* Added simplecov when executing specs locally
* Fixed bug in poll model validation that was causing an 500 error.
This commit is contained in:
Juan Salvador Pérez García
2018-08-01 13:26:30 +02:00
parent 0e20247682
commit 5b5095b37b
11 changed files with 88 additions and 115 deletions

View File

@@ -5,7 +5,7 @@ class Dashboard::PosterController < Dashboard::BaseController
respond_to do |format| respond_to do |format|
format.html format.html
format.pdf do format.pdf do
render pdf: 'poster', page_size: 'A3' render pdf: 'poster', page_size: 'A3', show_as_html: Rails.env.test?
end end
end end
end end

View File

@@ -77,8 +77,4 @@ class Dashboard::Action < ActiveRecord::Base
def self.next_goal_for(proposal) def self.next_goal_for(proposal)
course_for(proposal).first course_for(proposal).first
end end
def request_to_administrators?
request_to_administrators || false
end
end end

View File

@@ -104,9 +104,10 @@ class Poll < ActiveRecord::Base
end end
def only_one_active def only_one_active
if Poll.overlaping_with(self).any? return unless starts_at.present?
errors.add(:starts_at, I18n.t('activerecord.errors.messages.another_poll_active')) return unless ends_at.present?
end return unless Poll.overlaping_with(self).any?
errors.add(:starts_at, I18n.t('activerecord.errors.messages.another_poll_active'))
end end
def public? def public?

View File

@@ -1,88 +0,0 @@
class ProposalDashboardAction < ActiveRecord::Base
include Documentable
documentable max_documents_allowed: 3,
max_file_size: 3.megabytes,
accepted_content_types: [ 'application/pdf' ]
include Linkable
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases
has_many :proposal_executed_dashboard_actions, dependent: :restrict_with_error
has_many :proposals, through: :proposal_executed_dashboard_actions
enum action_type: [:proposed_action, :resource]
validates :title,
presence: true,
allow_blank: false,
length: { in: 4..80 }
validates :action_type, presence: true
validates :day_offset,
presence: true,
numericality: {
only_integer: true,
greater_than_or_equal_to: 0
}
validates :required_supports,
presence: true,
numericality: {
only_integer: true,
greater_than_or_equal_to: 0
}
scope :active, -> { where(active: true) }
scope :inactive, -> { where(active: false) }
scope :resources, -> { where(action_type: 1) }
scope :proposed_actions, -> { where(action_type: 0) }
scope :active_for, ->(proposal) do
published_at = proposal.published_at&.to_date || Date.today
active
.where('required_supports <= ?', proposal.votes_for.size)
.where('day_offset <= ?', (Date.today - published_at).to_i)
end
scope :course_for, lambda { |proposal|
active
.resources
.where('required_supports > ?', proposal.votes_for.size)
.order(required_supports: :asc)
}
def active_for?(proposal)
published_at = proposal.published_at&.to_date || Date.today
required_supports <= proposal.votes_for.size && day_offset <= (Date.today - published_at).to_i
end
def requested_for?(proposal)
executed_action = proposal_executed_dashboard_actions.find_by(proposal: proposal)
return false if executed_action.nil?
executed_action.administrator_tasks.any?
end
def executed_for?(proposal)
executed_action = proposal_executed_dashboard_actions.find_by(proposal: proposal)
return false if executed_action.nil?
executed_action.administrator_tasks.where.not(executed_at: nil).any?
end
def self.next_goal_for(proposal)
course_for(proposal).first
end
def request_to_administrators?
request_to_administrators || false
end
def request_to_administrators?
request_to_administrators
end
end

View File

@@ -1,10 +0,0 @@
class ProposalExecutedDashboardAction < ActiveRecord::Base
belongs_to :proposal
belongs_to :proposal_dashboard_action
has_many :administrator_tasks, as: :source, dependent: :destroy
validates :proposal, presence: true, uniqueness: { scope: :proposal_dashboard_action }
validates :proposal_dashboard_action, presence: true
validates :executed_at, presence: true
end

View File

@@ -1038,7 +1038,7 @@ LOREM_IPSUM
end end
factory :dashboard_action, class: 'Dashboard::Action' do factory :dashboard_action, class: 'Dashboard::Action' do
title { Faker::Lorem.sentence[0..80] } title { Faker::Lorem.sentence[0..79] }
description { Faker::Lorem.sentence } description { Faker::Lorem.sentence }
link nil link nil
request_to_administrators true request_to_administrators true

View File

@@ -47,6 +47,11 @@ feature 'Admin dashboard actions' do
expect(page).to have_content(action.title) expect(page).to have_content(action.title)
end end
scenario 'Renders create form in case data is invalid' do
click_button 'Save'
expect(page).to have_content('errors prevented this Dashboard/Action from being saved.')
end
end end
context 'when editing an action' do context 'when editing an action' do
@@ -64,6 +69,12 @@ feature 'Admin dashboard actions' do
expect(page).to have_content(title) expect(page).to have_content(title)
end end
scenario 'Renders edit form in case data is invalid' do
fill_in 'dashboard_action_title', with: 'x'
click_button 'Save'
expect(page).to have_content('error prevented this Dashboard/Action from being saved.')
end
end end
context 'when destroying an action' do context 'when destroying an action' do
@@ -80,5 +91,15 @@ feature 'Admin dashboard actions' do
expect(page).not_to have_content(action.title) expect(page).not_to have_content(action.title)
end end
scenario 'can not delete actions that have been executed', js: true do
_executed_action = create(:dashboard_executed_action, action: action)
page.accept_confirm do
click_link 'Delete'
end
expect(page).to have_content('Cannot delete record because dependent executed actions exist')
end
end end
end end

View File

@@ -126,6 +126,18 @@ feature "Proposal's dashboard" do
expect(page).to have_content('The request for the administrator has been successfully sent.') expect(page).to have_content('The request for the administrator has been successfully sent.')
end end
scenario 'Request already requested resource with admin request', js: true do
feature = create(:dashboard_action, :resource, :active, :admin_request)
visit proposal_dashboard_index_path(proposal)
click_link(feature.title)
create(:dashboard_executed_action, action: feature, proposal: proposal)
click_button 'Request'
expect(page).to have_content('Proposal has already been taken')
end
scenario 'Resource without admin request do not have a request link', js: true do scenario 'Resource without admin request do not have a request link', js: true do
feature = create(:dashboard_action, :resource, :active) feature = create(:dashboard_action, :resource, :active)

View File

@@ -19,13 +19,13 @@ feature 'Polls' do
start_date = 1.week.from_now start_date = 1.week.from_now
end_date = 2.weeks.from_now end_date = 2.weeks.from_now
fill_in "poll_name", with: "Upcoming poll" fill_in "poll_name", with: 'Upcoming poll'
fill_in 'poll_starts_at', with: start_date.strftime("%d/%m/%Y") fill_in 'poll_starts_at', with: start_date.strftime('%d/%m/%Y')
fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y") fill_in 'poll_ends_at', with: end_date.strftime('%d/%m/%Y')
fill_in 'poll_description', with: "Upcomming poll's description. This poll..." fill_in 'poll_description', with: "Upcomming poll's description. This poll..."
expect(page).not_to have_css("#poll_results_enabled") expect(page).not_to have_css('#poll_results_enabled')
expect(page).not_to have_css("#poll_stats_enabled") expect(page).not_to have_css('#poll_stats_enabled')
click_link 'Add question' click_link 'Add question'
@@ -34,13 +34,22 @@ feature 'Polls' do
click_link 'Add answer' click_link 'Add answer'
fill_in 'Title', with: 'First answer' fill_in 'Title', with: 'First answer'
click_button "Create poll" click_button 'Create poll'
expect(page).to have_content "Poll created successfully" expect(page).to have_content 'Poll created successfully'
expect(page).to have_content "Upcoming poll" expect(page).to have_content 'Upcoming poll'
expect(page).to have_content I18n.l(start_date.to_date) expect(page).to have_content I18n.l(start_date.to_date)
end end
scenario 'Create a poll redirects back to form when invalid data', js: true do
click_link 'Polls'
click_link 'Create poll'
click_button 'Create poll'
expect(page).to have_content('New poll')
end
scenario 'Edit poll is allowed for upcoming polls' do scenario 'Edit poll is allowed for upcoming polls' do
poll = create(:poll, :incoming, related: proposal) poll = create(:poll, :incoming, related: proposal)
@@ -57,6 +66,24 @@ feature 'Polls' do
expect(page).to have_content 'Poll updated successfully' expect(page).to have_content 'Poll updated successfully'
end end
scenario 'Edit poll redirects back when invalid data', js: true do
poll = create(:poll, :incoming, related: proposal)
visit proposal_dashboard_polls_path(proposal)
within "div#poll_#{poll.id}" do
expect(page).to have_content('Edit survey')
click_link 'Edit survey'
end
fill_in "poll_name", with: ''
click_button 'Update poll'
expect(page).to have_content('Edit poll')
end
scenario 'Edit poll is not allowed for current polls' do scenario 'Edit poll is not allowed for current polls' do
poll = create(:poll, :current, related: proposal) poll = create(:poll, :current, related: proposal)

View File

@@ -29,5 +29,14 @@ feature 'Poster' do
expect(page).not_to have_link('Preview') expect(page).not_to have_link('Preview')
expect(page).to have_link('Download') expect(page).to have_link('Download')
end end
scenario 'PDF contains the proposal details', js: true do
click_link 'Download'
page.driver.browser.switch_to.window page.driver.browser.window_handles.last do
expect(page).to have_content(proposal.title)
expect(page).to have_content(proposal.code)
end
end
end end

View File

@@ -1,3 +1,8 @@
unless ENV['TRAVIS']
require 'simplecov'
SimpleCov.start 'rails'
end
ENV['RAILS_ENV'] ||= 'test' ENV['RAILS_ENV'] ||= 'test'
if ENV['TRAVIS'] if ENV['TRAVIS']
require 'coveralls' require 'coveralls'