diff options
| author | Luc Donnet | 2017-12-15 12:39:19 +0100 | 
|---|---|---|
| committer | Luc Donnet | 2017-12-21 13:55:09 +0100 | 
| commit | 723bee2cafff84c0b948f85ed450408d6cbc866a (patch) | |
| tree | 4bf418ed68d16f0ef04b8c658ab216260ef30de1 | |
| parent | 70e46cba80443fb615e1f7182422c8de6d96e7c3 (diff) | |
| download | chouette-core-723bee2cafff84c0b948f85ed450408d6cbc866a.tar.bz2 | |
Fix business calendar views, decorator, filters and policy Refs #5301
| -rw-r--r-- | app/controllers/business_calendars_controller.rb | 69 | ||||
| -rw-r--r-- | app/decorators/business_calendar_decorator.rb | 13 | ||||
| -rw-r--r-- | app/models/business_calendar.rb | 29 | ||||
| -rw-r--r-- | app/models/organisation.rb | 1 | ||||
| -rw-r--r-- | app/views/business_calendars/_filters.html.slim | 11 | ||||
| -rw-r--r-- | app/views/business_calendars/edit.html.slim | 7 | ||||
| -rw-r--r-- | app/views/business_calendars/index.html.slim | 16 | ||||
| -rw-r--r-- | app/views/business_calendars/show.html.slim | 21 | ||||
| -rw-r--r-- | config/breadcrumbs.rb | 2 | ||||
| -rw-r--r-- | config/locales/business_calendars.en.yml | 2 | ||||
| -rw-r--r-- | config/locales/business_calendars.fr.yml | 8 | 
11 files changed, 114 insertions, 65 deletions
| diff --git a/app/controllers/business_calendars_controller.rb b/app/controllers/business_calendars_controller.rb index 617a07855..c5fbe9c89 100644 --- a/app/controllers/business_calendars_controller.rb +++ b/app/controllers/business_calendars_controller.rb @@ -1,14 +1,13 @@  class BusinessCalendarsController < ChouetteController    include PolicyChecker -  include RansackDateFilter    defaults resource_class: BusinessCalendar -  before_action only: [:index] { set_date_time_params("bounding_dates", Date) } +  before_action :ransack_contains_date, only: [:index] +  respond_to :html +  respond_to :js, only: :index    def index      index! do -      scope = self.ransack_period_range(scope: @business_calendars, error_message: t('compliance_check_sets.filters.error_period_filter'), query: :overlapping) -      @q = scope.ransack(params[:q]) -      @business_calendars = decorate_business_calendars(@q.result.paginate(page: params[:page], per_page: 30)) +      @business_calendars = ModelDecorator.decorate(@business_calendars, with: BusinessCalendarDecorator)      end    end @@ -18,16 +17,62 @@ class BusinessCalendarsController < ChouetteController      end    end -  private +  def create +    puts "CREATE" +    puts build_resource.inspect +    create! +  end +  private    def business_calendar_params -    permitted_params = [:id, :name, :short_name, :color, periods_attributes: [:id, :begin, :end, :_destroy], date_values_attributes: [:id, :value, :_destroy]] -    params.require(:business_calendar).permit(*permitted_params) +    params.require(:business_calendar).permit( +      :id, +      :name, +      :short_name, +      :color, +      periods_attributes: [:id, :begin, :end, :_destroy], +      date_values_attributes: [:id, :value, :_destroy]) +  end + +  def sort_column +    BusinessCalendar.column_names.include?(params[:sort]) ? params[:sort] : 'short_name' +  end + +  def sort_direction +    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'    end -  def decorate_business_calendars(business_calendars) -    ModelDecorator.decorate( -      business_calendars, -      with: BusinessCalendarDecorator) +  protected + +  def begin_of_association_chain +    current_organisation    end +  # +  # def build_resource +  #   @business_calendar ||= current_organisation.business_calendars.new +  # end +  # +  # def resource +  #   @business_calendar ||= current_organisation.business_calendars.find(params[:id]) +  # end + +  def collection +    @q = current_organisation.business_calendars.ransack(params[:q]) + +    business_calendars = @q.result +    business_calendars = business_calendars.order(sort_column + ' ' + sort_direction) if sort_column && sort_direction +    @business_calendars = business_calendars.paginate(page: params[:page]) +  end + +  def ransack_contains_date +    date =[] +    if params[:q] && !params[:q]['contains_date(1i)'].empty? +      ['contains_date(1i)', 'contains_date(2i)', 'contains_date(3i)'].each do |key| +        date << params[:q][key].to_i +        params[:q].delete(key) +      end +      params[:q]['contains_date'] = Date.new(*date) rescue nil +    end +  end +  end diff --git a/app/decorators/business_calendar_decorator.rb b/app/decorators/business_calendar_decorator.rb index 9e8f21572..3990c3b31 100644 --- a/app/decorators/business_calendar_decorator.rb +++ b/app/decorators/business_calendar_decorator.rb @@ -3,23 +3,24 @@ class BusinessCalendarDecorator < Draper::Decorator    def action_links      links = [] +    policy = h.policy(object) -    if h.policy(object).update? +    if policy.edit?        links << Link.new( -        content: h.update_link_content, +        content: h.t('business_calendars.actions.edit'),          href: h.edit_business_calendar_path(object)        )      end -    if h.policy(object).destroy? +    if policy.destroy?        links << Link.new(          content: h.destroy_link_content, -        href: h.calendar_path(object), +        href: h.business_calendar_path(object),          method: :delete, -        data: { confirm: h.t('calendars.actions.destroy_confirm') } +        data: { confirm: h.t('business_calendars.actions.destroy_confirm') }        )      end      links    end -end
\ No newline at end of file +end diff --git a/app/models/business_calendar.rb b/app/models/business_calendar.rb index 45fbdb6ac..9ada43b6f 100644 --- a/app/models/business_calendar.rb +++ b/app/models/business_calendar.rb @@ -7,31 +7,10 @@ class BusinessCalendar < ActiveRecord::Base    extend Enumerize    enumerize :color, in: %w(#9B9B9B #FFA070 #C67300 #7F551B #41CCE3 #09B09C #3655D7 #6321A0 #E796C6 #DD2DAA) -  scope :overlapping, -> (period_range) do -    where("(periods.begin <= :end AND periods.end >= :begin) OR (dates BETWEEN :begin AND :end)", {begin: period_range.begin, end: period_range.end}) -  end - -  def bounding_dates -    bounding_min = self.dates.min -    bounding_max = self.dates.max - -    unless self.periods.empty? -      bounding_min = periods_min_date if periods_min_date && (bounding_min.nil? || (periods_min_date < bounding_min)) - -      bounding_max = periods_max_date if periods_max_date && (bounding_max.nil? || (bounding_max < periods_max_date)) -    end - -    [bounding_min, bounding_max].compact -  end - -  def periods_max_date -    return nil if self.periods.empty? -    self.periods.max.end -  end +  scope :contains_date, ->(date) { where('date ? = any (dates) OR date ? <@ any (date_ranges)', date, date) } -  def periods_min_date -    return nil if self.periods.empty? -    self.periods.min.begin +  def self.ransackable_scopes(auth_object = nil) +    [:contains_date]    end -end
\ No newline at end of file +end diff --git a/app/models/organisation.rb b/app/models/organisation.rb index 4343c87af..ea13b327c 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -13,6 +13,7 @@ class Organisation < ActiveRecord::Base    has_many :workbenches    has_many :calendars +  has_many :business_calendars    has_many :api_keys, class_name: 'Api::V1::ApiKey'    validates_presence_of :name diff --git a/app/views/business_calendars/_filters.html.slim b/app/views/business_calendars/_filters.html.slim index 8bc9ac2ae..7f1783af2 100644 --- a/app/views/business_calendars/_filters.html.slim +++ b/app/views/business_calendars/_filters.html.slim @@ -6,12 +6,11 @@          button.btn.btn-default#search_btn type='submit'            span.fa.fa-search -  .form-group.togglable -    = f.label BusinessCalendar.human_attribute_name(:bounding_dates), required: false, class: 'control-label' -    .filter_menu -      = f.simple_fields_for :bounding_dates do |p| -        = p.input :start_date, as: :date, label: t('simple_form.from'), wrapper_html: { class: 'date smart_date filter_menu-item' }, default: @begin_range, include_blank: @begin_range ? false : true -        = p.input :end_date, as: :date, label: t('simple_form.to'), wrapper_html: { class: 'date smart_date filter_menu-item' }, default: @end_range, include_blank: @end_range ? false : true +  .ffg-row +    .form-group +      = f.label BusinessCalendar.human_attribute_name(:date), class: 'control-label' +      = f.input :contains_date, as: :date, label: false, wrapper_html: { class: 'date smart_date' }, class: 'form-control', include_blank: true +    .actions      = link_to 'Effacer', business_calendars_path, class: 'btn btn-link' diff --git a/app/views/business_calendars/edit.html.slim b/app/views/business_calendars/edit.html.slim new file mode 100644 index 000000000..6d63b3489 --- /dev/null +++ b/app/views/business_calendars/edit.html.slim @@ -0,0 +1,7 @@ +- breadcrumb :business_calendar, @business_calendar +- page_header_content_for @business_calendar +.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/business_calendars/index.html.slim b/app/views/business_calendars/index.html.slim index 0cb81f0ac..4ee1d6854 100644 --- a/app/views/business_calendars/index.html.slim +++ b/app/views/business_calendars/index.html.slim @@ -1,7 +1,7 @@  - breadcrumb :business_calendars  - content_for :page_header_actions do    - if policy(BusinessCalendar).create? -  = link_to(t('actions.add'), new_business_calendar_path, class: 'btn btn-default') +    = link_to(t('actions.add'), new_business_calendar_path, class: 'btn btn-default')  .page_content    .container-fluid @@ -19,7 +19,7 @@                  key: :name, \                  attribute: 'name', \                  link_to: lambda do |business_calendar| \ -                  calendar_path(business_calendar) \ +                  business_calendar_path(business_calendar) \                  end \                ), \                TableBuilderHelper::Column.new( \ @@ -27,15 +27,15 @@                  attribute: Proc.new { |tt| tt.color ? content_tag(:span, '', class: 'fa fa-circle', style: "color:#{tt.color}") : '-' }\                ), \                TableBuilderHelper::Column.new( \ -                key: :created_at, \ -                attribute: 'created_at' \ +                key: :short_name, \ +                attribute: 'short_name' \                ), \                TableBuilderHelper::Column.new( \ -                key: :updated_at, \ -                attribute: 'updated_at' \ -              ) \ +                key: :organisation, \ +                attribute: Proc.new { |c| c.organisation.name } \ +              ), \              ], -            links: [:show, :edit], +            #links: [:show, :edit],              cls: 'table has-filter'            = new_pagination @business_calendars, 'pull-right' diff --git a/app/views/business_calendars/show.html.slim b/app/views/business_calendars/show.html.slim new file mode 100644 index 000000000..7c0b2ca41 --- /dev/null +++ b/app/views/business_calendars/show.html.slim @@ -0,0 +1,21 @@ +- breadcrumb :business_calendar, @business_calendar +- page_header_content_for @business_calendar +- content_for :page_header_content do +  .row.mb-sm +    .col-lg-12.text-right +      - @business_calendar.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'), +          { 'Nom court' => @business_calendar.try(:short_name), +            'Organisation' => @business_calendar.organisation.name, +            BusinessCalendar.human_attribute_name(:dates) =>  @business_calendar.dates.collect{|d| l(d, format: :short)}.join(', ').html_safe, +            BusinessCalendar.human_attribute_name(:date_ranges) => @business_calendar.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 1a252273b..0bca4a0be 100644 --- a/config/breadcrumbs.rb +++ b/config/breadcrumbs.rb @@ -166,7 +166,7 @@ crumb :business_calendars do    link I18n.t('business_calendars.index.title'), business_calendars_path  end -crumb :business_calendar do |calendar| +crumb :business_calendar do |business_calendar|    link breadcrumb_name(business_calendar), business_calendar_path(business_calendar)    parent :business_calendars  end diff --git a/config/locales/business_calendars.en.yml b/config/locales/business_calendars.en.yml index 19ac3a8f5..494136e1d 100644 --- a/config/locales/business_calendars.en.yml +++ b/config/locales/business_calendars.en.yml @@ -22,8 +22,6 @@ en:        10: October        11: November        12: December -    standard_calendars: Standard calendars -    standard_calendar: Standard calendar      actions:        new: Add a new business calendar        edit: Edit this business calendar diff --git a/config/locales/business_calendars.fr.yml b/config/locales/business_calendars.fr.yml index 2c15fb9e7..3ec08ebbd 100644 --- a/config/locales/business_calendars.fr.yml +++ b/config/locales/business_calendars.fr.yml @@ -22,12 +22,10 @@ fr:        10: Octobre        11: Novembre        12: Décembre -    standard_calendars: Calendriers standards -    standard_calendar: Calendrier standard      actions: -      new: Ajouter un calendrier commercial -      edit: Editer cet calendrier commercial -      destroy: Supprimer ce calendrier commercial +      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 | 
