@@ -17,12 +17,13 @@ class Source < ApplicationRecord
1717
1818 belongs_to :user
1919 belongs_to :content_provider
20+ has_many :source_filters , dependent : :destroy
2021
2122 validates :url , :method , presence : true
2223 validates :url , url : true
2324 validates :approval_status , inclusion : { in : APPROVAL_STATUS . values }
24- validates :method , inclusion : { in : -> ( _ ) { TeSS ::Config . user_ingestion_methods } } ,
25- unless : -> { User . current_user &.is_admin? || User . current_user &.has_role? ( :scraper_user ) }
25+ validates :method , inclusion : { in : -> ( _ ) { TeSS ::Config . user_ingestion_methods } } ,
26+ unless : -> { User . current_user &.is_admin? || User . current_user &.has_role? ( :scraper_user ) }
2627 validates :default_language , controlled_vocabulary : { dictionary : 'LanguageDictionary' ,
2728 allow_blank : true }
2829 validate :check_method
@@ -31,6 +32,8 @@ class Source < ApplicationRecord
3132 before_update :log_approval_status_change
3233 before_update :reset_approval_status
3334
35+ accepts_nested_attributes_for :source_filters , allow_destroy : true
36+
3437 if TeSS ::Config . solr_enabled
3538 # :nocov:
3639 searchable do
@@ -45,7 +48,7 @@ class Source < ApplicationRecord
4548 ingestor_title
4649 end
4750 string :content_provider do
48- self . content_provider . try ( :title )
51+ content_provider . try ( :title )
4952 end
5053 string :node , multiple : true do
5154 associated_nodes . pluck ( :name )
@@ -79,12 +82,10 @@ def self.facet_fields
7982 end
8083
8184 def self . check_exists ( source_params )
82- given_source = self . new ( source_params )
85+ given_source = new ( source_params )
8386 source = nil
8487
85- if given_source . url . present?
86- source = self . find_by_url ( given_source . url )
87- end
88+ source = find_by_url ( given_source . url ) if given_source . url . present?
8889
8990 source
9091 end
@@ -135,30 +136,41 @@ def self.approval_required?
135136 TeSS ::Config . feature [ 'user_source_creation' ] && !User . current_user &.is_admin?
136137 end
137138
139+ def passes_filter? ( item )
140+ source_filters . each do |filter |
141+ if filter . allow?
142+ return false unless filter . match ( item )
143+ elsif filter . block?
144+ return false if filter . match ( item )
145+ end
146+ end
147+
148+ true
149+ end
150+
138151 private
139152
140153 def set_approval_status
141- if self . class . approval_required?
142- self . approval_status = :not_approved
143- else
144- self . approval_status = :approved
145- end
154+ self . approval_status = if self . class . approval_required?
155+ :not_approved
156+ else
157+ :approved
158+ end
146159 end
147160
148161 def reset_approval_status
149- if self . class . approval_required?
150- if method_changed? || url_changed?
151- self . approval_status = :not_approved
152- end
153- end
162+ return unless self . class . approval_required?
163+ return unless method_changed? || url_changed?
164+
165+ self . approval_status = :not_approved
154166 end
155167
156168 def log_approval_status_change
157- if approval_status_changed?
158- old = ( APPROVAL_STATUS [ approval_status_before_last_save . to_i ] || APPROVAL_STATUS [ 0 ] ) . to_s
159- new = approval_status . to_s
160- create_activity ( :approval_status_changed , owner : User . current_user , parameters : { old : old , new : new } )
161- end
169+ return unless approval_status_changed?
170+
171+ old = ( APPROVAL_STATUS [ approval_status_before_last_save . to_i ] || APPROVAL_STATUS [ 0 ] ) . to_s
172+ new = approval_status . to_s
173+ create_activity ( :approval_status_changed , owner : User . current_user , parameters : { old : , new : } )
162174 end
163175
164176 def loggable_changes
0 commit comments