Class: RailsOmnibar::Command::RecordSearch
- Defined in:
- lib/rails_omnibar/command/search.rb
Overview
ActiveRecord-specific search.
Instance Attribute Summary
Attributes inherited from Base
#description, #example, #pattern, #resolver
Instance Method Summary collapse
-
#initialize(model:, columns: :id, pattern: nil, finder: nil, itemizer: nil, example: nil) ⇒ RecordSearch
constructor
A new instance of RecordSearch.
Methods inherited from Base
Constructor Details
#initialize(model:, columns: :id, pattern: nil, finder: nil, itemizer: nil, example: nil) ⇒ RecordSearch
Returns a new instance of RecordSearch.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rails_omnibar/command/search.rb', line 28 def initialize(model:, columns: :id, pattern: nil, finder: nil, itemizer: nil, example: nil) # casting and validations model = model.to_s.classify.constantize unless model.is_a?(Class) model < ActiveRecord::Base || raise(ArgumentError, 'model: must be a model') columns = Array(columns).map(&:to_s) columns.present? || raise(ArgumentError, 'columns: must be given') columns.each { |c| c.in?(model.column_names) || raise(ArgumentError, "bad column #{c}") } # default finder, uses LIKE/ILIKE for non-id columns finder ||= ->(q) do return model.none if q.blank? columns.inject(model.none) do |rel, col| rel.or(col =~ /id$/ ? model.where(col => q) : model.where("#{col} #{RailsOmnibar.like} ?", "%#{q}%")) end end # default itemizer itemizer ||= ->(record) do { title: "#{record.class.name}##{record.id}", url: "/#{model.model_name.route_key}/#{record.to_param}", } end super( description: "Find #{model.name} by #{columns.join(' OR ')}".tr('_', ' '), example: example, pattern: pattern, finder: finder, itemizer: itemizer, ) end |