diff options
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | app/models/organisation.rb | 63 | ||||
| -rw-r--r-- | lib/stif/codif_line_id.rb | 16 | ||||
| -rw-r--r-- | spec/factories/organisations.rb | 3 | ||||
| -rw-r--r-- | spec/models/organisation_spec.rb | 15 | 
5 files changed, 71 insertions, 30 deletions
| diff --git a/.gitignore b/.gitignore index 03a39be90..0fe0d5e6c 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,6 @@ coverage  /bin/rails  /bin/rspec  /bin/spring +/spec/fixtures/zip/* -# FIXME Ignore ./spec/services/zip_service/regression_4273_spec.rb files -/spec/fixtures/target_*.zip +spec/fixtures/target_* diff --git a/app/models/organisation.rb b/app/models/organisation.rb index f6fba2d67..0dab1fc16 100644 --- a/app/models/organisation.rb +++ b/app/models/organisation.rb @@ -18,36 +18,39 @@ class Organisation < ActiveRecord::Base    validates_presence_of :name    validates_uniqueness_of :code -  def self.portail_api_request -    conf = Rails.application.config.try(:stif_portail_api) -    raise 'Rails.application.config.stif_portail_api configuration is not defined' unless conf - -    HTTPService.get_json_resource( -      host: conf[:url], -      path: '/api/v1/organizations', -      token: conf[:key]) -  end +  class << self + +    def portail_api_request +      conf = Rails.application.config.try(:stif_portail_api) +      raise 'Rails.application.config.stif_portail_api configuration is not defined' unless conf -  def self.sync_update code, name, scope -    org = Organisation.find_or_initialize_by(code: code) -    if scope -      org.sso_attributes ||= {} -      if org.sso_attributes['functional_scope'] != scope -        org.sso_attributes['functional_scope'] = scope -        # FIXME see #1941 -        org.sso_attributes_will_change! +      HTTPService.get_json_resource( +        host: conf[:url], +        path: '/api/v1/organizations', +        token: conf[:key]) +    end + +    def sync_update code, name, scope +      org = Organisation.find_or_initialize_by(code: code) +      if scope +        org.sso_attributes ||= {} +        if org.sso_attributes['functional_scope'] != scope +          org.sso_attributes['functional_scope'] = scope +          # FIXME see #1941 +          org.sso_attributes_will_change! +        end        end +      org.name      = name +      org.synced_at = Time.now +      org.save +      org      end -    org.name      = name -    org.synced_at = Time.now -    org.save -    org -  end -  def self.portail_sync -    self.portail_api_request.each do |el| -      org = self.sync_update el['code'], el['name'], el['functional_scope'] -      puts "✓ Organisation #{org.name} has been updated" unless Rails.env.test? +    def portail_sync +      portail_api_request.each do |el| +        org = self.sync_update el['code'], el['name'], el['functional_scope'] +        puts "✓ Organisation #{org.name} has been updated" unless Rails.env.test? +      end      end    end @@ -64,4 +67,12 @@ class Organisation < ActiveRecord::Base      raise ActiveRecord::RecordNotFound    end +  def functional_scope +    JSON.parse( (sso_attributes || {}).fetch('functional_scope', '[]') ) +  end + +  def lines_set +    STIF::CodifLineId.lines_set_from_functional_scope( functional_scope ) +  end +  end diff --git a/lib/stif/codif_line_id.rb b/lib/stif/codif_line_id.rb new file mode 100644 index 000000000..b7f3c8bd5 --- /dev/null +++ b/lib/stif/codif_line_id.rb @@ -0,0 +1,16 @@ +module STIF +  module CodifLineId extend self + +    LINE_OBJECT_ID_SEPERATOR = ':' + +    def extract_codif_line_id line_name +      line_name.split(LINE_OBJECT_ID_SEPERATOR).last +    end + +    def lines_set_from_functional_scope(functional_scope) +      Set.new( +        functional_scope +          .map{ |line| extract_codif_line_id line }) +    end +  end +end diff --git a/spec/factories/organisations.rb b/spec/factories/organisations.rb index 239557a0e..2914c30cb 100644 --- a/spec/factories/organisations.rb +++ b/spec/factories/organisations.rb @@ -2,5 +2,8 @@ FactoryGirl.define do    factory :organisation do      sequence(:name) { |n| "Organisation #{n}" }      sequence(:code) { |n| "000#{n}" } +    factory :org_with_lines do +      sso_attributes { { 'functional_scope' => %w{STIF:CODIFLIGNE:Line:C00108 STIF:CODIFLIGNE:Line:C00109}.to_json } } +    end    end  end diff --git a/spec/models/organisation_spec.rb b/spec/models/organisation_spec.rb index 1217666f7..359417d88 100644 --- a/spec/models/organisation_spec.rb +++ b/spec/models/organisation_spec.rb @@ -2,8 +2,19 @@ describe Organisation, :type => :model do    it { should validate_presence_of(:name) }    it { should validate_uniqueness_of(:code) } -  it 'should have a valid factory' do -    expect(FactoryGirl.build(:organisation)).to be_valid +  subject { build_stubbed :organisation } + +  it 'has a valid factory' do +    expect_it.to be_valid +  end + +  context 'lines_set' do +    it 'has no lines' do +      expect( subject.lines_set ).to eq(Set.new()) +    end +    it 'has two lines' do +      expect( build_stubbed(:org_with_lines).lines_set ).to eq(Set.new(%w{C00109 C00108})) +    end    end    # it "create a rule_parameter_set" do | 
