class Query < ActiveRecord::Base
  has_one :query_result

  validates :name, :query_string, presence: true
  validates :name, uniqueness: true

  FORMATS           = ['article', 'book', 'other']
  PUBLICATION_YEARS = [-1, 0, 1]

  def self.common_filter_query
    current_year = Time.new.year
    [
      "format:(#{FORMATS.join(' OR ')})",
      "pub_date_tis:(#{PUBLICATION_YEARS.map {|y| current_year + y}.join(' OR ')})",
      "access_ss:dtu"
    ]
  end

  def self.normalize(query_string)
    query_string.gsub(/^\s+|\s+$/, '')
                .gsub(/\s+/, ' ')
  end

  def to_solr_query
    [::Query.normalize(query_string), ::Query.common_filter_query].flatten.join(' AND ')
  end
end

class Query < ActiveRecord::Base
  has_one :query_result

  validates :name, :query_string, presence: true
  validates :name, uniqueness: true

  FORMATS           = ['article', 'book', 'other']
  PUBLICATION_YEARS = [-1, 0, 1]
  ACCESS            = ['dtu']

  # Params:
  #   options[:formats]           : which formats to search for
  #   options[:publication_years] : which publication years (offsets from current year) to search for
  #   options[:access]            : which access modifiers to search for
  def self.common_filter_query(options = {})
    options = {
      :formats           => FORMATS,
      :publication_years => PUBLICATION_YEARS,
      :access            => ACCESS
    }.merge(options)

    current_year = Time.new.year

    [
      "format:(#{options[:formats].join(' OR ')})",
      "pub_date_tis:(#{options[:publication_years].map {|y| current_year + y}.join(' OR ')})",
      "access_ss:(#{options[:access].join(' OR ')})"
    ]
  end

  def self.normalize(query_string)
    query_string.strip.squeeze
  end

  # Combine this query and the common filter query
  def to_solr_query
    [::Query.normalize(query_string), ::Query.common_filter_query].flatten.join(' AND ')
  end
end

Add options to adjust the common filter queries
Add options to adjust the common filter queries
