diff options
| author | Robert | 2018-01-09 12:24:23 +0100 | 
|---|---|---|
| committer | Robert | 2018-01-09 12:24:36 +0100 | 
| commit | 0e91d206fd8278d0c43c34777f4a585481882b70 (patch) | |
| tree | 9ef641b3455de5d6da2c22ffb702661e4e69ff8d | |
| parent | 7ea0a6079017c561aa8286198bd77b258cb2d240 (diff) | |
| download | chouette-core-0e91d206fd8278d0c43c34777f4a585481882b70.tar.bz2 | |
Refs: #5413@0.5h; Refactoring of #archived_or_finalised?
| -rw-r--r-- | DEVNOTES.md | 2 | ||||
| -rw-r--r-- | app/helpers/newapplication_helper.rb | 2 | ||||
| -rw-r--r-- | app/models/referential.rb | 9 | ||||
| -rw-r--r-- | app/policies/access_link_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/access_point_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/application_policy.rb | 17 | ||||
| -rw-r--r-- | app/policies/connection_link_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/journey_pattern_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/line_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/purchase_window_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/referential_policy.rb | 8 | ||||
| -rw-r--r-- | app/policies/route_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/routing_constraint_zone_policy.rb | 6 | ||||
| -rw-r--r-- | app/policies/time_table_combination_policy.rb | 2 | ||||
| -rw-r--r-- | app/policies/time_table_policy.rb | 10 | ||||
| -rw-r--r-- | app/policies/vehicle_journey_policy.rb | 6 | ||||
| -rw-r--r-- | app/views/referentials/show.html.slim | 6 | ||||
| -rw-r--r-- | app/views/workbenches/show.html.slim | 2 | ||||
| -rw-r--r-- | spec/models/referential_spec.rb | 38 | 
19 files changed, 82 insertions, 68 deletions
| diff --git a/DEVNOTES.md b/DEVNOTES.md index 17a198446..bcdd37f5e 100644 --- a/DEVNOTES.md +++ b/DEVNOTES.md @@ -37,7 +37,7 @@ They are overriden as follows  ```ruby        def <destructive>? -        !archived_or_finalised? && organisation_match? && user.has_permission('<resource in plural form>.<action>') +        !referential_read_only? && organisation_match? && user.has_permission('<resource in plural form>.<action>')        end  ``` diff --git a/app/helpers/newapplication_helper.rb b/app/helpers/newapplication_helper.rb index c1a1c189e..4ca69560b 100644 --- a/app/helpers/newapplication_helper.rb +++ b/app/helpers/newapplication_helper.rb @@ -147,7 +147,7 @@ module NewapplicationHelper              content_tag :li, link_to(t("actions.#{action}"), polymorph_url)            end          elsif action == :archive -          unless item.archived_or_finalised? +          unless item.referntial_read_only?              content_tag :li, link_to(t("actions.#{action}"), polymorph_url, method: :put)            end          elsif action == :unarchive diff --git a/app/models/referential.rb b/app/models/referential.rb index 75c1889ca..73d29fee4 100644 --- a/app/models/referential.rb +++ b/app/models/referential.rb @@ -252,6 +252,10 @@ class Referential < ActiveRecord::Base    before_destroy :destroy_schema    before_destroy :destroy_jobs +  def referential_read_only? +    in_referential_suite? || archived_at +  end +    def in_referential_suite?      referential_suite_id.present?    end @@ -422,11 +426,6 @@ class Referential < ActiveRecord::Base      GeoRuby::SimpleFeatures::Geometry.from_ewkt(bounds.present? ? bounds : default_bounds ).envelope    end -  # For Delegator -  def archived_or_finalised? -    archived_at || in_referential_suite? -  end -    # Archive    def archive! diff --git a/app/policies/access_link_policy.rb b/app/policies/access_link_policy.rb index c9a48d61f..f2ea7027f 100644 --- a/app/policies/access_link_policy.rb +++ b/app/policies/access_link_policy.rb @@ -6,14 +6,14 @@ class AccessLinkPolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('access_links.create') +    !referential_read_only? && organisation_match? && user.has_permission?('access_links.create')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('access_links.update') +    !referential_read_only? && organisation_match? && user.has_permission?('access_links.update')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('access_links.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('access_links.destroy')    end  end diff --git a/app/policies/access_point_policy.rb b/app/policies/access_point_policy.rb index 469ed9739..4fa887b9e 100644 --- a/app/policies/access_point_policy.rb +++ b/app/policies/access_point_policy.rb @@ -6,14 +6,14 @@ class AccessPointPolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('access_points.create') +    !referential_read_only? && organisation_match? && user.has_permission?('access_points.create')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('access_points.update') +    !referential_read_only? && organisation_match? && user.has_permission?('access_points.update')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('access_points.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('access_points.destroy')    end  end diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 7b4d1b0c0..c44937c9e 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -81,16 +81,11 @@ class ApplicationPolicy      @is_archived = is_archived    end -  def finalised? -    return @is_finalised if instance_variable_defined?(:@is_finalised) -    @is_finalised = is_finalised +  def referential_read_only? +    return @is_referential_read_only if instance_variable_defined?(:@is_referential_read_only) +    @is_referential_read_only = is_referential_read_only    end -  def archived_or_finalised? -    archived? || finalised? -  end - -    def organisation_match?      user.organisation_id == organisation_id    end @@ -135,12 +130,12 @@ class ApplicationPolicy      end    end -  def is_finalised +  def is_referential_read_only      !!case referential      when Referential -      referential.in_referential_suite? +      referential.referential_read_only?      else -      current_referential.try(:in_referential_suite?) +      current_referential.try(:referential_read_only?)      end    end  end diff --git a/app/policies/connection_link_policy.rb b/app/policies/connection_link_policy.rb index 5a8e41ee8..9bab5e4db 100644 --- a/app/policies/connection_link_policy.rb +++ b/app/policies/connection_link_policy.rb @@ -6,14 +6,14 @@ class ConnectionLinkPolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('connection_links.create') +    !referential_read_only? && organisation_match? && user.has_permission?('connection_links.create')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('connection_links.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('connection_links.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('connection_links.update') +    !referential_read_only? && organisation_match? && user.has_permission?('connection_links.update')    end  end diff --git a/app/policies/journey_pattern_policy.rb b/app/policies/journey_pattern_policy.rb index 57220033d..beb18d151 100644 --- a/app/policies/journey_pattern_policy.rb +++ b/app/policies/journey_pattern_policy.rb @@ -7,14 +7,14 @@ class JourneyPatternPolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('journey_patterns.create') +    !referential_read_only? && organisation_match? && user.has_permission?('journey_patterns.create')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('journey_patterns.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('journey_patterns.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('journey_patterns.update') +    !referential_read_only? && organisation_match? && user.has_permission?('journey_patterns.update')    end  end diff --git a/app/policies/line_policy.rb b/app/policies/line_policy.rb index 8028a063e..f7b03b0b5 100644 --- a/app/policies/line_policy.rb +++ b/app/policies/line_policy.rb @@ -26,15 +26,15 @@ class LinePolicy < ApplicationPolicy    end    def create_footnote? -    !archived_or_finalised? && organisation_match? && user.has_permission?('footnotes.create') +    !referential_read_only? && organisation_match? && user.has_permission?('footnotes.create')    end    def edit_footnote? -    !archived_or_finalised? && organisation_match? && user.has_permission?('footnotes.update') +    !referential_read_only? && organisation_match? && user.has_permission?('footnotes.update')    end    def destroy_footnote? -    !archived_or_finalised? && organisation_match? && user.has_permission?('footnotes.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('footnotes.destroy')    end    def update_footnote?  ; edit_footnote? end diff --git a/app/policies/purchase_window_policy.rb b/app/policies/purchase_window_policy.rb index 87cde4a7a..eb3b04bf7 100644 --- a/app/policies/purchase_window_policy.rb +++ b/app/policies/purchase_window_policy.rb @@ -6,15 +6,15 @@ class PurchaseWindowPolicy < ApplicationPolicy    end    def create? -     !archived_or_finalised? && organisation_match? && user.has_permission?('purchase_windows.create') +     !referential_read_only? && organisation_match? && user.has_permission?('purchase_windows.create')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('purchase_windows.update') +    !referential_read_only? && organisation_match? && user.has_permission?('purchase_windows.update')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('purchase_windows.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('purchase_windows.destroy')    end  end diff --git a/app/policies/referential_policy.rb b/app/policies/referential_policy.rb index ce4956460..1fce6b2c7 100644 --- a/app/policies/referential_policy.rb +++ b/app/policies/referential_policy.rb @@ -10,19 +10,19 @@ class ReferentialPolicy < ApplicationPolicy    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('referentials.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('referentials.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('referentials.update') +    !referential_read_only? && organisation_match? && user.has_permission?('referentials.update')    end    def clone? -    !archived_or_finalised? && create? +    !referential_read_only? && create?    end    def validate? -    !archived_or_finalised? && create? && organisation_match? +    !referential_read_only? && create? && organisation_match?    end    def archive? diff --git a/app/policies/route_policy.rb b/app/policies/route_policy.rb index 3e1d46c97..0337a5300 100644 --- a/app/policies/route_policy.rb +++ b/app/policies/route_policy.rb @@ -6,15 +6,15 @@ class RoutePolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('routes.create') +    !referential_read_only? && organisation_match? && user.has_permission?('routes.create')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('routes.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('routes.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('routes.update') +    !referential_read_only? && organisation_match? && user.has_permission?('routes.update')    end    def duplicate? diff --git a/app/policies/routing_constraint_zone_policy.rb b/app/policies/routing_constraint_zone_policy.rb index a903e3728..fd8081bef 100644 --- a/app/policies/routing_constraint_zone_policy.rb +++ b/app/policies/routing_constraint_zone_policy.rb @@ -6,14 +6,14 @@ class RoutingConstraintZonePolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('routing_constraint_zones.create') +    !referential_read_only? && organisation_match? && user.has_permission?('routing_constraint_zones.create')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('routing_constraint_zones.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('routing_constraint_zones.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('routing_constraint_zones.update') +    !referential_read_only? && organisation_match? && user.has_permission?('routing_constraint_zones.update')    end  end diff --git a/app/policies/time_table_combination_policy.rb b/app/policies/time_table_combination_policy.rb index 25ac9df67..bba458c18 100644 --- a/app/policies/time_table_combination_policy.rb +++ b/app/policies/time_table_combination_policy.rb @@ -7,6 +7,6 @@ class TimeTableCombinationPolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('time_tables.update') +    !referential_read_only? && organisation_match? && user.has_permission?('time_tables.update')    end  end diff --git a/app/policies/time_table_policy.rb b/app/policies/time_table_policy.rb index 1f5a7a259..390c170c7 100644 --- a/app/policies/time_table_policy.rb +++ b/app/policies/time_table_policy.rb @@ -7,23 +7,23 @@ class TimeTablePolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('time_tables.create') +    !referential_read_only? && organisation_match? && user.has_permission?('time_tables.create')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('time_tables.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('time_tables.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('time_tables.update') +    !referential_read_only? && organisation_match? && user.has_permission?('time_tables.update')    end    def actualize? -    !archived_or_finalised? && organisation_match? && edit? +    !referential_read_only? && organisation_match? && edit?    end    def duplicate? -    !archived_or_finalised? && organisation_match? && create? +    !referential_read_only? && organisation_match? && create?    end    def month? diff --git a/app/policies/vehicle_journey_policy.rb b/app/policies/vehicle_journey_policy.rb index a7e0dfe03..adbc5fd89 100644 --- a/app/policies/vehicle_journey_policy.rb +++ b/app/policies/vehicle_journey_policy.rb @@ -6,14 +6,14 @@ class VehicleJourneyPolicy < ApplicationPolicy    end    def create? -    !archived_or_finalised? && organisation_match? && user.has_permission?('vehicle_journeys.create') +    !referential_read_only? && organisation_match? && user.has_permission?('vehicle_journeys.create')    end    def destroy? -    !archived_or_finalised? && organisation_match? && user.has_permission?('vehicle_journeys.destroy') +    !referential_read_only? && organisation_match? && user.has_permission?('vehicle_journeys.destroy')    end    def update? -    !archived_or_finalised? && organisation_match? && user.has_permission?('vehicle_journeys.update') +    !referential_read_only? && organisation_match? && user.has_permission?('vehicle_journeys.update')    end  end diff --git a/app/views/referentials/show.html.slim b/app/views/referentials/show.html.slim index 889ea7ad1..51041198c 100644 --- a/app/views/referentials/show.html.slim +++ b/app/views/referentials/show.html.slim @@ -1,7 +1,7 @@  - breadcrumb @referential  - page_header_content_for @referential  - content_for :page_header_actions do -  - unless (@referential.archived_or_finalised? || !policy(@referential).edit?) +  - unless (@referential.referential_read_only? || !policy(@referential).edit?)      = link_to(t('actions.edit'), edit_referential_path(@referential), class: 'btn btn-default')  - content_for :page_header_content do @@ -22,7 +22,7 @@      .row        .col-lg-6.col-md-6.col-sm-12.col-xs-12          = definition_list t('metadatas'), -          { t('activerecord.attributes.referential.status') => @referential.archived_or_finalised? ? "<div class='td-block'><span class='fa fa-archive'></span><span>#{t('activerecord.attributes.referential.archived_at')}</span></div>".html_safe : "<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>#{t('activerecord.attributes.referential.archived_at_null')}</span></div>".html_safe, +          { t('activerecord.attributes.referential.status') => @referential.referential_read_only? ? "<div class='td-block'><span class='fa fa-archive'></span><span>#{t('activerecord.attributes.referential.archived_at')}</span></div>".html_safe : "<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>#{t('activerecord.attributes.referential.archived_at_null')}</span></div>".html_safe,              @referential.human_attribute_name(:validity_period) => (@referential.validity_period.present? ? t('validity_range', debut: l(@referential.try(:validity_period).try(:begin), format: :short), end: l(@referential.try(:validity_period).try(:end), format: :short)) : '-'),              @referential.human_attribute_name(:organisation) => @referential.organisation.name,              @referential.human_attribute_name(:published_at) => '-' } @@ -102,5 +102,5 @@      .modal-footer        button.btn.btn-link type='button' data-dismiss='modal' #{t('cancel')} -      - unless policy(@referential).archived_or_finalised? +      - unless policy(@referential).referential_read_only?          = f.button :submit, t('actions.clean_up') , class: 'btn btn-primary' diff --git a/app/views/workbenches/show.html.slim b/app/views/workbenches/show.html.slim index 79f180e4d..17ad75051 100644 --- a/app/views/workbenches/show.html.slim +++ b/app/views/workbenches/show.html.slim @@ -30,7 +30,7 @@                  ), \                  TableBuilderHelper::Column.new( \                    key: :status, \ -                  attribute: Proc.new {|w| w.archived_or_finalised? ? ("<div class='td-block'><span class='fa fa-archive'></span><span>#{t('activerecord.attributes.referential.archived_at')}</span></div>").html_safe : ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>#{t('activerecord.attributes.referential.archived_at_null')}</span></div>").html_safe} \ +                  attribute: Proc.new {|w| w.referential_read_only? ? ("<div class='td-block'><span class='fa fa-archive'></span><span>#{t('activerecord.attributes.referential.archived_at')}</span></div>").html_safe : ("<div class='td-block'><span class='sb sb-lg sb-preparing'></span><span>#{t('activerecord.attributes.referential.archived_at_null')}</span></div>").html_safe} \                  ), \                  TableBuilderHelper::Column.new( \                    key: :organisation, \ diff --git a/spec/models/referential_spec.rb b/spec/models/referential_spec.rb index 45881333f..6d699f759 100644 --- a/spec/models/referential_spec.rb +++ b/spec/models/referential_spec.rb @@ -125,19 +125,39 @@ describe Referential, :type => :model do      end    end -  context "used in a ReferentialSuite" do -    before do -      ref.referential_suite_id = 42 +  context "to be referential_read_only or not to be referential_read_only" do +    let( :referential ){ build_stubbed( :referential ) } + +    context "in the beginning" do +      it{ expect( referential ).not_to be_referential_read_only } +    end + +    context "after archivation" do +      before{ referential.archived_at = 1.day.ago } +      it{ expect( referential ).to be_referential_read_only }      end -    it "return true to in_referential_suite?" do -      expect(ref.in_referential_suite?).to be(true) +    context "used in a ReferentialSuite" do +      before { referential.referential_suite_id = 42 } + +      it{ expect( referential ).to be_referential_read_only } + +      it "return true to in_referential_suite?" do +        expect(referential).to be_in_referential_suite +      end + +      it "don't use detect_overlapped_referentials in validation" do +        expect(referential).to_not receive(:detect_overlapped_referentials) +        expect(referential).to be_valid +      end      end -    it "don't use detect_overlapped_referentials in validation" do -      expect(ref).to_not receive(:detect_overlapped_referentials) -      ref.valid? +    context "archived and finalised" do +      before do +        referential.archived_at = 1.month.ago +        referential.referential_suite_id = 53 +      end +      it{ expect( referential ).to be_referential_read_only }      end    end -  end | 
