diff options
| author | cedricnjanga | 2018-01-30 23:07:07 -0800 | 
|---|---|---|
| committer | cedricnjanga | 2018-01-30 23:07:07 -0800 | 
| commit | 4023ea52097a47458ac2fcad36d343aba0c8e68b (patch) | |
| tree | e891b85392aa911bb43d1b0ef73f526226b18216 | |
| parent | 7ec19d8dc153e1491cb1a693b11b3b0a351f316c (diff) | |
| download | chouette-core-4023ea52097a47458ac2fcad36d343aba0c8e68b.tar.bz2 | |
Make some changes to avoid unnacessaty lines of code
| -rw-r--r-- | app/controllers/calendars_controller.rb | 33 | ||||
| -rw-r--r-- | app/views/calendars/index.html.slim | 4 | ||||
| -rw-r--r-- | spec/factories/calendars.rb | 1 | ||||
| -rw-r--r-- | spec/features/calendars_permissions_spec.rb | 7 | 
4 files changed, 26 insertions, 19 deletions
| diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 27c944f5f..193680342 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -1,10 +1,11 @@  class CalendarsController < ChouetteController -  include WorkgroupSupport    include PolicyChecker    defaults resource_class: Calendar    before_action :ransack_contains_date, only: [:index]    respond_to :html    respond_to :js, only: :index +   +  belongs_to :workgroup    def index      index! do @@ -15,7 +16,7 @@ class CalendarsController < ChouetteController    def show      show! do        @calendar = @calendar.decorate(context: { -        workgroup: current_workgroup +        workgroup: workgroup        })      end    end @@ -27,7 +28,7 @@ class CalendarsController < ChouetteController        calendars,        with: CalendarDecorator,        context: { -        workgroup: current_workgroup +        workgroup: workgroup        }      )    end @@ -47,26 +48,30 @@ class CalendarsController < ChouetteController    end    protected + +  alias_method :workgroup, :parent +  helper_method :workgroup +    def resource -    @calendar = Calendar.where('(organisation_id = ? OR shared = ?) AND workgroup_id = ?', current_organisation.id).find_by_id(params[:id], true, @workgroup.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 = current_workgroup +      calendar.workgroup = workgroup        calendar.organisation = current_organisation      end    end    def collection -    return @calendars if @calendars -    scope = Calendar.where('(organisation_id = ? OR shared = ?) AND workgroup_id = ?', current_organisation.id, true, @workgroup.id) -    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 @@ -91,4 +96,4 @@ class CalendarsController < ChouetteController      scope    end -end +end
\ No newline at end of file diff --git a/app/views/calendars/index.html.slim b/app/views/calendars/index.html.slim index 309f0e6f9..0b58c0c72 100644 --- a/app/views/calendars/index.html.slim +++ b/app/views/calendars/index.html.slim @@ -1,4 +1,4 @@ -- breadcrumb :calendars, current_workgroup +- breadcrumb :calendars, workgroup  .page_content    .container-fluid @@ -16,7 +16,7 @@                  key: :name, \                  attribute: 'name', \                  link_to: lambda do |calendar| \ -                  workgroup_calendar_path(current_workgroup, calendar) \ +                  workgroup_calendar_path(workgroup, calendar) \                  end \                ), \                TableBuilderHelper::Column.new( \ diff --git a/spec/factories/calendars.rb b/spec/factories/calendars.rb index 5f3188bee..d9fd242d1 100644 --- a/spec/factories/calendars.rb +++ b/spec/factories/calendars.rb @@ -6,6 +6,7 @@ FactoryGirl.define do      sequence(:dates) { |n| [ Date.yesterday - n, Date.yesterday - 2*n ] }      shared false      organisation +    workgroup    end    sequence :date_range do |n| diff --git a/spec/features/calendars_permissions_spec.rb b/spec/features/calendars_permissions_spec.rb index 9b47ab2bb..4857592d5 100644 --- a/spec/features/calendars_permissions_spec.rb +++ b/spec/features/calendars_permissions_spec.rb @@ -2,6 +2,7 @@ RSpec.describe 'Calendars', type: :feature do    login_user    let(:calendar) { create :calendar, organisation_id: 1 } +  let(:workgroup) { calendar.workgroup }    describe 'permissions' do      before do @@ -13,7 +14,7 @@ RSpec.describe 'Calendars', type: :feature do      end      context 'on show view' do -      let( :path ){ calendar_path(calendar) } +      let( :path ){ workgroup_calendar_path(workgroup, calendar) }        context 'if present → ' do          let( :permission ){ true } @@ -33,7 +34,7 @@ RSpec.describe 'Calendars', type: :feature do      end      context 'on edit view' do -      let( :path ){ edit_calendar_path(calendar) } +      let( :path ){ edit_workgroup_calendar_path(workgroup, calendar) }        context 'if present → ' do          let( :permission ){ true } @@ -51,7 +52,7 @@ RSpec.describe 'Calendars', type: :feature do      end      context 'on index view' do -      let( :path ){ calendars_path } +      let( :path ){ workgroup_calendars_path(workgroup) }        context 'if present → ' do          let( :permission ){ true } | 
