diff options
Diffstat (limited to 'app/controllers/calendars_controller.rb')
| -rw-r--r-- | app/controllers/calendars_controller.rb | 75 | 
1 files changed, 62 insertions, 13 deletions
| diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 2ed10a111..75d4cbd09 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -1,25 +1,69 @@  class CalendarsController < ChouetteController    include PolicyChecker +  include TimeTablesHelper +    defaults resource_class: Calendar    before_action :ransack_contains_date, only: [:index]    respond_to :html +  respond_to :json, only: :show    respond_to :js, only: :index +  belongs_to :workgroup +    def index      index! do -      @calendars = ModelDecorator.decorate(@calendars, with: CalendarDecorator) +      @calendars = decorate_calendars(@calendars)      end    end    def show      show! do -      @calendar = @calendar.decorate +      @year = params[:year] ? params[:year].to_i : Date.today.cwyear +      @calendar = @calendar.decorate(context: { +        workgroup: workgroup +      }) +    end +  end + +  def month +    @date = params['date'] ? Date.parse(params['date']) : Date.today +    @calendar = resource +  end + +  def create +    create! do +      if @calendar.valid? && has_feature?('application_days_on_calendars') +        redirect_to([:edit, @calendar]) +        return +      end +    end +  end + +  def update +    if params[:calendar] +      super +    else +      state  = JSON.parse request.raw_post +      resource.state_update state +      respond_to do |format| +        format.json { render json: state, status: state['errors'] ? :unprocessable_entity : :ok } +      end      end    end    private + +  def decorate_calendars(calendars) +    CalendarDecorator.decorate( +      calendars, +      context: { +        workgroup: workgroup +      } +    ) +  end +    def calendar_params -    permitted_params = [:id, :name, :short_name, periods_attributes: [:id, :begin, :end, :_destroy], date_values_attributes: [:id, :value, :_destroy]] +    permitted_params = [:id, :name, :short_name, :shared, periods_attributes: [:id, :begin, :end, :_destroy], date_values_attributes: [:id, :value, :_destroy]]      permitted_params << :shared if policy(Calendar).share?      params.require(:calendar).permit(*permitted_params)    end @@ -33,25 +77,30 @@ class CalendarsController < ChouetteController    end    protected + +  alias_method :workgroup, :parent +  helper_method :workgroup +    def resource -    @calendar = Calendar.where('organisation_id = ? OR shared = true', current_organisation.id).find_by_id(params[:id]) +    @calendar ||= workgroup.calendars.where('(organisation_id = ? OR shared = ?)', current_organisation.id, true).find_by_id(params[:id])    end    def build_resource      super.tap do |calendar| +      calendar.workgroup = workgroup        calendar.organisation = current_organisation      end    end    def collection -    return @calendars if @calendars -    scope = Calendar.where('organisation_id = ? OR shared = ?', current_organisation.id, true) -    scope = shared_scope(scope) -    @q = scope.ransack(params[:q]) - -    calendars = @q.result -    calendars = calendars.order(sort_column + ' ' + sort_direction) if sort_column && sort_direction -    @calendars = calendars.paginate(page: params[:page]) +    @calendars ||= begin +      scope = workgroup.calendars.where('(organisation_id = ? OR shared = ?)', current_organisation.id, true) +      scope = shared_scope(scope) +      @q = scope.ransack(params[:q]) +      calendars = @q.result +      calendars = calendars.order(sort_column + ' ' + sort_direction) if sort_column && sort_direction +      calendars = calendars.paginate(page: params[:page]) +    end    end    def ransack_contains_date @@ -76,4 +125,4 @@ class CalendarsController < ChouetteController      scope    end -end +end
\ No newline at end of file | 
