Skip to content

Commit

Permalink
add decline event
Browse files Browse the repository at this point in the history
  • Loading branch information
raul-fuentes committed Dec 15, 2017
1 parent e7d5101 commit 427e748
Show file tree
Hide file tree
Showing 17 changed files with 204 additions and 4 deletions.
27 changes: 27 additions & 0 deletions app/assets/javascripts/events.js.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
$(function(){

$('#accept_link').bind('click', function(e){
e.preventDefault();
$('#accept_link').hide();
$('#accept-reason').show();
});

$('#decline_link').bind('click', function(e){
e.preventDefault();
$('#decline_link').hide();
Expand Down Expand Up @@ -67,6 +74,9 @@ $(function(){
"event[published_at]" : {
required : true
},
"event[accept]" : {
checkMessage : "accepted_reasons"
},
"event[decline]" : {
checkMessage : "declined_reasons"
},
Expand Down Expand Up @@ -106,6 +116,9 @@ $(function(){
"event[event_agents_attributes][0][name]" : {
valueNotEquals: "<%= I18n.translate('backend.event.event_agent_needed')%>"
},
"event[accept]" : {
checkMessage : "<%= I18n.translate('backend.event.accept_reasons_needed')%>"
},
"event[decline]" : {
checkMessage : "<%= I18n.translate('backend.event.decline_reasons_needed')%>"
},
Expand Down Expand Up @@ -190,6 +203,20 @@ $(function(){
$("#decline-reason").hide();
});

$('#accept-reason').hide();

$("#event_accept_true").click(function(){
$("#accept-reason").show();
});

if ($("#event_accept_true").is(":checked")) {
$("#accept-reason").show();
};

$("#event_accept_false").click(function(){
$("#accept-reason").hide();
});

});

function fill_represented_entities_select_options() {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def get_title
def event_params
params.require(:event).permit(:title, :description, :location, :scheduled, :position_id, :search_title, :search_person,
:lobby_activity, :notes, :status, :reasons, :published_at, :cancel, :decline, :declined_reasons,:organization_id,
:organization_name, :lobby_scheduled, :general_remarks, :lobby_contact_firstname,
:organization_name, :lobby_scheduled, :general_remarks, :lobby_contact_firstname, :accept, :accepted_reasons,
:lobby_contact_lastname, :lobby_contact_phone, :lobby_contact_email, :lobby_general_remarks,
event_represented_entities_attributes: [:id, :name, :_destroy],
event_agents_attributes: [:id, :name, :_destroy],
Expand Down
4 changes: 4 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ def declined_event(event)
!event.declined_at.nil?
end

def accepted_event(event)
!event.accepted_at.nil?
end

end
10 changes: 10 additions & 0 deletions app/mailers/event_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@ def decline(event)
mail(to: event.lobby_contact_email, subject: subject)
end

def accept(event)
@lobby_name = event.lobby_user_name
@reasons = event.accepted_reasons
@event_title = event.title
@name = event.user.full_name
@accepted_at = l event.accepted_at, format: :short
subject = t('mailers.accept_event.subject', title: @event_title)
mail(to: event.lobby_contact_email, subject: subject)
end

end
11 changes: 10 additions & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Event < ActiveRecord::Base
include PublicActivity::Model

attr_accessor :cancel, :decline
attr_accessor :cancel, :decline, :accept

tracked owner: Proc.new { |controller, model| controller.present? ? controller.current_user : model.user }
tracked title: Proc.new { |controller, model| controller.present? ? controller.get_title : model.title }
Expand All @@ -14,10 +14,12 @@ class Event < ActiveRecord::Base
validate :participants_uniqueness, :position_not_in_participants, :role_validate_published_at, :role_validate_scheduled
validates :reasons, presence: true, if: Proc.new { |a| !a.canceled_at.blank? }
validates :declined_reasons, presence: true, if: Proc.new { |a| !a.declined_at.blank? }
validates :accepted_reasons, presence: true, if: Proc.new { |a| !a.accepted_at.blank? }

before_create :set_status
before_validation :decline_event
before_validation :cancel_event
before_validation :accept_event

belongs_to :user
belongs_to :position
Expand Down Expand Up @@ -68,6 +70,13 @@ def decline_event
EventMailer.decline(self).deliver_now
end

def accept_event
return unless accept == 'true' && accepted_at.nil?
self.accepted_at = Time.zone.today
self.status = 'accepted'
EventMailer.accept(self).deliver_now
end

def self.managed_by(user)
holder_ids = Holder.managed_by(user.id).pluck(:id)
titular_event_ids = Event.by_holders(holder_ids).pluck(:id)
Expand Down
25 changes: 25 additions & 0 deletions app/views/event_mailer/accept.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<td style="padding-bottom: 20px; padding-left: 10px;">
<h1 style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;">
<%= t('mailers.accept_event.welcome', name: @lobby_name) %>
</h1>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.accept_event.text1', title: @event_title) %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.accept_event.text2', name: @name) %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= raw @reasons %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.accept_event.accepted_at', accepted_at: @accepted_at) %>
</p>

<p style="font-family: 'Open Sans','Helvetica Neue',arial,sans-serif;font-size: 14px;font-weight: normal;line-height: 24px;">
<%= t('mailers.accept_event.thanks') %>
</p>
</td>
File renamed without changes.
25 changes: 25 additions & 0 deletions app/views/events/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,31 @@
</div>
<% end %>
</fieldset>

<fieldset class="accept-reason-activity">
<% accepted_event = accepted_event(f.object) %>
<% if accepted_event %>
<legend><%= t('backend.accepted_event') %></legend>
<%= f.label :reasons, t('backend.reasons') %>
<br/>
<%= raw f.object.accepted_reasons %>
<% else %>
<legend><%= t('backend.accept_event') %></legend>
<%= f.radio_button :accept, true, checked: accepted_event %>
<%= f.label :accept, t('backend.accept_reason_yes'), value: true %>
<%= f.radio_button :accept, false, checked: !accepted_event %>
<%= f.label :accept, t('backend.accept_reason_no'), value: false %>
<div class="small-12 error-radio-buttons"></div>
<div id="accept-reason" style="display: none">
<div class="row">
<div class="small-12 columns">
<%= f.label :accepted_reasons, t('backend.reasons') %>
<%= f.text_area :accepted_reasons, class: 'tinymce' %>
</div>
</div>
</div>
<% end %>
</fieldset>
<% end %>
<%= submit_tag t('backend.save'), :class=> "button radius success right" %>
Expand Down
8 changes: 7 additions & 1 deletion config/locales/admin.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ es:
not_available_agents: "Not available agents."
reasons_needed: "You must provide some reasons to cancel the event"
decline_reasons_needed: "You must provide some reasons to decline the event"
accept_reasons_needed: "You must provide some reasons to accept the event"
search:
title: "Búsqueda por título"
placeholder_title: "Búsqueda por título"
Expand Down Expand Up @@ -162,10 +163,15 @@ es:
done: "Done"
declined: "Rejected"
canceled: "Canceled"
decline_event: Decline evento
accepted: "Accepted"
decline_event: Decline event
declined_event: Event declined
decline_reason_yes: Yes
decline_reason_no: No
accept_event: Decline event
accepted_event: Event accepted
accept_reason_yes: Yes
accept_reason_no: No

backend_header:
open_gov: "Gobierno abierto"
Expand Down
6 changes: 6 additions & 0 deletions config/locales/admin.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ es:
not_available_agents: "No hay agentes disponibles."
reasons_needed: "Debe dar alguna razon para cancelar el evento"
decline_reasons_needed: "Debe dar alguna razon para declinar el evento"
accept_reasons_needed: "Debe dar alguna razon para aceptar el evento"
search:
title: "Búsqueda por título"
placeholder_title: "Búsqueda por título"
Expand Down Expand Up @@ -163,10 +164,15 @@ es:
done: "Realizada"
declined: "Rechazada"
canceled: "Cancelada"
accepted: "Aceptada"
decline_event: Declinar evento
declined_event: Evento declinado
decline_reason_yes: Si
decline_reason_no: "No"
accept_event: Aceptar evento
accepted_event: Evento aceptado
accept_reason_yes: Si
accept_reason_no: "No"

backend_header:
open_gov: "Gobierno abierto"
Expand Down
7 changes: 7 additions & 0 deletions config/locales/mailers.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ en:
text1: "The event: %{title}"
text2: "Has been declined by %{name}, for the following reason:"
declined_at: "On %{declined_at}"
thanks: "Regards"
accept_event:
subject: "Accepted event: %{title}"
welcome: "Hello %{name}"
text1: "The event: %{title}"
text2: "Has been accept by %{name}, for the following reason:"
accepted_at: "On %{accepted_at}"
thanks: "Regards"
7 changes: 7 additions & 0 deletions config/locales/mailers.es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,11 @@ es:
text1: "El evento: %{title}"
text2: "Ha sido declinado por %{name}, por la siguiente razón:"
declined_at: "A día %{declined_at}"
thanks: "Un saludo"
accept_event:
subject: "Aceptado evento: %{title}"
welcome: "Hola %{name}"
text1: "El evento: %{title}"
text2: "Ha sido aceptado por %{name}, por la siguiente razón:"
accepted_at: "A día %{accepted_at}"
thanks: "Un saludo"
6 changes: 6 additions & 0 deletions db/migrate/20171214164211_add_addepted_at_to_event.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddAddeptedAtToEvent < ActiveRecord::Migration
def change
add_column :events, :accepted_at, :date
add_column :events, :accepted_reasons, :string
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171213171029) do
ActiveRecord::Schema.define(version: 20171214164211) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -135,6 +135,8 @@
t.integer "organization_id"
t.date "declined_at"
t.string "declined_reasons"
t.date "accepted_at"
t.string "accepted_reasons"
end

add_index "events", ["position_id"], name: "index_events_on_position_id", using: :btree
Expand Down
2 changes: 2 additions & 0 deletions spec/events_exporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
expect(row).to include(event.reasons)
expect(row).to include(event.published_at)
expect(row).to include(event.canceled_at)
expect(row).to include(event.accepted_at)
expect(row).to include(event.declined_at)
expect(row).to include(event.organization_name)
expect(row).to include(event.lobby_scheduled)
expect(row).to include(event.general_remarks)
Expand Down
43 changes: 43 additions & 0 deletions spec/features/admin/events_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,49 @@
expect(page).not_to have_selector "#event_decline_true"
end

scenario "User can accept events only once!" do
event_requested = create(:event, title: 'Event on request', position: @position, status: 0,
accepted_reasons: 'test', accepted_at: Time.zone.today, organization: @organization)
visit edit_event_path(event_requested)

expect(page).not_to have_selector "#event_accept_true"
end

scenario "User can accept events", :js do
event = create(:event, organization: @organization)
visit edit_event_path(event)

page.find_by_id("accept-reason", visible: false)
page.choose('event_accept_true')
page.find_by_id("accept-reason", visible: true)
editor = page.find_by_id('accept-reason')
editor.native.send_keys 'test'

click_button "Guardar"

expect(page).not_to have_selector "#event_accept_true"
end

scenario "User can't accept events without a reason", :js do
event = create(:event, organization: @organization)
visit edit_event_path(event)
page.find_by_id("accept-reason", visible: false)
page.choose('event_accept_true')
page.find_by_id("accept-reason", visible: true)

click_button "Guardar"

expect(page).to have_content I18n.t('backend.event.accept_reasons_needed'), count: 1
end

scenario "User can accept events only once!" do
event_requested = create(:event, title: 'Event on request', position: @position, status: 0,
accepted_reasons: 'test', accepted_at: Time.zone.today, organization: @organization)
visit edit_event_path(event_requested)

expect(page).not_to have_selector "#event_accept_true"
end

scenario 'Lobby user can see on page the name of the organization' do
event = create(:event, organization_name: "Organization name", position: @position,
organization: @organization)
Expand Down
21 changes: 21 additions & 0 deletions spec/features/mailers/events_mailer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,25 @@
end

end

describe "Accept Event" do

background do
clear_emails
@event = create(:event, title: 'New event from Capybara',
user: create(:user, :lobby))
@event.lobby_contact_firstname = 'test_name'
@event.lobby_contact_lastname = 'test_other_name'
@event.lobby_contact_email = 'test_lobby_mail'
@event.accept = 'true'
@event.accepted_reasons = 'test'
@event.save!
open_email(@event.lobby_contact_email)
end

scenario 'accept event mail' do
expect(current_email).to have_content I18n.t('mailers.accept_event.text1', title: @event.title)
end

end
end

0 comments on commit 427e748

Please sign in to comment.