diff options
| author | Marc Florisson | 2015-02-03 12:26:37 +0100 | 
|---|---|---|
| committer | Marc Florisson | 2015-02-03 12:26:37 +0100 | 
| commit | a98cf1bdae71922512abb284429c96777d01db18 (patch) | |
| tree | e9f76cfa66b50e48b9061a1d3b3e34022e9e87a2 | |
| parent | 04148815395986714ac6c1f98abcc23744cb97eb (diff) | |
| download | chouette-core-a98cf1bdae71922512abb284429c96777d01db18.tar.bz2 | |
first refactor to add rule parameter set to organisation
26 files changed, 531 insertions, 75 deletions
| diff --git a/app/models/referential.rb b/app/models/referential.rb index 1cd0a16a8..01dba95ab 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -208,7 +208,7 @@ Rails.application.config.after_initialize do      end      def hub_restricted? -      referential.organisation.hub_restrictions +      referential.organisation.hub_restrictions == true      end      # override prefix for good prefix in objectid generation @@ -218,14 +218,17 @@ Rails.application.config.after_initialize do    end -  Chouette::Route - -  class Chouette::Route -    include NinoxeExtension::Hub::RouteRestrictions - -    #validates_numericality_of :number -    #validates_numericality_of :wayback_code -  end +  # Hub constraints +  Chouette::Route; class Chouette::Route; include NinoxeExtension::Hub::RouteRestrictions; end +  Chouette::JourneyPattern; class Chouette::JourneyPattern; include NinoxeExtension::Hub::JourneyPatternRestrictions; end +  Chouette::VehicleJourney; class Chouette::VehicleJourney; include NinoxeExtension::Hub::VehicleJourneyRestrictions; end +  Chouette::TimeTable; class Chouette::TimeTable; include NinoxeExtension::Hub::TimeTableRestrictions; end +  Chouette::ConnectionLink; class Chouette::ConnectionLink; include NinoxeExtension::Hub::ConnectionLinkRestrictions; end +  Chouette::StopArea; class Chouette::StopArea; include NinoxeExtension::Hub::StopAreaRestrictions; end +  Chouette::Line; class Chouette::Line; include NinoxeExtension::Hub::LineRestrictions; end +  Chouette::GroupOfLine; class Chouette::GroupOfLine; include NinoxeExtension::Hub::GroupOfLineRestrictions; end +  Chouette::Company; class Chouette::Company; include NinoxeExtension::Hub::CompanyRestrictions; end +  Chouette::Network; class Chouette::Network; include NinoxeExtension::Hub::NetworkRestrictions; end    Chouette::TimeTable diff --git a/app/models/rule_parameter_set.rb b/app/models/rule_parameter_set.rb index 01c2e48a6..3eb74fdcf 100644 --- a/app/models/rule_parameter_set.rb +++ b/app/models/rule_parameter_set.rb @@ -1,12 +1,13 @@  class RuleParameterSet < ActiveRecord::Base    belongs_to :referential +  belongs_to :organisation -  validates_presence_of :referential +  #validates_presence_of :referential    validates_presence_of :name    serialize :parameters, JSON -  attr_accessible :name, :referential_id +  attr_accessible :name, :referential_id, :organisation_id    def self.mode_attribute_prefixes      %w( allowed_transport inter_stop_area_distance_min inter_stop_area_distance_max speed_max speed_min inter_stop_duration_variation_max) @@ -15,25 +16,25 @@ class RuleParameterSet < ActiveRecord::Base      %w( inter_stop_area_distance_min parent_stop_area_distance_max stop_areas_area inter_access_point_distance_min        inter_connection_link_distance_max walk_default_speed_max        walk_occasional_traveller_speed_max walk_frequent_traveller_speed_max walk_mobility_restricted_traveller_speed_max -      inter_access_link_distance_max inter_stop_duration_max facility_stop_area_distance_max  -      check_allowed_transport_modes check_lines_in_groups check_line_routes  +      inter_access_link_distance_max inter_stop_duration_max facility_stop_area_distance_max +      check_allowed_transport_modes check_lines_in_groups check_line_routes        check_stop_parent check_connection_link_on_physical)    end -   +    def self.validable_objects        [Chouette::Network,Chouette::Company,Chouette::GroupOfLine,          Chouette::StopArea,Chouette::AccessPoint,Chouette::AccessLink,Chouette::ConnectionLink,          Chouette::TimeTable,Chouette::Line,Chouette::Route,          Chouette::JourneyPattern,Chouette::VehicleJourney]    end -   +    def self.validable_object_names        ["network","company","group_of_line",          "stop_area","access_point","access_link","connection_link",          "time_table","line","route",          "journey_pattern","vehicle_journey"]    end -   +    def self.validable_columns        {"network" => ['objectid','name','registration_number'],          "company" => ['objectid','name','registration_number'], @@ -48,7 +49,7 @@ class RuleParameterSet < ActiveRecord::Base          "journey_pattern" => ['objectid','name','registration_number','published_name'],          "vehicle_journey" => ['objectid','published_journey_name','published_journey_identifier','number'] }    end -   +    def self.column_attribute_prefixes      %w( unique pattern min_size max_size )    end @@ -63,14 +64,14 @@ class RuleParameterSet < ActiveRecord::Base      mode_attribute_prefixes.include?( $1) && self.class.all_modes.include?( $2)    end -   +    def self.column_attribute?( method_name )      pattern = /(\w+)_column_(\w+)_object_(\w+)/      return false unless method_name.match( pattern)      return false unless validable_object_names.include?($3)      column_attribute_prefixes.include?( $1) && validable_columns[$3].include?( $2)    end -   +    def self.mode_of_mode_attribute( method_name )      method_name.match( /(\w+)_mode_(\w+)/)      $2 @@ -109,7 +110,7 @@ class RuleParameterSet < ActiveRecord::Base        ((self.parameters ||= {})["mode_#{mode}"] ||= {})[attribute_name] = prefix      end    end -   +    def self.column_parameter(obj,column,prefix)      name = "#{prefix}_column_#{column}_object_#{obj}"      attr_accessible name @@ -123,7 +124,7 @@ class RuleParameterSet < ActiveRecord::Base        attribute_name, column, obj = prefix, column, obj        (((self.parameters ||= {})[obj] ||= {})[column]||= {})[attribute_name] = key      end -     +     end    def self.object_parameter(clazz) @@ -135,11 +136,11 @@ class RuleParameterSet < ActiveRecord::Base        column_attribute_prefixes.each do |prefix|           column_parameter name,column,prefix        end -       +      end    end -   +    def self.parameter(name)      name = name.to_s      attr_accessible name @@ -169,7 +170,7 @@ class RuleParameterSet < ActiveRecord::Base        :check_lines_in_groups => false,        :check_line_routes  => false,        :check_stop_parent  => false, -      :check_connection_link_on_physical  => false  +      :check_connection_link_on_physical  => false      }      if mode && self.mode_default_params[ mode.to_sym]        base.merge!( self.mode_default_params[ mode.to_sym]) @@ -320,13 +321,13 @@ class RuleParameterSet < ActiveRecord::Base          :name => "valeurs par defaut"        }.merge( mode_attributes))    end -   +    def allowed(mode)      return true unless self.check_allowed_transport_modes      # puts "#{mode} = "+self.send("allowed_transport_mode_#{mode}").to_s      return self.send("allowed_transport_mode_#{mode}") == "1"    end -   +    def selected(object)      # puts "#{mode} = "+self.send("allowed_transport_mode_#{mode}").to_s      return self.send("check_#{object}") == "1" @@ -337,7 +338,7 @@ class RuleParameterSet < ActiveRecord::Base      return self.send("unique_column_#{column}_object_#{object}") == "1" ||             self.send("pattern_column_#{column}_object_#{object}") != "0" ||             !self.send("min_size_column_#{column}_object_#{object}").empty? || -           !self.send("max_size_column_#{column}_object_#{object}").empty?  +           !self.send("max_size_column_#{column}_object_#{object}").empty?    end @@ -346,15 +347,15 @@ class RuleParameterSet < ActiveRecord::Base        mode_parameter "#{prefix}_mode_#{mode}".to_sym      end    end -   +    general_attributes.each do |attribute|      parameter attribute.to_sym      unless attribute == "stop_areas_area" || attribute == "check_allowed_transport_modes"        validates attribute.to_sym, :numericality => true, :allow_nil => true, :allow_blank => true      end    end -   -   + +    validable_objects.each do |obj|       object_parameter obj    end diff --git a/app/models/stop_area_copy.rb b/app/models/stop_area_copy.rb index 041d30eb9..6bfa11d4d 100644 --- a/app/models/stop_area_copy.rb +++ b/app/models/stop_area_copy.rb @@ -6,13 +6,13 @@ class StopAreaCopy    extend  ActiveModel::Naming    attr_accessor :source_id, :hierarchy, :area_type, :source, :copy -   +    validates_presence_of :source_id, :hierarchy, :area_type -   +    validates :hierarchy, inclusion: { in: %w(child parent) } -     -  def initialize(attributes = {})     + +  def initialize(attributes = {})      attributes.each { |name, value| send("#{name}=", value) } if attributes      if self.area_type.blank? && self.source != nil        self.source_id = self.source.id @@ -31,38 +31,61 @@ class StopAreaCopy        end      end    end -   +    def persisted?      false    end -   + +  def source +    @source ||= Chouette::StopArea.find self.source_id +  end + +  def copy +    @copy ||= self.source.duplicate +  end + +  def copy_is_source_parent? +    self.hierarchy == "parent" +  end + +  def copy_is_source_child? +    self.hierarchy == "child" +  end + +  def copy_modfied_attributes +    { :name => self.source.name, # TODO: change ninoxe to avoid that !!! +      :area_type => self.area_type.camelcase, +      :registration_number => nil, +      :parent_id => copy_is_source_child? ? self.source_id : self.copy.parent_id +    } +  end + +  def source_modified_attributes +    return {} unless copy_is_source_parent? +    { :parent_id => self.copy.id +    } +  end +    def save      begin        if self.valid? -        self.source ||= Chouette::StopArea.find self.source_id -        self.copy = source.duplicate -        self.copy.name = source.name -        self.copy.area_type = self.area_type.camelcase          Chouette::StopArea.transaction do -          if self.hierarchy == "child" -            self.copy.parent_id = source.id -          end -          self.copy.save! -          if self.hierarchy == "parent" -            self.source.parent_id = copy.id -            self.source.save! +          copy.update_attributes copy_modfied_attributes +          unless source_modified_attributes.empty? +            source.update_attributes source_modified_attributes            end          end          true        else -        false   +        false        end      rescue Exception => exception        Rails.logger.error(exception.message) +      Rails.logger.error(exception.backtrace.join("\n"))        errors.add :base, I18n.t("stop_area_copies.errors.exception")        false      end -  end    -   -   +  end + +  end diff --git a/app/models/vehicle_identifier.rb b/app/models/vehicle_identifier.rb new file mode 100644 index 000000000..f53725c74 --- /dev/null +++ b/app/models/vehicle_identifier.rb @@ -0,0 +1,16 @@ +class VehicleIdentifier + +  def self.auto_identify_vehicles + +    #max_identifier = Chouette::VehicleJourney. +    Chouette::VehicleJourney.transaction do +      vehicles = Chouette::VehicleJourney.lock(true).all +      max = vehicles.map(&:vehicle_type_identifier).compact.map(&:to_i).max + +      vehicles.select {|v| v.vehicle_type_identifier.nil? }.each_with_index do |v, index| +        v.update_attributes :vehicle_type_identifier => ( max.to_i + index + 1) +      end +    end +  end + +end diff --git a/config/routes.rb b/config/routes.rb index a2da2b071..c629877b7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -63,26 +63,20 @@ ChouetteIhm::Application.routes.draw do      resources :rule_parameter_sets      resources :autocomplete_stop_areas      resources :autocomplete_time_tables -    match 'lines' => 'lines#destroy_all', :via => :delete      resources :group_of_lines do        collection do -        get :name_filter +        get 'name_filter'        end      end +    resources :networks + +    match 'lines' => 'lines#destroy_all', :via => :delete      resources :lines do -      collection do -        get :name_filter -      end        resources :routes do          member do            get 'edit_boarding_alighting'            put 'save_boarding_alighting'          end -      end -    end - -    resources :lines, :networks, :group_of_lines do -      resources :routes do          resources :journey_patterns do            member do              get 'new_vehicle_journey' diff --git a/db/migrate/20150129170104_add_organisation_id_rule_parameter_set.rb b/db/migrate/20150129170104_add_organisation_id_rule_parameter_set.rb new file mode 100644 index 000000000..8b3f52caf --- /dev/null +++ b/db/migrate/20150129170104_add_organisation_id_rule_parameter_set.rb @@ -0,0 +1,9 @@ +class AddOrganisationIdRuleParameterSet < ActiveRecord::Migration +  def up +    add_column :rule_parameter_sets, :organisation_id, :integer, :limit => 8 +  end + +  def down +    remove_column :rule_parameter_sets, :organisation_id +  end +end diff --git a/db/schema.rb b/db/schema.rb index fdcc40582..b49ca789f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@  #  # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20150121144108) do +ActiveRecord::Schema.define(:version => 20150129170104) do    create_table "access_links", :force => true do |t|      t.integer  "access_point_id",                        :limit => 8 @@ -384,11 +384,12 @@ ActiveRecord::Schema.define(:version => 20150121144108) do    end    create_table "rule_parameter_sets", :force => true do |t| -    t.integer  "referential_id", :limit => 8 +    t.integer  "referential_id",  :limit => 8      t.text     "parameters"      t.string   "name" -    t.datetime "created_at",                  :null => false -    t.datetime "updated_at",                  :null => false +    t.datetime "created_at",                   :null => false +    t.datetime "updated_at",                   :null => false +    t.integer  "organisation_id", :limit => 8    end    create_table "stop_areas", :force => true do |t| diff --git a/lib/ninoxe_extension/hub/company_restrictions.rb b/lib/ninoxe_extension/hub/company_restrictions.rb new file mode 100644 index 000000000..780e0e4d9 --- /dev/null +++ b/lib/ninoxe_extension/hub/company_restrictions.rb @@ -0,0 +1,20 @@ +module NinoxeExtension::Hub +  module CompanyRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |g| +        # HUB-7 +        g.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,3}$} +        # HUB-8 +        g.validates_format_of :name, :with => %r{^[\w]{1,75}$} +        # HUB-9 +        g.validates_format_of :registration_number, :with => %r{^[\d]{1,8}$} +        g.validates_uniqueness_of :registration_number +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/connection_link_restrictions.rb b/lib/ninoxe_extension/hub/connection_link_restrictions.rb new file mode 100644 index 000000000..bf433379b --- /dev/null +++ b/lib/ninoxe_extension/hub/connection_link_restrictions.rb @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module ConnectionLinkRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |jp| +        # HUB-34 +        jp.validates :link_distance, :numericality => { :max => 10000.0 } +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/group_of_line_restrictions.rb b/lib/ninoxe_extension/hub/group_of_line_restrictions.rb new file mode 100644 index 000000000..13688376f --- /dev/null +++ b/lib/ninoxe_extension/hub/group_of_line_restrictions.rb @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module GroupOfLineRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |g| +        # HUB-11 +        g.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,6}$} +        # HUB-12 +        g.validates_format_of :name, :with => %r{^[\w]{1,75}$} +        # HUB-13 +        g.validates_format_of :registration_number, :with => %r{^[\d]{1,8}$} +        g.validates_uniqueness_of :registration_number +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/journey_pattern_restrictions.rb b/lib/ninoxe_extension/hub/journey_pattern_restrictions.rb new file mode 100644 index 000000000..2726c563a --- /dev/null +++ b/lib/ninoxe_extension/hub/journey_pattern_restrictions.rb @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module JourneyPatternRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |jp| +        # HUB-39 +        jp.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,30}$} +        # HUB-40 +        jp.validates :registration_number, :numericality => { :less_than => 10 ** 8 } +        # HUB-41 +        jp.validates_format_of :name, :with => %r{^[\w]{0,75}$} +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/line_restrictions.rb b/lib/ninoxe_extension/hub/line_restrictions.rb new file mode 100644 index 000000000..119ab07b6 --- /dev/null +++ b/lib/ninoxe_extension/hub/line_restrictions.rb @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module LineRestrictions +    extend ActiveSupport::Concern + + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |l| +        # HUB-15 +        l.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,14}$} +        # HUB-16 +        l.validates_format_of :number, :with => %r{^[\w]{1,6}$} +        # HUB-17 +        l.validates_format_of :name, :with => %r{^[\w]{0,75}$} +        # HUB-21 +        l.validates :registration_number, :numericality => { :less_than => 10 ** 8 } +        # HUB-22 +        l.validates_uniqueness_of :name, :allow_blank => true +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/network_restrictions.rb b/lib/ninoxe_extension/hub/network_restrictions.rb new file mode 100644 index 000000000..b2e8d4be5 --- /dev/null +++ b/lib/ninoxe_extension/hub/network_restrictions.rb @@ -0,0 +1,20 @@ +module NinoxeExtension::Hub +  module NetworkRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |g| +        # HUB-3 +        g.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,3}$} +        # HUB-4 +        g.validates_format_of :name, :with => %r{^[\w]{1,75}$} +        # HUB-5 +        g.validates_format_of :registration_number, :with => %r{^[\d]{1,8}$} +        g.validates_uniqueness_of :registration_number +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/objectid_restrictions.rb b/lib/ninoxe_extension/hub/objectid_restrictions.rb new file mode 100644 index 000000000..675994359 --- /dev/null +++ b/lib/ninoxe_extension/hub/objectid_restrictions.rb @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub::ObjectidRestrictions +    extend ActiveSupport::Concern + +    included do +      # HUB 1 +      validate :third_part_objectid_uniqueness + +      def third_part_objectid +        self.objectid.match(/:(\w+)$/)[1] +      end +      def third_part_objectid_uniqueness +        return unless hub_restricted? + +        return true unless third_part_objectid +        likes = Chouette::Line.where( "objectid LIKE ?", "%:#{self.third_part_objectid}" ) +        likes.size.zero? || ( likes.size==1 && likes.first.id==self.id) +      end + +    end +end + diff --git a/lib/ninoxe_extension/hub/route_restrictions.rb b/lib/ninoxe_extension/hub/route_restrictions.rb index c6c403796..6b996ec3d 100644 --- a/lib/ninoxe_extension/hub/route_restrictions.rb +++ b/lib/ninoxe_extension/hub/route_restrictions.rb @@ -1,25 +1,29 @@  # -*- coding: utf-8 -*- -module NinoxeExtension::Hub::RouteRestrictions +module NinoxeExtension::Hub +  module RouteRestrictions      extend ActiveSupport::Concern      included do +      include ObjectidRestrictions        validate :max_instance_limitation, :wayback_code_limitation +      # HUB-37        def wayback_code_limitation          return unless hub_restricted?          errors.add( :wayback_code, "déjà pris!") if line.routes.reject {|r| r.id==id}.map(&:wayback_code).include?( wayback_code)        end +      # HUB-37        def max_instance_limitation          return unless hub_restricted?          errors.add( :flash, "2 routes max")        end +      # HUB-38        with_options if: :hub_restricted? do |route|          route.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,8}$} -        #admin.validates :password, length: { minimum: 10 } -        #admin.validates :email, presence: true        end      end +  end  end diff --git a/lib/ninoxe_extension/hub/stop_area_restrictions.rb b/lib/ninoxe_extension/hub/stop_area_restrictions.rb new file mode 100644 index 000000000..f09510cc4 --- /dev/null +++ b/lib/ninoxe_extension/hub/stop_area_restrictions.rb @@ -0,0 +1,53 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module StopAreaRestrictions +    extend ActiveSupport::Concern + +    included do +      #include ObjectidRestrictions +      def physical? +        self.area_type=="BoardingPosition" ||  self.area_type=="Quay" +      end +      def commercial? +        self.area_type=="CommercialStopPoint" +      end +      def self.physical_hub_restricted? +        Proc.new { |s| s.hub_restricted? && s.commercial?} +      end +      def self.commercial_hub_restricted? +        Proc.new { |s| s.hub_restricted? && s.physical?} +      end +      def self.commercial_and_physical_hub_restricted? +        physical_hub_restricted? || commercial_hub_restricted? +      end + + +      with_options if: commercial_and_physical_hub_restricted? do |sa| +      # HUB-23 +        sa.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,12}$} +        sa.validates_format_of :name, :with => %r{^[\w]{1,75}$} +      end +      with_options if: commercial_hub_restricted? do |sa| +        # HUB-24 +        validates_format_of :nearest_topic_name, :with => %r{^[\w]{0,255}$} +      end + +      with_options if: physical_hub_restricted? do |sa| +        # HUB-25 +        sa.validates_format_of :nearest_topic_name, :with => %r{^[\w]{0,60}$} +        # HUB-28 +        sa.validates_presence_of :longitude +        sa.validates_presence_of :latitude +        # HUB-29 +        sa.validates_format_of :city_name, :with => %r{^[\w]{1,80}$} +        # HUB-30 +        sa.validates_format_of :zip_code, :with => %r{^[\d]{5}$} +        # HUB-31 +        sa.validates_format_of :comment, :with => %r{^[\w]{0,255}$} +        # HUB-32 +        sa.validates_format_of :registration_number, :with => %r{^[\w]{1,8}$}, :allow_blank => true, :allow_nil => true +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/time_table_restrictions.rb b/lib/ninoxe_extension/hub/time_table_restrictions.rb new file mode 100644 index 000000000..6f2984dda --- /dev/null +++ b/lib/ninoxe_extension/hub/time_table_restrictions.rb @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module TimeTableRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      with_options if: :hub_restricted? do |jp| +        # HUB-44 +        jp.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,6}$} +        # HUB-45 +        jp.validates_format_of :comment, :with => %r{^[\w]{0,75}$} +      end +    end +  end +end + diff --git a/lib/ninoxe_extension/hub/vehicle_journey_restrictions.rb b/lib/ninoxe_extension/hub/vehicle_journey_restrictions.rb new file mode 100644 index 000000000..81a023dca --- /dev/null +++ b/lib/ninoxe_extension/hub/vehicle_journey_restrictions.rb @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +module NinoxeExtension::Hub +  module VehicleJourneyRestrictions +    extend ActiveSupport::Concern + +    included do +      include ObjectidRestrictions + +      # HUB-42 +      with_options if: :hub_restricted? do |jp| +        jp.validates_format_of :objectid, :with => %r{^\w+:\w+:[\w]{1,8}$} +      end +    end +  end +end + diff --git a/spec/models/line_spec.rb b/spec/models/line_spec.rb new file mode 100644 index 000000000..0b7fcac1c --- /dev/null +++ b/spec/models/line_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe "sdflkjskdjf" do + +  subject { Factory(:line) } + +  def set_large_object_id( line) +    line.update_attributes :objectid => "AA:Line:123456789012345" +  end + +  describe "validation objectid unique constraint" do +    let(:organisation){subject.referential.organisation} +    let(:objectid_a){ "A:Line:1234" } +    let(:objectid_b){ "B:Line:1234" } +    let!(:second_line){ Factory( :line, :objectid => objectid_a, :registration_number => "123456") } +    context "when organisation works with HUB" do +      before( :each) do +        organisation.update_attributes :hub_restrictions => true +        subject.update_attributes :objectid => objectid_a +      end +      it "should have objectid with a third part shorter than 14 char" do +        subject.update_attributes :objectid => objectid_b +        subject.should_not be_valid +      end +    end +    context "when organisation doesn't works with HUB" do +      before( :each) do +        organisation.update_attributes :hub_restrictions => false +      end +      it "should have objectid with a third part shorter than 14 char" do +        subject.update_attributes :objectid => objectid_b +        subject.should be_valid +      end +    end +  end +  describe "validation objectid size" do +    let(:organisation){subject.referential.organisation} +    context "when organisation works with HUB" do +      before( :each) do +        organisation.update_attributes :hub_restrictions => true +      end +      it "should have objectid with a third part shorter than 14 char" do +        set_large_object_id( subject) +        subject.should_not be_valid +      end +    end +    context "when organisation doesn't works with HUB" do +      before( :each) do +        organisation.update_attributes :hub_restrictions => false +      end +      it "should have objectid with a third part shorter than 14 char" do +        set_large_object_id( subject) +        subject.should be_valid +      end +    end +  end +end + diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb new file mode 100644 index 000000000..6baf13c46 --- /dev/null +++ b/spec/models/organisation_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe Organisation do + +  it { should validate_presence_of(:name) } +  it { should validate_uniqueness_of(:name) } +end diff --git a/spec/models/rule_parameter_set_spec.rb b/spec/models/rule_parameter_set_spec.rb index bf785d6f7..3e872598f 100644 --- a/spec/models/rule_parameter_set_spec.rb +++ b/spec/models/rule_parameter_set_spec.rb @@ -38,7 +38,7 @@ describe RuleParameterSet do    end    describe "#referential" do -    it { should validate_presence_of(:referential) } +    #it { should validate_presence_of(:referential) }      it { should allow_mass_assignment_of :referential_id }    end diff --git a/spec/models/stop_area_copy_spec.rb b/spec/models/stop_area_copy_spec.rb index 66aa56d30..d9fac2409 100644 --- a/spec/models/stop_area_copy_spec.rb +++ b/spec/models/stop_area_copy_spec.rb @@ -2,29 +2,32 @@  require 'spec_helper'  describe StopAreaCopy do -   +    subject { StopAreaCopy.new(:source_id => 1, :hierarchy => "child", :area_type => "Quay") }    it { should validate_presence_of :source_id }    it { should validate_presence_of :hierarchy }    it { should validate_presence_of :area_type } -   -   + +    describe ".save" do      it "should create a child for source" do -      source = Chouette::StopArea.new( :area_type => "CommercialStopPoint", :name => "test1" ) +      source = Factory(:stop_area, :area_type => "CommercialStopPoint", :name => "test1", +                      :registration_number => "123456", :city_name => "dummy", :zip_code => "12345")        source.save        subject.source_id = source.id        subject.hierarchy = "child"        subject.area_type = "Quay"        subject.save +        source.reload        source.children.length.should == 1        source.children[0].name.should == "test1"      end      it "should create a parent for source" do -      source = Chouette::StopArea.new( :area_type => "CommercialStopPoint", :name => "test2" ) +      source = Factory(:stop_area, :area_type => "CommercialStopPoint", :name => "test2", +                      :registration_number => "123456", :city_name => "dummy", :zip_code => "12345")        source.save        subject.source_id = source.id        subject.hierarchy = "parent" @@ -34,7 +37,7 @@ describe StopAreaCopy do        source.parent.should_not be_nil        source.parent.name.should == 'test2'      end -         +    end  end diff --git a/spec/routing/companies_spec.rb b/spec/routing/companies_spec.rb new file mode 100644 index 000000000..25f86325b --- /dev/null +++ b/spec/routing/companies_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe CompaniesController do +  describe "routing" do +    it "not recognize #routes" do +      get( "/referentials/1/companies/2/routes").should_not route_to( +        :controller => "companies", :action => "routes", +        :referential_id => "1", :id => "2" +      ) +    end +    it "not recognize #lines" do +      get( "/referentials/1/companies/2/lines").should_not route_to( +        :controller => "companies", :action => "lines", +        :referential_id => "1", :id => "2" +      ) +    end +    it "recognize and generate #show" do +      get( "/referentials/1/companies/2").should route_to( +        :controller => "companies", :action => "show", +        :referential_id => "1", :id => "2" +      ) +    end +  end +end + diff --git a/spec/routing/group_of_lines_spec.rb b/spec/routing/group_of_lines_spec.rb new file mode 100644 index 000000000..ee5596cf7 --- /dev/null +++ b/spec/routing/group_of_lines_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe GroupOfLinesController do +  describe "routing" do +    it "not recognize #routes" do +      get( "/referentials/1/group_of_lines/2/routes").should_not route_to( +        :controller => "group_of_lines", :action => "routes", +        :referential_id => "1", :id => "2" +      ) +    end +    it "not recognize #lines" do +      get( "/referentials/1/group_of_lines/2/lines").should_not route_to( +        :controller => "group_of_lines", :action => "lines", +        :referential_id => "1", :id => "2" +      ) +    end +    it "recognize and generate #show" do +      get( "/referentials/1/group_of_lines/2").should route_to( +        :controller => "group_of_lines", :action => "show", +        :referential_id => "1", :id => "2" +      ) +    end +  end +end + diff --git a/spec/routing/networks_spec.rb b/spec/routing/networks_spec.rb new file mode 100644 index 000000000..c0544980d --- /dev/null +++ b/spec/routing/networks_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe NetworksController do +  describe "routing" do +    it "not recognize #routes" do +      get( "/referentials/1/networks/2/routes").should_not route_to( +        :controller => "networks", :action => "routes", +        :referential_id => "1", :id => "2" +      ) +    end +    it "not recognize #lines" do +      get( "/referentials/1/networks/2/lines").should_not route_to( +        :controller => "networks", :action => "lines", +        :referential_id => "1", :id => "2" +      ) +    end +    it "recognize and generate #show" do +      get( "/referentials/1/networks/2").should route_to( +        :controller => "networks", :action => "show", +        :referential_id => "1", :id => "2" +      ) +    end +  end +end + diff --git a/spec/routing/time_tables_spec.rb b/spec/routing/time_tables_spec.rb new file mode 100644 index 000000000..45e426f4c --- /dev/null +++ b/spec/routing/time_tables_spec.rb @@ -0,0 +1,31 @@ +require 'spec_helper' + +describe TimeTablesController do +  describe "routing" do +    it "not recognize #routes" do +      get( "/referentials/1/time_tables/2/routes").should_not route_to( +        :controller => "time_tables", :action => "routes", +        :referential_id => "1", :id => "2" +      ) +    end +    it "not recognize #lines" do +      get( "/referentials/1/time_tables/2/lines").should_not route_to( +        :controller => "time_tables", :action => "lines", +        :referential_id => "1", :id => "2" +      ) +    end +    it "not recognize #time_table_periods" do +      get( "/referentials/1/time_tables/2/time_table_periods").should_not route_to( +        "time_table_periods#index", +        :referential_id => "1", :time_table_id => "2" +      ) +    end +    it "recognize and generate #show" do +      get( "/referentials/1/time_tables/2").should route_to( +        :controller => "time_tables", :action => "show", +        :referential_id => "1", :id => "2" +      ) +    end +  end +end + | 
