diff options
| -rw-r--r-- | app/models/referential.rb | 145 | ||||
| -rw-r--r-- | config/initializers/ninoxe_extension.rb | 44 | ||||
| -rw-r--r-- | lib/ninoxe_extension/projection_fields.rb | 63 | ||||
| -rw-r--r-- | spec/models/ninoxe_extension_spec.rb | 57 | ||||
| -rw-r--r-- | spec/models/referential_spec.rb | 55 |
5 files changed, 164 insertions, 200 deletions
diff --git a/app/models/referential.rb b/app/models/referential.rb index 41d239961..5940b57ea 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -193,149 +193,4 @@ class Referential < ActiveRecord::Base end -Rails.application.config.after_initialize do - - Chouette::TridentActiveRecord - - class Chouette::TridentActiveRecord - - # add referential relationship for objectid and localization functions - def referential - @referential ||= Referential.where(:slug => Apartment::Database.current_database).first! - end - - # override prefix for good prefix in objectid generation - def prefix - self.referential.prefix - end - - end - - Chouette::StopArea - - class Chouette::StopArea - - attr_accessible :projection_x,:projection_y,:projection_xy - - # override default_position method to add referential envelope when no stoparea is positioned - def default_position - # for first StopArea ... the bounds is nil , set to referential center - Chouette::StopArea.bounds ? Chouette::StopArea.bounds.center : self.referential.envelope.center - end - - # add projection_type set on pre-insert and pre_update action - before_save :set_projections - def set_projections - if ! self.coordinates.blank? - self.long_lat_type = 'WGS84' - else - self.long_lat_type = nil - end - end - - def projection - if self.referential.projection_type.nil? || self.referential.projection_type.empty? - nil - else - self.referential.projection_type - end - end - @point = nil - - def projection_x - if self.long_lat_type.nil? || self.projection.nil? - nil - else - @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) - @point.x - end - end - def projection_y - if self.long_lat_type.nil? || self.projection.nil? - nil - else - @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) - @point.y - end - end - def projection_xy - if self.long_lat_type.nil? || self.projection.nil? - nil - else - @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) - @point.x.to_s+","+@point.y.to_s - end - end - def projection_x=(dummy) - # dummy method - end - def projection_y=(dummy) - # dummy method - end - def projection_xy=(dummy) - # dummy method - end - - end - - Chouette::AccessPoint - - class Chouette::AccessPoint - attr_accessible :projection_x,:projection_y,:projection_xy - - # add projection_type set on pre-insert and pre_update action - before_save :set_projections - def set_projections - if ! self.coordinates.blank? - self.long_lat_type = 'WGS84' - else - self.long_lat_type = nil - end - end - - def projection - if self.referential.projection_type.nil? || self.referential.projection_type.empty? - nil - else - self.referential.projection_type - end - end - @point = nil - - def projection_x - if self.long_lat_type.nil? || self.projection.nil? - nil - else - @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) - @point.x - end - end - def projection_y - if self.long_lat_type.nil? || self.projection.nil? - nil - else - @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) - @point.y - end - end - def projection_xy - if self.long_lat_type.nil? || self.projection.nil? - nil - else - @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) - @point.x.to_s+","+@point.y.to_s - end - end - def projection_x=(dummy) - # dummy method - end - def projection_y=(dummy) - # dummy method - end - def projection_xy=(dummy) - # dummy method - end - end - -end diff --git a/config/initializers/ninoxe_extension.rb b/config/initializers/ninoxe_extension.rb new file mode 100644 index 000000000..aaeb16d76 --- /dev/null +++ b/config/initializers/ninoxe_extension.rb @@ -0,0 +1,44 @@ +# encoding: utf-8 + + +Rails.application.config.after_initialize do + + + Chouette::TridentActiveRecord + + class Chouette::TridentActiveRecord + + # add referential relationship for objectid and localization functions + def referential + @referential ||= Referential.where(:slug => Apartment::Database.current_database).first! + end + + # override prefix for good prefix in objectid generation + def prefix + self.referential.prefix + end + + end + + Chouette::StopArea + + class Chouette::StopArea + + include NinoxeExtension::ProjectionFields + + # override default_position method to add referential envelope when no stoparea is positioned + def default_position + # for first StopArea ... the bounds is nil , set to referential center + Chouette::StopArea.bounds ? Chouette::StopArea.bounds.center : self.referential.envelope.center + end + + + end + + Chouette::AccessPoint + + class Chouette::AccessPoint + include NinoxeExtension::ProjectionFields + end + +end
\ No newline at end of file diff --git a/lib/ninoxe_extension/projection_fields.rb b/lib/ninoxe_extension/projection_fields.rb new file mode 100644 index 000000000..1d12e9519 --- /dev/null +++ b/lib/ninoxe_extension/projection_fields.rb @@ -0,0 +1,63 @@ + + module NinoxeExtension::ProjectionFields + extend ActiveSupport::Concern + + included do + attr_accessible :projection_x,:projection_y,:projection_xy + + # add projection_type set on pre-insert and pre_update action + before_save :set_projections + def set_projections + if ! self.coordinates.blank? + self.long_lat_type = 'WGS84' + else + self.long_lat_type = nil + end + end + + def projection + if self.referential.projection_type.nil? || self.referential.projection_type.empty? + nil + else + self.referential.projection_type + end + end + @point = nil + + def projection_x + if self.long_lat_type.nil? || self.projection.nil? + nil + else + @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) + @point.x + end + end + def projection_y + if self.long_lat_type.nil? || self.projection.nil? + nil + else + @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) + @point.y + end + end + def projection_xy + if self.long_lat_type.nil? || self.projection.nil? + nil + else + @point ||= GeoRuby::SimpleFeatures::Point::from_lat_lng(Geokit::LatLng.new(self.latitude,self.longitude)).project_to(self.projection.to_i) + @point.x.to_s+","+@point.y.to_s + end + end + def projection_x=(dummy) + # dummy method + end + def projection_y=(dummy) + # dummy method + end + def projection_xy=(dummy) + # dummy method + end + end + end + + diff --git a/spec/models/ninoxe_extension_spec.rb b/spec/models/ninoxe_extension_spec.rb new file mode 100644 index 000000000..91d259fb6 --- /dev/null +++ b/spec/models/ninoxe_extension_spec.rb @@ -0,0 +1,57 @@ +require 'spec_helper' + +describe Chouette::StopArea do + # check override methods + + subject {Factory(:stop_area)} + + it "should return referential projection " do + subject.referential.projection_type='27572' + subject.projection.should == subject.referential.projection_type + end + + it "should return projection coordinates when referential has projection" do + subject.latitude = 45 + subject.longitude = 0 + subject.referential.projection_type='27572' + subject.projection_x.should_not be_nil + subject.projection_y.should_not be_nil + end + + it "should return nil projection coordinates when referential has no projection" do + subject.latitude = 45 + subject.longitude = 0 + subject.referential.projection_type=nil + subject.projection_x.should be_nil + subject.projection_y.should be_nil + end + +end + +describe Chouette::AccessPoint do + # check override methods + + subject {Factory(:access_point)} + + it "should return referential projection " do + subject.referential.projection_type='27572' + subject.projection.should == subject.referential.projection_type + end + + it "should return projection coordinates when referential has projection" do + subject.latitude = 45 + subject.longitude = 0 + subject.referential.projection_type='27572' + subject.projection_x.should_not be_nil + subject.projection_y.should_not be_nil + end + + it "should return nil projection coordinates when referential has no projection" do + subject.latitude = 45 + subject.longitude = 0 + subject.referential.projection_type=nil + subject.projection_x.should be_nil + subject.projection_y.should be_nil + end + +end diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb index a11bbd9de..5ea88ad20 100644 --- a/spec/models/referential_spec.rb +++ b/spec/models/referential_spec.rb @@ -13,58 +13,3 @@ describe Referential do end end -describe Chouette::StopArea do - # check override methods - - subject {Factory(:stop_area)} - - it "should return referential projection " do - subject.referential.projection_type='27572' - subject.projection.should == subject.referential.projection_type - end - - it "should return projection coordinates when referential has projection" do - subject.latitude = 45 - subject.longitude = 0 - subject.referential.projection_type='27572' - subject.projection_x.should_not be_nil - subject.projection_y.should_not be_nil - end - - it "should return nil projection coordinates when referential has no projection" do - subject.latitude = 45 - subject.longitude = 0 - subject.referential.projection_type=nil - subject.projection_x.should be_nil - subject.projection_y.should be_nil - end - -end - -describe Chouette::AccessPoint do - # check override methods - - subject {Factory(:access_point)} - - it "should return referential projection " do - subject.referential.projection_type='27572' - subject.projection.should == subject.referential.projection_type - end - - it "should return projection coordinates when referential has projection" do - subject.latitude = 45 - subject.longitude = 0 - subject.referential.projection_type='27572' - subject.projection_x.should_not be_nil - subject.projection_y.should_not be_nil - end - - it "should return nil projection coordinates when referential has no projection" do - subject.latitude = 45 - subject.longitude = 0 - subject.referential.projection_type=nil - subject.projection_x.should be_nil - subject.projection_y.should be_nil - end - -end |
