diff options
| author | Xinhui | 2016-05-11 11:14:28 +0200 |
|---|---|---|
| committer | Xinhui | 2016-05-11 11:16:17 +0200 |
| commit | f0b822710320a3dde22782334b436b87f5d47054 (patch) | |
| tree | 899fc166ac178d07ddeef3627b4d699f446eb1ab | |
| parent | 972bbcb308d360c5468cd629dc5cdf356d556a2e (diff) | |
| download | chouette-core-f0b822710320a3dde22782334b436b87f5d47054.tar.bz2 | |
Add belongs_to line_referential for StopArea
Refs #825
| -rw-r--r-- | Gemfile | 1 | ||||
| -rw-r--r-- | Gemfile.lock | 2 | ||||
| -rw-r--r-- | app/models/chouette/access_point.rb | 20 | ||||
| -rw-r--r-- | app/models/chouette/stop_area.rb | 1 | ||||
| -rw-r--r-- | config/initializers/apartment.rb | 2 | ||||
| -rw-r--r-- | db/migrate/20160511082825_add_line_referential_to_stop_areas.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 4 | ||||
| -rw-r--r-- | spec/models/chouette/stop_area_spec.rb | 85 |
8 files changed, 66 insertions, 54 deletions
@@ -127,6 +127,7 @@ group :development do end group :test, :development do + gem 'awesome_print' gem 'pry-rails' gem 'rspec-rails', '~> 3.1.0' gem 'fakeweb' diff --git a/Gemfile.lock b/Gemfile.lock index a1f6e8b70..3b97d82ae 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -53,6 +53,7 @@ GEM ast (2.0.0) astrolabe (1.3.0) parser (>= 2.2.0.pre.3, < 3.0) + awesome_print (1.6.1) bcrypt (3.1.10) bcrypt (3.1.10-java) better_errors (2.1.1) @@ -472,6 +473,7 @@ DEPENDENCIES acts_as_list (~> 0.6.0) acts_as_tree (~> 2.1.0) apartment (~> 1.0.0) + awesome_print better_errors binding_of_caller breadcrumbs_on_rails diff --git a/app/models/chouette/access_point.rb b/app/models/chouette/access_point.rb index 317b19b26..da1f9524a 100644 --- a/app/models/chouette/access_point.rb +++ b/app/models/chouette/access_point.rb @@ -9,10 +9,10 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord has_many :access_links, :dependent => :destroy belongs_to :stop_area - + attr_accessor :access_point_type attr_writer :coordinates - + validates_presence_of :name validates_presence_of :access_type @@ -33,14 +33,14 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord if self.latitude.nil? || self.longitude.nil? "" else - self.latitude.to_s+","+self.longitude.to_s + self.latitude.to_s+","+self.longitude.to_s end end - + def coordinates @coordinates || combine_lat_lng end - + def coordinates_to_lat_lng if ! @coordinates.nil? if @coordinates.empty? @@ -50,9 +50,9 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord self.latitude = BigDecimal.new(@coordinates.split(",").first) self.longitude = BigDecimal.new(@coordinates.split(",").last) end - @coordinates = nil + @coordinates = nil end - end + end def to_lat_lng Geokit::LatLng.new(latitude, longitude) if latitude and longitude @@ -67,7 +67,7 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord self.latitude, self.longitude, self.long_lat_type = geometry.lat, geometry.lng, "WGS84" end - def position + def position geometry end @@ -78,7 +78,7 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord self.longitude = position.lng end - def default_position + def default_position stop_area.geometry or stop_area.default_position end @@ -87,7 +87,7 @@ class Chouette::AccessPoint < Chouette::TridentActiveRecord access_type && Chouette::AccessPointType.new(access_type.underscore) end - def access_point_type=(access_point_type) + def access_point_type=(access_point_type) self.access_type = (access_point_type ? access_point_type.camelcase : nil) end diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 588014fff..84ea47693 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -15,6 +15,7 @@ class Chouette::StopArea < Chouette::TridentActiveRecord has_and_belongs_to_many :routing_stops, :class_name => 'Chouette::StopArea', :foreign_key => "parent_id", :association_foreign_key => "child_id", :join_table => "stop_areas_stop_areas", :order => "stop_areas.name" belongs_to :stop_area_referential + belongs_to :line_referential validates_presence_of :stop_area_referential_id acts_as_tree :foreign_key => 'parent_id',:order => "name" diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index 9b8d3c6a6..1a309f718 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -17,7 +17,7 @@ Apartment.configure do |config| # # config.excluded_models = %w{Tenant} # - config.excluded_models = ["Referential", "Organisation", "User", "Delayed::Backend::ActiveRecord::Job", "Api::V1::ApiKey", "RuleParameterSet", "StopAreaReferential", "Chouette::StopArea", "LineReferential"] + config.excluded_models = ["Referential", "Organisation", "User", "Delayed::Backend::ActiveRecord::Job", "Api::V1::ApiKey", "RuleParameterSet", "StopAreaReferential", "Chouette::StopArea", "LineReferential", "Chouette::Line"] # use postgres schemas? config.use_schemas = true diff --git a/db/migrate/20160511082825_add_line_referential_to_stop_areas.rb b/db/migrate/20160511082825_add_line_referential_to_stop_areas.rb new file mode 100644 index 000000000..f71f5ee45 --- /dev/null +++ b/db/migrate/20160511082825_add_line_referential_to_stop_areas.rb @@ -0,0 +1,5 @@ +class AddLineReferentialToStopAreas < ActiveRecord::Migration + def change + add_reference :stop_areas, :line_referential, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6b7441190..61f64707f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160510125443) do +ActiveRecord::Schema.define(version: 20160511082825) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -453,8 +453,10 @@ ActiveRecord::Schema.define(version: 20160510125443) do t.string "url" t.string "time_zone" t.integer "stop_area_referential_id" + t.integer "line_referential_id" end + add_index "stop_areas", ["line_referential_id"], :name => "index_stop_areas_on_line_referential_id" add_index "stop_areas", ["objectid"], :name => "stop_areas_objectid_key", :unique => true add_index "stop_areas", ["parent_id"], :name => "index_stop_areas_on_parent_id" add_index "stop_areas", ["stop_area_referential_id"], :name => "index_stop_areas_on_stop_area_referential_id" diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb index 3b19aa17c..f522d02bd 100644 --- a/spec/models/chouette/stop_area_spec.rb +++ b/spec/models/chouette/stop_area_spec.rb @@ -12,12 +12,13 @@ describe Chouette::StopArea, :type => :model do it { is_expected.to be_kind_of(Chouette::ObjectId) } end + it { is_expected.to belong_to(:line_referential) } it { is_expected.to validate_presence_of :name } it { is_expected.to validate_presence_of :area_type } it { is_expected.to validate_numericality_of :latitude } it { is_expected.to validate_numericality_of :longitude } - + describe ".latitude" do it "should accept -90 value" do subject = create :stop_area, :area_type => "BoardingPosition" @@ -89,20 +90,20 @@ describe Chouette::StopArea, :type => :model do subject.latitude = nil expect(subject.valid?).to be_falsey end - end - + end + describe ".children_in_depth" do it "should return all the deepest children from stop area" do subject = create :stop_area, :area_type => "StopPlace" - commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint", :parent => subject + commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint", :parent => subject commercial_stop_point2 = create :stop_area, :area_type => "CommercialStopPoint", :parent => commercial_stop_point quay = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" expect(subject.children_in_depth).to match_array([commercial_stop_point, commercial_stop_point2, quay]) end it "should return only the deepest children from stop area" do subject = create :stop_area, :area_type => "StopPlace" - commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint", :parent => subject + commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint", :parent => subject commercial_stop_point2 = create :stop_area, :area_type => "CommercialStopPoint", :parent => commercial_stop_point quay = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" expect(subject.children_at_base).to match_array([quay]) @@ -139,31 +140,31 @@ describe Chouette::StopArea, :type => :model do end end - describe ".possible_children" do - + describe ".possible_children" do + it "should find no possible descendant for stop area type quay" do subject = create :stop_area, :area_type => "Quay" - expect(subject.possible_children).to eq([]) + expect(subject.possible_children).to eq([]) end it "should find no possible descendant for stop area type boarding position" do subject = create :stop_area, :area_type => "BoardingPosition" - expect(subject.possible_children).to eq([]) + expect(subject.possible_children).to eq([]) end it "should find descendant of type quay or boarding position for stop area type commercial stop point" do subject = create :stop_area, :area_type => "CommercialStopPoint" - expect(subject.possible_children).to match_array([quay, boarding_position]) + expect(subject.possible_children).to match_array([quay, boarding_position]) end it "should find no children of type stop place or commercial stop point for stop area type stop place" do subject = create :stop_area, :area_type => "StopPlace" - expect(subject.possible_children).to match_array([stop_place, commercial_stop_point]) + expect(subject.possible_children).to match_array([stop_place, commercial_stop_point]) end it "should find no children of type ITL for stop area type ITL" do subject = create :stop_area, :area_type => "ITL" - expect(subject.possible_children).to match_array([stop_place, commercial_stop_point, quay, boarding_position]) + expect(subject.possible_children).to match_array([stop_place, commercial_stop_point, quay, boarding_position]) end end @@ -172,23 +173,23 @@ describe Chouette::StopArea, :type => :model do it "should find parent type commercial stop point for stop area type boarding position" do subject = create :stop_area, :area_type => "BoardingPosition" - expect(subject.possible_parents).to eq([commercial_stop_point]) + expect(subject.possible_parents).to eq([commercial_stop_point]) end it "should find parent type commercial stop point for stop area type quay" do subject = create :stop_area, :area_type => "Quay" - expect(subject.possible_parents).to eq([commercial_stop_point]) - end + expect(subject.possible_parents).to eq([commercial_stop_point]) + end it "should find parent type stop place for stop area type commercial stop point" do subject = create :stop_area, :area_type => "CommercialStopPoint" - expect(subject.possible_parents).to eq([stop_place]) - end + expect(subject.possible_parents).to eq([stop_place]) + end it "should find parent type stop place for stop area type stop place" do subject = create :stop_area, :area_type => "StopPlace" - expect(subject.possible_parents).to eq([stop_place]) - end + expect(subject.possible_parents).to eq([stop_place]) + end end @@ -197,7 +198,7 @@ describe Chouette::StopArea, :type => :model do let(:stop_area) { create :stop_area, :latitude => 1, :longitude => 1 } let(:stop_area2) { create :stop_area, :latitude => 1, :longitude => 1 } - + it "should find a StopArea at 300m from given origin" do expect(Chouette::StopArea.near(stop_area.to_lat_lng.endpoint(0, 0.250, :units => :kms))).to eq([stop_area]) end @@ -209,7 +210,7 @@ describe Chouette::StopArea, :type => :model do end describe "#to_lat_lng" do - + it "should return nil if latitude is nil" do subject.latitude = nil expect(subject.to_lat_lng).to be_nil @@ -223,7 +224,7 @@ describe Chouette::StopArea, :type => :model do end describe "#geometry" do - + it "should be nil when to_lat_lng is nil" do allow(subject).to receive_messages :to_lat_lng => nil expect(subject.geometry).to be_nil @@ -232,7 +233,7 @@ describe Chouette::StopArea, :type => :model do end describe ".bounds" do - + it "should return transform coordinates in floats" do allow(Chouette::StopArea.connection).to receive_messages :select_rows => [["113.5292500000000000", "22.1127580000000000", "113.5819330000000000", "22.2157050000000000"]] expect(GeoRuby::SimpleFeatures::Envelope).to receive(:from_coordinates).with([[113.5292500000000000, 22.1127580000000000], [113.5819330000000000, 22.2157050000000000]]) @@ -242,7 +243,7 @@ describe Chouette::StopArea, :type => :model do end describe "#default_position" do - + it "should return referential center point when StopArea.bounds is nil" do allow(Chouette::StopArea).to receive_messages :bounds => nil expect(subject.default_position).not_to be_nil @@ -257,39 +258,39 @@ describe Chouette::StopArea, :type => :model do describe "#children_at_base" do it "should have 2 children_at_base" do - subject = create :stop_area, :area_type => "StopPlace" + subject = create :stop_area, :area_type => "StopPlace" commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject quay1 = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" quay2 = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" expect(subject.children_at_base.size).to eq(2) end - end + end describe "#generic_access_link_matrix" do it "should have no access_links in matrix with no access_point" do - subject = create :stop_area, :area_type => "StopPlace" + subject = create :stop_area, :area_type => "StopPlace" commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject expect(subject.generic_access_link_matrix.size).to eq(0) end it "should have 4 generic_access_links in matrix with 2 access_points" do - subject = create :stop_area, :area_type => "StopPlace" + subject = create :stop_area, :area_type => "StopPlace" commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject access_point1 = create :access_point, :stop_area => subject access_point2 = create :access_point, :stop_area => subject expect(subject.generic_access_link_matrix.size).to eq(4) end - end + end describe "#detail_access_link_matrix" do it "should have no access_links in matrix with no access_point" do - subject = create :stop_area, :area_type => "StopPlace" + subject = create :stop_area, :area_type => "StopPlace" commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject quay1 = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" quay2 = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" expect(subject.detail_access_link_matrix.size).to eq(0) end it "should have 8 detail_access_links in matrix with 2 children_at_base and 2 access_points" do - subject = create :stop_area, :area_type => "StopPlace" + subject = create :stop_area, :area_type => "StopPlace" commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => subject quay1 = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" quay2 = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" @@ -297,10 +298,10 @@ describe Chouette::StopArea, :type => :model do access_point2 = create :access_point, :stop_area => subject expect(subject.detail_access_link_matrix.size).to eq(8) end - end + end describe "#parents" do it "should return parent hireachy list" do - stop_place = create :stop_area, :area_type => "StopPlace" + stop_place = create :stop_area, :area_type => "StopPlace" commercial_stop_point = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place subject = create :stop_area, :parent => commercial_stop_point, :area_type => "Quay" expect(subject.parents.size).to eq(2) @@ -310,24 +311,24 @@ describe Chouette::StopArea, :type => :model do expect(subject.parents.size).to eq(0) end end - + describe "#clean_invalid_access_links" do it "should remove invalid access links" do # subject is a CSP with a SP as parent, a quay as child # 2 access_points of SP have access_link, one on subject, one on subject child # when detaching subject from SP, both access_links must be deleted - stop_place = create :stop_area, :area_type => "StopPlace" + stop_place = create :stop_area, :area_type => "StopPlace" subject = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place access_point1 = create :access_point, :stop_area => stop_place access_point2 = create :access_point, :stop_area => stop_place quay = create :stop_area, :parent => subject, :area_type => "Quay" access_link1 = create :access_link, :stop_area => subject, :access_point => access_point1 access_link2 = create :access_link, :stop_area => quay, :access_point => access_point2 - subject.save + subject.save expect(subject.access_links.size).to eq(1) expect(quay.access_links.size).to eq(1) subject.parent=nil - subject.save + subject.save subject.reload expect(subject.access_links.size).to eq(0) expect(quay.access_links.size).to eq(0) @@ -337,7 +338,7 @@ describe Chouette::StopArea, :type => :model do # 2 access_points, one of SP, one of CSP have access_link on subject # when changing subject CSP to another CSP of same SP # one access_links must be kept - stop_place = create :stop_area, :area_type => "StopPlace" + stop_place = create :stop_area, :area_type => "StopPlace" commercial_stop_point1 = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place commercial_stop_point2 = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place access_point1 = create :access_point, :stop_area => stop_place @@ -345,15 +346,15 @@ describe Chouette::StopArea, :type => :model do subject = create :stop_area, :parent => commercial_stop_point1, :area_type => "Quay" access_link1 = create :access_link, :stop_area => subject, :access_point => access_point1 access_link2 = create :access_link, :stop_area => subject, :access_point => access_point2 - subject.save + subject.save expect(subject.access_links.size).to eq(2) subject.parent=commercial_stop_point2 - subject.save + subject.save subject.reload expect(subject.access_links.size).to eq(1) end end - + describe "#coordinates" do it "should convert coordinates into latitude/longitude" do subject = create :stop_area, :area_type => "BoardingPosition", :coordinates => "45.123,120.456" @@ -422,7 +423,7 @@ describe Chouette::StopArea, :type => :model do describe "#duplicate" do it "should be a copy of" do - stop_place = create :stop_area, :area_type => "StopPlace" + stop_place = create :stop_area, :area_type => "StopPlace" subject = create :stop_area, :area_type => "CommercialStopPoint" ,:parent => stop_place, :coordinates => "45.123,120.456" access_point1 = create :access_point, :stop_area => subject access_point2 = create :access_point, :stop_area => subject |
