diff options
| author | Vlatka Pavisic | 2016-12-30 16:44:30 +0100 |
|---|---|---|
| committer | Vlatka Pavisic | 2016-12-30 16:44:30 +0100 |
| commit | ad71715b42f5278fb46cedec874cb7e9a4fe3cf3 (patch) | |
| tree | 67cd1473f7f3c6d7e08401050bceddb16d3daeae /app | |
| parent | adfb4aebfab39b5a7a4b9a70ac62f639567aead6 (diff) | |
| download | chouette-core-ad71715b42f5278fb46cedec874cb7e9a4fe3cf3.tar.bz2 | |
Refs #2262 Refs #2263 Refs #2264 Refs #2265 : Calendars 80% done
Diffstat (limited to 'app')
| -rw-r--r-- | app/assets/stylesheets/main/calendars.sass | 7 | ||||
| -rw-r--r-- | app/controllers/calendars_controller.rb | 64 | ||||
| -rw-r--r-- | app/helpers/breadcrumb_helper.rb | 2 | ||||
| -rw-r--r-- | app/models/calendar.rb | 83 | ||||
| -rw-r--r-- | app/views/calendars/_calendars.html.slim | 2 | ||||
| -rw-r--r-- | app/views/calendars/_form.html.slim | 35 | ||||
| -rw-r--r-- | app/views/calendars/_period_fields.html.slim (renamed from app/views/calendars/_range_fields.html.slim) | 0 | ||||
| -rw-r--r-- | app/views/calendars/edit.html.slim | 2 | ||||
| -rw-r--r-- | app/views/calendars/index.html.slim | 27 |
9 files changed, 114 insertions, 108 deletions
diff --git a/app/assets/stylesheets/main/calendars.sass b/app/assets/stylesheets/main/calendars.sass new file mode 100644 index 000000000..c3f47f733 --- /dev/null +++ b/app/assets/stylesheets/main/calendars.sass @@ -0,0 +1,7 @@ +#calendar_form + .btn + margin: 5px + +#calendar_search_form + button + margin-top: 22px diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 84b432bf3..563c126e4 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -2,42 +2,13 @@ class CalendarsController < BreadcrumbController defaults resource_class: Calendar before_action :check_policy, only: [:edit, :update, :destroy] - def new - new! do - @calendar.date_ranges = [] - @calendar.dates = [] - end - end - - def create - @calendar = current_organisation.calendars.build(calendar_params) - - if @calendar.valid? - respond_with @calendar - else - render action: 'new' - end - end - - def update - update! do |success, failure| - success.html { redirect_to calendar_path(@calendar) } - end - end - - def destroy - destroy! do |success, failure| - success.html { redirect_to calendars_path } - end - end - private def calendar_params params.require(:calendar).permit(:id, :name, :short_name, :shared, ranges_attributes: [:id, :begin, :end, :_destroy], dates: []) end def sort_column - current_organisation.calendars.column_names.include?(params[:sort]) ? params[:sort] : 'short_name' + Calendar.column_names.include?(params[:sort]) ? params[:sort] : 'short_name' end def sort_direction @@ -45,24 +16,27 @@ class CalendarsController < BreadcrumbController end protected - def collection - # if params[:q] - # if params[:q][:shared_eq] - # if params[:q][:shared_eq] == 'all' - # params[:q].delete(:shared_eq) - # else - # params[:q][:shared_eq] = params[:q][:shared_eq] == 'true' - # end - # end - # end + def resource + @calendar = current_organisation.calendars.find_by_id(params[:id]) + end - @q = current_organisation.calendars.search(params[:q]) + def build_resource + super.tap do |calendar| + calendar.organisation = current_organisation + end + end - if sort_column && sort_direction - @calendars ||= @q.result(distinct: true).order(sort_column + ' ' + sort_direction).paginate(page: params[:page]) - else - @calendars ||= @q.result(distinct: true).paginate(page: params[:page]) + def collection + return @calendars if @calendars + if params[:q] + params[:q].delete(:shared_eq) if params[:q][:shared_eq] == '' + params[:q].delete(:short_name_cont) if params[:q][:short_name_cont] == '' end + + @q = current_organisation.calendars.search(params[:q]) + calendars = @q.result(distinct: true) + calendars = calendars.order(sort_column + ' ' + sort_direction) if sort_column && sort_direction + @calendars = calendars.paginate(page: params[:page]) end def check_policy diff --git a/app/helpers/breadcrumb_helper.rb b/app/helpers/breadcrumb_helper.rb index d764ad60f..da8cf8425 100644 --- a/app/helpers/breadcrumb_helper.rb +++ b/app/helpers/breadcrumb_helper.rb @@ -73,7 +73,7 @@ module BreadcrumbHelper end def calendar_breadcrumb(action) - add_breadcrumb Calendar.model_name.human.pluralize + add_breadcrumb I18n.t('calendars.index.title') add_breadcrumb @calendar.name if %i(show edit).include? action end diff --git a/app/models/calendar.rb b/app/models/calendar.rb index a04aa6d0a..40af2d0e3 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -5,6 +5,13 @@ class Calendar < ActiveRecord::Base validates_uniqueness_of :short_name validate :date_not_in_date_ranges + after_initialize :init_dates_and_date_ranges + + def init_dates_and_date_ranges + self.dates ||= [] + self.date_ranges ||= [] + end + private def date_not_in_date_ranges errors.add(:dates, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_date_ranges')) if dates && date_ranges && dates_and_date_ranges_overlap? @@ -20,14 +27,14 @@ class Calendar < ActiveRecord::Base overlap end - class DateRange + class Period include ActiveAttr::Model attribute :id, type: Integer attribute :begin, type: Date attribute :end, type: Date - validates_presence_of :begin, :end + validates :begin, :end, presence: true validate :check_end_greather_than_begin def check_end_greather_than_begin @@ -37,7 +44,7 @@ class Calendar < ActiveRecord::Base end def self.from_range(index, range) - DateRange.new id: index, begin: range.begin, end: range.end + Period.new id: index, begin: range.begin, end: range.end end def range @@ -52,8 +59,8 @@ class Calendar < ActiveRecord::Base other = other.flatten other = other.delete_if { |o| o.id == id } if id - other.any? do |date_range| - if other_range = date_range.range + other.any? do |period| + if other_range = period.range (range & other_range).present? end end @@ -71,6 +78,7 @@ class Calendar < ActiveRecord::Base id.present? end + def mark_for_destruction self._destroy = true end @@ -80,47 +88,49 @@ class Calendar < ActiveRecord::Base end # Required by coocon - def build_date_range - DateRange.new + def build_period + Period.new end - def ranges - @ranges ||= init_ranges + def periods + @periods ||= init_periods end - def init_ranges + def init_periods if date_ranges - date_ranges.each_with_index.map { |r, index| DateRange.from_range(index, r) } + date_ranges.each_with_index.map { |r, index| Period.from_range(index, r) } else [] end end - private :init_ranges + private :init_periods - validate :validate_ranges + validate :validate_periods - def validate_ranges - ranges_are_valid = true + def validate_periods + periods_are_valid = true - unless ranges.all?(&:valid?) - ranges_are_valid = false + unless periods.all?(&:valid?) + periods_are_valid = false end - ranges.each do |range| - if range.intersect?(ranges) - range.errors.add(:base, I18n.t("referentials.errors.overlapped_period")) - ranges_are_valid = false + periods.each do |period| + if period.intersect?(periods) + period.errors.add(:base, I18n.t('calendars.errors.overlapped_period')) + periods_are_valid = false end end - errors.add(:ranges, :invalid) unless ranges_are_valid + unless periods_are_valid + errors.add(:periods, :invalid) + end end - def ranges_attributes=(attributes = {}) - @ranges = [] - attributes.each do |index, range_attribute| - range = DateRange.new(range_attribute.merge(id: index)) - @ranges << range unless range.marked_for_destruction? + def periods_attributes=(attributes = {}) + @periods = [] + attributes.each do |index, period_attribute| + period = Period.new(period_attribute.merge(id: index)) + @periods << period unless period.marked_for_destruction? end date_ranges_will_change! @@ -129,17 +139,23 @@ class Calendar < ActiveRecord::Base before_validation :fill_date_ranges def fill_date_ranges - if @ranges - self.date_ranges = @ranges.map(&:range).compact.sort_by(&:begin) + if @periods + self.date_ranges = @periods.map(&:range).compact.sort_by(&:begin) end end - after_save :clear_ranges + after_save :clear_periods - def clear_ranges - @ranges = nil + def clear_periods + @periods = nil + end + private :clear_periods + + def self.new_from from + from.dup.tap do |metadata| + metadata.referential_id = nil + end end - private :clear_ranges end class Range @@ -149,4 +165,3 @@ class Range end alias_method :&, :intersection end - diff --git a/app/views/calendars/_calendars.html.slim b/app/views/calendars/_calendars.html.slim index 7813ff89e..261052baf 100644 --- a/app/views/calendars/_calendars.html.slim +++ b/app/views/calendars/_calendars.html.slim @@ -8,5 +8,5 @@ = will_paginate @calendars, container: false, renderer: RemoteBootstrapPaginationLinkRenderer - else - = replacement_msg t('calendars.search_no_results') + = replacement_msg t('.search_no_results') diff --git a/app/views/calendars/_form.html.slim b/app/views/calendars/_form.html.slim index d12473b3e..2d36d7d9e 100644 --- a/app/views/calendars/_form.html.slim +++ b/app/views/calendars/_form.html.slim @@ -1,20 +1,23 @@ -= simple_form_for @calendar, html: { class: 'form-horizontal' } do |f| - = f.input :name - = f.input :short_name - / = f.label :dates - / = f.simple_fields_for :dates do |date| - / = render 'date_fields', f: date - / .links - / = link_to_add_association '+', f, :dates - = f.label :date_ranges - = f.simple_fields_for :ranges do |range| - = render 'range_fields', f: range - .links - = link_to_add_association '+', f, :ranges +#calendar_form + .row + .col-xs-8.col-xs-offset-2 + = simple_form_for @calendar, html: { class: 'form-horizontal' } do |f| + = f.input :name + = f.input :short_name + / = f.label :dates + / = f.simple_fields_for :dates do |date| + / = render 'date_fields', f: date + / .links + / = link_to_add_association '+', f, :dates + = f.label :date_ranges + = f.simple_fields_for :periods do |period| + = render 'period_fields', f: period + .links + /= link_to_add_association '+', f, :periods - .form-actions - = f.button :submit, as: :button - = link_to t('cancel'), calendars_path + .form-actions + = f.button :submit, as: :button, class: 'btn btn-info' + = link_to t('cancel'), calendars_path, class: 'btn btn-default' / TODO : cocoon diff --git a/app/views/calendars/_range_fields.html.slim b/app/views/calendars/_period_fields.html.slim index 92cadc4e9..92cadc4e9 100644 --- a/app/views/calendars/_range_fields.html.slim +++ b/app/views/calendars/_period_fields.html.slim diff --git a/app/views/calendars/edit.html.slim b/app/views/calendars/edit.html.slim index f827e2eb6..22645cf24 100644 --- a/app/views/calendars/edit.html.slim +++ b/app/views/calendars/edit.html.slim @@ -1,3 +1,3 @@ -= title_tag t('.title') += title_tag t('.title', calendar: @calendar.name) = render 'form' diff --git a/app/views/calendars/index.html.slim b/app/views/calendars/index.html.slim index 3feca1b1c..57f30eaef 100644 --- a/app/views/calendars/index.html.slim +++ b/app/views/calendars/index.html.slim @@ -1,16 +1,23 @@ = title_tag t('.title') -= search_form_for @q, url: calendars_path, remote: true, html: { method: :get, class: 'form', id: 'search', role: 'form' } do |f| +#calendar_search_form + = search_form_for @q, url: calendars_path, remote: true, html: { method: :get, class: 'form', id: 'search', role: 'form' } do |f| + .panel.panel-default + .panel-heading + .row + .col-md-3 + = f.label t('calendars.activerecord.attributes.calendar.short_name') + = f.search_field :short_name_cont, class: 'form-control' + .col-md-3 + = f.label t('calendars.activerecord.attributes.calendar.shared') + = f.select :shared_eq, [[t('.shared'), true], [t('.not_shared'), false]], { include_blank: '' }, { class: 'form-control', style: 'width: 100%', 'data-select2ed': 'true', 'data-select2ed-placeholder': t('.all') } + .col-md-3 + = f.label t('.date') + = f.date_field :dates_or_date_ranges_cont, class: 'form-control' - .panel.panel-default - .panel-heading - .input-group.col-md-12 - = f.search_field :short_name_cont, placeholder: t('.short_name_cont'), class: 'form-control' - - .input-group-btn - button.btn.btn-primary#search-btn type='submit' - span.fa.fa-search + .col-md-3 + button.btn.btn-primary#search-btn type='submit' + span.fa.fa-search #calendars = render 'calendars' - |
