diff options
| author | Teddy Wing | 2017-06-19 18:11:36 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-06-19 18:11:36 +0200 |
| commit | 7172eaf839b57c1003537713286033e7e5f6c005 (patch) | |
| tree | fda4bf8b47f994a128433dddaa719e96d1a35fca | |
| parent | 533c1a55df630080cd0ff449e229717ba7ace212 (diff) | |
| parent | 7b11f89ca24ceb4949c27baf279564034f0a5106 (diff) | |
| download | chouette-core-7172eaf839b57c1003537713286033e7e5f6c005.tar.bz2 | |
Merge remote-tracking branch 'origin/master' into 3479-refactor-table_builder-helper
| -rw-r--r-- | app/controllers/devise/cas_sessions_controller.rb | 10 | ||||
| -rw-r--r-- | app/models/chouette/journey_pattern.rb | 3 | ||||
| -rw-r--r-- | app/models/concerns/default_attributes_support.rb | 4 | ||||
| -rw-r--r-- | app/models/user.rb | 3 | ||||
| -rw-r--r-- | spec/models/chouette/journey_pattern_spec.rb | 82 |
5 files changed, 92 insertions, 10 deletions
diff --git a/app/controllers/devise/cas_sessions_controller.rb b/app/controllers/devise/cas_sessions_controller.rb index 0a9d9ecb2..d000d0309 100644 --- a/app/controllers/devise/cas_sessions_controller.rb +++ b/app/controllers/devise/cas_sessions_controller.rb @@ -20,14 +20,14 @@ class Devise::CasSessionsController < Devise::SessionsController if LoginPolicy.new(current_user).boiv? redirect_to after_sign_in_path_for(current_user) else - redirect_to root_path, flash: {alert: t('devise.sessions.new.unauthorized')} + destroy message: t('devise.sessions.new.unauthorized') end end def unregistered end - def destroy + def destroy message: nil # if :cas_create_user is false a CAS session might be open but not signed_in # in such case we destroy the session here if signed_in?(resource_name) @@ -36,7 +36,11 @@ class Devise::CasSessionsController < Devise::SessionsController reset_session end - redirect_to(cas_logout_url) + if message + redirect_to(cas_logout_url, flash: {alert: message}) + else + redirect_to(cas_logout_url) + end end def single_sign_out diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index 34b0d9345..3c902946d 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -14,19 +14,18 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord validates_presence_of :route validates_presence_of :name + validates :stop_points, length: { minimum: 2 }, on: :update enum section_status: { todo: 0, completed: 1, control: 2 } attr_accessor :control_checked after_update :control_route_sections, :unless => "control_checked" - def self.state_update route, state transaction do state.each do |item| item.delete('errors') jp = find_by(objectid: item['object_id']) || state_create_instance(route, item) next if item['deletable'] && jp.persisted? && jp.destroy - # Update attributes and stop_points associations jp.update_attributes(state_permited_attributes(item)) jp.state_stop_points_update(item) if !jp.errors.any? && jp.persisted? diff --git a/app/models/concerns/default_attributes_support.rb b/app/models/concerns/default_attributes_support.rb index ecad26856..7928093e6 100644 --- a/app/models/concerns/default_attributes_support.rb +++ b/app/models/concerns/default_attributes_support.rb @@ -63,10 +63,10 @@ module DefaultAttributesSupport def fix_uniq_objectid base_objectid = objectid.rpartition(":").first self.objectid = "#{base_objectid}:#{id}" - if !valid? + if !valid?(:objectid) base_objectid="#{objectid}_" cnt=1 - while !valid? + while !valid?(:objectid) self.objectid = "#{base_objectid}#{cnt}" cnt += 1 end diff --git a/app/models/user.rb b/app/models/user.rb index 1a06746da..4ba05b164 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -31,13 +31,14 @@ class User < ActiveRecord::Base @@edit_offer_permissions = ['routes.create', 'routes.edit', 'routes.destroy', 'journey_patterns.create', 'journey_patterns.edit', 'journey_patterns.destroy', 'vehicle_journeys.create', 'vehicle_journeys.edit', 'vehicle_journeys.destroy', 'time_tables.create', 'time_tables.edit', 'time_tables.destroy', 'footnotes.edit', 'footnotes.create', 'footnotes.destroy', 'routing_constraint_zones.create', 'routing_constraint_zones.edit', - 'routing_constraint_zones.destroy', 'referentials.create', 'referentials.edit', 'referentials.destroy'] + 'routing_constraint_zones.destroy', 'referentials.create', 'referentials.edit', 'referentials.destroy', 'boiv:edit-offer'] mattr_reader :edit_offer_permissions def self.all_permissions edit_offer_permissions end + # Callback invoked by DeviseCasAuthenticable::Model#authernticate_with_cas_ticket def cas_extra_attributes=(extra_attributes) extra = extra_attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} self.name = extra[:full_name] diff --git a/spec/models/chouette/journey_pattern_spec.rb b/spec/models/chouette/journey_pattern_spec.rb index 19b5060d2..f7006efc7 100644 --- a/spec/models/chouette/journey_pattern_spec.rb +++ b/spec/models/chouette/journey_pattern_spec.rb @@ -2,6 +2,25 @@ require 'spec_helper' describe Chouette::JourneyPattern, :type => :model do + context 'validate minimum stop_points size' do + let(:journey_pattern) { create :journey_pattern } + let(:stop_points) { journey_pattern.stop_points } + + it 'should be valid if it has at least two sp' do + journey_pattern.stop_points.first(stop_points.size - 2).each do |sp| + journey_pattern.stop_points.delete(sp) + end + expect(journey_pattern).to be_valid + end + + it 'should not be valid if it has less then two sp' do + journey_pattern.stop_points.first(stop_points.size - 1).each do |sp| + journey_pattern.stop_points.delete(sp) + end + expect(journey_pattern).to_not be_valid + end + end + describe "state_update" do def journey_pattern_to_state jp jp.attributes.slice('name', 'published_name', 'registration_number').tap do |item| @@ -24,12 +43,14 @@ describe Chouette::JourneyPattern, :type => :model do end it 'should attach checked stop_points' do - state['stop_points'].each{|sp| sp['checked'] = true} # Make sure journey_pattern has no stop_points - journey_pattern.stop_points.delete_all + state['stop_points'].each{|sp| sp['checked'] = false} + journey_pattern.state_stop_points_update(state) expect(journey_pattern.reload.stop_points).to be_empty + state['stop_points'].each{|sp| sp['checked'] = true} journey_pattern.state_stop_points_update(state) + expect(journey_pattern.reload.stop_points.count).to eq(5) end @@ -89,6 +110,63 @@ describe Chouette::JourneyPattern, :type => :model do expect(collection.first).to_not have_key('object_id') end + + it 'should create journey_pattern' do + new_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route)) + Chouette::JourneyPattern.state_create_instance route, new_state + expect(new_state['object_id']).to be_truthy + expect(new_state['new_record']).to be_truthy + end + + it 'should delete journey_pattern' do + state['deletable'] = true + collection = [state] + expect { + Chouette::JourneyPattern.state_update route, collection + }.to change{Chouette::JourneyPattern.count}.from(1).to(0) + + expect(collection).to be_empty + end + + it 'should delete multiple journey_pattern' do + collection = 5.times.collect{journey_pattern_to_state(create(:journey_pattern, route: route))} + collection.map{|i| i['deletable'] = true} + + expect { + Chouette::JourneyPattern.state_update route, collection + }.to change{Chouette::JourneyPattern.count}.from(5).to(0) + end + + it 'should validate journey_pattern on update' do + journey_pattern.name = '' + collection = [state] + Chouette::JourneyPattern.state_update route, collection + expect(collection.first['errors']).to have_key(:name) + end + + it 'should validate journey_pattern on create' do + new_state = journey_pattern_to_state(build(:journey_pattern, name: '', objectid: nil, route: route)) + collection = [new_state] + expect { + Chouette::JourneyPattern.state_update route, collection + }.to_not change{Chouette::JourneyPattern.count} + + expect(collection.first['errors']).to have_key(:name) + expect(collection.first).to_not have_key('object_id') + end + + it 'should not save any journey_pattern of collection if one is invalid' do + journey_pattern.name = '' + valid_state = journey_pattern_to_state(build(:journey_pattern, objectid: nil, route: route)) + invalid_state = journey_pattern_to_state(journey_pattern) + collection = [valid_state, invalid_state] + + expect { + Chouette::JourneyPattern.state_update route, collection + }.to_not change{Chouette::JourneyPattern.count} + + expect(collection.first).to_not have_key('object_id') + end end describe "#stop_point_ids" do |
