diff options
| author | Luc Donnet | 2017-12-21 14:50:08 +0100 | 
|---|---|---|
| committer | GitHub | 2017-12-21 14:50:08 +0100 | 
| commit | 7aec5b94868bc2e7473f7ded4be7a585fb7b174e (patch) | |
| tree | 80218bcbec4d2740f6ca5be1d37bb0ba493cc879 /app/controllers/purchase_windows_controller.rb | |
| parent | 9050a68f40829fc1b08e2f60c3eea9cf0c76e879 (diff) | |
| parent | 610a3e133b6405b32ce5d8aa48be075d4b137b4f (diff) | |
| download | chouette-core-7aec5b94868bc2e7473f7ded4be7a585fb7b174e.tar.bz2 | |
Merge pull request #150 from af83/5301-add_business_calendars
Refs #5301 First draft for Business Calendars
Diffstat (limited to 'app/controllers/purchase_windows_controller.rb')
| -rw-r--r-- | app/controllers/purchase_windows_controller.rb | 58 | 
1 files changed, 58 insertions, 0 deletions
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  | 
