Sha256: c27b7e7463311a0b4dc63d29c20d60a06826436896e6142f66594777999e5a03

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

class Task < ActiveRecord::Base
  # Assume this model has attributes: :id, :due_date, and :name
end

class TaskSearch < SearchLingo::AbstractSearch
  parser SearchLingo::Parsers::DateParser.new :tasks,
    :due_date
  parser SearchLingo::Parsers::DateRangeParser.new :tasks,
    :due_date
  parser SearchLingo::Parsers::LTEDateParser.new :tasks,
    :due_date, connection: ActiveRecord::Base.connection
  parser SearchLingo::Parsers::GTEDateParser.new :tasks,
    :due_date, connection: ActiveRecord::Base.connection

  parser do |token|
    token.match /\Aid:\s*([[:digit:]]+)\z/ do |m|
      [:where, { tasks: { id: m[1] } }]
    end
  end

  def default_parse(token)
    [:where, 'tasks.name LIKE ?', "%#{token}%"]
  end
end

search = TaskSearch.new('6/4/15 id: 42 foo "bar baz"', Task)
search.results  # =>  Task
                #       .where(tasks: { due_date: Date.new(2015, 6, 4) })
                #       .where(tasks: { id: '42' })
                #       .where('tasks.name LIKE ?', '%foo%')
                #       .where('tasks.name LIKE ?', '%bar baz%')

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
search_lingo-1.0.1 examples/simple.rb
search_lingo-1.0.0 examples/simple.rb
search_lingo-1.0.0.beta3 examples/simple.rb
search_lingo-1.0.0.beta2 examples/simple.rb