From 0296af92db96e573ba981aa9778aa5d48987e363 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Mon, 19 Oct 2015 00:47:39 +0300 Subject: [PATCH] Make approved events confirmable --- app/controllers/public/events_controller.rb | 12 ++++++++++++ app/models/concerns/proposable.rb | 5 +++-- app/models/proposition.rb | 4 ++++ config/locales/bg.yml | 2 ++ config/locales/en.yml | 2 ++ config/routes.rb | 6 +++++- 6 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/controllers/public/events_controller.rb b/app/controllers/public/events_controller.rb index 2e37e11..66c81f1 100644 --- a/app/controllers/public/events_controller.rb +++ b/app/controllers/public/events_controller.rb @@ -40,6 +40,18 @@ module Public end end + def confirm + @event = current_user.events.approved.find(params[:id]) + + if @event.confirm! + flash[:notice] = I18n.t('views.events.successfully_confirmed', event_type: @event.event_type.name.mb_chars.downcase) + else + flash[:alert] = I18n.t('views.events.error_on_confirmation', event_type: @event.event_type.name.mb_chars.downcase) + end + + after_save_redirect + end + private def event_params diff --git a/app/models/concerns/proposable.rb b/app/models/concerns/proposable.rb index 77226a9..b60ff47 100644 --- a/app/models/concerns/proposable.rb +++ b/app/models/concerns/proposable.rb @@ -5,10 +5,11 @@ module Proposable has_one :proposition, as: :proposable, dependent: :destroy has_one :proposer, through: :proposition delegate :email, to: :proposer, prefix: true - scope :confirmed, -> { joins(:proposition).where.not(propositions: {confirmed_at: nil}) } + delegate :confirm!, to: :proposition + scope :confirmed, -> { where.not(propositions: {confirmed_at: nil}) } Proposition.defined_enums["status"].keys.each do |status| - scope status.to_sym, -> { joins(:proposition).where(propositions: {status: Proposition.defined_enums["status"][status]}) } + scope status.to_sym, -> { where(propositions: {status: Proposition.defined_enums["status"][status]}) } end end diff --git a/app/models/proposition.rb b/app/models/proposition.rb index 97b54be..c3ab063 100644 --- a/app/models/proposition.rb +++ b/app/models/proposition.rb @@ -7,6 +7,10 @@ class Proposition < ActiveRecord::Base after_create :send_creation_notification before_destroy :send_withdrawal_notification + def confirm! + update(confirmed_at: Time.now) + end + def send_creation_notification PropositionMailer.new_proposition_notification(self).deliver_later end diff --git a/config/locales/bg.yml b/config/locales/bg.yml index e00be93..b96c5f2 100644 --- a/config/locales/bg.yml +++ b/config/locales/bg.yml @@ -289,6 +289,8 @@ bg: edit_event: "Редактиране на %{event_type} %{title}" event_successfully_created: "Предложението Ви за %{event_type} беше създадено успешно" event_successfully_updated: "Предложението Ви за %{event_type} беше обновено успешно" + successfully_confirmed: "Предложението Ви за %{event_type} беше потвърдено успешно" + error_on_confirmation: "Възникна грешка при потвърждението на предложението Ви за %{event_type}" no_events: "Все още не сте предложили събитие" submit_event: "Предлагане на %{event_type}" navigation: diff --git a/config/locales/en.yml b/config/locales/en.yml index 8be9ba8..ac2d8cf 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -282,6 +282,8 @@ en: edit_event: Editing %{event_type} %{title} event_successfully_created: Your %{event_type} submission was successfully created event_successfully_updated: Your %{event_type} submission was successfully updated + successfully_confirmed: Your %{event_type} submission was successfully confirmed + error_on_confirmation: There was an error during the confirmation of your %{event_type} submission no_events: You are yet to submit an event submit_event: Submit a %{event_type} navigation: diff --git a/config/routes.rb b/config/routes.rb index 8ef59e1..2262ce7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,7 +4,11 @@ Rails.application.routes.draw do scope module: :public do root to: 'home#index' resource :personal_profile, path: 'profile' - resources :events + resources :events do + member do + get :confirm + end + end resources :volunteerships, only: [:index, :destroy] resources :volunteer_teams, only: [] do resource :volunteership, only: :create