diff options
35 files changed, 840 insertions, 152 deletions
diff --git a/app/assets/stylesheets/components/_color_selector.sass b/app/assets/stylesheets/components/_color_selector.sass new file mode 100644 index 000000000..07bfa0c80 --- /dev/null +++ b/app/assets/stylesheets/components/_color_selector.sass @@ -0,0 +1,21 @@ +select.color_selector + option[value='#9B9B9B'] + background-color: #9B9B9B + option[value='#FFA070'] + background-color: #FFA070 + option[value='#C67300'] + background-color: #C67300 + option[value='#7F551B'] + background-color: #7F551B + option[value='#41CCE3'] + background-color: #41CCE3 + option[value='#09B09C'] + background-color: #09B09C + option[value='#3655D7'] + background-color: #3655D7 + option[value='#6321A0'] + background-color: #6321A0 + option[value='#E796C6'] + background-color: #E796C6 + option[value='#DD2DAA'] + background-color: #DD2DAA
\ No newline at end of file diff --git a/app/assets/stylesheets/typography/_sboiv.sass b/app/assets/stylesheets/typography/_sboiv.sass index f694306c4..0ed2890fa 100644 --- a/app/assets/stylesheets/typography/_sboiv.sass +++ b/app/assets/stylesheets/typography/_sboiv.sass @@ -89,7 +89,7 @@ .sb-OAS:before content: '\e90f' -.sb-calendar:before +.sb-calendar:before, .sb-purchase_window:before content: '\e910' .sb-journey_pattern:before diff --git a/app/controllers/purchase_windows_controller.rb b/app/controllers/purchase_windows_controller.rb new file mode 100644 index 000000000..a70535150 --- /dev/null +++ b/app/controllers/purchase_windows_controller.rb @@ -0,0 +1,58 @@ +class PurchaseWindowsController < ChouetteController + include ReferentialSupport + include RansackDateFilter + include PolicyChecker + before_action :ransack_contains_date, only: [:index] + defaults :resource_class => Chouette::PurchaseWindow, collection_name: 'purchase_windows', instance_name: 'purchase_window' + belongs_to :referential + + def index + index! do + scope = self.ransack_period_range(scope: @purchase_windows, error_message: t('compliance_check_sets.filters.error_period_filter'), query: :overlapping) + @q = scope.ransack(params[:q]) + @purchase_windows = decorate_purchase_windows(@q.result.paginate(page: params[:page], per_page: 30)) + end + end + + def show + show! do + @purchase_window = @purchase_window.decorate(context: { + referential: @referential + }) + end + end + + protected + + def create_resource(purchase_window) + purchase_window.referential = @referential + super + end + + private + + def purchase_window_params + params.require(:purchase_window).permit(:id, :name, :color, :referential_id, periods_attributes: [:id, :begin, :end, :_destroy]) + end + + def decorate_purchase_windows(purchase_windows) + ModelDecorator.decorate( + purchase_windows, + with: PurchaseWindowDecorator, + context: { + referential: @referential + } + ) + end + + def ransack_contains_date + date =[] + if params[:q] && !params[:q]['date_ranges(1i)'].empty? + ['date_ranges(1i)', 'date_ranges(2i)', 'date_ranges(3i)'].each do |key| + date << params[:q][key].to_i + params[:q].delete(key) + end + params[:q]['date_ranges'] = Date.new(*date) rescue nil + end + end +end diff --git a/app/decorators/purchase_window_decorator.rb b/app/decorators/purchase_window_decorator.rb new file mode 100644 index 000000000..13bc4d666 --- /dev/null +++ b/app/decorators/purchase_window_decorator.rb @@ -0,0 +1,34 @@ +class PurchaseWindowDecorator < Draper::Decorator + decorates Chouette::PurchaseWindow + delegate_all + + def action_links + policy = h.policy(object) + links = [] + + if policy.update? + links << Link.new( + content: I18n.t('actions.edit'), + href: h.edit_referential_purchase_window_path(context[:referential].id, object) + ) + end + + if policy.destroy? + links << Link.new( + content: I18n.t('actions.destroy'), + href: h.referential_purchase_window_path(context[:referential].id, object), + method: :delete, + data: { confirm: h.t('purchase_window.actions.destroy_confirm') } + ) + end + + links + end + + def bounding_dates + unless object.date_ranges.empty? + object.date_ranges.map(&:min).min..object.date_ranges.map(&:max).max + end + end + +end diff --git a/app/helpers/table_builder_helper/url.rb b/app/helpers/table_builder_helper/url.rb index a53ac5620..28f1ade76 100644 --- a/app/helpers/table_builder_helper/url.rb +++ b/app/helpers/table_builder_helper/url.rb @@ -10,7 +10,7 @@ module TableBuilderHelper polymorph_url << item.route.line if item.is_a?(Chouette::RoutingConstraintZone) polymorph_url << item if item.respond_to? :line_referential polymorph_url << item.stop_area if item.respond_to? :stop_area - polymorph_url << item if item.respond_to?(:stop_points) || item.is_a?(Chouette::TimeTable) + polymorph_url << item if item.respond_to?(:stop_points) || item.is_a?(Chouette::TimeTable) || item.is_a?(Chouette::PurchaseWindow) elsif item.respond_to? :referential if item.respond_to? :workbench polymorph_url << item.workbench diff --git a/app/models/calendar.rb b/app/models/calendar.rb index b2e73929f..34ed51374 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -3,22 +3,19 @@ require_relative 'calendar/date_value' require_relative 'calendar/period' class Calendar < ActiveRecord::Base + include DateSupport + include PeriodSupport + has_paper_trail belongs_to :organisation - has_many :time_tables validates_presence_of :name, :short_name, :organisation validates_uniqueness_of :short_name - after_initialize :init_dates_and_date_ranges + has_many :time_tables scope :contains_date, ->(date) { where('date ? = any (dates) OR date ? <@ any (date_ranges)', date, date) } - def init_dates_and_date_ranges - self.dates ||= [] - self.date_ranges ||= [] - end - def self.ransackable_scopes(auth_object = nil) [:contains_date] end @@ -35,144 +32,4 @@ class Calendar < ActiveRecord::Base end end - - ### Calendar::Period - # Required by coocon - def build_period - Calendar::Period.new - end - - def periods - @periods ||= init_periods - end - - def init_periods - (date_ranges || []) - .each_with_index - .map( &Calendar::Period.method(:from_range) ) - end - private :init_periods - - validate :validate_periods - - def validate_periods - periods_are_valid = periods.all?(&:valid?) - - periods.each do |period| - if period.intersect?(periods) - period.errors.add(:base, I18n.t('calendars.errors.overlapped_periods')) - periods_are_valid = false - end - end - - unless periods_are_valid - errors.add(:periods, :invalid) - end - end - - def flatten_date_array attributes, key - date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i } - Date.new(*date_int) - end - - def periods_attributes=(attributes = {}) - @periods = [] - attributes.each do |index, period_attribute| - # Convert date_select to date - ['begin', 'end'].map do |attr| - period_attribute[attr] = flatten_date_array(period_attribute, attr) - end - period = Calendar::Period.new(period_attribute.merge(id: index)) - @periods << period unless period.marked_for_destruction? - end - - date_ranges_will_change! - end - - before_validation :fill_date_ranges - - def fill_date_ranges - if @periods - self.date_ranges = @periods.map(&:range).compact.sort_by(&:begin) - end - end - - after_save :clear_periods - - def clear_periods - @periods = nil - end - - private :clear_periods - - ### Calendar::DateValue - - # Required by coocon - def build_date_value - Calendar::DateValue.new - end - - def date_values - @date_values ||= init_date_values - end - - def init_date_values - if dates - dates.each_with_index.map { |d, index| Calendar::DateValue.from_date(index, d) } - else - [] - end - end - private :init_date_values - - validate :validate_date_values - - def validate_date_values - date_values_are_valid = date_values.all?(&:valid?) - - date_values.each do |date_value| - if date_values.count { |d| d.value == date_value.value } > 1 - date_value.errors.add(:base, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates')) - date_values_are_valid = false - end - date_ranges.each do |date_range| - if date_range.cover? date_value.value - date_value.errors.add(:base, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_date_ranges')) - date_values_are_valid = false - end - end - end - - unless date_values_are_valid - errors.add(:date_values, :invalid) - end - end - - def date_values_attributes=(attributes = {}) - @date_values = [] - attributes.each do |index, date_value_attribute| - date_value_attribute['value'] = flatten_date_array(date_value_attribute, 'value') - date_value = Calendar::DateValue.new(date_value_attribute.merge(id: index)) - @date_values << date_value unless date_value.marked_for_destruction? - end - - dates_will_change! - end - - before_validation :fill_dates - - def fill_dates - if @date_values - self.dates = @date_values.map(&:value).compact.sort - end - end - - after_save :clear_date_values - - def clear_date_values - @date_values = nil - end - - private :clear_date_values - end diff --git a/app/models/chouette/purchase_window.rb b/app/models/chouette/purchase_window.rb new file mode 100644 index 000000000..8786c7252 --- /dev/null +++ b/app/models/chouette/purchase_window.rb @@ -0,0 +1,27 @@ +require 'range_ext' +require_relative '../calendar/period' + +module Chouette + class PurchaseWindow < Chouette::TridentActiveRecord + # include ChecksumSupport + include ObjectidSupport + include PeriodSupport + extend Enumerize + enumerize :color, in: %w(#9B9B9B #FFA070 #C67300 #7F551B #41CCE3 #09B09C #3655D7 #6321A0 #E796C6 #DD2DAA) + + has_paper_trail + belongs_to :referential + + validates_presence_of :name, :referential + + scope :contains_date, ->(date) { where('date ? <@ any (date_ranges)', date) } + + def local_id + "IBOO-#{self.referential.id}-#{self.id}" + end + + # def checksum_attributes + # end + + end +end
\ No newline at end of file diff --git a/app/models/concerns/date_support.rb b/app/models/concerns/date_support.rb new file mode 100644 index 000000000..fbfe19af1 --- /dev/null +++ b/app/models/concerns/date_support.rb @@ -0,0 +1,80 @@ +module DateSupport + extend ActiveSupport::Concern + + included do + after_initialize :init_dates + + def init_dates + self.dates ||= [] + end + + ### Calendar::DateValue + # Required by coocon + def build_date_value + Calendar::DateValue.new + end + + def date_values + @date_values ||= init_date_values + end + + def init_date_values + if dates + dates.each_with_index.map { |d, index| Calendar::DateValue.from_date(index, d) } + else + [] + end + end + private :init_date_values + + validate :validate_date_values + + def validate_date_values + date_values_are_valid = date_values.all?(&:valid?) + + date_values.each do |date_value| + if date_values.count { |d| d.value == date_value.value } > 1 + date_value.errors.add(:base, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates')) + date_values_are_valid = false + end + date_ranges.each do |date_range| + if date_range.cover? date_value.value + date_value.errors.add(:base, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_date_ranges')) + date_values_are_valid = false + end + end + end + + unless date_values_are_valid + errors.add(:date_values, :invalid) + end + end + + def date_values_attributes=(attributes = {}) + @date_values = [] + attributes.each do |index, date_value_attribute| + date_value_attribute['value'] = flatten_date_array(date_value_attribute, 'value') + date_value = Calendar::DateValue.new(date_value_attribute.merge(id: index)) + @date_values << date_value unless date_value.marked_for_destruction? + end + + dates_will_change! + end + + before_validation :fill_dates + + def fill_dates + if @date_values + self.dates = @date_values.map(&:value).compact.sort + end + end + + after_save :clear_date_values + + def clear_date_values + @date_values = nil + end + + private :clear_date_values + end +end
\ No newline at end of file diff --git a/app/models/concerns/period_support.rb b/app/models/concerns/period_support.rb new file mode 100644 index 000000000..f512c4e89 --- /dev/null +++ b/app/models/concerns/period_support.rb @@ -0,0 +1,80 @@ +module PeriodSupport + extend ActiveSupport::Concern + + included do + after_initialize :init_date_ranges + + def init_date_ranges + self.date_ranges ||= [] + end + + ### Calendar::Period + # Required by coocon + def build_period + Calendar::Period.new + end + + def periods + @periods ||= init_periods + end + + def init_periods + (date_ranges || []) + .each_with_index + .map( &Calendar::Period.method(:from_range) ) + end + private :init_periods + + validate :validate_periods + + def validate_periods + periods_are_valid = periods.all?(&:valid?) + + periods.each do |period| + if period.intersect?(periods) + period.errors.add(:base, I18n.t('calendars.errors.overlapped_periods')) + periods_are_valid = false + end + end + + unless periods_are_valid + errors.add(:periods, :invalid) + end + end + + def flatten_date_array attributes, key + date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i } + Date.new(*date_int) + end + + def periods_attributes=(attributes = {}) + @periods = [] + attributes.each do |index, period_attribute| + # Convert date_select to date + ['begin', 'end'].map do |attr| + period_attribute[attr] = flatten_date_array(period_attribute, attr) + end + period = Calendar::Period.new(period_attribute.merge(id: index)) + @periods << period unless period.marked_for_destruction? + end + + date_ranges_will_change! + end + + before_validation :fill_date_ranges + + def fill_date_ranges + if @periods + self.date_ranges = @periods.map(&:range).compact.sort_by(&:begin) + end + end + + after_save :clear_periods + + def clear_periods + @periods = nil + end + + private :clear_periods + end +end
\ No newline at end of file diff --git a/app/models/referential.rb b/app/models/referential.rb index 851a33653..122af65a1 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -128,6 +128,10 @@ class Referential < ActiveRecord::Base Chouette::RoutingConstraintZone.all end + def purchase_windows + Chouette::PurchaseWindow.all + end + before_validation :define_default_attributes def define_default_attributes diff --git a/app/policies/purchase_window_policy.rb b/app/policies/purchase_window_policy.rb new file mode 100644 index 000000000..75143a8bd --- /dev/null +++ b/app/policies/purchase_window_policy.rb @@ -0,0 +1,20 @@ +class PurchaseWindowPolicy < ApplicationPolicy + class Scope < Scope + def resolve + scope + end + end + + def create? + !archived? && organisation_match? && user.has_permission?('purchase_windows.create') + end + + def update? + !archived? && organisation_match? && user.has_permission?('purchase_windows.update') + end + + def destroy? + !archived? && organisation_match? && user.has_permission?('purchase_windows.destroy') + end + +end
\ No newline at end of file diff --git a/app/views/dashboards/_dashboard.html.slim b/app/views/dashboards/_dashboard.html.slim index 7d547bf4c..b468fed27 100644 --- a/app/views/dashboards/_dashboard.html.slim +++ b/app/views/dashboards/_dashboard.html.slim @@ -31,13 +31,29 @@ = link_to calendar.name, calendar_path(calendar), class: 'list-group-item' - else .panel-body - em.small.text-muted Aucun modèle de calendrier défini + em.small.text-muted + = t('dasboard.calendars.none') + + .panel.panel-default + .panel-heading + h3.panel-title.with_actions + = t('dasboard.purchase_windows.title') + div + = link_to '', purchase_windows_path, class: ' fa fa-chevron-right pull-right' + - if @dashboard.current_organisation.purchase_windows.present? + .list-group + - @dashboard.current_organisation.purchase_windows.order("updated_at desc").limit(5).each do |purchase_window| + = link_to purchase_window.name, referential_purchase_window_path(purchase_window.referential, purchase_window), class: 'list-group-item' + - else + .panel-body + em.small.text-muted + = t('dasboard.purchase_windows.none') .col-lg-6.col-md-6.col-sm-6.col-xs-12 .panel.panel-default .panel-heading h3.panel-title - = "Référentiels d'arrêts" + = t('dashboard.stop_area_referentials.title') .list-group - @dashboard.current_organisation.stop_area_referentials.each do |referential| = link_to referential.name, stop_area_referential_stop_areas_path(referential), class: 'list-group-item' @@ -45,7 +61,7 @@ .panel.panel-default .panel-heading h3.panel-title - = "Référentiels de lignes" + = t('dashboard.line_referentials.title') .list-group - @dashboard.current_organisation.line_referentials.all.each do |referential| = link_to referential.name, line_referential_lines_path(referential), class: 'list-group-item' diff --git a/app/views/purchase_windows/_date_value_fields.html.slim b/app/views/purchase_windows/_date_value_fields.html.slim new file mode 100644 index 000000000..7bde06a94 --- /dev/null +++ b/app/views/purchase_windows/_date_value_fields.html.slim @@ -0,0 +1,13 @@ +.nested-fields + - if f.object.errors.has_key? :base + .row + .col-lg-12 + .alert.alert-danger + - f.object.errors[:base].each do |message| + p.small = message + + .wrapper + div + = f.input :value, as: :date, label: false, wrapper_html: { class: 'date smart_date' } + div + = link_to_remove_association '', f, class: 'fa fa-trash', data: { confirm: 'Etes-vous sûr(e) ?' }, title: t('actions.delete') diff --git a/app/views/purchase_windows/_filters.html.slim b/app/views/purchase_windows/_filters.html.slim new file mode 100644 index 000000000..9c83d20db --- /dev/null +++ b/app/views/purchase_windows/_filters.html.slim @@ -0,0 +1,15 @@ += search_form_for @q, url: referential_purchase_windows_path, builder: SimpleForm::FormBuilder, html: { method: :get, class: 'form form-filter' } do |f| + .ffg-row + .input-group.search_bar + = f.search_field :name_cont, class: 'form-control', placeholder: t('purchase_windows.index.filter_placeholder') + span.input-group-btn + button.btn.btn-default#search_btn type='submit' + span.fa.fa-search + + .form-group + = f.label Chouette::PurchaseWindow.human_attribute_name(:date), class: 'control-label' + = f.input :date_ranges, as: :date, label: false, wrapper_html: { class: 'date smart_date' }, class: 'form-control', include_blank: true + + .actions + = link_to t('actions.erase'), referential_purchase_windows_path, class: 'btn btn-link' + = f.submit t('actions.filter'), id: 'purchase_window_filter_btn', class: 'btn btn-default' diff --git a/app/views/purchase_windows/_form.html.slim b/app/views/purchase_windows/_form.html.slim new file mode 100644 index 000000000..8f3ba769d --- /dev/null +++ b/app/views/purchase_windows/_form.html.slim @@ -0,0 +1,29 @@ += simple_form_for [@referential, @purchase_window], html: { class: 'form-horizontal', id: 'purchase_window_form' }, wrapper: :horizontal_form do |f| + .row + .col-lg-12 + = f.input :name + = f.input :color, as: :select, boolean_style: :inline, collection: Chouette::PurchaseWindow.color.values, input_html: {class: 'color_selector'} + + .separator + + .row + .col-lg-12 + .subform + .nested-head + .wrapper + div + .form-group + label.control-label + = t('simple_form.labels.purchase_window.ranges.begin') + div + .form-group + label.control-label + = t('simple_form.labels.purchase_window.ranges.end') + div + + = f.simple_fields_for :periods do |period| + = render 'period_fields', f: period + .links.nested-linker + = link_to_add_association t('simple_form.labels.purchase_window.add_a_date_range'), f, :periods, class: 'btn btn-outline-primary' + + = f.button :submit, t('actions.submit'), class: 'btn btn-default formSubmitr', form: 'purchase_window_form' diff --git a/app/views/purchase_windows/_period_fields.html.slim b/app/views/purchase_windows/_period_fields.html.slim new file mode 100644 index 000000000..95e204554 --- /dev/null +++ b/app/views/purchase_windows/_period_fields.html.slim @@ -0,0 +1,15 @@ +.nested-fields + - if f.object.errors.has_key? :base + .row + .col-lg-12 + .alert.alert-danger + - f.object.errors[:base].each do |message| + p.small = message + + .wrapper + div + = f.input :begin, as: :date, label: false, wrapper_html: { class: 'date smart_date' } + div + = f.input :end, as: :date, label: false, wrapper_html: { class: 'date smart_date' } + div + = link_to_remove_association '', f, class: 'fa fa-trash', data: { confirm: 'Etes-vous sûr(e) ?' }, title: t('actions.delete') diff --git a/app/views/purchase_windows/edit.html.slim b/app/views/purchase_windows/edit.html.slim new file mode 100644 index 000000000..6354db853 --- /dev/null +++ b/app/views/purchase_windows/edit.html.slim @@ -0,0 +1,7 @@ +- breadcrumb :purchase_window, @referential, @purchase_window +- page_header_content_for @purchase_window +.page_content + .container-fluid + .row + .col-lg-8.col-lg-offset-2.col-md-8.col-md-offset-2.col-sm-10.col-sm-offset-1 + = render 'form' diff --git a/app/views/purchase_windows/index.html.slim b/app/views/purchase_windows/index.html.slim new file mode 100644 index 000000000..38954b5dc --- /dev/null +++ b/app/views/purchase_windows/index.html.slim @@ -0,0 +1,44 @@ +- breadcrumb :purchase_windows, @referential +- content_for :page_header_actions do + - if policy(Chouette::PurchaseWindow).create? + = link_to(t('actions.add'), new_referential_purchase_window_path(@referential), class: 'btn btn-default') + +.page_content + .container-fluid + - if params[:q].present? or @purchase_windows.any? + .row + .col-lg-12 + = render 'filters' + + - if @purchase_windows.any? + .row + .col-lg-12 + = table_builder_2 @purchase_windows, + [ \ + TableBuilderHelper::Column.new( \ + key: :name, \ + attribute: 'name', \ + link_to: lambda do |purchase_window| \ + referential_purchase_window_path(purchase_window.referential, purchase_window) \ + end \ + ), \ + TableBuilderHelper::Column.new( \ + key: :color, \ + attribute: Proc.new { |tt| tt.color ? content_tag(:span, '', class: 'fa fa-circle', style: "color:#{tt.color}") : '-' }\ + ), \ + TableBuilderHelper::Column.new( \ + key: :bounding_dates, \ + attribute: Proc.new {|w| w.bounding_dates.nil? ? '-' : t('validity_range', debut: l(w.bounding_dates.begin, format: :short), end: l(w.bounding_dates.end, format: :short))} \ + ) \ + ], + links: [:show], + cls: 'table has-filter' + + = new_pagination @purchase_windows, 'pull-right' + + - unless @purchase_windows.any? + .row.mt-xs + .col-lg-12 + = replacement_msg t('purchase_windows.search_no_results') + += javascript_pack_tag 'date_filters' diff --git a/app/views/purchase_windows/new.html.slim b/app/views/purchase_windows/new.html.slim new file mode 100644 index 000000000..402084167 --- /dev/null +++ b/app/views/purchase_windows/new.html.slim @@ -0,0 +1,6 @@ +- breadcrumb :purchase_windows, @referential +.page_content + .container-fluid + .row + .col-lg-8.col-lg-offset-2.col-md-8.col-md-offset-2.col-sm-10.col-sm-offset-1 + = render 'form' diff --git a/app/views/purchase_windows/show.html.slim b/app/views/purchase_windows/show.html.slim new file mode 100644 index 000000000..9f3abb267 --- /dev/null +++ b/app/views/purchase_windows/show.html.slim @@ -0,0 +1,20 @@ +- breadcrumb :purchase_window, @referential, @purchase_window +- page_header_content_for @purchase_window +- content_for :page_header_content do + .row.mb-sm + .col-lg-12.text-right + - @purchase_window.action_links.each do |link| + = link_to link.href, + method: link.method, + data: link.data, + class: 'btn btn-primary' do + = link.content + +.page_content + .container-fluid + .row + .col-lg-6.col-md-6.col-sm-12.col-xs-12 + = definition_list t('metadatas'), + { Chouette::PurchaseWindow.human_attribute_name(:name) => @purchase_window.try(:name), + 'Organisation' => @purchase_window.referential.organisation.name, + Chouette::PurchaseWindow.human_attribute_name(:date_ranges) => @purchase_window.periods.map{|d| t('validity_range', debut: l(d.begin, format: :short), end: l(d.end, format: :short))}.join('<br>').html_safe } diff --git a/config/breadcrumbs.rb b/config/breadcrumbs.rb index 9a748cb21..2cafc4419 100644 --- a/config/breadcrumbs.rb +++ b/config/breadcrumbs.rb @@ -167,6 +167,16 @@ crumb :line do |line| parent :lines, line.line_referential end +crumb :purchase_windows do |referential| + link I18n.t('purchase_windows.index.title'), referential_purchase_windows_path(referential) + parent :referential, referential +end + +crumb :purchase_window do |referential, purchase_window| + link breadcrumb_name(purchase_window), referential_purchase_window_path(referential, purchase_window) + parent :purchase_windows, referential +end + crumb :calendars do link I18n.t('calendars.index.title'), calendars_path end diff --git a/config/locales/calendars.en.yml b/config/locales/calendars.en.yml index d3cc57677..a2110d053 100644 --- a/config/locales/calendars.en.yml +++ b/config/locales/calendars.en.yml @@ -41,6 +41,8 @@ en: date: Date new: title: Add a new calendar + create: + title: Add a new calendar edit: title: Update calendar %{name} show: diff --git a/config/locales/calendars.fr.yml b/config/locales/calendars.fr.yml index fc895bf89..bd3051730 100644 --- a/config/locales/calendars.fr.yml +++ b/config/locales/calendars.fr.yml @@ -41,6 +41,8 @@ fr: date: Date new: title: Ajouter un calendrier + create: + title: Ajouter un calendrier edit: title: Editer le calendrier %{name} show: diff --git a/config/locales/dashboard.en.yml b/config/locales/dashboard.en.yml new file mode 100644 index 000000000..8d46ff7aa --- /dev/null +++ b/config/locales/dashboard.en.yml @@ -0,0 +1,16 @@ +en: + dashboards: + show: + title: "Dashboard %{organisation}" + calendars: + title: Calendars + none: No calendar created + purchase_windows: + title: Purchase windows + none: No purchase window created + line_referentials: + title: Line referential + none: No line referential created + stop_area_referentials: + title: Stop area referential + none: No stop area referential created diff --git a/config/locales/dashboard.fr.yml b/config/locales/dashboard.fr.yml index fffb36cd1..d0aa36d61 100644 --- a/config/locales/dashboard.fr.yml +++ b/config/locales/dashboard.fr.yml @@ -2,3 +2,15 @@ fr: dashboards: show: title: "Tableau de bord %{organisation}" + calendars: + title: Modèles de calendrier + none: Aucun calendrier défini + purchase_windows: + title: Calendriers commerciaux + none: Aucun calendrier commercial défini + line_referentials: + title: Référentiels de lignes + none: Aucun référentiels de lignes défini + stop_area_referentials: + title: Référentiels d'arrêts + none: Aucun référentiels d'arrêts défini diff --git a/config/locales/purchase_windows.en.yml b/config/locales/purchase_windows.en.yml new file mode 100644 index 000000000..5ffed305a --- /dev/null +++ b/config/locales/purchase_windows.en.yml @@ -0,0 +1,78 @@ +en: + purchase_windows: + search_no_results: 'No purchase window matching your query' + days: + monday: M + tuesday: Tu + wednesday: W + thursday: Th + friday: F + saturday: Sa + sunday: Su + months: + 1: January + 2: February + 3: March + 4: April + 5: May + 6: June + 7: July + 8: August + 9: September + 10: October + 11: November + 12: December + actions: + new: Add a new purchase window + edit: Edit this purchase window + destroy: Remove this purchase window + destroy_confirm: Are you sure you want destroy this purchase window? + errors: + overlapped_periods: Another period is overlapped with this period + short_period: A period needs to last at least two days + index: + title: purchase windows + all: All + shared: Shared + not_shared: Not shared + search_no_results: No purchase window matching your query + date: Date + filter_placeholder: Put the name of a purchase window... + create: + title: Add a new purchase window + new: + title: Add a new purchase window + edit: + title: Update purchase window %{name} + show: + title: purchase window %{name} + simple_form: + labels: + purchase_windows: + date_value: Date + add_a_date: Add a date + add_a_date_range: Add a date range + ranges: + begin: Beginning + end: End + activerecord: + models: + purchase_window: + zero: purchase window + one: purchase window + other: purchase windows + attributes: + purchase_windows: + name: Name + date_ranges: Date ranges + referential: Referential + color: Color + bounding_dates: Bounding Dates + errors: + models: + purchase_windows: + attributes: + dates: + date_in_date_ranges: A date can not be in Dates and in Date ranges. + date_in_dates: A date can appear only once in the list of dates. + illegal_date: The date %{date} does not exist. diff --git a/config/locales/purchase_windows.fr.yml b/config/locales/purchase_windows.fr.yml new file mode 100644 index 000000000..df5d45d82 --- /dev/null +++ b/config/locales/purchase_windows.fr.yml @@ -0,0 +1,79 @@ +fr: + purchase_windows: + search_no_results: 'Aucun calendrier commercial ne correspond à votre recherche' + days: + monday: L + tuesday: Ma + wednesday: Me + thursday: J + friday: V + saturday: S + sunday: D + months: + 1: Janvier + 2: Février + 3: Mars + 4: Avril + 5: Mai + 6: Juin + 7: Juillet + 8: Août + 9: Septembre + 10: Octobre + 11: Novembre + 12: Décembre + actions: + new: Créer + edit: Editer + destroy: Supprimer + destroy_confirm: Etes vous sûr de supprimer cet calendrier commercial ? + errors: + overlapped_periods: Une autre période chevauche cette période + short_period: Une période doit être d'un duréé de deux jours minimum + index: + title: Calendriers commerciaux + all: Tous + shared: Partagées + not_shared: Non partagées + search_no_results: Aucun calendrier commercial ne correspond à votre recherche + date: Date + filter_placeholder: Indiquez un nom de calendrier commercial... + new: + title: Ajouter un calendrier commercial + create: + title: Ajouter un calendrier commercial + edit: + title: Editer le calendrier commercial %{name} + show: + title: Calendrier commercial %{name} + simple_form: + labels: + calendar: + date_value: Date + add_a_date: Ajouter une date + add_a_date_range: Ajouter un intervalle de dates + ranges: + begin: Début + end: Fin + activerecord: + models: + purchase_window: + zero: "calendrier commercial" + one: "calendrier commercial" + other: "calendriers commerciaux" + attributes: + purchase_window: + name: Nom + short_name: Nom court + date_ranges: Intervalles de dates + referential: Jeu de données + color: Couleur + bounding_dates: Période englobante + errors: + models: + purchase_window: + attributes: + dates: + date_in_date_ranges: Une même date ne peut pas être incluse à la fois dans la liste et dans les intervalles de dates. + date_in_dates: Une même date ne peut pas être incluse plusieurs fois dans la liste. + illegal_date: La date %{date} n'existe pas. diff --git a/config/routes.rb b/config/routes.rb index 6668aa402..d097d2d71 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -173,6 +173,8 @@ ChouetteIhm::Application.routes.draw do resources :companies, controller: "referential_companies" + resources :purchase_windows + resources :time_tables do collection do get :tags diff --git a/db/migrate/20171214131755_create_business_calendars.rb b/db/migrate/20171214131755_create_business_calendars.rb new file mode 100644 index 000000000..aa7c1ab12 --- /dev/null +++ b/db/migrate/20171214131755_create_business_calendars.rb @@ -0,0 +1,14 @@ +class CreateBusinessCalendars < ActiveRecord::Migration + def change + create_table :business_calendars do |t| + t.string :name + t.string :short_name + t.string :color + t.daterange :date_ranges, array: true + t.date :dates, array: true + t.belongs_to :organisation, index: true + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20171215144543_rename_business_calendars_to_purchase_windows.rb b/db/migrate/20171215144543_rename_business_calendars_to_purchase_windows.rb new file mode 100644 index 000000000..d4467d6a7 --- /dev/null +++ b/db/migrate/20171215144543_rename_business_calendars_to_purchase_windows.rb @@ -0,0 +1,5 @@ +class RenameBusinessCalendarsToPurchaseWindows < ActiveRecord::Migration + def change + rename_table :business_calendars, :purchase_windows + end +end diff --git a/db/migrate/20171215145023_update_purchase_windows_attributes.rb b/db/migrate/20171215145023_update_purchase_windows_attributes.rb new file mode 100644 index 000000000..48dfb15bc --- /dev/null +++ b/db/migrate/20171215145023_update_purchase_windows_attributes.rb @@ -0,0 +1,13 @@ +class UpdatePurchaseWindowsAttributes < ActiveRecord::Migration + def change + add_column :purchase_windows, :objectid, :string + add_column :purchase_windows, :checksum, :string + add_column :purchase_windows, :checksum_source, :text + + remove_column :purchase_windows, :short_name, :string + remove_column :purchase_windows, :dates, :date + remove_column :purchase_windows, :organisation_id, :integer + + add_reference :purchase_windows, :referential, type: :bigint, index: true + end +end diff --git a/spec/factories/chouette_purchase_windows.rb b/spec/factories/chouette_purchase_windows.rb new file mode 100644 index 000000000..2e2faf4d8 --- /dev/null +++ b/spec/factories/chouette_purchase_windows.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + factory :purchase_window, class: Chouette::PurchaseWindow do + sequence(:name) { |n| "Purchase Window #{n}" } + sequence(:objectid) { |n| "organisation:PurchaseWindow:#{n}:LOC" } + date_ranges { [generate(:periods)] } + end + + sequence :periods do |n| + date = Date.today + 2*n + date..(date+1) + end +end diff --git a/spec/features/purchase_windows_permission_spec.rb b/spec/features/purchase_windows_permission_spec.rb new file mode 100644 index 000000000..e74fb5c17 --- /dev/null +++ b/spec/features/purchase_windows_permission_spec.rb @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +require 'spec_helper' + +describe "PurchaseWindows", :type => :feature do + login_user + + let(:purchase_window) { create :purchase_window, referential: first_referential} + + describe 'permissions' do + before do + allow_any_instance_of(PurchaseWindowPolicy).to receive(:create?).and_return permission + allow_any_instance_of(PurchaseWindowPolicy).to receive(:destroy?).and_return permission + allow_any_instance_of(PurchaseWindowPolicy).to receive(:update?).and_return permission + visit path + end + + context 'on show view' do + let( :path ){ referential_purchase_window_path(first_referential, purchase_window) } + + context 'if present → ' do + let( :permission ){ true } + it 'view shows the corresponding buttons' do + expect(page).to have_content(I18n.t('purchase_windows.actions.edit')) + expect(page).to have_content(I18n.t('purchase_windows.actions.destroy')) + end + end + + context 'if absent → ' do + let( :permission ){ false } + it 'view does not show the corresponding buttons' do + expect(page).not_to have_content(I18n.t('purchase_windows.actions.edit')) + expect(page).not_to have_content(I18n.t('purchase_windows.actions.destroy')) + end + end + end + + context 'on index view' do + let( :path ){ referential_purchase_windows_path(first_referential) } + + context 'if present → ' do + let( :permission ){ true } + it 'index shows an edit button' do + expect(page).to have_content(I18n.t('purchase_windows.actions.new')) + end + end + + context 'if absent → ' do + let( :permission ){ false } + it 'index does not show any edit button' do + expect(page).not_to have_content(I18n.t('purchase_windows.actions.new')) + end + end + end + end +end diff --git a/spec/models/chouette/purchase_window_spec.rb b/spec/models/chouette/purchase_window_spec.rb new file mode 100644 index 000000000..702a44eeb --- /dev/null +++ b/spec/models/chouette/purchase_window_spec.rb @@ -0,0 +1,27 @@ +RSpec.describe Chouette::PurchaseWindow, :type => :model do + let(:referential) {create(:referential)} + subject { create(:purchase_window, referential: referential) } + + it { should belong_to(:referential) } + it { is_expected.to validate_presence_of(:name) } + + describe 'validations' do + it 'validates and date_ranges do not overlap' do + expect(build(:purchase_window, referential: referential,date_ranges: [Date.today..Date.today + 10.day, Date.yesterday..Date.tomorrow])).to_not be_valid + # expect(build(periods: [Date.today..Date.today + 10.day, Date.yesterday..Date.tomorrow ])).to_not be_valid + end + end + + describe 'before_validation' do + let(:purchase_window) { build(:purchase_window, referential: referential, date_ranges: []) } + + it 'shoud fill date_ranges with date ranges' do + expected_range = Date.today..Date.tomorrow + purchase_window.date_ranges << expected_range + purchase_window.valid? + + expect(purchase_window.date_ranges.map { |period| period.begin..period.end }).to eq([expected_range]) + end + end + +end diff --git a/spec/policies/purchase_window_policy_spec.rb b/spec/policies/purchase_window_policy_spec.rb new file mode 100644 index 000000000..f078bf288 --- /dev/null +++ b/spec/policies/purchase_window_policy_spec.rb @@ -0,0 +1,15 @@ +RSpec.describe PurchaseWindowPolicy, type: :policy do + + let( :record ){ build_stubbed :purchase_window } + before { stub_policy_scope(record) } + + permissions :create? do + it_behaves_like 'permitted policy and same organisation', "purchase_windows.create", archived: true + end + permissions :destroy? do + it_behaves_like 'permitted policy and same organisation', "purchase_windows.destroy", archived: true + end + permissions :update? do + it_behaves_like 'permitted policy and same organisation', "purchase_windows.update", archived: true + end +end |
