Sha256: 9222f79e8a7a60c73053f05153ff52708708d0fd0946e6c30112f44e1ad247ca

Contents?: true

Size: 1.8 KB

Versions: 6

Compression:

Stored size: 1.8 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

          # Whitelisting attributes
          base.send :attr_accessible, :combined_search_id, :display_order, :operator_id, :search_id, :search, :combined_search
        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

6 entries across 6 versions & 1 rubygems

Version Path
discerner-1.2.2 lib/discerner/methods/models/search_combination.rb
discerner-1.2.1 lib/discerner/methods/models/search_combination.rb
discerner-1.2.0 lib/discerner/methods/models/search_combination.rb
discerner-1.1.20 lib/discerner/methods/models/search_combination.rb
discerner-1.1.19 lib/discerner/methods/models/search_combination.rb
discerner-1.1.18 lib/discerner/methods/models/search_combination.rb