diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index 2df4eb16..62b423c2 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -35,7 +35,7 @@ class EventsController < ApplicationController # POST /events.json def create respond_to do |format| - if @event.save && send_creation_mails + if @event.save format.html { redirect_to :root, notice: t('.ok') } # 201 means :created format.json { render action: 'show', status: 201, location: @event } @@ -58,7 +58,7 @@ class EventsController < ApplicationController # PATCH/PUT /events/1.json def update respond_to do |format| - if @event.update(event_params) && send_update_mails + if @event.update event_params format.html { redirect_to :root, notice: t('.ok') } format.json { head :no_content } else @@ -108,26 +108,14 @@ class EventsController < ApplicationController :contact, :submitter, :tags end + def locked + redirect_to edit_event_url(@event, secret: @event.secret), + alert: t('staleObjectError') + end + # Check that you can only edit an existing event if you know its secret def check_secret redirect_to :root, alert: t(:forbidden, scope: [:events, :edit]) \ unless params[:secret] == @event.secret end - - def send_creation_mails - # Send an event creation mail to its author - EventMailer.create(@event).deliver_now - # Send a mail to moderators - ModerationMailer.create(@event).deliver_now - end - - def send_update_mails - # Send an update mail to moderators - ModerationMailer.update(@event, nil).deliver_now - end - - def locked - redirect_to edit_event_url(@event, secret: @event.secret), - alert: t('staleObjectError') - end end diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 6070cd62..75698e52 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -20,7 +20,7 @@ class ModerationsController < ApplicationController # PATCH/PUT /moderations/1.json def update respond_to do |format| - if @moderation.update_attributes(moderation_params) && send_mails + if @moderation.update_attributes moderation_params format.html { redirect_to :moderations, notice: t('.ok') } format.json { head :no_content } else @@ -35,7 +35,6 @@ class ModerationsController < ApplicationController # PATCH/PUT /accept/1.json def accept @moderation.update moderated: true - send_accept_mails respond_to do |format| format.html { redirect_to :moderations, notice: t('.ok') } format.json { head :no_content } @@ -45,7 +44,8 @@ class ModerationsController < ApplicationController # DELETE /events/1 # DELETE /events/1.json def destroy - send_destroy_mails if @moderation.destroy + @moderation.reason_for_deletion = get_reason_from_params params[:reason] + @moderation.destroy! respond_to do |format| format.html { redirect_to :moderations, notice: t('.ok') } format.json { head :no_content } @@ -74,45 +74,15 @@ class ModerationsController < ApplicationController ActionMailer::Base.default_url_options[:host] = request.host_with_port end - def send_mails - # Send an update mail to moderators - ModerationMailer.update(@moderation, current_user).deliver_now - end - - def send_accept_mails - tweet - - # Send an acceptation mail to its author - EventMailer.accept(@moderation, current_user).deliver_now - - # Send an acceptation mail to moderators - ModerationMailer.accept(@moderation, current_user).deliver_now - end - - # Tweet this event, if configured using apache/system variables! - def tweet - client = Twitter::REST::Client.new do |config| - config.consumer_key = ENV['TWITTER_CONSUMER_KEY'] - config.consumer_secret = ENV['TWITTER_CONSUMER_SECRET'] - config.access_token = ENV['TWITTER_ACCESS_TOKEN'] - config.access_token_secret = ENV['TWITTER_ACCESS_SECRET'] - end - client.update "#{@event.to_tweet event_url(@event)}" if client.consumer_key - end - - def send_destroy_mails - # Send a notification to its author - if params[:reason] == 'r_4' - @reason = params[:reason_text] - else - @reason = t "moderations.refuse.reason_#{params[:reason]}_long" - end - - EventMailer.destroy(@moderation, current_user, @reason).deliver_now - ModerationMailer.destroy(@moderation, current_user, @reason).deliver_now - end - def locked redirect_to edit_moderation_url(@moderation), alert: t('staleObjectError') end + + def get_reason_from_params(reason) + if reason == 'r_4' + reason + else + t "moderations.refuse.reason_#{reason}_long" + end + end end diff --git a/app/mailers/event_mailer.rb b/app/mailers/event_mailer.rb index 050ea3d4..1661ae98 100644 --- a/app/mailers/event_mailer.rb +++ b/app/mailers/event_mailer.rb @@ -12,9 +12,9 @@ class EventMailer < ApplicationMailer subject: event.title}" end - def accept(event, current_user) + def accept(event) @event = event - @current_user = current_user + @current_user = User.find_by id: event.paper_trail_originator mail 'In-Reply-To' => "", @@ -23,10 +23,9 @@ class EventMailer < ApplicationMailer subject: event.title}" end - def destroy(event, current_user, reason) + def destroy(event) @event = event - @current_user = current_user - @reason = reason + @current_user = User.find_by id: event.paper_trail_originator mail 'In-Reply-To' => "", diff --git a/app/mailers/moderation_mailer.rb b/app/mailers/moderation_mailer.rb index 59e413c5..694cbfd3 100644 --- a/app/mailers/moderation_mailer.rb +++ b/app/mailers/moderation_mailer.rb @@ -11,9 +11,9 @@ class ModerationMailer < ApplicationMailer subject: event.title}" end - def update(event, current_user) + def update(event) @event = event - @current_user = current_user + @current_user = User.find_by id: event.paper_trail_originator mail 'In-Reply-To' => "", @@ -21,9 +21,9 @@ class ModerationMailer < ApplicationMailer subject: event.title}" end - def accept(event, current_user) + def accept(event) @event = event - @current_user = current_user + @current_user = User.find_by id: event.paper_trail_originator mail 'In-Reply-To' => "", @@ -31,10 +31,9 @@ class ModerationMailer < ApplicationMailer subject: event.title}" end - def destroy(event, current_user, reason) + def destroy(event) @event = event - @current_user = current_user - @reason = reason + @current_user = User.find_by id: event.paper_trail_originator mail 'In-Reply-To' => "", diff --git a/app/models/event.rb b/app/models/event.rb index 5c75c582..6a91dd38 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -23,6 +23,12 @@ class Event < ActiveRecord::Base # after_validation :geocode, if: -> (obj) { obj.address_changed? } after_validation :geocode + attr_accessor :reason_for_deletion + + after_create EventCallbacks + after_update EventCallbacks + after_destroy EventCallbacks + scope :moderated, -> { where moderated: true } scope :unmoderated, -> { where moderated: false } scope :last_year, -> { where '? <= end_time', 1.year.ago } @@ -72,19 +78,14 @@ class Event < ActiveRecord::Base end before_update do - if moderated? && moderated_was != moderated - self.decision_time = Time.zone.now - end + self.decision_time = Time.zone.now if moderated? && moderated_changed? end def as_json(_options = {}) popup = "