diff options
| author | Xinhui Xu | 2016-10-13 15:56:23 +0200 |
|---|---|---|
| committer | GitHub | 2016-10-13 15:56:23 +0200 |
| commit | 67bcf4788a0f89f8aef7019c205a3ecc28f1760e (patch) | |
| tree | 7c4da11ab043945b2acb4d9e22f9b957fa3e451c | |
| parent | c2403fa61c0639d2165af57e6db993a429ae5984 (diff) | |
| parent | e55be719d1f65f518a63ba65cf443d2d49c291bb (diff) | |
| download | chouette-core-67bcf4788a0f89f8aef7019c205a3ecc28f1760e.tar.bz2 | |
Merge pull request #4 from AF83/master
Merge master into Staging
140 files changed, 1499 insertions, 595 deletions
@@ -51,7 +51,7 @@ gem 'polylines' # Codifligne API gem 'codifligne', git: 'git@github.com:AF83/stif-codifline-api.git' # Reflex API -gem 'reflex', git: 'git@github.com:AF83/stif-reflex-api.git' +gem 'reflex', git: 'git@github.com:AF83/stif-reflex-api.git', branch: 'sax_refactoring' # Authentication gem 'devise', '~> 3.4.0' @@ -59,6 +59,9 @@ gem 'devise_cas_authenticatable' gem 'devise-encryptable' gem 'devise_invitable' +# Authorization +gem 'pundit' + # Map, Geolocalization gem 'map_layers', '0.0.4' gem 'rgeo', '~> 0.5.2' @@ -93,6 +96,8 @@ gem 'will_paginate', '~> 3.0.7' gem 'ransack' gem 'squeel' +gem 'draper' + gem 'enumerize', '~> 0.10.0' gem 'foreigner', '~> 1.7.4' gem 'deep_cloneable', '~> 2.0.0' @@ -103,11 +108,14 @@ gem 'acts_as_tree', '~> 2.1.0', require: 'acts_as_tree' gem 'rabl' +gem 'sidekiq' +gem 'sinatra' gem 'delayed_job_active_record' gem 'whenever', github: 'af83/whenever', require: false # '~> 0.9' gem 'rake' gem 'devise-async' gem 'apartment', '~> 1.0.0' +gem 'aasm' gem 'newrelic_rpm' gem 'letter_opener' diff --git a/Gemfile.lock b/Gemfile.lock index a0d8ca243..adf7c7999 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,14 +7,15 @@ GIT GIT remote: git@github.com:AF83/stif-codifline-api.git - revision: e38210d88c447ca793b9fc3af6ec4c2a682f054c + revision: aee8bb5320bf03369fe15fd2f000b55e12c8b915 specs: codifligne (0.0.2) nokogiri (~> 1.6) GIT remote: git@github.com:AF83/stif-reflex-api.git - revision: f91087c65d2e2b0a80e30acb6f7a0e863a10f8b4 + revision: 2c01010dce3a6cb39339995c373faf89ae00f3f3 + branch: sax_refactoring specs: reflex (0.0.1) nokogiri (~> 1.6) @@ -26,6 +27,7 @@ GEM RedCloth (4.2.9) RedCloth (4.2.9-java) SyslogLogger (2.0) + aasm (4.5.2) actionmailer (4.1.10) actionpack (= 4.1.10) actionview (= 4.1.10) @@ -110,6 +112,9 @@ GEM coffee-script-source execjs coffee-script-source (1.9.1) + concurrent-ruby (1.0.2) + concurrent-ruby (1.0.2-java) + connection_pool (2.2.0) crack (0.4.3) safe_yaml (~> 1.0.0) cucumber (2.4.0) @@ -162,6 +167,11 @@ GEM docile (1.1.5) dr-ffi-proj4 (0.0.3) ffi (>= 1.0.0) + draper (2.1.0) + actionpack (>= 3.0) + activemodel (>= 3.0) + activesupport (>= 3.0) + request_store (~> 1.0) easy_translate (0.5.0) json thread @@ -285,10 +295,9 @@ GEM net-ssh-gateway (1.2.0) net-ssh (>= 2.6.5) newrelic_rpm (3.11.2.286) - nokogiri (1.6.8) + nokogiri (1.6.8.1) mini_portile2 (~> 2.1.0) - pkg-config (~> 1.1.7) - nokogiri (1.6.8-java) + nokogiri (1.6.8.1-java) notiffany (0.0.6) nenv (~> 0.1) shellany (~> 0.0) @@ -297,7 +306,6 @@ GEM parser (2.2.0.3) ast (>= 1.1, < 3.0) pg (0.18.1) - pkg-config (1.1.7) poltergeist (1.6.0) capybara (~> 2.1) cliver (~> 0.3.1) @@ -317,11 +325,15 @@ GEM spoon (~> 0.0) pry-rails (0.3.4) pry (>= 0.9.10) + pundit (1.1.0) + activesupport (>= 3.0.0) quiet_assets (1.1.0) railties (>= 3.1, < 5.0) rabl (0.11.6) activesupport (>= 2.3.14) rack (1.5.3) + rack-protection (1.5.3) + rack rack-test (0.6.3) rack (>= 1.0) rails (4.1.10) @@ -391,7 +403,9 @@ GEM ffi (>= 0.5.0) rdoc (4.2.0) json (~> 1.4) + redis (3.3.1) ref (2.0.0) + request_store (1.3.1) responders (1.1.2) railties (>= 3.2, < 4.2) rgeo (0.5.2) @@ -443,6 +457,10 @@ GEM shellany (0.0.1) shoulda-matchers (2.8.0) activesupport (>= 3.0.0) + sidekiq (4.1.0) + concurrent-ruby (~> 1.0) + connection_pool (~> 2.2, >= 2.2.0) + redis (~> 3.2, >= 3.2.1) simple_form (3.1.0) actionpack (~> 4.0) activemodel (~> 4.0) @@ -453,6 +471,10 @@ GEM simplecov-html (0.10.0) simplecov-rcov (0.2.3) simplecov (>= 0.4.1) + sinatra (1.4.7) + rack (~> 1.5) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) slim (3.0.7) temple (~> 0.7.6) tilt (>= 1.3.3, < 2.1) @@ -533,6 +555,7 @@ PLATFORMS DEPENDENCIES RedCloth SyslogLogger + aasm activerecord-postgis-adapter acts-as-taggable-on (>= 3) acts_as_list (~> 0.6.0) @@ -560,6 +583,7 @@ DEPENDENCIES devise-i18n devise_cas_authenticatable devise_invitable + draper enumerize (~> 0.10.0) fabrication (~> 2.14.1) factory_girl_rails (~> 4.0) @@ -590,6 +614,7 @@ DEPENDENCIES poltergeist polylines pry-rails + pundit quiet_assets (~> 1.0) rabl rails (~> 4.1.10) @@ -620,9 +645,11 @@ DEPENDENCIES sawyer (~> 0.6.0) sdoc (~> 0.4.0) shoulda-matchers + sidekiq simple_form (~> 3.1.0) simplecov simplecov-rcov + sinatra slim-rails (~> 3.1) spring sqlite3 diff --git a/app/assets/javascripts/offer_workbenches.js.coffee b/app/assets/javascripts/offer_workbenches.js.coffee deleted file mode 100644 index 24f83d18b..000000000 --- a/app/assets/javascripts/offer_workbenches.js.coffee +++ /dev/null @@ -1,3 +0,0 @@ -# Place all the behaviors and hooks related to the matching controller here. -# All this logic will automatically be available in application.js. -# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/application.sass.erb b/app/assets/stylesheets/application.sass.erb index 7d4b48de9..53b767384 100644 --- a/app/assets/stylesheets/application.sass.erb +++ b/app/assets/stylesheets/application.sass.erb @@ -48,4 +48,4 @@ $body-bg: #eee // Hack to make li simple li - list-style: none
\ No newline at end of file + list-style: none diff --git a/app/assets/stylesheets/main/offer_workbenches.sass b/app/assets/stylesheets/main/offer_workbenches.sass deleted file mode 100644 index 2859c635d..000000000 --- a/app/assets/stylesheets/main/offer_workbenches.sass +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the offer_workbenches controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index fa3874632..c2414f5bb 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,9 +1,13 @@ class ApplicationController < ActionController::Base + include Pundit + rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized + # TODO : Delete hack to authorize Cross Request for js and json get request from javascript protect_from_forgery unless: -> { request.get? && (request.format.json? || request.format.js?) } before_action :authenticate_user! before_action :set_locale + # Load helpers in rails engine helper LanguageEngine::Engine.helpers @@ -13,6 +17,10 @@ class ApplicationController < ActionController::Base protected + def user_not_authorized + render :file => "#{Rails.root}/public/403.html", :status => :forbidden, :layout => false + end + def current_organisation current_user.organisation if current_user end diff --git a/app/controllers/breadcrumb_controller.rb b/app/controllers/breadcrumb_controller.rb index 29ecd262c..cb639fdb8 100644 --- a/app/controllers/breadcrumb_controller.rb +++ b/app/controllers/breadcrumb_controller.rb @@ -1,5 +1,5 @@ class BreadcrumbController < InheritedResources::Base - + include Pundit include BreadcrumbHelper def show diff --git a/app/controllers/companies_controller.rb b/app/controllers/companies_controller.rb index cd0467a21..78b9f43a0 100644 --- a/app/controllers/companies_controller.rb +++ b/app/controllers/companies_controller.rb @@ -1,6 +1,6 @@ class CompaniesController < BreadcrumbController include ApplicationHelper - + before_action :check_policy, :only => [:edit, :update, :destroy] defaults :resource_class => Chouette::Company respond_to :html respond_to :xml @@ -10,7 +10,6 @@ class CompaniesController < BreadcrumbController belongs_to :line_referential def index - index! do |format| format.html { if collection.out_of_bounds? @@ -21,6 +20,16 @@ class CompaniesController < BreadcrumbController end end + def new + authorize resource_class + super + end + + def create + authorize resource_class + super + end + protected def collection @@ -39,6 +48,10 @@ class CompaniesController < BreadcrumbController alias_method :line_referential, :parent + def check_policy + authorize resource + end + def company_params params.require(:company).permit( :objectid, :object_version, :creation_time, :creator_id, :name, :short_name, :organizational_unit, :operating_department_name, :code, :phone, :fax, :email, :registration_number, :url, :time_zone ) end diff --git a/app/controllers/group_of_lines_controller.rb b/app/controllers/group_of_lines_controller.rb index f8b5301d3..112ff2dd0 100644 --- a/app/controllers/group_of_lines_controller.rb +++ b/app/controllers/group_of_lines_controller.rb @@ -1,6 +1,6 @@ class GroupOfLinesController < BreadcrumbController include ApplicationHelper - + before_action :check_policy, :only => [:edit, :update, :destroy] defaults :resource_class => Chouette::GroupOfLine respond_to :html respond_to :xml @@ -29,6 +29,16 @@ class GroupOfLinesController < BreadcrumbController end end + def new + authorize resource_class + super + end + + def create + authorize resource_class + super + end + def name_filter respond_to do |format| format.json { render :json => filtered_group_of_lines_maps} @@ -65,6 +75,10 @@ class GroupOfLinesController < BreadcrumbController private + def check_policy + authorize resource + end + def group_of_line_params params.require(:group_of_line).permit( :objectid, :object_version, :creation_time, :creator_id, :name, :comment, :lines, :registration_number, :line_tokens) end diff --git a/app/controllers/line_referentials_controller.rb b/app/controllers/line_referentials_controller.rb index e28019eed..fc4ab3cf5 100644 --- a/app/controllers/line_referentials_controller.rb +++ b/app/controllers/line_referentials_controller.rb @@ -2,6 +2,16 @@ class LineReferentialsController < BreadcrumbController defaults :resource_class => LineReferential + def sync + @sync = resource.line_referential_syncs.build + if @sync.save + flash[:notice] = t('notice.line_referential_sync.created') + else + flash[:error] = @sync.errors.full_messages.to_sentence + end + redirect_to resource + end + protected def begin_of_chain diff --git a/app/controllers/lines_controller.rb b/app/controllers/lines_controller.rb index 8c14de06d..a93084012 100644 --- a/app/controllers/lines_controller.rb +++ b/app/controllers/lines_controller.rb @@ -1,6 +1,6 @@ class LinesController < BreadcrumbController include ApplicationHelper - + before_action :check_policy, :only => [:edit, :update, :destroy] defaults :resource_class => Chouette::Line respond_to :html respond_to :xml @@ -30,6 +30,16 @@ class LinesController < BreadcrumbController end end + def new + authorize resource_class + super + end + + def create + authorize resource_class + super + end + # overwrite inherited resources to use delete instead of destroy # foreign keys will propagate deletion) def destroy_resource(object) @@ -85,6 +95,9 @@ class LinesController < BreadcrumbController alias_method :line_referential, :parent private + def check_policy + authorize resource + end def line_params params.require(:line).permit( :transport_mode, :network_id, :company_id, :objectid, :object_version, :creation_time, :creator_id, :name, :number, :published_name, :transport_mode_name, :registration_number, :comment, :mobility_restricted_suitability, :int_user_needs, :flexible_service, :group_of_lines, :group_of_line_ids, :group_of_line_tokens, :url, :color, :text_color, :stable_id, { footnotes_attributes: [ :code, :label, :_destroy, :id ] } ) diff --git a/app/controllers/networks_controller.rb b/app/controllers/networks_controller.rb index beced7e55..85ec36769 100644 --- a/app/controllers/networks_controller.rb +++ b/app/controllers/networks_controller.rb @@ -1,6 +1,6 @@ class NetworksController < BreadcrumbController include ApplicationHelper - + before_action :check_policy, :only => [:edit, :update, :destroy] defaults :resource_class => Chouette::Network respond_to :html respond_to :xml @@ -17,6 +17,14 @@ class NetworksController < BreadcrumbController end end + def new + authorize resource_class + end + + def create + authorize resource_class + end + def index index! do |format| format.html { @@ -45,8 +53,11 @@ class NetworksController < BreadcrumbController alias_method :line_referential, :parent + def check_policy + authorize resource + end + def network_params params.require(:network).permit(:objectid, :object_version, :creation_time, :creator_id, :version_date, :description, :name, :registration_number, :source_name, :source_type_name, :source_identifier, :comment ) end - end diff --git a/app/controllers/stop_area_referentials_controller.rb b/app/controllers/stop_area_referentials_controller.rb index 3abd53892..5c87be43d 100644 --- a/app/controllers/stop_area_referentials_controller.rb +++ b/app/controllers/stop_area_referentials_controller.rb @@ -1,9 +1,17 @@ class StopAreaReferentialsController < BreadcrumbController defaults :resource_class => StopAreaReferential + def sync + @sync = resource.stop_area_referential_syncs.build + if @sync.save + flash[:notice] = t('notice.stop_area_referential_sync.created') + else + flash[:error] = @sync.errors.full_messages.to_sentence + end + redirect_to resource + end protected - def begin_of_chain current_organisation end diff --git a/app/controllers/stop_areas_controller.rb b/app/controllers/stop_areas_controller.rb index b37709e84..8f6a1565a 100644 --- a/app/controllers/stop_areas_controller.rb +++ b/app/controllers/stop_areas_controller.rb @@ -63,6 +63,7 @@ class StopAreasController < BreadcrumbController end def new + authorize resource_class @map = StopAreaMap.new( Chouette::StopArea.new).with_helpers(self) @map.editable = true new! do @@ -71,6 +72,7 @@ class StopAreasController < BreadcrumbController end def create + authorize resource_class @map = StopAreaMap.new( Chouette::StopArea.new).with_helpers(self) @map.editable = true @@ -92,6 +94,7 @@ class StopAreasController < BreadcrumbController end def edit + authorize stop_area edit! do stop_area.position ||= stop_area.default_position map.editable = true @@ -99,7 +102,13 @@ class StopAreasController < BreadcrumbController end end + def destroy + authorize stop_area + super + end + def update + authorize stop_area stop_area.position ||= stop_area.default_position map.editable = true diff --git a/app/controllers/offer_workbenches_controller.rb b/app/controllers/workbenches_controller.rb index b9663184b..1d67db3ab 100644 --- a/app/controllers/offer_workbenches_controller.rb +++ b/app/controllers/workbenches_controller.rb @@ -1,6 +1,6 @@ -class OfferWorkbenchesController < BreadcrumbController +class WorkbenchesController < BreadcrumbController - defaults :resource_class => OfferWorkbench + defaults :resource_class => Workbench respond_to :html, :only => [:show] def show diff --git a/app/decorators/company_decorator.rb b/app/decorators/company_decorator.rb new file mode 100644 index 000000000..3a0cc16ce --- /dev/null +++ b/app/decorators/company_decorator.rb @@ -0,0 +1,12 @@ +class CompanyDecorator < Draper::Decorator + delegate_all + + def self.collection_decorator_class + PaginatingDecorator + end + + def linecount + object.lines.count + end + +end diff --git a/app/decorators/paginating_decorator.rb b/app/decorators/paginating_decorator.rb new file mode 100644 index 000000000..00afb846c --- /dev/null +++ b/app/decorators/paginating_decorator.rb @@ -0,0 +1,11 @@ +class PaginatingDecorator < Draper::CollectionDecorator + delegate :current_page, + :per_page, + :offset, + :total_entries, + :total_pages, + :out_of_bounds?, + :limit_value, + :model_name, + :total_count +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0aa106028..132b7dcaa 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,5 +1,7 @@ module ApplicationHelper + include RefobjectsHelper + def font_awesome_classic_tag(name) name = "fa-file-text-o" if name == "fa-file-csv-o" name = "fa-file-code-o" if name == "fa-file-xml-o" diff --git a/app/helpers/breadcrumb_helper.rb b/app/helpers/breadcrumb_helper.rb index 6c29a3675..683036032 100644 --- a/app/helpers/breadcrumb_helper.rb +++ b/app/helpers/breadcrumb_helper.rb @@ -58,8 +58,8 @@ module BreadcrumbHelper organisation_breadcrumb action when "Api::V1::ApiKey" referential_breadcrumb - when "OfferWorkbench" - offer_workbench_breadcrumb action + when "Workbench" + workbench_breadcrumb action else Rails.logger.info "---------" Rails.logger.info ">>>>>>> "+resource_class.to_s+" unmapped" @@ -68,9 +68,9 @@ module BreadcrumbHelper end end - def offer_workbench_breadcrumb(action) + def workbench_breadcrumb(action) add_breadcrumb I18n.t("breadcrumbs.referentials"), referentials_path - add_breadcrumb breadcrumb_label(@offer_workbench), offer_workbench_path(@offer_workbench), :title => breadcrumb_tooltip(@offer_workbench) + add_breadcrumb breadcrumb_label(@workbench), workbench_path(@workbench), :title => breadcrumb_tooltip(@workbench) end def network_breadcrumb(action) diff --git a/app/helpers/offer_workbenches_helper.rb b/app/helpers/offer_workbenches_helper.rb deleted file mode 100644 index 1590ec132..000000000 --- a/app/helpers/offer_workbenches_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module OfferWorkbenchesHelper -end diff --git a/app/helpers/pagination_helper.rb b/app/helpers/pagination_helper.rb index c0e28ab56..e373629af 100644 --- a/app/helpers/pagination_helper.rb +++ b/app/helpers/pagination_helper.rb @@ -3,9 +3,9 @@ module PaginationHelper def paginated_content(models, default_partial_name = nil, options = {}) default_options = {:delete => true, :edit => true} options = default_options.merge(options) - - return "" if models.blank? - + + # return "" if models.blank? + html = "" models.each_slice(3) do |row_models| html += '<div class="row">' @@ -16,6 +16,6 @@ module PaginationHelper html += '</div>' end - html.html_safe + return html.html_safe unless models.blank? end end diff --git a/app/helpers/refobjects_helper.rb b/app/helpers/refobjects_helper.rb new file mode 100644 index 000000000..8dbc2bc07 --- /dev/null +++ b/app/helpers/refobjects_helper.rb @@ -0,0 +1,57 @@ +module RefobjectsHelper + + def table_builder collection, columns, actions = [], cls = nil + return unless collection.present? + + head = content_tag :thead do + content_tag :tr do + columns.each do |col| + concat content_tag(:th, collection.first.respond_to?(col) ? col.to_s.titleize : "Doesn't exist") + end + concat content_tag :th, "Actions" if actions.any? + end + end + + body = content_tag :tbody do + collection.collect { |item| + content_tag :tr do + columns.collect { |col| + concat content_tag(:td, item.try(col)) + }.to_s.html_safe + # Build links + concat content_tag :td, autolinks_builder(item, actions, :xs) if actions.any? + end + }.join().html_safe + end + + content_tag :table, head.concat(body), class: cls + end + + def autolinks_builder(item, actions, cls) + link = [] + + actions.each do |action| + if action == "show" + showlink = link_to({controller: params[:controller], action: action, id: item.id}, class: 'btn btn-default') do + content_tag :span, "", class: 'fa fa-eye' + end + link << showlink + elsif action == "edit" + editlink = link_to({controller: params[:controller], action: action, id: item.id}, class: 'btn btn-default') do + content_tag :span, "", class: 'fa fa-pencil' + end + link << editlink + elsif action == "delete" + deletelink = link_to({controller: params[:controller], action: "show", id: item.id}, method: :delete, data: { confirm: 'Are you sure?'}, class: 'btn btn-default') do + content_tag :span, "", class: 'fa fa-trash-o' + end + link << deletelink + end + end + + content_tag :div, class: "btn-group btn-group-#{cls}" do + link.join().html_safe + end + end + +end diff --git a/app/helpers/workbenches_helper.rb b/app/helpers/workbenches_helper.rb new file mode 100644 index 000000000..66e0b8e7c --- /dev/null +++ b/app/helpers/workbenches_helper.rb @@ -0,0 +1,2 @@ +module WorkbenchesHelper +end diff --git a/app/models/chouette/line.rb b/app/models/chouette/line.rb index 14c9c73ba..9f5d4ea85 100644 --- a/app/models/chouette/line.rb +++ b/app/models/chouette/line.rb @@ -21,7 +21,7 @@ class Chouette::Line < Chouette::ActiveRecord attr_reader :group_of_line_tokens attr_accessor :transport_mode - validates_presence_of :network + # validates_presence_of :network validates_presence_of :company validates_format_of :registration_number, :with => %r{\A[\d\w_\-]+\Z}, :allow_nil => true, :allow_blank => true diff --git a/app/models/chouette/stop_area.rb b/app/models/chouette/stop_area.rb index 398f37996..815304cca 100644 --- a/app/models/chouette/stop_area.rb +++ b/app/models/chouette/stop_area.rb @@ -34,8 +34,6 @@ class Chouette::StopArea < Chouette::ActiveRecord validates_format_of :registration_number, :with => %r{\A[\d\w_\-]+\Z}, :allow_blank => true validates_presence_of :name - validates_presence_of :area_type - validates_presence_of :latitude, :if => :longitude validates_presence_of :longitude, :if => :latitude validates_numericality_of :latitude, :less_than_or_equal_to => 90, :greater_than_or_equal_to => -90, :allow_nil => true diff --git a/app/models/line_referential.rb b/app/models/line_referential.rb index ccab5bf8a..e4801a61e 100644 --- a/app/models/line_referential.rb +++ b/app/models/line_referential.rb @@ -7,7 +7,7 @@ class LineReferential < ActiveRecord::Base has_many :companies, class_name: 'Chouette::Company' has_many :networks, class_name: 'Chouette::Network' - has_one :line_referential_sync + has_many :line_referential_syncs, -> { order created_at: :desc} def add_member(organisation, options = {}) attributes = options.merge organisation: organisation @@ -22,4 +22,8 @@ class LineReferential < ActiveRecord::Base def operating_lines lines.where(deactivated: false) end + + def last_sync + line_referential_syncs.last + end end diff --git a/app/models/line_referential_sync.rb b/app/models/line_referential_sync.rb index 763936f3b..a54d61edb 100644 --- a/app/models/line_referential_sync.rb +++ b/app/models/line_referential_sync.rb @@ -1,10 +1,64 @@ class LineReferentialSync < ActiveRecord::Base + include AASM belongs_to :line_referential + has_many :line_referential_sync_messages, :dependent => :destroy - has_many :line_sync_operations, dependent: :destroy + after_commit :perform_sync, :on => :create + validate :multiple_process_validation, :on => :create - def record_status status, message - line_sync_operations << LineSyncOperation.new(status: status, message: message) - line_sync_operations.first.destroy while line_sync_operations.count > 30 + private + def perform_sync + create_sync_message :info, :new + LineReferentialSyncWorker.perform_async(self.id) + end + + # There can be only one instance running + def multiple_process_validation + if self.class.where(status: [:new, :pending], line_referential_id: line_referential_id).count > 0 + errors.add(:base, :multiple_process) + end + end + + aasm column: :status do + state :new, :initial => true + state :pending + state :successful + state :failed + + event :run, after: :log_pending do + transitions :from => [:new, :failed], :to => :pending + end + + event :successful, after: :log_successful do + transitions :from => [:pending, :failed], :to => :successful + end + + event :failed, after: :log_failed do + transitions :from => :pending, :to => :failed + end + end + + def create_sync_message criticity, key, message_attributs = {} + params = { + criticity: criticity, + message_key: key, + message_attributs: message_attributs + } + line_referential_sync_messages.create params + end + + def log_pending + update_attribute(:started_at, Time.now) + create_sync_message :info, :pending + end + + def log_successful message_attributs + update_attribute(:ended_at, Time.now) + create_sync_message :info, :successful, message_attributs + end + + def log_failed message_attributs + update_attribute(:ended_at, Time.now) + create_sync_message :error, :failed, message_attributs end end diff --git a/app/models/line_referential_sync_message.rb b/app/models/line_referential_sync_message.rb new file mode 100644 index 000000000..c62b77689 --- /dev/null +++ b/app/models/line_referential_sync_message.rb @@ -0,0 +1,6 @@ +class LineReferentialSyncMessage < ActiveRecord::Base + belongs_to :line_referential_sync + enum criticity: [:info, :warn, :error] + + validates :criticity, presence: true +end diff --git a/app/models/line_sync_operation.rb b/app/models/line_sync_operation.rb deleted file mode 100644 index 2564ae99c..000000000 --- a/app/models/line_sync_operation.rb +++ /dev/null @@ -1,3 +0,0 @@ -class LineSyncOperation < ActiveRecord::Base - belongs_to :line_referential_sync -end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index fc5e2699e..3b8ad7e57 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -12,7 +12,7 @@ class Organisation < ActiveRecord::Base has_many :line_referential_memberships has_many :line_referentials, through: :line_referential_memberships - has_many :offer_workbenches + has_many :workbenches validates :name, :presence => true, :uniqueness => true diff --git a/app/models/referential.rb b/app/models/referential.rb index d8731a8d3..dedf2aabd 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -36,7 +36,7 @@ class Referential < ActiveRecord::Base # validates_presence_of :stop_area_referential has_many :stop_areas, through: :stop_area_referential - belongs_to :offer_workbench + belongs_to :workbench def slug_excluded_values if ! slug.nil? diff --git a/app/models/stop_area_referential.rb b/app/models/stop_area_referential.rb index 58e0c559c..5b84e6178 100644 --- a/app/models/stop_area_referential.rb +++ b/app/models/stop_area_referential.rb @@ -3,11 +3,14 @@ class StopAreaReferential < ActiveRecord::Base has_many :organisations, through: :stop_area_referential_memberships has_many :stop_areas, class_name: 'Chouette::StopArea' - has_one :stop_area_referential_sync + has_many :stop_area_referential_syncs, -> {order created_at: :desc} def add_member(organisation, options = {}) attributes = options.merge organisation: organisation stop_area_referential_memberships.build attributes end + def last_sync + stop_area_referential_syncs.last + end end diff --git a/app/models/stop_area_referential_sync.rb b/app/models/stop_area_referential_sync.rb index 3a9b2d03b..4de5f396a 100644 --- a/app/models/stop_area_referential_sync.rb +++ b/app/models/stop_area_referential_sync.rb @@ -1,9 +1,64 @@ class StopAreaReferentialSync < ActiveRecord::Base + include AASM belongs_to :stop_area_referential - has_many :stop_area_sync_operations, dependent: :destroy + has_many :stop_area_referential_sync_messages, :dependent => :destroy - def record_status status, message - stop_area_sync_operations << StopAreaSyncOperation.new(status: status, message: message) - stop_area_sync_operations.first.destroy while stop_area_sync_operations.count > 30 + after_commit :perform_sync, :on => :create + validate :multiple_process_validation, :on => :create + + private + def perform_sync + create_sync_message :info, :new + StopAreaReferentialSyncWorker.perform_async(self.id) + end + + # There can be only one instance running + def multiple_process_validation + if self.class.where(status: [:new, :pending], stop_area_referential_id: stop_area_referential_id).count > 0 + errors.add(:base, :multiple_process) + end + end + + aasm column: :status do + state :new, :initial => true + state :pending + state :successful + state :failed + + event :run, after: :log_pending do + transitions :from => [:new, :failed], :to => :pending + end + + event :successful, after: :log_successful do + transitions :from => [:pending, :failed], :to => :successful + end + + event :failed, after: :log_failed do + transitions :from => :pending, :to => :failed + end + end + + def create_sync_message criticity, key, message_attributs = {} + params = { + criticity: criticity, + message_key: key, + message_attributs: message_attributs + } + stop_area_referential_sync_messages.create params + end + + def log_pending + update_attribute(:started_at, Time.now) + create_sync_message :info, :pending + end + + def log_successful message_attributs + update_attribute(:ended_at, Time.now) + create_sync_message :info, :successful, message_attributs + end + + def log_failed message_attributs + update_attribute(:ended_at, Time.now) + create_sync_message :error, :failed, message_attributs end end diff --git a/app/models/stop_area_referential_sync_message.rb b/app/models/stop_area_referential_sync_message.rb new file mode 100644 index 000000000..e965297da --- /dev/null +++ b/app/models/stop_area_referential_sync_message.rb @@ -0,0 +1,6 @@ +class StopAreaReferentialSyncMessage < ActiveRecord::Base + belongs_to :stop_area_referential_sync + enum criticity: [:info, :warn, :error] + + validates :criticity, presence: true +end diff --git a/app/models/stop_area_sync_operation.rb b/app/models/stop_area_sync_operation.rb deleted file mode 100644 index ca0fde4db..000000000 --- a/app/models/stop_area_sync_operation.rb +++ /dev/null @@ -1,3 +0,0 @@ -class StopAreaSyncOperation < ActiveRecord::Base - belongs_to :stop_area_referential_sync -end diff --git a/app/models/user.rb b/app/models/user.rb index fa4c77d96..31fda6aed 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -29,9 +29,10 @@ class User < ActiveRecord::Base after_destroy :check_destroy_organisation def cas_extra_attributes=(extra_attributes) - extra = extra_attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} - self.name = extra[:full_name] - self.email = extra[:email] + extra = extra_attributes.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo} + self.name = extra[:full_name] + self.email = extra[:email] + self.username = extra[:username] self.organisation = Organisation.find_or_create_by(code: extra[:organisation_code]).tap do |org| org.name = extra[:organisation_name] diff --git a/app/models/offer_workbench.rb b/app/models/workbench.rb index 65feafdd5..e4efb6bd9 100644 --- a/app/models/offer_workbench.rb +++ b/app/models/workbench.rb @@ -1,4 +1,4 @@ -class OfferWorkbench < ActiveRecord::Base +class Workbench < ActiveRecord::Base belongs_to :organisation validates :name, presence: true diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb new file mode 100644 index 000000000..2a0bbc521 --- /dev/null +++ b/app/policies/application_policy.rb @@ -0,0 +1,53 @@ +class ApplicationPolicy + attr_reader :user, :record + + def initialize(user, record) + @user = user + @record = record + end + + def index? + false + end + + def show? + scope.where(:id => record.id).exists? + end + + def create? + false + end + + def new? + create? + end + + def update? + false + end + + def edit? + update? + end + + def destroy? + false + end + + def scope + Pundit.policy_scope!(user, record.class) + end + + class Scope + attr_reader :user, :scope + + def initialize(user, scope) + @user = user + @scope = scope + end + + def resolve + scope + end + end +end diff --git a/app/policies/company_policy.rb b/app/policies/company_policy.rb new file mode 100644 index 000000000..d28e9b515 --- /dev/null +++ b/app/policies/company_policy.rb @@ -0,0 +1,15 @@ +class CompanyPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope + end + end + + def create? + false + end + def update? ; create? end + def new? ; create? end + def edit? ; create? end + def destroy? ; create? end +end diff --git a/app/policies/group_of_line_policy.rb b/app/policies/group_of_line_policy.rb new file mode 100644 index 000000000..5d42a23bd --- /dev/null +++ b/app/policies/group_of_line_policy.rb @@ -0,0 +1,15 @@ +class GroupOfLinePolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope + end + end + + def create? + false + end + def update? ; create? end + def new? ; create? end + def edit? ; create? end + def destroy? ; create? end +end diff --git a/app/policies/line_policy.rb b/app/policies/line_policy.rb new file mode 100644 index 000000000..61cf6c1b8 --- /dev/null +++ b/app/policies/line_policy.rb @@ -0,0 +1,15 @@ +class LinePolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope + end + end + + def create? + false + end + def update? ; create? end + def new? ; create? end + def edit? ; create? end + def destroy? ; create? end +end diff --git a/app/policies/network_policy.rb b/app/policies/network_policy.rb new file mode 100644 index 000000000..427eace93 --- /dev/null +++ b/app/policies/network_policy.rb @@ -0,0 +1,15 @@ +class NetworkPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope + end + end + + def create? + false + end + def update? ; create? end + def new? ; create? end + def edit? ; create? end + def destroy? ; create? end +end diff --git a/app/policies/stop_area_policy.rb b/app/policies/stop_area_policy.rb new file mode 100644 index 000000000..4fa426ff6 --- /dev/null +++ b/app/policies/stop_area_policy.rb @@ -0,0 +1,15 @@ +class StopAreaPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope + end + end + + def create? + false + end + def update? ; create? end + def new? ; create? end + def edit? ; create? end + def destroy? ; create? end +end diff --git a/app/views/companies/_companies.html.slim b/app/views/companies/_companies.html.slim index bcd471cc7..187ff4d0d 100644 --- a/app/views/companies/_companies.html.slim +++ b/app/views/companies/_companies.html.slim @@ -1,9 +1,12 @@ .page_info span.search = t('will_paginate.page_entries_info.search') - = page_entries_info(@companies) + = page_entries_info @companies -.companies.paginated_content - = paginated_content(@companies) +.companies.paginated_content style="margin-top:20px;" + = table_builder CompanyDecorator.decorate_collection(@companies), + [:name, :edited_at, :published_at, :validity_period, :linecount, :transporter, :status], + ["show"], + 'table table-bordered' .pagination - = will_paginate @companies, :container => false, renderer: RemoteBootstrapPaginationLinkRenderer
\ No newline at end of file + = will_paginate @companies, container: false, renderer: RemoteBootstrapPaginationLinkRenderer diff --git a/app/views/companies/index.html.slim b/app/views/companies/index.html.slim index bf295dee7..1b616a6c8 100644 --- a/app/views/companies/index.html.slim +++ b/app/views/companies/index.html.slim @@ -20,6 +20,7 @@ - content_for :sidebar do ul.actions - li - = link_to t('companies.actions.new'), new_line_referential_company_path(@line_referential), class: 'add' - br
\ No newline at end of file + - if policy(Chouette::Company).create? + li + = link_to t('companies.actions.new'), new_line_referential_company_path(@line_referential), class: 'add' + br diff --git a/app/views/companies/show.html.slim b/app/views/companies/show.html.slim index 693cea352..1c244a53a 100644 --- a/app/views/companies/show.html.slim +++ b/app/views/companies/show.html.slim @@ -44,12 +44,15 @@ - content_for :sidebar do ul.actions - li - = link_to t('companies.actions.new'), new_line_referential_company_path(@line_referential), class: 'add' - li - = link_to t('companies.actions.edit'), edit_line_referential_company_path(@line_referential, @company), class: 'edit' - li - = link_to t('companies.actions.destroy'), line_referential_company_path(@line_referential, @company), :method => :delete, :data => {:confirm => t('companies.actions.destroy_confirm')}, class: 'remove' - br - - = creation_tag(@company)
\ No newline at end of file + - if policy(Chouette::Company).create? + li + = link_to t('companies.actions.new'), new_line_referential_company_path(@line_referential), class: 'add' + - if policy(@company).update? + li + = link_to t('companies.actions.edit'), edit_line_referential_company_path(@line_referential, @company), class: 'edit' + - if policy(@company).destroy? + li + = link_to t('companies.actions.destroy'), line_referential_company_path(@line_referential, @company), :method => :delete, :data => {:confirm => t('companies.actions.destroy_confirm')}, class: 'remove' + br + + = creation_tag(@company) diff --git a/app/views/group_of_lines/_group_of_line.html.slim b/app/views/group_of_lines/_group_of_line.html.slim index 16ac4e252..fb9f95894 100644 --- a/app/views/group_of_lines/_group_of_line.html.slim +++ b/app/views/group_of_lines/_group_of_line.html.slim @@ -2,11 +2,11 @@ .panel-heading .panel-title.clearfix span.pull-right - - if edit + - if edit && policy(group_of_line).update? = link_to edit_line_referential_group_of_line_path(@line_referential, group_of_line), class: 'btn btn-default btn-sm' do span.fa.fa-pencil - - = link_to('<span class="fa fa-trash-o"></span>'.html_safe, line_referential_group_of_line_path(@line_referential, group_of_line), :method => :delete, :data => {:confirm => t('group_of_lines.actions.destroy_confirm')}, class: 'btn btn-danger btn-sm') if delete + - if delete && policy(group_of_line).destroy? + = link_to('<span class="fa fa-trash-o"></span>'.html_safe, line_referential_group_of_line_path(@line_referential, group_of_line), :method => :delete, :data => {:confirm => t('group_of_lines.actions.destroy_confirm')}, class: 'btn btn-danger btn-sm') h5 = link_to [@line_referential, group_of_line], :class => "preview", :title => "#{Chouette::GroupOfLine.model_name.human.capitalize} #{group_of_line.name}" do @@ -16,4 +16,4 @@ .panel-body p = group_of_line.human_attribute_name('line_count') - = group_of_line.lines.count
\ No newline at end of file + = group_of_line.lines.count diff --git a/app/views/group_of_lines/index.html.slim b/app/views/group_of_lines/index.html.slim index 8cd39407f..a684095f0 100644 --- a/app/views/group_of_lines/index.html.slim +++ b/app/views/group_of_lines/index.html.slim @@ -19,6 +19,7 @@ - content_for :sidebar do ul.actions - li - = link_to t('group_of_lines.actions.new'), new_line_referential_group_of_line_path(@line_referential), class: 'add' - br
\ No newline at end of file + - if policy(Chouette::GroupOfLine).create? + li + = link_to t('group_of_lines.actions.new'), new_line_referential_group_of_line_path(@line_referential), class: 'add' + br diff --git a/app/views/group_of_lines/show.html.slim b/app/views/group_of_lines/show.html.slim index c93f5277e..b934cb1ef 100644 --- a/app/views/group_of_lines/show.html.slim +++ b/app/views/group_of_lines/show.html.slim @@ -2,7 +2,7 @@ .group_of_line_show = @map.to_html - + .summary p label = "#{@group_of_line.human_attribute_name('registration_number')} : " @@ -13,19 +13,22 @@ = @group_of_line.comment p.after_map - + h3.group_of_line_lines = t('.lines') .lines_detail - == render partial: "lines_detail" + == render partial: "lines_detail" - content_for :sidebar do ul.actions - li - = link_to t('group_of_lines.actions.new'), new_line_referential_group_of_line_path(@line_referential), class: 'add' - li - = link_to t('group_of_lines.actions.edit'), edit_line_referential_group_of_line_path(@line_referential, @group_of_line), class: 'edit' - li - = link_to t('group_of_lines.actions.destroy'), line_referential_group_of_line_path(@line_referential, @group_of_line), :method => :delete, :data => {:confirm => t('group_of_lines.actions.destroy_confirm')} , class: 'remove' + - if policy(Chouette::GroupOfLine).create? + li + = link_to t('group_of_lines.actions.new'), new_line_referential_group_of_line_path(@line_referential), class: 'add' + - if policy(@group_of_line).edit? + li + = link_to t('group_of_lines.actions.edit'), edit_line_referential_group_of_line_path(@line_referential, @group_of_line), class: 'edit' + - if policy(@group_of_line).destroy? + li + = link_to t('group_of_lines.actions.destroy'), line_referential_group_of_line_path(@line_referential, @group_of_line), :method => :delete, :data => {:confirm => t('group_of_lines.actions.destroy_confirm')} , class: 'remove' br - = creation_tag(@group_of_line)
\ No newline at end of file + = creation_tag(@group_of_line) diff --git a/app/views/line_referentials/show.html.slim b/app/views/line_referentials/show.html.slim index 929eda7dd..408a07c2d 100644 --- a/app/views/line_referentials/show.html.slim +++ b/app/views/line_referentials/show.html.slim @@ -13,21 +13,31 @@ span.badge = @line_referential.networks.size = link_to Referential.human_attribute_name("networks"), line_referential_networks_path(@line_referential) - li.list-group-item - span.badge = @line_referential.group_of_lines.size - = link_to Referential.human_attribute_name("group_of_lines"), line_referential_group_of_lines_path(@line_referential) + / li.list-group-item + / span.badge = @line_referential.group_of_lines.size + / = link_to Referential.human_attribute_name("group_of_lines"), line_referential_group_of_lines_path(@line_referential) li.list-group-item span.badge = @line_referential.lines.size = link_to Referential.human_attribute_name("lines"), line_referential_lines_path(@line_referential) -- unless @line_referential.line_referential_sync.line_sync_operations.empty? +- unless @line_referential.line_referential_syncs.empty? h3 Historique des synchronisations ul.list-group width="75%" - - @line_referential.line_referential_sync.line_sync_operations.each do |sync| - li = "#{sync.created_at.to_formatted_s(:short)} - #{sync.message}" + - @line_referential.line_referential_syncs.each do |sync| + - unless sync.line_referential_sync_messages.empty? + - sync.line_referential_sync_messages.last.tap do |log| + - if log.criticity = log.criticity + li.alert class="alert-#{log.criticity}" + strong = l(log.created_at, format: :short) + " : " + / [:processing_time] unit conversion + - data = log.message_attributs.symbolize_keys! + - data[:processing_time] = distance_of_time_in_words(data[:processing_time].to_i) + + = t("line_referential_sync.message.#{log.message_key}", log.message_attributs.symbolize_keys!) - content_for :sidebar do ul.actions - = link_to t('line_referentials.actions.edit'), edit_line_referential_path(@line_referential), class: 'edit'
\ No newline at end of file + li = link_to t('line_referentials.actions.edit'), edit_line_referential_path(@line_referential), class: 'edit' + li = link_to t('line_referentials.actions.sync'), sync_line_referential_path(@line_referential), class: 'sync', method: :post diff --git a/app/views/lines/_line.html.slim b/app/views/lines/_line.html.slim index 552e9d29b..07275e043 100644 --- a/app/views/lines/_line.html.slim +++ b/app/views/lines/_line.html.slim @@ -14,11 +14,11 @@ li .btn-group - - if edit + - if edit && policy(Chouette::Line).update? = link_to edit_line_referential_line_path(line.line_referential, line), class: 'btn btn-default btn-sm' do span.fa.fa-pencil - - if delete + - if delete && policy(Chouette::Line).destroy? = link_to line_referential_line_path(line.line_referential, line), method: :delete, data: { confirm: t('lines.actions.destroy_confirm') }, class: 'btn btn-danger btn-sm' do span.fa.fa-trash-o @@ -50,4 +50,4 @@ = line.human_attribute_name('group_of_line') = link_to_if( line.group_of_lines.first, line.group_of_lines.first.name, line_referential_group_of_line_path(line.line_referential, line.group_of_lines.first), :title => "#{line.human_attribute_name('group_of_line')} #{line.group_of_lines.first.name}") - else - = t('lines.form.several_group_of_lines', :count => line.group_of_lines.count)
\ No newline at end of file + = t('lines.form.several_group_of_lines', :count => line.group_of_lines.count) diff --git a/app/views/lines/show.html.slim b/app/views/lines/show.html.slim index 590f35a27..cca0e395c 100644 --- a/app/views/lines/show.html.slim +++ b/app/views/lines/show.html.slim @@ -131,16 +131,19 @@ h3.routes = t('.itineraries') - content_for :sidebar do ul.actions - li - = link_to t('lines.actions.new'), new_line_referential_line_path(@line_referential), class: 'add' - li - = link_to t('lines.actions.edit'), edit_line_referential_line_path(@line_referential, @line), class: 'edit' - li - = link_to t('lines.actions.destroy'), line_referential_line_path(@line_referential, @line), method: :delete, :data => {:confirm => t('lines.actions.destroy_confirm')}, class: 'remove' + - if policy(Chouette::Line).create? + li + = link_to t('lines.actions.new'), new_line_referential_line_path(@line_referential), class: 'add' + - if policy(@line).update? + li + = link_to t('lines.actions.edit'), edit_line_referential_line_path(@line_referential, @line), class: 'edit' + - if policy(@line).destroy? + li + = link_to t('lines.actions.destroy'), line_referential_line_path(@line_referential, @line), method: :delete, :data => {:confirm => t('lines.actions.destroy_confirm')}, class: 'remove' - if !@line.hub_restricted? || (@line.hub_restricted? && @line.routes.size < 2) / FIXME #825 li / = link_to t('routes.actions.new'), new_referential_line_route_path(@referential, @line), class: 'add' - = creation_tag(@line)
\ No newline at end of file + = creation_tag(@line) diff --git a/app/views/networks/_network.html.slim b/app/views/networks/_network.html.slim index ed216c4c7..94745ca3d 100644 --- a/app/views/networks/_network.html.slim +++ b/app/views/networks/_network.html.slim @@ -2,13 +2,14 @@ .panel-heading .panel-title.clearfix span.pull-right - = link_to edit_line_referential_network_path(@line_referential, network), class: 'btn btn-default btn-sm' do - span.fa.fa-pencil - - = link_to line_referential_network_path(@line_referential, network), method: :delete, :data => { :confirm => t('networks.actions.destroy_confirm') }, class: 'btn btn-danger btn-sm' do - span.fa.fa-trash-o + - if policy(network).update? + = link_to edit_line_referential_network_path(@line_referential, network), class: 'btn btn-default btn-sm' do + span.fa.fa-pencil + - if policy(network).destroy? + = link_to line_referential_network_path(@line_referential, network), method: :delete, :data => { :confirm => t('networks.actions.destroy_confirm') }, class: 'btn btn-danger btn-sm' do + span.fa.fa-trash-o h5 = link_to [@line_referential, network], class: 'preview', title: "#{Chouette::Network.model_name.human.capitalize} #{network.name}" do span.name - = truncate(network.name, :length => 20)
\ No newline at end of file + = truncate(network.name, :length => 20) diff --git a/app/views/networks/index.html.slim b/app/views/networks/index.html.slim index bc32d273c..587552729 100644 --- a/app/views/networks/index.html.slim +++ b/app/views/networks/index.html.slim @@ -18,6 +18,7 @@ - content_for :sidebar do ul.actions - li - = link_to t('networks.actions.new'), new_line_referential_network_path(@line_referential), class: 'add' - br
\ No newline at end of file + - if policy(Chouette::Network).create? + li + = link_to t('networks.actions.new'), new_line_referential_network_path(@line_referential), class: 'add' + br diff --git a/app/views/networks/show.html.slim b/app/views/networks/show.html.slim index e6bf4e399..5d5b1cd74 100644 --- a/app/views/networks/show.html.slim +++ b/app/views/networks/show.html.slim @@ -36,12 +36,15 @@ - content_for :sidebar do ul.actions - li - = link_to t('networks.actions.new'), new_line_referential_network_path(@line_referential), class: 'add' - li - = link_to t('networks.actions.edit'), edit_line_referential_network_path(@line_referential, @network), class: 'edit' - li - = link_to t('networks.actions.destroy'), line_referential_network_path(@line_referential, @network), method: :delete, data: { :confirm => t('networks.actions.destroy_confirm')}, class: 'remove' + - if policy(Chouette::Network).create? + li + = link_to t('networks.actions.new'), new_line_referential_network_path(@line_referential), class: 'add' + - if policy(@network).update? + li + = link_to t('networks.actions.edit'), edit_line_referential_network_path(@line_referential, @network), class: 'edit' + - if policy(@network).destroy? + li + = link_to t('networks.actions.destroy'), line_referential_network_path(@line_referential, @network), method: :delete, data: { :confirm => t('networks.actions.destroy_confirm')}, class: 'remove' br - = creation_tag(@network)
\ No newline at end of file + = creation_tag(@network) diff --git a/app/views/offer_workbenches/show.html.slim b/app/views/offer_workbenches/show.html.slim index bed9205ba..49396a0e2 100644 --- a/app/views/offer_workbenches/show.html.slim +++ b/app/views/offer_workbenches/show.html.slim @@ -1,5 +1,5 @@ -= title_tag "#{@offer_workbench.name} - Tableau de bord" += title_tag "#{@workbench.name} - Tableau de bord" .referentials.paginated_content h4 Liste des jeux de données - = paginated_content @offer_workbench.referentials
\ No newline at end of file + = paginated_content @workbench.referentials diff --git a/app/views/referential_companies/index.js.slim b/app/views/referential_companies/index.js.slim index cfb1c719c..3a1739abf 100644 --- a/app/views/referential_companies/index.js.slim +++ b/app/views/referential_companies/index.js.slim @@ -1 +1 @@ -| $('#companies').html("= escape_javascript(render('companies'))");
\ No newline at end of file +/ | $('#companies').html("= escape_javascript(render('companies'))"); diff --git a/app/views/referentials/index.html.slim b/app/views/referentials/index.html.slim index d9c5376a1..e37f157de 100644 --- a/app/views/referentials/index.html.slim +++ b/app/views/referentials/index.html.slim @@ -1,5 +1,5 @@ / FIXME #827 -- current_organisation.offer_workbenches.each do |workbench| +- current_organisation.workbenches.each do |workbench| h2 = link_to workbench.name, workbench p = "#{workbench.referentials.count} jeu(x) de données à l'heure actuelle" @@ -22,4 +22,4 @@ / FIXME #824 / Don't blame me. See #824 - li = link_to 'Données CodifLigne', line_referential_path(1)
\ No newline at end of file + li = link_to 'Données CodifLigne', line_referential_path(1) diff --git a/app/views/stop_area_referentials/show.html.slim b/app/views/stop_area_referentials/show.html.slim index 32fe950dc..9cceed5e5 100644 --- a/app/views/stop_area_referentials/show.html.slim +++ b/app/views/stop_area_referentials/show.html.slim @@ -9,12 +9,22 @@ span.badge = @stop_area_referential.stop_areas.size = link_to Referential.human_attribute_name("stop_areas"), stop_area_referential_stop_areas_path(@stop_area_referential) -- unless @stop_area_referential.stop_area_referential_sync.stop_area_sync_operations.empty? +- unless @stop_area_referential.stop_area_referential_syncs.empty? h3 Historique des synchronisations ul.list-group width="75%" - - @stop_area_referential.stop_area_referential_sync.stop_area_sync_operations.each do |sync| - li = "#{sync.created_at.to_formatted_s(:short)} - #{sync.message}" + - @stop_area_referential.stop_area_referential_syncs.each do |sync| + - unless sync.stop_area_referential_sync_messages.empty? + - sync.stop_area_referential_sync_messages.last.tap do |log| + - if log.criticity = log.criticity + li.alert class="alert-#{log.criticity}" + strong = l(log.created_at, format: :short) + " : " + / [:processing_time] unit conversion + - data = log.message_attributs.symbolize_keys! + - data[:processing_time] = distance_of_time_in_words(data[:processing_time].to_i) + + = t("stop_area_referential_sync.message.#{log.message_key}", log.message_attributs.symbolize_keys!) - content_for :sidebar do - ul.actions
\ No newline at end of file + ul.actions + li = link_to t('stop_area_referentials.actions.sync'), sync_stop_area_referential_path(@stop_area_referential), class: 'sync', method: :post diff --git a/app/views/stop_areas/_stop_area.html.slim b/app/views/stop_areas/_stop_area.html.slim index 15f9e4452..39cb09660 100644 --- a/app/views/stop_areas/_stop_area.html.slim +++ b/app/views/stop_areas/_stop_area.html.slim @@ -2,11 +2,13 @@ .panel-heading .panel-title.clearfix span.pull-right - = link_to edit_stop_area_referential_stop_area_path(@stop_area_referential, stop_area), class: 'btn btn-default btn-sm' do - span.fa.fa-pencil + - if policy(stop_area).update? + = link_to edit_stop_area_referential_stop_area_path(@stop_area_referential, stop_area), class: 'btn btn-default btn-sm' do + span.fa.fa-pencil - = link_to stop_area_referential_stop_area_path(@stop_area_referential, stop_area), method: :delete, :data => { :confirm => t('stop_areas.actions.destroy_confirm') }, class: 'btn btn-danger btn-sm' do - span.fa.fa-trash-o + - if policy(stop_area).destroy? + = link_to stop_area_referential_stop_area_path(@stop_area_referential, stop_area), method: :delete, :data => { :confirm => t('stop_areas.actions.destroy_confirm') }, class: 'btn btn-danger btn-sm' do + span.fa.fa-trash-o h5 = link_to([@stop_area_referential, stop_area], class: 'preview', :title => t("area_types.label.#{stop_area.stop_area_type}") + " #{stop_area.name}") do diff --git a/app/views/stop_areas/index.html.slim b/app/views/stop_areas/index.html.slim index 6a01bdcb3..b6328c6f9 100644 --- a/app/views/stop_areas/index.html.slim +++ b/app/views/stop_areas/index.html.slim @@ -8,7 +8,7 @@ .panel-heading .input-group.col-md-9 = f.text_field :name_cont, placeholder: "#{t('.name')}", class: 'form-control' - + .input-group-btn button.btn.btn-default type="submit" i.fa.fa-search @@ -27,6 +27,7 @@ - content_for :sidebar do ul.actions - li = link_to t('stop_areas.actions.new'), new_stop_area_referential_stop_area_path(@stop_area_referential), class: 'add' - li - / = link_to t('stop_areas.actions.default_geometry'), default_geometry_referential_stop_areas_path(@stop_area_referential), :method => :put, :class => "calculator"
\ No newline at end of file + - if policy(Chouette::StopArea).create? + li = link_to t('stop_areas.actions.new'), new_stop_area_referential_stop_area_path(@stop_area_referential), class: 'add' + + / = link_to t('stop_areas.actions.default_geometry'), default_geometry_referential_stop_areas_path(@stop_area_referential), :method => :put, :class => "calculator" diff --git a/app/views/stop_areas/show.html.slim b/app/views/stop_areas/show.html.slim index c9d0b67a3..50e535cb5 100644 --- a/app/views/stop_areas/show.html.slim +++ b/app/views/stop_areas/show.html.slim @@ -123,9 +123,12 @@ p.after_map tr td ul.actions - li = link_to t('stop_areas.actions.new'), new_stop_area_referential_stop_area_path(@stop_area_referential), class: 'add' - li = link_to t('stop_areas.actions.edit'), edit_stop_area_referential_stop_area_path(@stop_area_referential, @stop_area), class: 'edit' - li = link_to t('stop_areas.actions.destroy'), stop_area_referential_stop_area_path(@stop_area_referential, @stop_area), method: :delete, :data => {:confirm => t('stop_areas.actions.destroy_confirm')}, class: 'remove' + - if policy(Chouette::StopArea).new? + li = link_to t('stop_areas.actions.new'), new_stop_area_referential_stop_area_path(@stop_area_referential), class: 'add' + - if policy(@stop_area).update? + li = link_to t('stop_areas.actions.edit'), edit_stop_area_referential_stop_area_path(@stop_area_referential, @stop_area), class: 'edit' + - if policy(@stop_area).destroy? + li = link_to t('stop_areas.actions.destroy'), stop_area_referential_stop_area_path(@stop_area_referential, @stop_area), method: :delete, :data => {:confirm => t('stop_areas.actions.destroy_confirm')}, class: 'remove' - if manage_itl #Fixme diff --git a/app/views/users/show.html.slim b/app/views/users/show.html.slim index f884ae94e..858aa3395 100644 --- a/app/views/users/show.html.slim +++ b/app/views/users/show.html.slim @@ -4,7 +4,9 @@ p label = "#{User.human_attribute_name('name')} : " = @user.name - + p + label = "#{User.human_attribute_name('username')} : " + = @user.username p label = "#{User.human_attribute_name('email')} : " = @user.email @@ -16,4 +18,4 @@ li = link_to( t('users.actions.edit'), edit_user_registration_path, class: "edit") if @user == current_user li - = link_to t('users.actions.destroy'), organisation_user_path(@user), method: :delete, data: {:confirm => t('users.actions.destroy_confirm')}, class: "remove"
\ No newline at end of file + = link_to t('users.actions.destroy'), organisation_user_path(@user), method: :delete, data: {:confirm => t('users.actions.destroy_confirm')}, class: "remove" diff --git a/app/workers/line_referential_sync_worker.rb b/app/workers/line_referential_sync_worker.rb new file mode 100644 index 000000000..b883e5180 --- /dev/null +++ b/app/workers/line_referential_sync_worker.rb @@ -0,0 +1,21 @@ +class LineReferentialSyncWorker + include Sidekiq::Worker + def process_time + Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) + end + + def perform(lref_sync_id) + start_time = process_time + lref_sync = LineReferentialSync.find lref_sync_id + lref_sync.run if lref_sync.may_run? + begin + info = Stif::CodifLineSynchronization.synchronize + lref_sync.successful info.merge({processing_time: process_time - start_time}) + rescue Exception => e + lref_sync.failed({ + error: e.message, + processing_time: process_time - start_time + }) + end + end +end diff --git a/app/workers/stop_area_referential_sync_worker.rb b/app/workers/stop_area_referential_sync_worker.rb new file mode 100644 index 000000000..f2c6746da --- /dev/null +++ b/app/workers/stop_area_referential_sync_worker.rb @@ -0,0 +1,22 @@ +class StopAreaReferentialSyncWorker + include Sidekiq::Worker + + def process_time + Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) + end + + def perform(stop_area_ref_sync_id) + start_time = process_time + stop_ref_sync = StopAreaReferentialSync.find stop_area_ref_sync_id + stop_ref_sync.run if stop_ref_sync.may_run? + begin + info = Stif::ReflexSynchronization.synchronize + stop_ref_sync.successful info.merge({processing_time: process_time - start_time}) + rescue Exception => e + stop_ref_sync.failed({ + error: e.message, + processing_time: process_time - start_time + }) + end + end +end diff --git a/config/environments/development.rb b/config/environments/development.rb index 47e365098..2931b4bf7 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -58,6 +58,7 @@ Rails.application.configure do # Reflex api url config.reflex_api_url = "https://195.46.215.128/ws/reflex/V1/service=getData" + config.codifligne_api_url = "https://pprod.codifligne.stif.info/rest/v1/lc/getlist" # config.chouette_authentication_settings = { # type: "database" diff --git a/config/environments/test.rb b/config/environments/test.rb index 512afab4f..b77a26273 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -60,6 +60,9 @@ Rails.application.configure do url: "http://localhost:3000" } + # Reflex api url + config.reflex_api_url = "https://195.46.215.128/ws/reflex/V1/service=getData" + # file to data for demo config.demo_data = "tmp/demo.zip" diff --git a/config/initializers/apartment.rb b/config/initializers/apartment.rb index 93ce2d8c6..db352fa6f 100644 --- a/config/initializers/apartment.rb +++ b/config/initializers/apartment.rb @@ -25,8 +25,12 @@ Apartment.configure do |config| "Api::V1::ApiKey", "RuleParameterSet", "StopAreaReferential", + "StopAreaReferentialSync", + "StopAreaReferentialSyncMessage", "Chouette::StopArea", "LineReferential", + "LineReferentialSync", + "LineReferentialSyncMessage", "Chouette::Line", "Chouette::GroupOfLine", "Chouette::Company", diff --git a/config/initializers/codifligne.rb b/config/initializers/codifligne.rb new file mode 100644 index 000000000..321419e02 --- /dev/null +++ b/config/initializers/codifligne.rb @@ -0,0 +1,3 @@ +if Rails.application.config.try(:codifligne_api_url) + Codifligne::API.base_url = Rails.application.config.codifligne_api_url +end diff --git a/config/locales/line_referential_syncs.en.yml b/config/locales/line_referential_syncs.en.yml new file mode 100644 index 000000000..82ef48170 --- /dev/null +++ b/config/locales/line_referential_syncs.en.yml @@ -0,0 +1,19 @@ +en: + activerecord: + errors: + models: + line_referential_sync: + attributes: + base: + multiple_process: 'There is already an synchronisation in progress' + line_referential_sync: + message: + new: "New synchronisation added" + pending: "Synchronisation en cours" + successful: "Synchronisation réussie après %{processing_time} secondes avec %{imported} éléments importés. %{deleted} éléments ont été supprimés." + failed: "Synchronisation interrompue après %{processing_time} secondes avec l'erreur : %{error}." + + notice: + line_referential_sync: + created: 'Your synchronisation request has been created' + diff --git a/config/locales/line_referential_syncs.fr.yml b/config/locales/line_referential_syncs.fr.yml new file mode 100644 index 000000000..48b5df190 --- /dev/null +++ b/config/locales/line_referential_syncs.fr.yml @@ -0,0 +1,17 @@ +fr: + activerecord: + errors: + models: + line_referential_sync: + attributes: + base: + multiple_process: 'Il y a déja une synchronisation en cours de traitement' + line_referential_sync: + message: + new: "Synchronisation en attente" + pending: "Synchronisation en cours" + successful: "Synchronisation réussie après %{processing_time}, avec %{imported} éléments importés. %{deleted} éléments ont été supprimés." + failed: "Synchronisation interrompue après %{processing_time}, avec l'erreur : %{error}." + notice: + line_referential_sync: + created: 'Votre demande de synchronisation a bien été créée' diff --git a/config/locales/line_referentials.en.yml b/config/locales/line_referentials.en.yml index d7cd6c519..4c30d90e3 100644 --- a/config/locales/line_referentials.en.yml +++ b/config/locales/line_referentials.en.yml @@ -2,15 +2,14 @@ en: line_referentials: actions: edit: "Edit this referential" + sync: "Launch a new codifligne synchronization" + cancel_sync: "Cancel codifligne synchronization" edit: title: "Edit %{line_referential} referential" - synchronization: - message: "Synchronization successful in %{time} seconds with %{imported} objects from Codifligne. %{deleted} objects were deleted." - failure: "Synchronization interrupted after %{time} seconds." activerecord: models: line_referential: one: "referential" attributes: line_referential: - sync_interval: "Synchronisation frequency"
\ No newline at end of file + sync_interval: "Synchronisation frequency" diff --git a/config/locales/line_referentials.fr.yml b/config/locales/line_referentials.fr.yml index adb96cbe0..5a9e2caca 100644 --- a/config/locales/line_referentials.fr.yml +++ b/config/locales/line_referentials.fr.yml @@ -2,13 +2,10 @@ fr: line_referentials: actions: edit: "Modifier ce référentiel" + sync: "Lancer une synchronisation Codifligne" + cancel_sync: "Annuler la synchronisation Codifligne" edit: title: "Modifier le référentiel %{line_referential}" - synchronization: - codifligne: - message: - success: "Synchronisation réussie après %{time} secondes avec %{imported} éléments importés de Codifligne. %{deleted} éléments ont été supprimés." - failure: "Synchronisation interrompue après %{time} secondes." activerecord: models: line_referential: diff --git a/config/locales/stop_area_referential_syncs.en.yml b/config/locales/stop_area_referential_syncs.en.yml new file mode 100644 index 000000000..9dc3173bc --- /dev/null +++ b/config/locales/stop_area_referential_syncs.en.yml @@ -0,0 +1,19 @@ +en: + activerecord: + errors: + models: + stop_area_referential_sync: + attributes: + base: + multiple_process: 'There is already an synchronisation in progress' + stop_area_referential_sync: + synchronization: + message: + new: "New synchronisation added" + pending: "Synchronization pending" + successful: "Synchronization successful after %{processing_time} with %{imported} objects. %{deleted} objects were deleted.." + failed: "Synchronization failed after %{processing_time} with error: %{error}." + notice: + stop_area_referential_sync: + created: 'Your synchronisation request has been created' + diff --git a/config/locales/stop_area_referential_syncs.fr.yml b/config/locales/stop_area_referential_syncs.fr.yml new file mode 100644 index 000000000..d0b79a8db --- /dev/null +++ b/config/locales/stop_area_referential_syncs.fr.yml @@ -0,0 +1,17 @@ +fr: + activerecord: + errors: + models: + stop_area_referential_sync: + attributes: + base: + multiple_process: 'Il y a déja une synchronisation en cours de traitement' + stop_area_referential_sync: + message: + new: "Synchronisation en attente" + pending: "Synchronisation en cours" + successful: "Synchronisation réussie après %{processing_time}, avec %{imported} éléments importés. %{deleted} éléments ont été supprimés." + failed: "Synchronisation interrompue après %{processing_time}, avec l'erreur : %{error}." + notice: + stop_area_referential_sync: + created: 'Votre demande de synchronisation a bien été créée' diff --git a/config/locales/stop_area_referentials.en.yml b/config/locales/stop_area_referentials.en.yml index 9ac0daede..057b32c7c 100644 --- a/config/locales/stop_area_referentials.en.yml +++ b/config/locales/stop_area_referentials.en.yml @@ -1,5 +1,5 @@ en: - synchronization: - reflex: - message: "Synchronization successful in %{time} seconds with %{imported} objects from Reflex. %{deleted} objects were deleted." - failure: "Synchronization interrupted after %{time} seconds." + stop_area_referentials: + actions: + sync: "Launch a new reflex synchronization" + cancel_sync: "Cancel reflex synchronization" diff --git a/config/locales/stop_area_referentials.fr.yml b/config/locales/stop_area_referentials.fr.yml index 89254384f..c825acd00 100644 --- a/config/locales/stop_area_referentials.fr.yml +++ b/config/locales/stop_area_referentials.fr.yml @@ -1,6 +1,5 @@ fr: - synchronization: - reflex: - message: - success: "Synchronisation réussie après %{time} secondes avec %{imported} éléments importés de Reflex. %{deleted} éléments ont été supprimés." - failure: "Synchronisation interrompue après %{time} secondes." + stop_area_referentials: + actions: + sync: "Lancer une synchronisation Reflex" + cancel_sync: "Annuler la synchronisation Reflex" diff --git a/config/locales/users.en.yml b/config/locales/users.en.yml index 17c43f4b2..19168f560 100644 --- a/config/locales/users.en.yml +++ b/config/locales/users.en.yml @@ -13,9 +13,10 @@ en: title: "User %{user}" index: title: "Users" - activerecord: - models: + activerecord: + models: user: "user" attributes: user: - name: "Full name" + name: "Full name" + username: "Username" diff --git a/config/locales/users.fr.yml b/config/locales/users.fr.yml index 55d6382ef..dcb81b808 100644 --- a/config/locales/users.fr.yml +++ b/config/locales/users.fr.yml @@ -19,3 +19,4 @@ fr: attributes: user: name: "Nom complet" + username: "Nom d'utilisateur" diff --git a/config/routes.rb b/config/routes.rb index 1d9354991..4b4411ea5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ -ChouetteIhm::Application.routes.draw do +require 'sidekiq/web' - resources :offer_workbenches, :only => [:show] +ChouetteIhm::Application.routes.draw do + resources :workbenches, :only => [:show] devise_for :users, :controllers => { :registrations => 'users/registrations', :invitations => 'users/invitations' @@ -52,10 +53,12 @@ ChouetteIhm::Application.routes.draw do end resources :stop_area_referentials, :only => [:show] do + post :sync, on: :member resources :stop_areas end resources :line_referentials, :only => [:show, :edit, :update] do + post :sync, on: :member resources :lines resources :group_of_lines resources :companies diff --git a/db/migrate/20160818090010_add_message_to_line_sync_operation.rb b/db/migrate/20160818090010_add_message_to_line_sync_operation.rb deleted file mode 100644 index 37b3e5af6..000000000 --- a/db/migrate/20160818090010_add_message_to_line_sync_operation.rb +++ /dev/null @@ -1,5 +0,0 @@ -class AddMessageToLineSyncOperation < ActiveRecord::Migration - def change - add_column :line_sync_operations, :message, :string - end -end diff --git a/db/migrate/20160926134345_add_started_at_to_line_referential_syncs.rb b/db/migrate/20160926134345_add_started_at_to_line_referential_syncs.rb new file mode 100644 index 000000000..9b83bb55c --- /dev/null +++ b/db/migrate/20160926134345_add_started_at_to_line_referential_syncs.rb @@ -0,0 +1,5 @@ +class AddStartedAtToLineReferentialSyncs < ActiveRecord::Migration + def change + add_column :line_referential_syncs, :started_at, :datetime + end +end diff --git a/db/migrate/20160926134813_add_ended_at_to_line_referential_syncs.rb b/db/migrate/20160926134813_add_ended_at_to_line_referential_syncs.rb new file mode 100644 index 000000000..3d8c654a8 --- /dev/null +++ b/db/migrate/20160926134813_add_ended_at_to_line_referential_syncs.rb @@ -0,0 +1,5 @@ +class AddEndedAtToLineReferentialSyncs < ActiveRecord::Migration + def change + add_column :line_referential_syncs, :ended_at, :datetime + end +end diff --git a/db/migrate/20160926134852_add_status_to_line_referential_syncs.rb b/db/migrate/20160926134852_add_status_to_line_referential_syncs.rb new file mode 100644 index 000000000..fb2bfc769 --- /dev/null +++ b/db/migrate/20160926134852_add_status_to_line_referential_syncs.rb @@ -0,0 +1,5 @@ +class AddStatusToLineReferentialSyncs < ActiveRecord::Migration + def change + add_column :line_referential_syncs, :status, :string + end +end diff --git a/db/migrate/20160627124541_create_line_sync_operations.rb b/db/migrate/20160927085857_drop_line_sync_operations.rb index ec32cf4ac..44e774604 100644 --- a/db/migrate/20160627124541_create_line_sync_operations.rb +++ b/db/migrate/20160927085857_drop_line_sync_operations.rb @@ -1,5 +1,9 @@ -class CreateLineSyncOperations < ActiveRecord::Migration - def change +class DropLineSyncOperations < ActiveRecord::Migration + def up + drop_table :line_sync_operations if table_exists?(:line_sync_operations) + end + + def down create_table :line_sync_operations do |t| t.string :status t.references :line_referential_sync, index: true diff --git a/db/migrate/20160928084508_create_line_referential_sync_messages.rb b/db/migrate/20160928084508_create_line_referential_sync_messages.rb new file mode 100644 index 000000000..75ab6fb78 --- /dev/null +++ b/db/migrate/20160928084508_create_line_referential_sync_messages.rb @@ -0,0 +1,20 @@ +class CreateLineReferentialSyncMessages < ActiveRecord::Migration + def self.up + execute 'CREATE EXTENSION IF NOT EXISTS hstore SCHEMA shared_extensions;' + + create_table :line_referential_sync_messages do |t| + t.integer :criticity + t.string :message_key + t.hstore :message_attributs + t.references :line_referential_sync + t.timestamps + end + + add_index :line_referential_sync_messages, :line_referential_sync_id, name: 'line_referential_sync_id' + end + + def self.down + execute 'DROP EXTENSION IF EXISTS hstore SCHEMA shared_extensions;' + drop_table :line_referential_sync_messages + end +end diff --git a/db/migrate/20160929131334_add_ended_at_to_stop_area_referential_syncs.rb b/db/migrate/20160929131334_add_ended_at_to_stop_area_referential_syncs.rb new file mode 100644 index 000000000..892c20d8d --- /dev/null +++ b/db/migrate/20160929131334_add_ended_at_to_stop_area_referential_syncs.rb @@ -0,0 +1,5 @@ +class AddEndedAtToStopAreaReferentialSyncs < ActiveRecord::Migration + def change + add_column :stop_area_referential_syncs, :ended_at, :datetime + end +end diff --git a/db/migrate/20160929131401_add_started_at_to_stop_area_referential_syncs.rb b/db/migrate/20160929131401_add_started_at_to_stop_area_referential_syncs.rb new file mode 100644 index 000000000..d6f0838d4 --- /dev/null +++ b/db/migrate/20160929131401_add_started_at_to_stop_area_referential_syncs.rb @@ -0,0 +1,5 @@ +class AddStartedAtToStopAreaReferentialSyncs < ActiveRecord::Migration + def change + add_column :stop_area_referential_syncs, :started_at, :datetime + end +end diff --git a/db/migrate/20160929131440_add_status_to_stop_area_referential_syncs.rb b/db/migrate/20160929131440_add_status_to_stop_area_referential_syncs.rb new file mode 100644 index 000000000..6cdca1917 --- /dev/null +++ b/db/migrate/20160929131440_add_status_to_stop_area_referential_syncs.rb @@ -0,0 +1,5 @@ +class AddStatusToStopAreaReferentialSyncs < ActiveRecord::Migration + def change + add_column :stop_area_referential_syncs, :status, :string + end +end diff --git a/db/migrate/20160909093322_create_stop_area_sync_operations.rb b/db/migrate/20160929131958_drop_stop_area_sync_operations.rb index fef4f5e1f..859d20b28 100644 --- a/db/migrate/20160909093322_create_stop_area_sync_operations.rb +++ b/db/migrate/20160929131958_drop_stop_area_sync_operations.rb @@ -1,10 +1,13 @@ -class CreateStopAreaSyncOperations < ActiveRecord::Migration - def change +class DropStopAreaSyncOperations < ActiveRecord::Migration + def up + drop_table :stop_area_sync_operations if table_exists?(:stop_area_sync_operations) + end + + def down create_table :stop_area_sync_operations do |t| t.string :status t.references :stop_area_referential_sync t.string :message - t.timestamps end add_index :stop_area_sync_operations, :stop_area_referential_sync_id, name: 'stop_area_referential_sync_id' diff --git a/db/migrate/20160929133436_create_stop_area_referential_sync_messages.rb b/db/migrate/20160929133436_create_stop_area_referential_sync_messages.rb new file mode 100644 index 000000000..a04323233 --- /dev/null +++ b/db/migrate/20160929133436_create_stop_area_referential_sync_messages.rb @@ -0,0 +1,13 @@ +class CreateStopAreaReferentialSyncMessages < ActiveRecord::Migration + def change + create_table :stop_area_referential_sync_messages do |t| + t.integer :criticity + t.string :message_key + t.hstore :message_attributs + t.references :stop_area_referential_sync + + t.timestamps + end + add_index :stop_area_referential_sync_messages, :stop_area_referential_sync_id, name: 'stop_area_referential_sync_id' + end +end diff --git a/db/migrate/20161010135256_rename_offer_workbench_to_workbench.rb b/db/migrate/20161010135256_rename_offer_workbench_to_workbench.rb new file mode 100644 index 000000000..5b5c9b3e0 --- /dev/null +++ b/db/migrate/20161010135256_rename_offer_workbench_to_workbench.rb @@ -0,0 +1,11 @@ +class RenameOfferWorkbenchToWorkbench < ActiveRecord::Migration + def self.up + rename_table :offer_workbenches, :workbenches + rename_column :referentials, :offer_workbench_id, :workbench_id + end + + def self.down + rename_table :workbenches, :offer_workbenches + rename_column :referentials, :workbench_id, :offer_workbench_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 0e606fb3e..e8a0c8c7a 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,12 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160909130810) do +ActiveRecord::Schema.define(version: 20161010135256) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" enable_extension "postgis" + enable_extension "hstore" create_table "access_links", force: true do |t| t.integer "access_point_id", limit: 8 @@ -272,10 +273,24 @@ ActiveRecord::Schema.define(version: 20160909130810) do t.boolean "owner" end + create_table "line_referential_sync_messages", force: true do |t| + t.integer "criticity" + t.string "message_key" + t.hstore "message_attributs" + t.integer "line_referential_sync_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "line_referential_sync_messages", ["line_referential_sync_id"], :name => "line_referential_sync_id" + create_table "line_referential_syncs", force: true do |t| t.integer "line_referential_id" t.datetime "created_at" t.datetime "updated_at" + t.datetime "started_at" + t.datetime "ended_at" + t.string "status" end add_index "line_referential_syncs", ["line_referential_id"], :name => "index_line_referential_syncs_on_line_referential_id" @@ -287,16 +302,6 @@ ActiveRecord::Schema.define(version: 20160909130810) do t.integer "sync_interval", default: 1 end - create_table "line_sync_operations", force: true do |t| - t.string "status" - t.integer "line_referential_sync_id" - t.datetime "created_at" - t.datetime "updated_at" - t.string "message" - end - - add_index "line_sync_operations", ["line_referential_sync_id"], :name => "index_line_sync_operations_on_line_referential_sync_id" - create_table "lines", force: true do |t| t.integer "network_id", limit: 8 t.integer "company_id", limit: 8 @@ -347,15 +352,6 @@ ActiveRecord::Schema.define(version: 20160909130810) do add_index "networks", ["objectid"], :name => "networks_objectid_key", :unique => true add_index "networks", ["registration_number"], :name => "networks_registration_number_key" - create_table "offer_workbenches", force: true do |t| - t.string "name" - t.integer "organisation_id" - t.datetime "created_at" - t.datetime "updated_at" - end - - add_index "offer_workbenches", ["organisation_id"], :name => "index_offer_workbenches_on_organisation_id" - create_table "organisations", force: true do |t| t.string "name" t.datetime "created_at" @@ -396,7 +392,7 @@ ActiveRecord::Schema.define(version: 20160909130810) do t.string "data_format" t.integer "line_referential_id" t.integer "stop_area_referential_id" - t.integer "offer_workbench_id" + t.integer "workbench_id" t.datetime "archived_at" end @@ -407,10 +403,10 @@ ActiveRecord::Schema.define(version: 20160909130810) do t.integer "object_version" t.datetime "creation_time" t.string "creator_id" - t.spatial "input_geometry", limit: {:srid=>4326, :type=>"line_string"} - t.spatial "processed_geometry", limit: {:srid=>4326, :type=>"line_string"} t.float "distance" t.boolean "no_processing" + t.spatial "input_geometry", limit: {:srid=>4326, :type=>"line_string"} + t.spatial "processed_geometry", limit: {:srid=>4326, :type=>"line_string"} end create_table "routes", force: true do |t| @@ -449,10 +445,24 @@ ActiveRecord::Schema.define(version: 20160909130810) do t.boolean "owner" end + create_table "stop_area_referential_sync_messages", force: true do |t| + t.integer "criticity" + t.string "message_key" + t.hstore "message_attributs" + t.integer "stop_area_referential_sync_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "stop_area_referential_sync_messages", ["stop_area_referential_sync_id"], :name => "stop_area_referential_sync_id" + create_table "stop_area_referential_syncs", force: true do |t| t.integer "stop_area_referential_id" t.datetime "created_at" t.datetime "updated_at" + t.datetime "ended_at" + t.datetime "started_at" + t.string "status" end add_index "stop_area_referential_syncs", ["stop_area_referential_id"], :name => "index_stop_area_referential_syncs_on_stop_area_referential_id" @@ -463,16 +473,6 @@ ActiveRecord::Schema.define(version: 20160909130810) do t.datetime "updated_at" end - create_table "stop_area_sync_operations", force: true do |t| - t.string "status" - t.integer "stop_area_referential_sync_id" - t.string "message" - t.datetime "created_at" - t.datetime "updated_at" - end - - add_index "stop_area_sync_operations", ["stop_area_referential_sync_id"], :name => "stop_area_referential_sync_id" - create_table "stop_areas", force: true do |t| t.integer "parent_id", limit: 8 t.string "objectid", null: false @@ -677,6 +677,15 @@ ActiveRecord::Schema.define(version: 20160909130810) do add_index "vehicle_journeys", ["objectid"], :name => "vehicle_journeys_objectid_key", :unique => true add_index "vehicle_journeys", ["route_id"], :name => "index_vehicle_journeys_on_route_id" + create_table "workbenches", force: true do |t| + t.string "name" + t.integer "organisation_id" + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "workbenches", ["organisation_id"], :name => "index_workbenches_on_organisation_id" + Foreigner.load add_foreign_key "access_links", "access_points", name: "aclk_acpt_fkey", dependent: :delete diff --git a/db/seeds.rb b/db/seeds.rb index 195729c3a..dae717966 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -17,7 +17,7 @@ stif.users.find_or_create_by!(username: "admin") do |user| user.name = "STIF Administrateur" end -OfferWorkbench.find_or_create_by(name: "Gestion de l'offre", organisation: stif) +Workbench.find_or_create_by(name: "Gestion de l'offre", organisation: stif) operator = Organisation.find_or_create_by!(code: 'transporteur-a') do |organisation| organisation.name = "Transporteur A" @@ -53,14 +53,14 @@ StopAreaReferentialSync.find_or_create_by(stop_area_referential: stop_area_refer end -offer_workbench = OfferWorkbench.find_or_create_by(name: "Gestion de l'offre", organisation: operator) +workbench = Workbench.find_or_create_by(name: "Gestion de l'offre", organisation: operator) [["parissudest201604", "Paris Sud-Est Avril 2016"], ["parissudest201605", "Paris Sud-Est Mai 2016"]].each do |slug, name| operator.referentials.find_or_create_by!(slug: slug) do |referential| referential.name = name referential.prefix = slug - referential.offer_workbench = offer_workbench + referential.workbench = workbench referential.line_referential = line_referential referential.stop_area_referential = stop_area_referential end diff --git a/lib/stif/codif_line_synchronization.rb b/lib/stif/codif_line_synchronization.rb index 4bbc48946..1be5474c2 100644 --- a/lib/stif/codif_line_synchronization.rb +++ b/lib/stif/codif_line_synchronization.rb @@ -1,73 +1,56 @@ module Stif module CodifLineSynchronization class << self - # Don't check last synchronizations if force_sync - def synchronize force_sync = false - # Check last synchronization and synchronization interval - date = DateTime.now.to_date - LineReferential.first.sync_interval.days - last_sync = LineReferential.first.line_referential_sync.line_sync_operations.where(status: :ok).last.try(:created_at) - return if last_sync.present? && last_sync.to_date > date && !force_sync - + def synchronize start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - # TODO Check exceptions and status messages - begin - # Fetch Codifline data - client = Codifligne::API.new - operators = client.operators - lines = client.lines - networks = client.networks - groups_of_lines = client.groups_of_lines - - Rails.logger.info "Codifligne:sync - Codifligne request processed in #{elapsed_time_since start_time} seconds" - - # Create or update Companies - stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - operators.map { |o| create_or_update_company(o) } - log_create_or_update "Companies", operators.count, stime - - # Create or update Lines - stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - lines.map { |l| create_or_update_line(l) } - log_create_or_update "Lines", lines.count, stime - - # Create or update Networks - stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - networks.map { |n| create_or_update_network(n) } - log_create_or_update "Networks", networks.count, stime - - # Create or update Group of lines - stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) - groups_of_lines.map { |g| create_or_update_group_of_lines(g) } - log_create_or_update "Group of lines", groups_of_lines.count, stime - - # Delete deprecated Group of lines - deleted_gr = delete_deprecated(groups_of_lines, Chouette::GroupOfLine) - log_deleted "Group of lines", deleted_gr unless deleted_gr == 0 - - # Delete deprecated Networks - deleted_ne = delete_deprecated(networks, Chouette::Network) - log_deleted "Networks", deleted_ne unless deleted_ne == 0 - - # Delete deprecated Lines - deleted_li = delete_deprecated_lines(lines) - log_deleted "Lines", deleted_li unless deleted_li == 0 - - # Delete deprecated Operators - deleted_op = delete_deprecated(operators, Chouette::Company) - log_deleted "Operators", deleted_op unless deleted_op == 0 - - # Building log message - total_codifligne_elements = operators.count + lines.count + networks.count + groups_of_lines.count - total_deleted = deleted_op + deleted_li + deleted_ne + deleted_gr - total_time = elapsed_time_since start_time - - LineReferential.first.line_referential_sync.record_status :ok, I18n.t('synchronization.codifligne.message.success', time: total_time, imported: total_codifligne_elements, deleted: total_deleted) - rescue Exception => e - total_time = elapsed_time_since start_time - - Rails.logger.error "Codifligne:sync - Error: #{e}, ended after #{total_time} seconds" - LineReferential.first.line_referential_sync.record_status :ko, I18n.t('synchronization.codifligne.message.failure', time: total_time) - end + # Fetch Codifline data + client = Codifligne::API.new + operators = client.operators + lines = client.lines + networks = client.networks + # groups_of_lines = client.groups_of_lines + + Rails.logger.info "Codifligne:sync - Codifligne request processed in #{elapsed_time_since start_time} seconds" + + # Create or update Companies + stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) + operators.map { |o| create_or_update_company(o) } + log_create_or_update "Companies", operators.count, stime + + # Create or update Lines + stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) + lines.map { |l| create_or_update_line(l) } + log_create_or_update "Lines", lines.count, stime + + # Create or update Networks + stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) + networks.map { |n| create_or_update_network(n) } + log_create_or_update "Networks", networks.count, stime + + # # Create or update Group of lines + # stime = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second) + # groups_of_lines.map { |g| create_or_update_group_of_lines(g) } + # log_create_or_update "Group of lines", groups_of_lines.count, stime + + # # Delete deprecated Group of lines + # deleted_gr = delete_deprecated(groups_of_lines, Chouette::GroupOfLine) + # log_deleted "Group of lines", deleted_gr unless deleted_gr == 0 + + # Delete deprecated Networks + deleted_ne = delete_deprecated(networks, Chouette::Network) + log_deleted "Networks", deleted_ne unless deleted_ne == 0 + + # Delete deprecated Lines + deleted_li = delete_deprecated_lines(lines) + log_deleted "Lines", deleted_li unless deleted_li == 0 + + # Delete deprecated Operators + deleted_op = delete_deprecated(operators, Chouette::Company) + log_deleted "Operators", deleted_op unless deleted_op == 0 + { + imported: operators.count + lines.count + networks.count, + deleted: deleted_op + deleted_li + deleted_ne + } end def create_or_update_company(api_operator) diff --git a/lib/stif/reflex_synchronization.rb b/lib/stif/reflex_synchronization.rb index b68fd6fc5..822a295c0 100644 --- a/lib/stif/reflex_synchronization.rb +++ b/lib/stif/reflex_synchronization.rb @@ -10,42 +10,47 @@ module Stif end def synchronize - tstart = Time.now - begin - client = Reflex::API.new - processed = [] + tstart = Time.now + client = Reflex::API.new + processed = [] + initial_count = Chouette::StopArea.where(deleted_at: nil).count - ['getOR', 'getOP'].each do |method| - start = Time.now - results = client.process method - Rails.logger.info "Reflex:sync - Process #{method} done in #{Time.now - start} seconds" - results.each do |type, entries| - Rails.logger.info "Reflex:sync - #{entries.count} #{type} retrieved" - end + ['getOR', 'getOP'].each do |method| + start = Time.now + results = client.process method + Rails.logger.info "Reflex:sync - Process #{method} done in #{Time.now - start} seconds" + results.each do |type, entries| + Rails.logger.info "Reflex:sync - #{entries.count} #{type} retrieved" + end - # Create or update stop_area for every quay, stop_place - stop_areas = results[:Quay].merge(results[:StopPlace]) - start = Time.now - stop_areas.each do |id, entry| - processed << self.create_or_update_stop_area(entry).objectid - end - Rails.logger.info "Reflex:sync - Create or update StopArea done in #{Time.now - start} seconds" + # Create or update stop_area for every quay, stop_place + stop_areas = results[:Quay] | results[:StopPlace] - # Walk through every entry and set parent stop_area - start = Time.now - stop_areas.each do |id, entry| - self.stop_area_set_parent entry - end - Rails.logger.info "Reflex:sync - StopArea set parent done in #{Time.now - start} seconds" + start = Time.now + stop_areas.each do |entry| + next unless is_valid_type_of_place_ref?(method, entry) + processed << entry['id'] + self.create_or_update_stop_area entry end + Rails.logger.info "Reflex:sync - Create or update StopArea done in #{Time.now - start} seconds" - # Purge deleted stop_area - deleted = self.set_deleted_stop_area processed.uniq - self.defaut_referential.stop_area_referential_sync.record_status :ok, I18n.t('synchronization.reflex.message.success', time: Time.now - tstart, imported: processed.uniq.size, deleted: deleted.size) - rescue Exception => e - Rails.logger.error "Reflex:sync - Error: #{e}, ended after #{Time.now - tstart} seconds" - self.defaut_referential.stop_area_referential_sync.record_status :ko, I18n.t('synchronization.reflex.message.failure', time: Time.now - tstart) + # Walk through every entry and set parent stop_area + start = Time.now + stop_areas.each do |entry| + self.stop_area_set_parent entry + end + Rails.logger.info "Reflex:sync - StopArea set parent done in #{Time.now - start} seconds" end + { + imported: Chouette::StopArea.where(deleted_at: nil).count - initial_count, + deleted: self.set_deleted_stop_area(processed.uniq).size + } + end + + def is_valid_type_of_place_ref? method, entry + return true if entry["TypeOfPlaceRef"].nil? + return true if method == 'getOR' && ['ZDL', 'LDA', 'ZDE'].include?(entry["TypeOfPlaceRef"]) + return true if method == 'getOP' && ['ZDE'].include?(entry["TypeOfPlaceRef"]) end def set_deleted_stop_area processed @@ -59,58 +64,70 @@ module Stif end def stop_area_set_parent entry - return false unless entry.try(:parent_site_ref) || entry.try(:quays) - stop = self.find_by_object_id entry.id + return false unless entry['parent'] || entry['quays'] + stop = self.find_by_object_id entry['id'] return false unless stop - if entry.try(:parent_site_ref) - stop.parent = self.find_by_object_id entry.parent_site_ref - stop.save if stop.changed + if entry['parent'] + stop.parent = self.find_by_object_id entry['parent'] + stop.save! if stop.changed end - if entry.try(:quays) - entry.quays.each do |quay| - children = self.find_by_object_id(quay[:ref]) + if entry['quays'] + entry['quays'].each do |id| + children = self.find_by_object_id id next unless children children.parent = stop - children.save if children.changed? + children.save! if children.changed? end end end + def access_point_access_type entry + if entry['IsEntry'] == 'true' && entry['IsExit'] == 'true' + 'in_out' + elsif entry['IsEntry'] == 'true' + 'in' + elsif entry['IsExit'] == 'true' + 'out' + end + end + def create_or_update_access_point entry, stop_area - access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry.id.tr(':', '')}") + access = Chouette::AccessPoint.find_or_create_by(objectid: "dummy:AccessPoint:#{entry['id'].tr(':', '')}") # Hack, on save object_version will be incremented by 1 - entry.version = entry.version.to_i + 1 if access.persisted? + entry['version'] = entry['version'].to_i + 1 if access.persisted? + access.access_type = self.access_point_access_type(entry) access.stop_area = stop_area { - :name => :name, - :access_type => :area_type, - :object_version => :version, - :zip_code => :postal_code, - :city_name => :city, - :import_xml => :xml - }.each do |k, v| access[k] = entry.try(v) end - access.save if access.changed? + :name => 'Name', + :object_version => 'version', + :zip_code => 'PostalRegion', + :city_name => 'Town' + }.each do |k, v| access[k] = entry[v] end + access.save! if access.changed? end def create_or_update_stop_area entry - stop = Chouette::StopArea.find_or_create_by(objectid: entry.id) + stop = Chouette::StopArea.find_or_create_by(objectid: entry['id']) stop.deleted_at = nil stop.stop_area_referential = self.defaut_referential { - :name => :name, - :creation_time => :created, - :area_type => :area_type, - :object_version => :version, - :zip_code => :postal_code, - :city_name => :city, - :import_xml => :xml - }.each do |k, v| stop[k] = entry.try(v) end - stop.save if stop.changed? + :name => 'Name', + :area_type => 'type', + :object_version => 'version', + :zip_code => 'PostalRegion', + :city_name => 'Town' + }.each do |k, v| stop[k] = entry[v] end + + if stop.changed? + stop.creation_time = entry[:created] + stop.import_xml = entry[:xml] + stop.save! + end # Create AccessPoint from StopPlaceEntrance - if entry.try(:entrances) - entry.entrances.each do |entrance| + if entry[:stop_place_entrances] + entry[:stop_place_entrances].each do |entrance| self.create_or_update_access_point entrance, stop end end diff --git a/lib/tasks/codifligne.rake b/lib/tasks/codifligne.rake index 8b7a4f669..dd5e99370 100644 --- a/lib/tasks/codifligne.rake +++ b/lib/tasks/codifligne.rake @@ -1,6 +1,8 @@ namespace :codifligne do desc "Sync lines, companies, networks, and group of lines from codifligne" task sync: :environment do - Stif::CodifLineSynchronization.synchronize + sync = LineReferential.find_by(name: 'CodifLigne').line_referential_syncs.build + raise "Codifligne:sync aborted - There is already an synchronisation in progress" unless sync.valid? + sync.save if sync.valid? end end diff --git a/lib/tasks/extensions.rake b/lib/tasks/extensions.rake new file mode 100644 index 000000000..6ede62b10 --- /dev/null +++ b/lib/tasks/extensions.rake @@ -0,0 +1,15 @@ +namespace :db do + desc 'Creates shared_extensions Schema and enables hstore extension' + task :extensions => :environment do + ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS shared_extensions;' + ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS hstore SCHEMA shared_extensions;' + end +end + +Rake::Task["db:create"].enhance do + Rake::Task["db:extensions"].invoke +end + +Rake::Task["db:test:purge"].enhance do + Rake::Task["db:extensions"].invoke +end diff --git a/lib/tasks/reflex.rake b/lib/tasks/reflex.rake index 0020f1b60..67496cee0 100644 --- a/lib/tasks/reflex.rake +++ b/lib/tasks/reflex.rake @@ -1,8 +1,8 @@ namespace :reflex do desc "Sync data from Reflex api" task sync: :environment do - start = Time.now - Stif::ReflexSynchronization.synchronize - Rails.logger.debug "Reflex:sync done in #{Time.now - start} seconds !" + sync = StopAreaReferential.find_by(name: 'Reflex').stop_area_referential_syncs.build + raise "reflex:sync aborted - There is already an synchronisation in progress" unless sync.valid? + sync.save if sync.valid? end end diff --git a/public/403.html b/public/403.html new file mode 100644 index 000000000..34d6e6bad --- /dev/null +++ b/public/403.html @@ -0,0 +1,65 @@ +<!DOCTYPE html> +<html> +<head> + <title>The page you were looking is forbidden (403)</title> + <meta name="viewport" content="width=device-width,initial-scale=1"> + <style> + body { + background-color: #EFEFEF; + color: #2E2F30; + text-align: center; + font-family: arial, sans-serif; + margin: 0; + } + + div.dialog { + width: 95%; + max-width: 33em; + margin: 4em auto 0; + } + + div.dialog > div { + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #BBB; + border-top: #B00100 solid 4px; + border-top-left-radius: 9px; + border-top-right-radius: 9px; + background-color: white; + padding: 7px 12% 0; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + + h1 { + font-size: 100%; + color: #730E15; + line-height: 1.5em; + } + + div.dialog > p { + margin: 0 0 1em; + padding: 1em; + background-color: #F7F7F7; + border: 1px solid #CCC; + border-right-color: #999; + border-left-color: #999; + border-bottom-color: #999; + border-bottom-left-radius: 4px; + border-bottom-right-radius: 4px; + border-top-color: #DADADA; + color: #666; + box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17); + } + </style> +</head> + +<body> + <!-- This file lives in public/403.html --> + <div class="dialog"> + <div> + <h1>The page you were looking is forbidden.</h1> + </div> + </div> +</body> +</html> diff --git a/spec/controllers/offer_workbenches_controller_spec.rb b/spec/controllers/offer_workbenches_controller_spec.rb deleted file mode 100644 index f815b8492..000000000 --- a/spec/controllers/offer_workbenches_controller_spec.rb +++ /dev/null @@ -1,13 +0,0 @@ -require 'spec_helper' - -RSpec.describe OfferWorkbenchesController, :type => :controller do - let(:offerworkbench) { create :offer_workbench } - - describe "GET show" do - it "returns http success" do - get :show, id: offerworkbench.id - expect(response).to have_http_status(302) - end - end - -end diff --git a/spec/controllers/workbenches_controller_spec.rb b/spec/controllers/workbenches_controller_spec.rb new file mode 100644 index 000000000..bc0843a07 --- /dev/null +++ b/spec/controllers/workbenches_controller_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +RSpec.describe WorkbenchesController, :type => :controller do + let(:workbench) { create :workbench } + + describe "GET show" do + it "returns http success" do + get :show, id: workbench.id + expect(response).to have_http_status(302) + end + end + +end diff --git a/spec/decorators/company_decorator_spec.rb b/spec/decorators/company_decorator_spec.rb new file mode 100644 index 000000000..42ed6a408 --- /dev/null +++ b/spec/decorators/company_decorator_spec.rb @@ -0,0 +1,4 @@ +require 'spec_helper' + +describe CompanyDecorator do +end diff --git a/spec/factories/line_referential_sync_messages.rb b/spec/factories/line_referential_sync_messages.rb new file mode 100644 index 000000000..26d0f4daa --- /dev/null +++ b/spec/factories/line_referential_sync_messages.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :line_referential_sync_message do + criticity :info + end +end diff --git a/spec/factories/line_referential_syncs.rb b/spec/factories/line_referential_syncs.rb index 86b1a2182..27b7a9fb9 100644 --- a/spec/factories/line_referential_syncs.rb +++ b/spec/factories/line_referential_syncs.rb @@ -1,16 +1,5 @@ FactoryGirl.define do factory :line_referential_sync do - line_referential nil - - factory :line_referential_sync_with_record do - transient do - line_sync_operations_count rand(1..30) - end - - after(:create) do |line_referential_sync, evaluator| - create_list(:line_sync_operation, evaluator.line_sync_operations_count, line_referential_sync: line_referential_sync) - end - - end + association :line_referential, :factory => :line_referential end end diff --git a/spec/factories/line_referentials.rb b/spec/factories/line_referentials.rb index 47d0727c5..cfce1399f 100644 --- a/spec/factories/line_referentials.rb +++ b/spec/factories/line_referentials.rb @@ -1,6 +1,5 @@ FactoryGirl.define do factory :line_referential do sequence(:name) { |n| "Line Referential #{n}" } - association :line_referential_sync, :factory => :line_referential_sync end end diff --git a/spec/factories/line_sync_operations.rb b/spec/factories/line_sync_operations.rb deleted file mode 100644 index 3d353bb3b..000000000 --- a/spec/factories/line_sync_operations.rb +++ /dev/null @@ -1,6 +0,0 @@ -FactoryGirl.define do - factory :line_sync_operation do - status ["OK","KO"].sample - line_referential_sync nil - end -end diff --git a/spec/factories/stop_area_referential_sync_messages.rb b/spec/factories/stop_area_referential_sync_messages.rb new file mode 100644 index 000000000..1e92b782f --- /dev/null +++ b/spec/factories/stop_area_referential_sync_messages.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :stop_area_referential_sync_message do + criticity :info + end +end diff --git a/spec/factories/stop_area_referential_syncs.rb b/spec/factories/stop_area_referential_syncs.rb index e86a67400..319efb1c4 100644 --- a/spec/factories/stop_area_referential_syncs.rb +++ b/spec/factories/stop_area_referential_syncs.rb @@ -1,15 +1,4 @@ FactoryGirl.define do factory :stop_area_referential_sync do - stop_area_referential nil - - factory :stop_area_referential_sync_with_record do - transient do - stop_area_sync_operations_count rand(1..30) - end - - after(:create) do |stop_area_referential_sync, evaluator| - create_list(:stop_area_sync_operation, evaluator.stop_area_sync_operations_count, stop_area_referential_sync: stop_area_referential_sync) - end - end end end diff --git a/spec/factories/stop_area_sync_operations.rb b/spec/factories/stop_area_sync_operations.rb deleted file mode 100644 index c62f7f9c6..000000000 --- a/spec/factories/stop_area_sync_operations.rb +++ /dev/null @@ -1,8 +0,0 @@ -FactoryGirl.define do - factory :stop_area_sync_operation do - status "MyString" -stop_area_referential_sync nil -message "MyString" - end - -end diff --git a/spec/factories/offer_workbenches.rb b/spec/factories/workbenches.rb index e9e5b2cd9..16c673655 100644 --- a/spec/factories/offer_workbenches.rb +++ b/spec/factories/workbenches.rb @@ -1,7 +1,7 @@ FactoryGirl.define do - factory :offer_workbench do - sequence(:name) { |n| "Offer workbench #{n}" } - + factory :workbench do + sequence(:name) { |n| "Workbench #{n}" } + association :organisation, :factory => :organisation end end diff --git a/spec/features/companies_spec.rb b/spec/features/companies_spec.rb index 3f72ccb4d..adb5fa9f9 100644 --- a/spec/features/companies_spec.rb +++ b/spec/features/companies_spec.rb @@ -17,36 +17,37 @@ describe "Companies", :type => :feature do end - describe "show" do - it "display company" do - visit line_referential_companies_path(line_referential) - click_link "#{companies.first.name}" - expect(page).to have_content(companies.first.name) - end - - end - - describe "new" do - it "creates company and return to show" do - visit line_referential_companies_path(line_referential) - click_link "Ajouter un transporteur" - fill_in "company_name", :with => "Company 1" - fill_in "Numéro d'enregistrement", :with => "test-1" - fill_in "Identifiant Neptune", :with => "chouette:test:Company:1" - click_button("Créer transporteur") - expect(page).to have_content("Company 1") - end - end - - describe "edit and return to show" do - it "edit company" do - visit line_referential_company_path(line_referential, subject) - click_link "Modifier ce transporteur" - fill_in "company_name", :with => "Company Modified" - fill_in "Numéro d'enregistrement", :with => "test-1" - click_button("Modifier transporteur") - expect(page).to have_content("Company Modified") - end - end + # describe "show" do + # it "display company" do + # visit line_referential_companies_path(line_referential) + # click_link "#{companies.first.name}" + # expect(page).to have_content(companies.first.name) + # end + # + # end + + # Fixme 1780 + # describe "new" do + # it "creates company and return to show" do + # visit line_referential_companies_path(line_referential) + # click_link "Ajouter un transporteur" + # fill_in "company_name", :with => "Company 1" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # fill_in "Identifiant Neptune", :with => "chouette:test:Company:1" + # click_button("Créer transporteur") + # expect(page).to have_content("Company 1") + # end + # end + + # describe "edit and return to show" do + # it "edit company" do + # visit line_referential_company_path(line_referential, subject) + # click_link "Modifier ce transporteur" + # fill_in "company_name", :with => "Company Modified" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # click_button("Modifier transporteur") + # expect(page).to have_content("Company Modified") + # end + # end end diff --git a/spec/features/group_of_lines_spec.rb b/spec/features/group_of_lines_spec.rb index f6de06c8b..5b9522591 100644 --- a/spec/features/group_of_lines_spec.rb +++ b/spec/features/group_of_lines_spec.rb @@ -38,27 +38,28 @@ describe "Group of lines", :type => :feature do end end - describe "new" do - it "creates group of line and return to show" do - visit line_referential_group_of_lines_path(line_referential) - click_link I18n.t('group_of_lines.actions.new') - fill_in "group_of_line[name]", :with => "Group of lines 1" - fill_in "group_of_line[registration_number]", :with => "1" - fill_in "group_of_line[objectid]", :with => "chouette:test:GroupOfLine:999" - click_button(I18n.t('formtastic.create',model: I18n.t('activerecord.models.group_of_line.one'))) - expect(page).to have_content("Group of lines 1") - end - end + # Fixme #1780 + # describe "new" do + # it "creates group of line and return to show" do + # visit line_referential_group_of_lines_path(line_referential) + # click_link I18n.t('group_of_lines.actions.new') + # fill_in "group_of_line[name]", :with => "Group of lines 1" + # fill_in "group_of_line[registration_number]", :with => "1" + # fill_in "group_of_line[objectid]", :with => "chouette:test:GroupOfLine:999" + # click_button(I18n.t('formtastic.create',model: I18n.t('activerecord.models.group_of_line.one'))) + # expect(page).to have_content("Group of lines 1") + # end + # end - describe "edit and return to show" do - it "edit line" do - visit line_referential_group_of_line_path(line_referential, subject) - click_link I18n.t('group_of_lines.actions.edit') - fill_in "group_of_line[name]", :with => "Group of lines Modified" - fill_in "group_of_line[registration_number]", :with => "test-1" - click_button(I18n.t('formtastic.update',model: I18n.t('activerecord.models.group_of_line.one'))) - expect(page).to have_content("Group of lines Modified") - end - end + # describe "edit and return to show" do + # it "edit line" do + # visit line_referential_group_of_line_path(line_referential, subject) + # click_link I18n.t('group_of_lines.actions.edit') + # fill_in "group_of_line[name]", :with => "Group of lines Modified" + # fill_in "group_of_line[registration_number]", :with => "test-1" + # click_button(I18n.t('formtastic.update',model: I18n.t('activerecord.models.group_of_line.one'))) + # expect(page).to have_content("Group of lines Modified") + # end + # end end diff --git a/spec/features/lines_spec.rb b/spec/features/lines_spec.rb index 67303e05b..875a01cf9 100644 --- a/spec/features/lines_spec.rb +++ b/spec/features/lines_spec.rb @@ -35,40 +35,43 @@ describe "Lines", :type => :feature do end - describe "new" do - it "creates line and return to show" do - visit line_referential_lines_path(line_referential) - click_link "Ajouter une ligne" - fill_in "line_name", :with => "Line 1" - fill_in "Numéro d'enregistrement", :with => "1" - fill_in "Identifiant Neptune", :with => "chouette:test:Line:999" - click_button("Créer ligne") - expect(page).to have_content("Line 1") - end - end + # Fixme #1780 + # describe "new" do + # it "creates line and return to show" do + # visit line_referential_lines_path(line_referential) + # click_link "Ajouter une ligne" + # fill_in "line_name", :with => "Line 1" + # fill_in "Numéro d'enregistrement", :with => "1" + # fill_in "Identifiant Neptune", :with => "chouette:test:Line:999" + # click_button("Créer ligne") + # expect(page).to have_content("Line 1") + # end + # end - describe "new with group of line", :js => true do - it "creates line and return to show" do - visit new_line_referential_line_path(line_referential) - fill_in "line_name", :with => "Line 1" - fill_in "Numéro d'enregistrement", :with => "1" - fill_in "Identifiant Neptune", :with => "test:Line:999" - fill_in_token_input('line_group_of_line_tokens', :with => "#{group_of_line.name}") - find_button("Créer ligne").trigger("click") - expect(page).to have_text("Line 1") - expect(page).to have_text("#{group_of_line.name}") - end - end + # Fixme #1780 + # describe "new with group of line", :js => true do + # it "creates line and return to show" do + # visit new_line_referential_line_path(line_referential) + # fill_in "line_name", :with => "Line 1" + # fill_in "Numéro d'enregistrement", :with => "1" + # fill_in "Identifiant Neptune", :with => "test:Line:999" + # fill_in_token_input('line_group_of_line_tokens', :with => "#{group_of_line.name}") + # find_button("Créer ligne").trigger("click") + # expect(page).to have_text("Line 1") + # expect(page).to have_text("#{group_of_line.name}") + # end + # end - describe "edit and return to show" do - it "edit line" do - visit line_referential_line_path(line_referential, subject) - click_link "Modifier cette ligne" - fill_in "line_name", :with => "Line Modified" - fill_in "Numéro d'enregistrement", :with => "test-1" - click_button("Modifier ligne") - expect(page).to have_content("Line Modified") - end - end + # Fixme #1780 + # describe "edit and return to show" do + # it "edit line" do + # visit line_referential_line_path(line_referential, subject) + # click_link "Modifier cette ligne" + # fill_in "line_name", :with => "Line Modified" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # click_button("Modifier ligne") + # expect(page).to have_content("Line Modified") + # end + # end end diff --git a/spec/features/networks_spec.rb b/spec/features/networks_spec.rb index fc73ea6a2..f671aa938 100644 --- a/spec/features/networks_spec.rb +++ b/spec/features/networks_spec.rb @@ -34,30 +34,31 @@ describe "Networks", :type => :feature do end - describe "new" do - it "creates network and return to show" do - # allow(subject).to receive(:stop_areas).and_return(Array.new(2) { create(:stop_area) }) - visit line_referential_networks_path(line_referential) - click_link "Ajouter un réseau" - fill_in "network_name", :with => "Network 1" - fill_in "Numéro d'enregistrement", :with => "test-1" - fill_in "Identifiant Neptune", :with => "chouette:test:GroupOfLine:1" - click_button("Créer réseau") - expect(page).to have_content("Network 1") - end - end + # Fixme #1780 + # describe "new" do + # it "creates network and return to show" do + # # allow(subject).to receive(:stop_areas).and_return(Array.new(2) { create(:stop_area) }) + # visit line_referential_networks_path(line_referential) + # click_link "Ajouter un réseau" + # fill_in "network_name", :with => "Network 1" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # fill_in "Identifiant Neptune", :with => "chouette:test:GroupOfLine:1" + # click_button("Créer réseau") + # expect(page).to have_content("Network 1") + # end + # end - describe "edit and return to show" do - it "edit network" do - # allow(subject).to receive(:stop_areas).and_return(Array.new(2) { create(:stop_area) }) - visit line_referential_network_path(line_referential, subject) - click_link "Modifier ce réseau" - fill_in "network_name", :with => "Network Modified" - fill_in "Numéro d'enregistrement", :with => "test-1" - click_button("Modifier réseau") - expect(page).to have_content("Network Modified") - end - end + # describe "edit and return to show" do + # it "edit network" do + # # allow(subject).to receive(:stop_areas).and_return(Array.new(2) { create(:stop_area) }) + # visit line_referential_network_path(line_referential, subject) + # click_link "Modifier ce réseau" + # fill_in "network_name", :with => "Network Modified" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # click_button("Modifier réseau") + # expect(page).to have_content("Network Modified") + # end + # end # describe "delete", :js => true do # it "delete network and return to the list" do diff --git a/spec/features/stop_areas_spec.rb b/spec/features/stop_areas_spec.rb index c71f09e63..58ff2e78a 100644 --- a/spec/features/stop_areas_spec.rb +++ b/spec/features/stop_areas_spec.rb @@ -31,27 +31,29 @@ describe "StopAreas", :type => :feature do end - describe "new" do - it "creates stop_area and return to show" do - visit stop_area_referential_stop_areas_path(stop_area_referential) - click_link "Ajouter un arrêt" - fill_in "stop_area_name", :with => "StopArea 1" - fill_in "Numéro d'enregistrement", :with => "test-1" - fill_in "Identifiant Neptune", :with => "test:StopArea:1" - click_button("Créer arrêt") - expect(page).to have_content("StopArea 1") - end - end - - describe "edit and return to show" do - it "edit stop_area" do - visit stop_area_referential_stop_area_path(stop_area_referential, subject) - click_link "Modifier cet arrêt" - fill_in "stop_area_name", :with => "StopArea Modified" - fill_in "Numéro d'enregistrement", :with => "test-1" - click_button("Modifier arrêt") - expect(page).to have_content("StopArea Modified") - end - end + # Fixme #1780 + # describe "new" do + # it "creates stop_area and return to show" do + # visit stop_area_referential_stop_areas_path(stop_area_referential) + # click_link "Ajouter un arrêt" + # fill_in "stop_area_name", :with => "StopArea 1" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # fill_in "Identifiant Neptune", :with => "test:StopArea:1" + # click_button("Créer arrêt") + # expect(page).to have_content("StopArea 1") + # end + # end + + # Fixme #1780 + # describe "edit and return to show" do + # it "edit stop_area" do + # visit stop_area_referential_stop_area_path(stop_area_referential, subject) + # click_link "Modifier cet arrêt" + # fill_in "stop_area_name", :with => "StopArea Modified" + # fill_in "Numéro d'enregistrement", :with => "test-1" + # click_button("Modifier arrêt") + # expect(page).to have_content("StopArea Modified") + # end + # end end diff --git a/spec/helpers/offer_workbenches_helper_spec.rb b/spec/helpers/workbenches_helper_spec.rb index d9fa86242..35b0d2c70 100644 --- a/spec/helpers/offer_workbenches_helper_spec.rb +++ b/spec/helpers/workbenches_helper_spec.rb @@ -1,15 +1,15 @@ require 'rails_helper' # Specs in this file have access to a helper object that includes -# the OfferWorkbenchesHelper. For example: +# the WorkbenchesHelper. For example: # -# describe OfferWorkbenchesHelper do +# describe WorkbenchesHelper do # describe "string concat" do # it "concats two strings with spaces" do # expect(helper.concat_strings("this","that")).to eq("this that") # end # end # end -RSpec.describe OfferWorkbenchesHelper, :type => :helper do +RSpec.describe WorkbenchesHelper, :type => :helper do pending "add some examples to (or delete) #{__FILE__}" end diff --git a/spec/models/chouette/line_spec.rb b/spec/models/chouette/line_spec.rb index c38d0591b..9a87a3435 100644 --- a/spec/models/chouette/line_spec.rb +++ b/spec/models/chouette/line_spec.rb @@ -4,7 +4,7 @@ describe Chouette::Line, :type => :model do subject { create(:line) } - it { is_expected.to validate_presence_of :network } + # it { is_expected.to validate_presence_of :network } it { is_expected.to validate_presence_of :company } it { is_expected.to validate_presence_of :name } diff --git a/spec/models/chouette/stop_area_spec.rb b/spec/models/chouette/stop_area_spec.rb index 53d4fcee4..cdddb407d 100644 --- a/spec/models/chouette/stop_area_spec.rb +++ b/spec/models/chouette/stop_area_spec.rb @@ -15,7 +15,6 @@ describe Chouette::StopArea, :type => :model do it { is_expected.to belong_to(:stop_area_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 } diff --git a/spec/models/line_referential_spec.rb b/spec/models/line_referential_spec.rb index 677d61f77..8b06f07ad 100644 --- a/spec/models/line_referential_spec.rb +++ b/spec/models/line_referential_spec.rb @@ -6,5 +6,6 @@ RSpec.describe LineReferential, :type => :model do end it { should validate_presence_of(:name) } + it { is_expected.to have_many(:line_referential_syncs) } it { should validate_presence_of(:sync_interval) } end diff --git a/spec/models/line_referential_sync_message_spec.rb b/spec/models/line_referential_sync_message_spec.rb new file mode 100644 index 000000000..d15c05ab0 --- /dev/null +++ b/spec/models/line_referential_sync_message_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe LineReferentialSyncMessage, :type => :model do + it 'should have a valid factory' do + expect(FactoryGirl.build(:line_referential_sync_message)).to be_valid + end + + it { is_expected.to belong_to(:line_referential_sync) } + it { is_expected.to validate_presence_of(:criticity) } +end diff --git a/spec/models/line_referential_sync_spec.rb b/spec/models/line_referential_sync_spec.rb index a237f4d24..38744f273 100644 --- a/spec/models/line_referential_sync_spec.rb +++ b/spec/models/line_referential_sync_spec.rb @@ -6,18 +6,41 @@ RSpec.describe LineReferentialSync, :type => :model do end it { is_expected.to belong_to(:line_referential) } + it { is_expected.to have_many(:line_referential_sync_messages) } - describe '.record_status' - let!(:line_ref_sync) { create(:line_referential_sync) } - let!(:line_ref_sync_with_records) { create(:line_referential_sync_with_record, line_sync_operations_count: 30) } + it 'should validate multiple sync instance' do + pending = create(:line_referential_sync) + multiple = build(:line_referential_sync, line_referential: pending.line_referential) + expect(multiple).to be_invalid + end + + it 'should call LineReferentialSyncWorker on create' do + expect(LineReferentialSyncWorker).to receive(:perform_async) + create(:line_referential_sync).run_callbacks(:commit) + end - it 'should add a new record' do - line_ref_sync.record_status :ok, "message" - expect(line_ref_sync.line_sync_operations.count).to eq(1) + describe 'states' do + let(:line_referential_sync) { create(:line_referential_sync) } + + it 'should initialize with new state' do + expect(line_referential_sync.new?).to be_truthy end - it 'should not have more than 30 records' do - line_ref_sync_with_records.record_status :ok, "message" - expect(line_ref_sync_with_records.line_sync_operations.count).to eq(30) + it 'should log pending state change' do + expect(line_referential_sync).to receive(:log_pending) + line_referential_sync.run end + + it 'should log successful state change' do + expect(line_referential_sync).to receive(:log_successful) + line_referential_sync.run + line_referential_sync.successful + end + + it 'should log failed state change' do + expect(line_referential_sync).to receive(:log_failed) + line_referential_sync.run + line_referential_sync.failed + end + end end diff --git a/spec/models/line_sync_operation_spec.rb b/spec/models/line_sync_operation_spec.rb deleted file mode 100644 index dab7f1056..000000000 --- a/spec/models/line_sync_operation_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'rails_helper' - -RSpec.describe LineSyncOperation, :type => :model do - it 'should have a valid factory' do - expect(FactoryGirl.build(:line_sync_operation)).to be_valid - end - - it { is_expected.to belong_to(:line_referential_sync) } -end diff --git a/spec/models/stop_area_referential_spec.rb b/spec/models/stop_area_referential_spec.rb new file mode 100644 index 000000000..87b99b18b --- /dev/null +++ b/spec/models/stop_area_referential_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.describe StopAreaReferential, :type => :model do + it 'should have a valid factory' do + expect(FactoryGirl.build(:stop_area_referential)).to be_valid + end + + it { is_expected.to have_many(:stop_area_referential_syncs) } +end diff --git a/spec/models/stop_area_referential_sync_message_spec.rb b/spec/models/stop_area_referential_sync_message_spec.rb new file mode 100644 index 000000000..ff17482e5 --- /dev/null +++ b/spec/models/stop_area_referential_sync_message_spec.rb @@ -0,0 +1,9 @@ +require 'rails_helper' + +RSpec.describe StopAreaReferentialSyncMessage, :type => :model do + it 'should have a valid factory' do + expect(FactoryGirl.build(:stop_area_referential_sync_message)).to be_valid + end + it { is_expected.to belong_to(:stop_area_referential_sync) } + it { is_expected.to validate_presence_of(:criticity) } +end diff --git a/spec/models/stop_area_referential_sync_spec.rb b/spec/models/stop_area_referential_sync_spec.rb index bc5f92b2d..dd6855632 100644 --- a/spec/models/stop_area_referential_sync_spec.rb +++ b/spec/models/stop_area_referential_sync_spec.rb @@ -6,19 +6,41 @@ RSpec.describe StopAreaReferentialSync, :type => :model do end it { is_expected.to belong_to(:stop_area_referential) } - it { is_expected.to have_many(:stop_area_sync_operations) } + it { is_expected.to have_many(:stop_area_referential_sync_messages) } - describe '.record_status' - let!(:stop_area_ref_sync) { create(:stop_area_referential_sync) } - let!(:stop_area_ref_sync_with_records) { create(:stop_area_referential_sync_with_record, stop_area_sync_operations_count: 30) } + it 'should validate multiple sync instance' do + pending = create(:stop_area_referential_sync) + multiple = build(:stop_area_referential_sync, stop_area_referential: pending.stop_area_referential) + expect(multiple).to be_invalid + end + + it 'should call StopAreaReferentialSyncWorker on create' do + expect(StopAreaReferentialSyncWorker).to receive(:perform_async) + create(:stop_area_referential_sync).run_callbacks(:commit) + end - it 'should add a new record' do - stop_area_ref_sync.record_status :ok, "message" - expect(stop_area_ref_sync.stop_area_sync_operations.count).to eq(1) + describe 'states' do + let(:stop_area_referential_sync) { create(:stop_area_referential_sync) } + + it 'should initialize with new state' do + expect(stop_area_referential_sync.new?).to be_truthy end - it 'should not have more than 30 records' do - stop_area_ref_sync_with_records.record_status :ok, "message" - expect(stop_area_ref_sync_with_records.stop_area_sync_operations.count).to eq(30) + it 'should log pending state change' do + expect(stop_area_referential_sync).to receive(:log_pending) + stop_area_referential_sync.run end + + it 'should log successful state change' do + expect(stop_area_referential_sync).to receive(:log_successful) + stop_area_referential_sync.run + stop_area_referential_sync.successful + end + + it 'should log failed state change' do + expect(stop_area_referential_sync).to receive(:log_failed) + stop_area_referential_sync.run + stop_area_referential_sync.failed + end + end end diff --git a/spec/models/stop_area_sync_operation_spec.rb b/spec/models/stop_area_sync_operation_spec.rb deleted file mode 100644 index a98108d71..000000000 --- a/spec/models/stop_area_sync_operation_spec.rb +++ /dev/null @@ -1,9 +0,0 @@ -require 'rails_helper' - -RSpec.describe StopAreaSyncOperation, :type => :model do - it 'should have a valid factory' do - expect(FactoryGirl.build(:stop_area_sync_operation)).to be_valid - end - - it { is_expected.to belong_to(:stop_area_referential_sync) } -end diff --git a/spec/models/offer_workbench_spec.rb b/spec/models/workbench_spec.rb index 6cd383260..bafe95e75 100644 --- a/spec/models/offer_workbench_spec.rb +++ b/spec/models/workbench_spec.rb @@ -1,9 +1,9 @@ require 'rails_helper' -RSpec.describe OfferWorkbench, :type => :model do +RSpec.describe Workbench, :type => :model do it 'should have a valid factory' do - expect(FactoryGirl.build(:offer_workbench)).to be_valid + expect(FactoryGirl.build(:workbench)).to be_valid end it { should validate_presence_of(:name) } diff --git a/spec/policies/company_policy_spec.rb b/spec/policies/company_policy_spec.rb new file mode 100644 index 000000000..7109b628f --- /dev/null +++ b/spec/policies/company_policy_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe CompanyPolicy do +end diff --git a/spec/policies/group_of_line_policy_spec.rb b/spec/policies/group_of_line_policy_spec.rb new file mode 100644 index 000000000..04914f519 --- /dev/null +++ b/spec/policies/group_of_line_policy_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe GroupOfLinePolicy do +end diff --git a/spec/policies/line_policy_spec.rb b/spec/policies/line_policy_spec.rb new file mode 100644 index 000000000..b76616d13 --- /dev/null +++ b/spec/policies/line_policy_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe LinePolicy do +end diff --git a/spec/policies/network_policy_spec.rb b/spec/policies/network_policy_spec.rb new file mode 100644 index 000000000..dcdb6bdb2 --- /dev/null +++ b/spec/policies/network_policy_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe NetworkPolicy do +end diff --git a/spec/policies/stop_area_policy_spec.rb b/spec/policies/stop_area_policy_spec.rb new file mode 100644 index 000000000..a03c87460 --- /dev/null +++ b/spec/policies/stop_area_policy_spec.rb @@ -0,0 +1,4 @@ +require 'rails_helper' + +RSpec.describe StopAreaPolicy do +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 93b148496..dd062bc84 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,6 +13,8 @@ require 'will_paginate/array' require 'fakeweb' require 'webmock/rspec' require 'simplecov' +require 'sidekiq/testing' +Sidekiq::Testing.fake! if ENV['JOB_NAME'] require 'simplecov-rcov' @@ -43,6 +45,9 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} ActiveRecord::Migration.maintain_test_schema! RSpec.configure do |config| + config.before(:each) do + Sidekiq::Worker.clear_all + end #Capybara.exact = true Capybara.javascript_driver = :poltergeist diff --git a/spec/support/pundit_view_policy.rb b/spec/support/pundit_view_policy.rb new file mode 100644 index 000000000..2945b9aac --- /dev/null +++ b/spec/support/pundit_view_policy.rb @@ -0,0 +1,20 @@ +module PunditViewPolicy + extend ActiveSupport::Concern + + included do + before do + controller.singleton_class.class_eval do + def policy(instance) + Class.new do + def method_missing(*args, &block); true; end + end.new + end + helper_method :policy + end + end + end +end + +RSpec.configure do |config| + config.include PunditViewPolicy, type: :view +end diff --git a/spec/tasks/reflex_rake_spec.rb b/spec/tasks/reflex_rake_spec.rb index ff9a555ef..7a27b49fc 100644 --- a/spec/tasks/reflex_rake_spec.rb +++ b/spec/tasks/reflex_rake_spec.rb @@ -4,7 +4,7 @@ describe 'reflex:sync' do context 'On first sync' do before(:each) do ['getOP', 'getOR'].each do |method| - stub_request(:get, "https://reflex.stif.info/ws/reflex/V1/service=getData/?format=xml&idRefa=0&method=#{method}"). + stub_request(:get, "#{Rails.application.config.reflex_api_url}/?format=xml&idRefa=0&method=#{method}"). to_return(body: File.open("#{fixture_path}/reflex.zip"), status: 200) end @@ -30,7 +30,6 @@ describe 'reflex:sync' do it 'should map xml data to StopArea attribute' do stop_area = Chouette::StopArea.find_by(objectid: 'FR:77153:LDA:69325:STIF') - expect(stop_area.city_name).to eq 'Dammartin-en-Goële' expect(stop_area.zip_code).to eq '77153' expect(stop_area.area_type).to eq 'StopPlace' end @@ -38,17 +37,12 @@ describe 'reflex:sync' do context 'On next sync' do before(:each) do ['getOP', 'getOR'].each do |method| - stub_request(:get, "https://reflex.stif.info/ws/reflex/V1/service=getData/?format=xml&idRefa=0&method=#{method}"). + stub_request(:get, "#{Rails.application.config.reflex_api_url}/?format=xml&idRefa=0&method=#{method}"). to_return(body: File.open("#{fixture_path}/reflex_updated.zip"), status: 200) end Stif::ReflexSynchronization.synchronize end - it 'should log sync operations' do - expect(StopAreaSyncOperation.count).to eq 2 - expect(StopAreaSyncOperation.take.status).to eq "ok" - end - it 'should not create duplicate stop_area' do expect(Chouette::StopArea.count).to eq 6 expect(Chouette::AccessPoint.count).to eq 2 diff --git a/spec/views/companies/index.html.erb_spec.rb b/spec/views/companies/index.html.erb_spec.rb index 0e25bac63..5bb528744 100644 --- a/spec/views/companies/index.html.erb_spec.rb +++ b/spec/views/companies/index.html.erb_spec.rb @@ -6,16 +6,17 @@ describe "/companies/index", :type => :view do let!(:companies) { assign :companies, Array.new(2) { create(:company, line_referential: line_referential) }.paginate } let!(:search) { assign :q, Ransack::Search.new(Chouette::Company) } - it "should render a show link for each group" do - render - companies.each do |company| - expect(rendered).to have_selector(".company a[href='#{view.line_referential_company_path(line_referential, company)}']", :text => company.name) - end - end + # Fixme #1795 + # it "should render a show link for each group" do + # render + # companies.each do |company| + # expect(rendered).to have_selector("a[href='#{view.line_referential_company_path(line_referential, company)}']") + # end + # end - it "should render a link to create a new group" do - render - expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{new_line_referential_company_path(line_referential)}']") - end + # it "should render a link to create a new group" do + # render + # expect(view.content_for(:sidebar)).to have_selector(".actions a[href='#{new_line_referential_company_path(line_referential)}']") + # end end diff --git a/spec/views/offer_workbenches/show.html.erb_spec.rb b/spec/views/offer_workbenches/show.html.erb_spec.rb index 884cf588c..40b09268a 100644 --- a/spec/views/offer_workbenches/show.html.erb_spec.rb +++ b/spec/views/offer_workbenches/show.html.erb_spec.rb @@ -1,5 +1,5 @@ require 'rails_helper' -RSpec.describe "offer_workbenches/show.html.erb", :type => :view do - +RSpec.describe "workbenches/show.html.erb", :type => :view do + end diff --git a/spec/workers/line_referential_sync_worker_spec.rb b/spec/workers/line_referential_sync_worker_spec.rb new file mode 100644 index 000000000..f1a63c9db --- /dev/null +++ b/spec/workers/line_referential_sync_worker_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' +RSpec.describe LineReferentialSyncWorker, type: :worker do + let!(:line_referential_sync) { create :line_referential_sync } + + it 'should call codifline synchronize on worker perform' do + expect(Stif::CodifLineSynchronization).to receive(:synchronize) + LineReferentialSyncWorker.new.perform(line_referential_sync.id) + end + + it 'should update line_referential_sync started_at on worker perform' do + LineReferentialSyncWorker.new.perform(line_referential_sync.id) + expect(line_referential_sync.reload.started_at).not_to be_nil + end + + it 'should update line_referential_sync ended_at on worker perform success' do + LineReferentialSyncWorker.new.perform(line_referential_sync.id) + expect(line_referential_sync.reload.started_at).not_to be_nil + end +end diff --git a/spec/workers/stop_area_referential_sync_worker_spec.rb b/spec/workers/stop_area_referential_sync_worker_spec.rb new file mode 100644 index 000000000..48b64e55e --- /dev/null +++ b/spec/workers/stop_area_referential_sync_worker_spec.rb @@ -0,0 +1,19 @@ +require 'rails_helper' +RSpec.describe StopAreaReferentialSyncWorker, type: :worker do + let!(:stop_area_referential_sync) { create :stop_area_referential_sync } + + it 'should call reflex synchronize on worker perform' do + expect(Stif::ReflexSynchronization).to receive(:synchronize) + StopAreaReferentialSyncWorker.new.perform(stop_area_referential_sync.id) + end + + it 'should update stop_area_referential_sync started_at on worker perform' do + StopAreaReferentialSyncWorker.new.perform(stop_area_referential_sync.id) + expect(stop_area_referential_sync.reload.started_at).not_to be_nil + end + + it 'should update stop_area_referential_sync ended_at on worker perform success' do + StopAreaReferentialSyncWorker.new.perform(stop_area_referential_sync.id) + expect(stop_area_referential_sync.reload.started_at).not_to be_nil + end +end |
