aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/concerns/objectid_restrictions.rb
blob: 49cc772c78224436e9b3125cfae288b8ea22cf6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module ObjectidRestrictions
    extend ActiveSupport::Concern

    included do
      # HUB 1
      validate :third_part_objectid_uniqueness
    end

    def validate_specific_objectid( size_max )
      #errors.add( :objectid, I18n.t('hub.invalid')) if ( %r{\A\w+:\w+:\w+\z}).match( self.objectid).nil?

      if third_part_objectid.nil? || ( !third_part_objectid.include?( "_pending_" ) && third_part_objectid.size > size_max)
        errors.add( :objectid, I18n.t('hub.invalid'))
      end
    end
    def third_part_objectid
      return nil if ( %r{\A\w+:\w+:[0-9A-Za-z_-]+\z}).match( self.objectid).nil?
      self.objectid.match(/:([0-9A-Za-z_-]+)\z/)[1]
    end
    def third_part_objectid_uniqueness
      return unless hub_restricted?

      return true unless third_part_objectid
      likes = self.class.where( "objectid LIKE ?", "%:#{self.third_part_objectid}" )
      likes.size.zero? || ( likes.size==1 && likes.first.id==self.id)
    end
end