diff options
| author | Zog | 2018-05-21 10:24:03 +0200 | 
|---|---|---|
| committer | Zog | 2018-05-21 10:24:03 +0200 | 
| commit | d3f131476c29e92dc3451ea6099d09149ab8658d (patch) | |
| tree | 8e37f8b81e11cadf9e53a9d9c6d5624b51f32695 | |
| parent | 878cc43fb836beb2b761026237ab545ab6da2e05 (diff) | |
| download | chouette-core-d3f131476c29e92dc3451ea6099d09149ab8658d.tar.bz2 | |
Refs #7101; Update ComplianceControlSets index view
| -rw-r--r-- | app/controllers/compliance_control_sets_controller.rb | 6 | ||||
| -rw-r--r-- | app/models/compliance_control_set.rb | 23 | ||||
| -rw-r--r-- | app/models/workgroup.rb | 30 | ||||
| -rw-r--r-- | app/views/compliance_control_sets/_filters.html.slim | 4 | ||||
| -rw-r--r-- | app/views/compliance_control_sets/index.html.slim | 18 | ||||
| -rw-r--r-- | app/views/layouts/navigation/_main_nav_left_content_stif.html.slim | 6 | ||||
| -rw-r--r-- | config/locales/compliance_check_sets.fr.yml | 2 | ||||
| -rw-r--r-- | config/locales/compliance_control_sets.fr.yml | 2 | ||||
| -rw-r--r-- | config/locales/layouts.fr.yml | 1 | ||||
| -rw-r--r-- | spec/controllers/compliance_control_sets_controller_spec.rb | 139 | 
10 files changed, 156 insertions, 75 deletions
| diff --git a/app/controllers/compliance_control_sets_controller.rb b/app/controllers/compliance_control_sets_controller.rb index 6461b38c8..5835bfb22 100644 --- a/app/controllers/compliance_control_sets_controller.rb +++ b/app/controllers/compliance_control_sets_controller.rb @@ -29,8 +29,9 @@ class ComplianceControlSetsController < ChouetteController    protected -  def begin_of_association_chain -    current_organisation +  def end_of_association_chain +    organisation_ids = [current_organisation.id] + current_organisation.workbenches.map{|w| w.workgroup.owner_id}.uniq +    ComplianceControlSet.where(organisation_id: organisation_ids)    end    private @@ -38,6 +39,7 @@ class ComplianceControlSetsController < ChouetteController    def collection      @compliance_control_sets ||= begin        scope = end_of_association_chain.all +      scope = scope.assigned_to_slots(current_organisation, params[:q].try(:[], :assigned_to_slots))        scope = self.ransack_period_range(scope: scope, error_message: t('imports.filters.error_period_filter'), query: :where_updated_at_between)        @q_for_form = scope.ransack(params[:q])        compliance_control_sets = @q_for_form.result diff --git a/app/models/compliance_control_set.rb b/app/models/compliance_control_set.rb index 4f0f86d08..365e813e4 100644 --- a/app/models/compliance_control_set.rb +++ b/app/models/compliance_control_set.rb @@ -11,4 +11,27 @@ class ComplianceControlSet < ApplicationModel    scope :where_updated_at_between, ->(period_range) do      where('updated_at BETWEEN :begin AND :end', begin: period_range.begin, end: period_range.end)    end + +  scope :assigned_to_slots, ->(organisation, slots) do +    if !slots.present? || slots.all?(&:empty?) +      all +    else +      ids = self.pluck(:id) +      kept_ids = [] +      organisation.workbenches.select do |w| +        kept_ids += (w.owner_compliance_control_set_ids||{}).values_at(*slots).flatten +      end +      ids = ids & kept_ids.uniq.map(&:to_i).compact +      self.where(id: ids) +    end +  end + +  def assignments current_user +    out = [] +    current_user.organisation.workbenches.each do |workbench| +      vals = workbench.owner_compliance_control_set_ids.select{|k, v| v == self.id.to_s}.keys +      out += Workgroup.send(:compliance_control_sets_labels, vals).values +    end +    out +  end  end diff --git a/app/models/workgroup.rb b/app/models/workgroup.rb index 6bb03c7fa..cb97085dd 100644 --- a/app/models/workgroup.rb +++ b/app/models/workgroup.rb @@ -27,7 +27,7 @@ class Workgroup < ApplicationModel      export_types.include? export_name    end -  def all_compliance_control_sets +  def self.all_compliance_control_sets      %i(after_import        after_import_by_workgroup        before_merge @@ -38,20 +38,40 @@ class Workgroup < ApplicationModel      )    end -  def compliance_control_sets_by_workgroup +  def self.all_compliance_control_sets_labels +    compliance_control_sets_labels all_compliance_control_sets +  end + +  def self.compliance_control_sets_by_workgroup      compliance_control_sets_labels all_compliance_control_sets.grep(/by_workgroup$/)    end -  def compliance_control_sets_by_workbench +  def self.compliance_control_sets_by_workbench      compliance_control_sets_labels all_compliance_control_sets.grep_v(/by_workgroup$/)    end -  def import_compliance_control_sets +  def self.import_compliance_control_sets      compliance_control_sets_labels all_compliance_control_sets.grep(/^after_import/)    end +  def all_compliance_control_sets_labels +    self.class.all_compliance_control_sets_labels +  end + +  def compliance_control_sets_by_workgroup +    self.class.compliance_control_sets_by_workgroup +  end + +  def compliance_control_sets_by_workbench +    self.class.compliance_control_sets_by_workbench +  end + +  def import_compliance_control_sets +    self.class.import_compliance_control_sets +  end +    private -  def compliance_control_sets_labels(keys) +  def self.compliance_control_sets_labels(keys)      keys.inject({}) do |h, k|        h[k] = "workgroups.compliance_control_sets.#{k}".t.capitalize        h diff --git a/app/views/compliance_control_sets/_filters.html.slim b/app/views/compliance_control_sets/_filters.html.slim index 5f6d9e27b..b9bfed2be 100644 --- a/app/views/compliance_control_sets/_filters.html.slim +++ b/app/views/compliance_control_sets/_filters.html.slim @@ -17,6 +17,10 @@            = p.input :start_date, as: :date, label: false, 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: false, wrapper_html: {class: 'date smart_date filter_menu-item'}, default: @end_range, include_blank: @end_range ? false : true +    .form-group.togglable class=filter_item_class(params[:q], :assigned_to_slots) +      = f.label ComplianceControlSet.tmf(:assigned_to), required: false, class: 'control-label' +      = f.input :assigned_to_slots, collection: Workgroup.compliance_control_sets_by_workgroup.update(Workgroup.compliance_control_sets_by_workbench).to_a, as: :check_boxes, value_method: :first, label: false, label_method: lambda {|w| ("<span>#{w.last}</span>").html_safe}, required: false, wrapper_html: {class: 'checkbox_list'}, checked: params[:q].try(:[], :assigned_to_slots) +    .actions      = link_to t('actions.erase'), @compliance_control_set, class: 'btn btn-link'      = f.submit t('actions.filter'), class: 'btn btn-default', id: 'compliance_control_set_filter_btn' diff --git a/app/views/compliance_control_sets/index.html.slim b/app/views/compliance_control_sets/index.html.slim index 144a4e5b9..f143682fd 100644 --- a/app/views/compliance_control_sets/index.html.slim +++ b/app/views/compliance_control_sets/index.html.slim @@ -14,27 +14,33 @@                    key: :name, \                    attribute: 'name', \                    link_to: lambda do |compliance_control_set| \ -                      compliance_control_set_path(compliance_control_set) \ -                    end \ +                    compliance_control_set_path(compliance_control_set) \ +                  end, \ +                  sortable: true,\                  ), \                  TableBuilderHelper::Column.new( \                    key: :assigned_to, \ -                  attribute: 'assignment' \ +                  attribute: Proc.new {|n| \ +                    n.assignments(current_user).to_sentence\ +                  }, \ +                  sortable: false,\                  ), \                  TableBuilderHelper::Column.new( \                    key: :owner_jdc, \ -                  attribute: Proc.new {|n| n.organisation.name } \ +                  attribute: Proc.new {|n| n.organisation.name }, \ +                  sortable: true,\                  ), \                  TableBuilderHelper::Column.new( \                    key: :control_numbers, \ -                  attribute: Proc.new {|n| n.compliance_controls.count }\ +                  attribute: Proc.new {|n| n.compliance_controls.count },\ +                  sortable: true,\                  ), \                  TableBuilderHelper::Column.new( \                    key: :updated_at, \                    attribute: Proc.new { |n| l(n.updated_at, format: :long) if n.updated_at }, \ +                  sortable: true,\                  ) \                ], -              sortable: true,                cls: 'table has-filter has-search'            = new_pagination @compliance_control_sets, 'pull-right' diff --git a/app/views/layouts/navigation/_main_nav_left_content_stif.html.slim b/app/views/layouts/navigation/_main_nav_left_content_stif.html.slim index 7bea0814e..182bf057e 100644 --- a/app/views/layouts/navigation/_main_nav_left_content_stif.html.slim +++ b/app/views/layouts/navigation/_main_nav_left_content_stif.html.slim @@ -14,6 +14,9 @@              span = t('layouts.navbar.workbench_outputs.organisation')            = link_to '#', class: 'list-group-item disabled' do              span = t('layouts.navbar.workbench_outputs.workgroup') +          - if policy(workbench.workgroup).edit? +            = link_to [:edit, workbench.workgroup], class: 'list-group-item' do +              span = t('layouts.navbar.workbench_outputs.edit_workgroup')      .menu-item.panel        .panel-heading @@ -35,6 +38,9 @@              span = t('activerecord.models.compliance_check_set.other').capitalize            = link_to compliance_control_sets_path, class: 'list-group-item' do              span = t('activerecord.models.compliance_control_set.other').capitalize +          - if policy(workbench).edit? +            = link_to [:edit, workbench], class: 'list-group-item' do +              span = t('workbenches.edit.link')      .menu-item.panel        .panel-heading diff --git a/config/locales/compliance_check_sets.fr.yml b/config/locales/compliance_check_sets.fr.yml index be54effde..f3c5037ab 100644 --- a/config/locales/compliance_check_sets.fr.yml +++ b/config/locales/compliance_check_sets.fr.yml @@ -35,7 +35,7 @@ fr:          ref: Ref          creation_date: Date et heure de création          associated_object: Objet associé -        assigned_to: Affectation +        assigned_to: Affectations          compliance_control_set: Jeu de contrôle exécuté          name: Nom        compliance_check_resource: diff --git a/config/locales/compliance_control_sets.fr.yml b/config/locales/compliance_control_sets.fr.yml index 19f6f08ee..b3736f5ea 100644 --- a/config/locales/compliance_control_sets.fr.yml +++ b/config/locales/compliance_control_sets.fr.yml @@ -32,7 +32,7 @@ fr:      attributes:        compliance_control_set:          name: Nom -        assigned_to: Affectation +        assigned_to: Affectations          owner_jdc: Propriétaire du jeu de contrôle          control_numbers: Nb contrôle          updated_at: Mis a jour diff --git a/config/locales/layouts.fr.yml b/config/locales/layouts.fr.yml index 8e739b6c7..52b3c2466 100644 --- a/config/locales/layouts.fr.yml +++ b/config/locales/layouts.fr.yml @@ -23,6 +23,7 @@ fr:        workbench_outputs:          organisation: Offre de mon organisation          workgroup: Offre du groupe de travail +        edit_workgroup: Configurer l'application        configuration: Paramétrages        workgroup_configuration:  Application        workbench_configuration: Espace de travail diff --git a/spec/controllers/compliance_control_sets_controller_spec.rb b/spec/controllers/compliance_control_sets_controller_spec.rb index 3809d7644..848b76b7f 100644 --- a/spec/controllers/compliance_control_sets_controller_spec.rb +++ b/spec/controllers/compliance_control_sets_controller_spec.rb @@ -1,60 +1,79 @@ -# require 'rails_helper' -# -# RSpec.describe ComplianceControlSetsController, type: :controller do -#   login_user -# -#   let(:compliance_control_set) { create :compliance_control_set } -# -#   describe "GET show" do -#     it 'should be successful' do -#       get :show, id: compliance_control_set.id -#       expect(response).to be_success -#     end -#   end -# -#   describe "GET index" do -#     it 'should be successful' do -#       get :index, id: compliance_control_set.id -#       expect(response).to be_success -#     end -#   end -# -#   describe "GET #edit" do -#     it 'should be successful' do -#       get :edit, id: compliance_control_set.id -#       expect(response).to be_success -#     end -#   end -# -#   describe 'GET #new' do -#     it 'should be successful' do -#       get :new, id: compliance_control_set.id -#       expect(response).to be_success -#     end -#   end -# -#   describe 'POST #create' do -#     it 'should be successful' do -#       post :create, compliance_control_set: build(:compliance_control_set).as_json -#       expect(response).to have_http_status(302) -#       # expect(flash[:notice]).to eq(I18n.t('notice.compliance_control.created')) -#     end -#   end -# -#   describe 'POST #update' do -#     it 'should be successful' do -#       post :update, id: compliance_control_set.id, compliance_control_set: compliance_control_set.as_json -#       expect(response).to redirect_to compliance_control_set_path(compliance_control_set) -#       # expect(flash[:notice]).to eq(I18n.t('notice.compliance_control.updated')) -#     end -#   end -# -#   describe 'DELETE #destroy' do -#     it 'should be successful' do -#       delete :destroy, id: compliance_control_set.id -#       # expect(flash[:notice]).to eq I18n.t('notice.compliance_control.destroyed') -#     end -#   end -# -# -# end +require 'rails_helper' + +RSpec.describe ComplianceControlSetsController, type: :controller do +  login_user + +  let(:owner){ create :organisation } +  let(:workgroup){create :workgroup, owner: owner} +  let(:other_organisation){ +    o  = create :organisation +    @owner_workbench = create :workbench, workgroup: workgroup, organisation: o +    o +  } + +  before do +    @workbench = create :workbench, workgroup: workgroup, organisation: @user.organisation +  end + +  let!(:same_organisation_cc_set){ create :compliance_control_set, organisation: @user.organisation } +  let!(:different_organisation_cc_set){ create :compliance_control_set, organisation: other_organisation } +  let!(:workgroups_owner_cc_set){ create :compliance_control_set, organisation: owner } + +  describe "GET index" do +    it 'should be successful' do +      get :index +      expect(response).to be_success +      expect(assigns(:compliance_control_sets)).to include(same_organisation_cc_set) +      expect(assigns(:compliance_control_sets)).to include(workgroups_owner_cc_set) +      expect(assigns(:compliance_control_sets)).to_not include(different_organisation_cc_set) +    end + +    context "with filters" do +      let(:assigned_to_slots){ [""] } + +      it "should filter the output" do +        get :index, q: {assigned_to_slots: assigned_to_slots} +        expect(response).to be_success +        expect(assigns(:compliance_control_sets)).to include(same_organisation_cc_set) +        expect(assigns(:compliance_control_sets)).to include(workgroups_owner_cc_set) +        expect(assigns(:compliance_control_sets)).to_not include(different_organisation_cc_set) +      end + +      context "when filtering on one assigned slots" do +        let(:assigned_to_slots){ ["after_import_by_workgroup", ""] } +        before do +          @workbench.update owner_compliance_control_set_ids: {after_import_by_workgroup: same_organisation_cc_set.id} +        end + +        it "should filter the output" do +          get :index, q: {assigned_to_slots: assigned_to_slots} +          expect(response).to be_success +          p same_organisation_cc_set +          expect(assigns(:compliance_control_sets)).to include(same_organisation_cc_set) +          expect(assigns(:compliance_control_sets)).to_not include(workgroups_owner_cc_set) +          expect(assigns(:compliance_control_sets)).to_not include(different_organisation_cc_set) +        end +      end + +      context "when filtering on multiple assigned slots" do +        let(:assigned_to_slots){ ["after_import_by_workgroup", "before_merge", ""] } +        let!(:other_cc_set){ +          create :compliance_control_set, organisation: @user.organisation +        } +        before do +          @workbench.update owner_compliance_control_set_ids: {after_import_by_workgroup: same_organisation_cc_set.id, before_merge: other_cc_set.id} +          @owner_workbench.update owner_compliance_control_set_ids: {before_merge: workgroups_owner_cc_set.id} +        end + +        it "should filter the output" do +          get :index, q: {assigned_to_slots: assigned_to_slots} +          expect(response).to be_success +          expect(assigns(:compliance_control_sets).object).to include(same_organisation_cc_set) +          expect(assigns(:compliance_control_sets).object).to include(other_cc_set) +          expect(assigns(:compliance_control_sets).object).to_not include(workgroups_owner_cc_set) +          expect(assigns(:compliance_control_sets).object).to_not include(different_organisation_cc_set) +        end +      end +    end +  end +end | 
