diff options
| author | Alban Peignier | 2016-11-20 22:05:38 +0100 | 
|---|---|---|
| committer | Alban Peignier | 2016-11-20 22:10:33 +0100 | 
| commit | 4791486858aeefed6291b55c73228f2728824265 (patch) | |
| tree | 8b9df89bd759a28b4d44c6acc64e829ff2d83ac6 | |
| parent | 983488065c7e8e30a6dda6cd0fbce9c3fa4c6622 (diff) | |
| download | chouette-core-4791486858aeefed6291b55c73228f2728824265.tar.bz2 | |
Add an observer to manage opposite_route on other routes. Refs #1999
| -rw-r--r-- | Gemfile | 2 | ||||
| -rw-r--r-- | Gemfile.lock | 3 | ||||
| -rw-r--r-- | app/models/referential.rb | 2 | ||||
| -rw-r--r-- | app/models/route_observer.rb | 21 | ||||
| -rw-r--r-- | config/application.rb | 3 | 
5 files changed, 31 insertions, 0 deletions
| @@ -31,6 +31,8 @@ gem 'spring', group: :development  # ActiveRecord associations on top of PostgreSQL arrays  gem 'has_array_of', git: 'git@github.com:AF83/has_array_of.git' +gem 'rails-observers' +  # Use ActiveModel has_secure_password  # gem 'bcrypt', '~> 3.1.7' diff --git a/Gemfile.lock b/Gemfile.lock index 235c02318..8ae079b8a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -393,6 +393,8 @@ GEM      rails-i18n (4.0.4)        i18n (~> 0.6)        railties (~> 4.0) +    rails-observers (0.1.2) +      activemodel (~> 4.0)      railties (4.1.10)        actionpack (= 4.1.10)        activesupport (= 4.1.10) @@ -649,6 +651,7 @@ DEPENDENCIES    rails-assets-typeahead.js-bootstrap3.less!    rails-erd    rails-i18n (~> 4.0.0) +  rails-observers    rake    ransack    rb-fsevent diff --git a/app/models/referential.rb b/app/models/referential.rb index be7c15535..84bc1110d 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -222,6 +222,8 @@ class Referential < ActiveRecord::Base      line_ids = metadatas.first.line_ids      period = metadatas.first.periodes.first +    return [] unless line_ids.present? && period +      not_myself = "and referential_id != #{id}" if persisted?      query = "SELECT distinct(referential_id) FROM diff --git a/app/models/route_observer.rb b/app/models/route_observer.rb new file mode 100644 index 000000000..71578c6da --- /dev/null +++ b/app/models/route_observer.rb @@ -0,0 +1,21 @@ +class RouteObserver < ActiveRecord::Observer +  observe Chouette::Route + +  def after_save(route) +    Rails.logger.debug "after_save #{route.inspect}" +    if route.opposite_route_id +      Rails.logger.debug "dereference_opposite_route all routes except #{route.opposite_route_id}" +      route.line.routes.where("id <> ?", route.opposite_route_id).where(opposite_route_id: route).update_all(opposite_route_id: nil) +      Rails.logger.debug "reference_opposite_route #{route.opposite_route_id}" +      route.opposite_route.update_column :opposite_route_id, route +    else +      Rails.logger.debug "dereference_opposite_route all routes associated to #{route.id}" +      route.line.routes.where(opposite_route_id: route).update_all(opposite_route_id: nil) +    end +  end + +  def after_destroy(route) +    Rails.logger.debug "after_destroy(#{route.inspect})" +    line.routes.where(opposite_route: route).update_all(opposite_route: nil) +  end +end diff --git a/config/application.rb b/config/application.rb index b9076abec..70b927855 100644 --- a/config/application.rb +++ b/config/application.rb @@ -29,6 +29,9 @@ module ChouetteIhm      # Configure Browserify to use babelify to compile ES6      config.browserify_rails.commandline_options = "-t [ babelify --presets [ react es2015 ] ]" + +    config.active_record.observers = :route_observer +      unless Rails.env.production?          # Work around sprockets+teaspoon mismatch:          Rails.application.config.assets.precompile += %w(spec_helper.js) | 
