From 87e63d0f34702c081f5b0f9a1feedda0a1cae962 Mon Sep 17 00:00:00 2001 From: echarp Date: Mon, 29 May 2017 22:18:04 +0200 Subject: [PATCH] Slightly cleaner code --- app/controllers/events_controller.rb | 2 +- app/controllers/moderations_controller.rb | 13 ++++--------- app/models/event.rb | 6 +++--- test/models/city_test.rb | 1 + test/models/region_test.rb | 1 + 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/app/controllers/events_controller.rb b/app/controllers/events_controller.rb index ad0131a8..dd693b98 100644 --- a/app/controllers/events_controller.rb +++ b/app/controllers/events_controller.rb @@ -111,7 +111,7 @@ class EventsController < ApplicationController end def filter_for_rss - if params[:future].present? && params[:future] == 'false' + if params[:future] == 'false' @events.order 'id desc' else @events.future.order('id desc').limit 20 diff --git a/app/controllers/moderations_controller.rb b/app/controllers/moderations_controller.rb index 4311be90..9eb0f66f 100644 --- a/app/controllers/moderations_controller.rb +++ b/app/controllers/moderations_controller.rb @@ -46,7 +46,6 @@ class ModerationsController < ApplicationController # DELETE /events/1.json def destroy if @moderation.reason == 'r_0' - logger.info 'SPAM! SPAM! SPAM!' @moderation.paper_trail.without_versioning :destroy else @moderation.destroy! @@ -71,7 +70,7 @@ class ModerationsController < ApplicationController params.require(:event) .permit :lock_version, :title, :start_time, :end_time, :repeat, :rule, :description, :place_name, :address, :city, :region_id, - :locality, :url, :contact, :submitter, :tag_list + :locality, :url, :contact, :submitter, :tag_list, :reason end # Useful to manage absolute url in mails @@ -84,12 +83,8 @@ class ModerationsController < ApplicationController end def generate_destroy_reason - if params[:reason] == 'r_4' - @moderation.reason = params[:event][:reason] - elsif params[:reason] == 'r_0' - @moderation.reason = 'r_0' - else - @moderation.reason = t "moderations.refuse.reason_#{params[:reason]}_long" - end + @moderation.attributes = moderation_params + return if params[:reason] == 'r_4' + @moderation.reason = t "moderations.refuse.reason_#{params[:reason]}_long" end end diff --git a/app/models/event.rb b/app/models/event.rb index 5d0ee205..853ed0f8 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -24,7 +24,7 @@ class Event < ApplicationRecord validates :region, presence: true validates :url, allow_nil: true, format: %r{\Ahttps?:\/\/.*\..*\z} validates :contact, email: true - validates :submitter, email: true + validates :submitter, email: true, presence: false geocoded_by :full_address after_validation :geocode, if: (lambda do |obj| @@ -47,7 +47,7 @@ class Event < ApplicationRecord scope :last_year, -> { where '? <= end_time', 1.year.ago } scope :past, -> { where 'start_time <= ?', Time.zone.now } scope :future, -> { where '? <= end_time', Time.zone.now } - scope :daylimit, ->(d) { where 'end_time <= ?', d.to_i.days.from_now } + scope :daylimit, ->(nb) { where 'end_time <= ?', nb.to_i.days.from_now } scope :year, (lambda do |year| where '? <= end_time and start_time <= ?', Date.new(year.to_i, 1, 1).beginning_of_week, @@ -73,7 +73,7 @@ class Event < ApplicationRecord self.decision_time = Time.zone.now # Populate submitter using contact info if absent - self.submitter = contact if submitter.blank? + self.submitter ||= contact end before_validation on: :update do diff --git a/test/models/city_test.rb b/test/models/city_test.rb index 660f995b..eec1d1bc 100644 --- a/test/models/city_test.rb +++ b/test/models/city_test.rb @@ -1,5 +1,6 @@ require 'test_helper' +# Will be removed in the future class CityTest < ActiveSupport::TestCase # test "the truth" do # assert true diff --git a/test/models/region_test.rb b/test/models/region_test.rb index 4fe24098..b822223f 100644 --- a/test/models/region_test.rb +++ b/test/models/region_test.rb @@ -1,5 +1,6 @@ require 'test_helper' +# Mostly a data holder model, to maintain coutry > region data class RegionTest < ActiveSupport::TestCase # test "the truth" do # assert true