diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/organisation.rb | 63 |
1 files changed, 37 insertions, 26 deletions
diff --git a/app/models/organisation.rb b/app/models/organisation.rb index f6fba2d67..4343c87af 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::CodifligneLineId.lines_set_from_functional_scope( functional_scope ) + end + end |
