diff options
| -rw-r--r-- | app/assets/stylesheets/main/calendars.sass | 2 | ||||
| -rw-r--r-- | app/controllers/calendars_controller.rb | 6 | ||||
| -rw-r--r-- | app/models/calendar.rb | 44 | ||||
| -rw-r--r-- | app/policies/calendar_policy.rb | 24 | ||||
| -rw-r--r-- | app/views/calendars/_calendars.html.slim | 2 | ||||
| -rw-r--r-- | app/views/calendars/_form.html.slim | 6 | ||||
| -rw-r--r-- | app/views/calendars/_period_fields.html.slim | 2 | ||||
| -rw-r--r-- | app/views/calendars/index.js.slim | 1 | ||||
| -rw-r--r-- | config/locales/calendars.en.yml | 16 | ||||
| -rw-r--r-- | spec/features/calendars_spec.rb | 27 |
10 files changed, 83 insertions, 47 deletions
diff --git a/app/assets/stylesheets/main/calendars.sass b/app/assets/stylesheets/main/calendars.sass index c3f47f733..214cff1b1 100644 --- a/app/assets/stylesheets/main/calendars.sass +++ b/app/assets/stylesheets/main/calendars.sass @@ -4,4 +4,4 @@ #calendar_search_form button - margin-top: 22px + margin-top: 24px diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index afa4c129c..697d8a507 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -2,6 +2,9 @@ class CalendarsController < BreadcrumbController defaults resource_class: Calendar before_action :check_policy, only: [:edit, :update, :destroy] + respond_to :html + respond_to :js, only: :index + private def calendar_params params.require(:calendar).permit(:id, :name, :short_name, :shared, periods_attributes: [:id, :begin, :end, :_destroy], dates: []) @@ -34,8 +37,7 @@ class CalendarsController < BreadcrumbController end @q = current_organisation.calendars.search(params[:q]) - calendars = @q.result(distinct: true) - ap "FILTERED COLLECTION LENGTH : #{@q.result.length}" + calendars = @q.result calendars = calendars.order(sort_column + ' ' + sort_direction) if sort_column && sort_direction @calendars = calendars.paginate(page: params[:page]) end diff --git a/app/models/calendar.rb b/app/models/calendar.rb index b1fac536c..9c228ad69 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -14,28 +14,7 @@ class Calendar < ActiveRecord::Base 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? - end - - def dates_and_date_ranges_overlap? - overlap = false - dates.each do |date| - date_ranges.each do |date_range| - overlap = true if date_range.cover? date - end - end - overlap - end - def dates_uniqueness - errors.add(:dates, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates')) if dates && dates.length > dates.uniq.length - end - - def self.ransackable_scopes(auth_object = nil) - [:contains_date] - end class Period include ActiveAttr::Model @@ -167,6 +146,29 @@ class Calendar < ActiveRecord::Base metadata.referential_id = nil end 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? + end + + def dates_and_date_ranges_overlap? + overlap = false + dates.each do |date| + date_ranges.each do |date_range| + overlap = true if date_range.cover? date + end + end + overlap + end + + def dates_uniqueness + errors.add(:dates, I18n.t('activerecord.errors.models.calendar.attributes.dates.date_in_dates')) if dates && dates.length > dates.uniq.length + end + + def self.ransackable_scopes(auth_object = nil) + [:contains_date] + end end class Range diff --git a/app/policies/calendar_policy.rb b/app/policies/calendar_policy.rb index 4fbed2ccb..d340bfdda 100644 --- a/app/policies/calendar_policy.rb +++ b/app/policies/calendar_policy.rb @@ -6,20 +6,28 @@ class CalendarPolicy < ApplicationPolicy end def show? - organisation_match? || share? + organisation_match? || record.shared end - def create? ; true end - def update? ; true end - def new? ; true end - def edit? ; true end - def destroy? ; true end + def new? ; modify? end + def create? ; new? end + + def edit? ; modify? end + def update? ; edit? end + + def destroy? ; modify? end def share? - record.shared + # something like current_user.has_permission(:shared_calendar) + true + end + + def modify? + organisation_match? end def organisation_match? - current_organisation == record.organisation + true + #current_organisation == record.organisation end end diff --git a/app/views/calendars/_calendars.html.slim b/app/views/calendars/_calendars.html.slim index 261052baf..25a4bb171 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('.search_no_results') + = replacement_msg t('calendars.index.search_no_results') diff --git a/app/views/calendars/_form.html.slim b/app/views/calendars/_form.html.slim index 2d36d7d9e..e490441ed 100644 --- a/app/views/calendars/_form.html.slim +++ b/app/views/calendars/_form.html.slim @@ -12,12 +12,14 @@ = f.label :date_ranges = f.simple_fields_for :periods do |period| = render 'period_fields', f: period - .links - /= link_to_add_association '+', f, :periods + .links + = link_to_add_association '+', f, :periods .form-actions = f.button :submit, as: :button, class: 'btn btn-info' = link_to t('cancel'), calendars_path, class: 'btn btn-default' + = f.input :shared + / TODO : cocoon diff --git a/app/views/calendars/_period_fields.html.slim b/app/views/calendars/_period_fields.html.slim index 92cadc4e9..50413ef20 100644 --- a/app/views/calendars/_period_fields.html.slim +++ b/app/views/calendars/_period_fields.html.slim @@ -1,6 +1,6 @@ .nested-fields = f.input :begin, as: :date, html5: true, label: t('simple_form.labels.calendar.ranges.begin') = f.input :end, as: :date, html5: true, label: t('simple_form.labels.calendar.ranges.end') - /= link_to_remove_association f, data: { confirm: t('are_you_sure') } do + = link_to_remove_association f, data: { confirm: t('are_you_sure') } do span.fa.fa-trash diff --git a/app/views/calendars/index.js.slim b/app/views/calendars/index.js.slim new file mode 100644 index 000000000..936f93e5e --- /dev/null +++ b/app/views/calendars/index.js.slim @@ -0,0 +1 @@ +| $('#calendars').html("#{escape_javascript(render('calendars'))}"); diff --git a/config/locales/calendars.en.yml b/config/locales/calendars.en.yml index 24acb7680..b7ad5df26 100644 --- a/config/locales/calendars.en.yml +++ b/config/locales/calendars.en.yml @@ -17,14 +17,6 @@ en: shared: Shared errors: overlapped_periods: Another period is overlapped with this period - activerecord: - errors: - models: - calendar: - 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. index: title: Calendars all: All @@ -49,4 +41,12 @@ en: ranges: begin: Beginning end: End + activerecord: + errors: + models: + calendar: + 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. diff --git a/spec/features/calendars_spec.rb b/spec/features/calendars_spec.rb index 354e211a3..9331427ef 100644 --- a/spec/features/calendars_spec.rb +++ b/spec/features/calendars_spec.rb @@ -15,13 +15,33 @@ describe 'Calendars', type: :feature do end context 'filtering' do - xit 'supports filtering by short name' do + it 'supports filtering by short name' do + fill_in 'q[short_name_cont]', with: calendars.first.short_name + click_button 'search-btn' + expect(page).to have_content(calendars.first.short_name) + expect(page).not_to have_content(calendars.last.short_name) end - xit 'supports filtering by shared' do + it 'supports filtering by shared' do + shared_calendar = create :calendar, organisation_id: 1, shared: true + visit calendars_path + select I18n.t('calendars.index.shared'), from: 'q[shared_eq]' + click_button 'search-btn' + expect(page).to have_content(shared_calendar.short_name) + expect(page).not_to have_content(calendars.first.short_name) end - xit 'supports filtering by date' do + it 'supports filtering by date' do + july_calendar = create :calendar, dates: [Date.new(2017, 7, 7)], date_ranges: [Date.new(2017, 7, 15)..Date.new(2017, 7, 30)], organisation_id: 1 + visit calendars_path + fill_in 'q_contains_date', with: '2017/07/07' + click_button 'search-btn' + expect(page).to have_content(july_calendar.short_name) + expect(page).not_to have_content(calendars.first.short_name) + fill_in 'q_contains_date', with: '2017/07/18' + click_button 'search-btn' + expect(page).to have_content(july_calendar.short_name) + expect(page).not_to have_content(calendars.first.short_name) end end end @@ -33,3 +53,4 @@ describe 'Calendars', type: :feature do end end end + |
