Commit 0386e004 authored by David F.'s avatar David F.

Merge branch '89-admin-choisir-la-date-anniversaire-de-changement-de-saison' into 'master'

Resolve "[Admin] choisir la date anniversaire de changement de saison"

See merge request !90
parents 679fe9c9 27d551d2
......@@ -58,6 +58,7 @@ gem 'fitvidsjs_rails'
gem 'font-awesome-rails'
gem 'jquery-minicolors-rails'
gem 'prettyphoto-rails'
gem 'validates_timeliness', '~> 5.0.0.alpha3'
group :development, :test do
gem "sqlite3"
......
......@@ -349,11 +349,14 @@ GEM
thread_safe (0.3.6)
tilt (2.0.8)
timecop (0.8.1)
timeliness (0.3.8)
truncate_html (0.9.3)
tzinfo (1.2.5)
thread_safe (~> 0.1)
uglifier (3.2.0)
execjs (>= 0.3.0, < 3)
validates_timeliness (5.0.0.alpha3)
timeliness (~> 0.3.8)
vcr (2.9.3)
web-console (3.5.1)
actionview (>= 5.0)
......@@ -425,6 +428,7 @@ DEPENDENCIES
timecop (~> 0.8.0)
truncate_html
uglifier
validates_timeliness (~> 5.0.0.alpha3)
vcr (~> 2.9.3)
web-console
webmock (~> 1.17.4)
......
......@@ -54,7 +54,7 @@ class SettingsController < ApplicationController
def settings_params
params.require(:settings).permit(:contact_email, :gcal_pass, :gcal_user, :home_message, :motto, :title,
:site_logo, :motto_logo, :newsletters_title, :contact_message,
:site_logo, :motto_logo, :newsletters_title, :contact_message, :season_birthdate,
:main_color, :secondary_color, :h1_color, :h2_color,
:mono_activity_collaborating_price, :mono_activity_standard_price,
:multi_activities_collaborating_price, :multi_activities_standard_price,
......
......@@ -47,7 +47,7 @@ class Activity < ApplicationRecord
result = {}
self.events.visible.chronology.each do |event|
year = event.begin_at.year
if event.begin_at >= Date.new(year,8,15)
if event.begin_at >= Season.birthdate.change(year: year)
(result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event
else
(result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event
......
......@@ -57,7 +57,7 @@ class EventType < ApplicationRecord
result = {}
self.events.with_dates.visible.reverse_chronology.each do |event|
year = event.begin_at.year
if event.begin_at >= Date.new(year,8,15)
if event.begin_at >= Season.birthdate.change(year: year)
(result["Saison #{year.to_s}-#{(year+1).to_s}"] ||= []) << event
else
(result["Saison #{(year-1).to_s}-#{year.to_s}"] ||= []) << event
......
......@@ -24,9 +24,17 @@ class Season
FIRST_MONTH = 8
FIRST_DAY = 15
def self.default_birthdate
Date.new(1970,FIRST_MONTH, FIRST_DAY)
end
def self.birthdate
Settings.instance.reload.season_birthdate || self.default_birthdate
end
def initialize(date)
year = (date < Date.new(date.year, FIRST_MONTH, FIRST_DAY) ? date.year-1 : date.year)
@begining = Date.new(year, FIRST_MONTH, FIRST_DAY)
year = (date < Season.birthdate.change(year: date.year) ? date.year-1 : date.year)
@begining = Season.birthdate.change(year: year)
return self
end
......
......@@ -36,8 +36,11 @@ class Website < ApplicationRecord
attr_accessor :_destroy_site_logo, :_destroy_motto_logo
validates :contact_email, email_format: true, allow_blank: true
validates_attachment_size :site_logo, :less_than => 1.megabyte, :message => "doit faire moins de 1 Mo", :if => Proc.new { |file| !file.site_logo_file_name.blank? }
do_not_validate_attachment_file_type :site_logo
validates_attachment_size :motto_logo, :less_than => 1.megabyte, :message => "doit faire moins de 1 Mo", :if => Proc.new { |file| !file.site_logo_file_name.blank? }
do_not_validate_attachment_file_type :motto_logo
validates_date :season_birthdate, allow_blank: true
end
......@@ -34,6 +34,7 @@
<%= f.input :title %>
<%= f.input :motto %>
<%= f.input :home_message %>
<%= f.input :season_birthdate, default: Season.default_birthdate, discard_year: true %>
<% if @settings.site_logo.file? %>
<%= image_tag @settings.site_logo.url(:standard) %>
<div>
......
ValidatesTimeliness.setup do |config|
config.enable_multiparameter_extension!
config.enable_date_time_select_extension!
end
......@@ -221,6 +221,7 @@ fr:
even: "doit être pair"
taken: "n'est pas disponible"
record_invalid: "La validation a échoué : %{errors}"
invalid_date: "n'est pas une date valide"
template: &errors_template
header:
......
......@@ -185,6 +185,7 @@ fr:
mono_activity_standard_price: "Structure non partenaire"
multi_activities_collaborating_price: "Structure partenaire"
multi_activities_standard_price: "Structure non partenaire"
season_birthdate: "Changement de saison"
newsletter:
title: "Intitulé"
published: "Publiée"
......
class AddSeasonBirthdateToWebsites < ActiveRecord::Migration[5.1]
def change
add_column :websites, :season_birthdate, :date
end
end
......@@ -32,7 +32,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20171211133706) do
ActiveRecord::Schema.define(version: 20180913142206) do
create_table "activities", force: :cascade do |t|
t.string "title", limit: 255
......@@ -400,6 +400,7 @@ ActiveRecord::Schema.define(version: 20171211133706) do
t.decimal "mono_activity_standard_price", precision: 5, scale: 2
t.decimal "multi_activities_collaborating_price", precision: 5, scale: 2
t.decimal "multi_activities_standard_price", precision: 5, scale: 2
t.date "season_birthdate"
end
end
......@@ -72,6 +72,28 @@ feature "Manage Settings" do
end
end
scenario "I can set the season switch day and month" do
day_select = "settings_season_birthdate_3i"
month_select = "settings_season_birthdate_2i"
visit edit_settings_path
expect(page).to have_field(day_select, with: "15")
expect(page).to have_field(month_select, with: "8")
within "form#edit_settings" do
select "1", from: day_select
select "septembre", from: month_select
click_button "Mettre à jour"
end
expect(current_path).to eq(edit_settings_path)
expect(page).to have_content "Les paramètres ont bien été mis à jour"
within "section#content" do
expect(page).to have_field(day_select, with: "1")
expect(page).to have_field(month_select, with: "9")
end
end
context "regarding images" do
scenario "I can provide a site logo" do
visit root_path
......
......@@ -23,19 +23,73 @@
require 'rails_helper'
RSpec.describe Season, type: :model do
context "regarding initialization" do
context "sets the begining ot the season to" do
scenario "15/08/2017 when date 19/09/2017 is given" do
season = Season.new(Date.new(2017,9,19))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15)
context "class methods" do
it ".default_birthdate set to 15/08/1970" do
# Given
# When
result = Season.default_birthdate
# Then
expect(result).to eq Date.new(1970,8,15)
end
context ".birthdate" do
it "return 15/08/1970 when season birthdate not provided" do
# Given
# When
result = Season.birthdate
# Then
expect(result).to eq Date.new(1970,8,15)
end
scenario "15/08/2017 when date 15/08/2017 is given" do
season = Season.new(Date.new(2017,8,15))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15)
it "returns season birthdate when provided" do
# Given
first_of_september = Date.new(1970,9,1)
@settings.update_attribute(:season_birthdate, first_of_september)
# When
result = Season.birthdate
# Then
expect(result).to eq first_of_september
end
scenario "15/08/2016 when date 14/08/2017 is given" do
season = Season.new(Date.new(2017,8,14))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2016,8,15)
end
end
context "regarding initialization" do
context "when season birthdate not provided" do
context "sets the begining ot the season to" do
scenario "15/08/2017 when date 19/09/2017 is given" do
season = Season.new(Date.new(2017,9,19))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15)
end
scenario "15/08/2017 when date 15/08/2017 is given" do
season = Season.new(Date.new(2017,8,15))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,8,15)
end
scenario "15/08/2016 when date 14/08/2017 is given" do
season = Season.new(Date.new(2017,8,14))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2016,8,15)
end
end
end
context "when season birthdate set to 01/09/1970" do
before { @settings.update_attribute(:season_birthdate, Date.new(1970,9,1)) }
context "sets the begining ot the season to" do
scenario "01/09/2017 when date 19/09/2017 is given" do
season = Season.new(Date.new(2017,9,19))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,9,1)
end
scenario "01/09/2017 when date 01/09/2017 is given" do
season = Season.new(Date.new(2017,9,1))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2017,9,1)
end
scenario "01/09/2016 when date 31/08/2017 is given" do
season = Season.new(Date.new(2017,8,31))
expect(season.instance_variable_get(:@begining)).to eq Date.new(2016,9,1)
end
end
end
end
......
......@@ -31,5 +31,23 @@ describe Website do
it { is_expected.to allow_value("contact-all@net.example.com").for(:contact_email) }
it { is_expected.not_to allow_value("contact").for(:contact_email) }
it { is_expected.not_to allow_value("contact@example").for(:contact_email) }
describe "regarding season birthdate" do
it "validates the date is correct" do
# Only check one value, the purpose is not to
# test again the validates_timeliness gem ;)
# Given
settings = build :website
birthdate_attributes = {
"season_birthdate(3i)": "31",
"season_birthdate(2i)": "2",
"season_birthdate(1i)": "1970"
}
# When
settings.assign_attributes(birthdate_attributes)
# Then
expect(settings).not_to be_valid
end
end
end
end
\ No newline at end of file
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment