Sha256: 3a830ff85ef164c5106d578a37278f6d4e5922438f2fe7ff9facd320cb979f0a

Contents?: true

Size: 1.62 KB

Versions: 16

Compression:

Stored size: 1.62 KB

Contents

module Discerner
  module Methods
    module Models
      module SearchCombination
        def self.included(base)
          base.send :include, SoftDelete
          base.send :include, Warning

          # Associations
          base.send :belongs_to, :operator, inverse_of: :search_combinations
          base.send :belongs_to, :search,   inverse_of: :search_combinations,  foreign_key: :search_id
          base.send :belongs_to, :combined_search, foreign_key: :combined_search_id, class_name: 'Discerner::Search'

          # Scopes
          base.send(:scope, :ordered_by_display_order, -> { base.order('discerner_search_combinations.display_order ASC') })

          # Validations
          base.send :validates_presence_of, :search, :combined_search
          base.send :validate, :validate_searches
        end

        # Instance Methods
        def initialize(*args)
          super(*args)
        end

        def validate_searches
          return if self.search_id.blank? || self.combined_search_id.blank?
          errors.add(:base,"Search cannot be combined with itself.") if self.search_id == self.combined_search_id
        end

        def disabled?
          return false unless persisted?
          return true if deleted?
          if combined_search.deleted?
            warnings.add(:base, "Combined search has been deleted and has to be removed from the search")
            return true
          elsif combined_search.disabled?
            warnings.add(:base, "Combined search has been disabled and has to be removed from the search")
            return true
          end
          return false
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
discerner-2.0.16 lib/discerner/methods/models/search_combination.rb
discerner-2.0.15 lib/discerner/methods/models/search_combination.rb
discerner-2.0.14 lib/discerner/methods/models/search_combination.rb
discerner-2.0.13 lib/discerner/methods/models/search_combination.rb
discerner-2.0.12 lib/discerner/methods/models/search_combination.rb
discerner-2.0.11 lib/discerner/methods/models/search_combination.rb
discerner-2.0.10 lib/discerner/methods/models/search_combination.rb
discerner-2.0.9 lib/discerner/methods/models/search_combination.rb
discerner-2.0.8 lib/discerner/methods/models/search_combination.rb
discerner-2.0.7 lib/discerner/methods/models/search_combination.rb
discerner-2.0.6 lib/discerner/methods/models/search_combination.rb
discerner-2.0.5 lib/discerner/methods/models/search_combination.rb
discerner-2.0.4 lib/discerner/methods/models/search_combination.rb
discerner-2.0.3 lib/discerner/methods/models/search_combination.rb
discerner-2.0.2 lib/discerner/methods/models/search_combination.rb
discerner-2.0.1 lib/discerner/methods/models/search_combination.rb