Sha256: bbb2810b5550ac4f00c38d8eb59128c469083f2e26f76993b5809ee877830f45

Contents?: true

Size: 766 Bytes

Versions: 6

Compression:

Stored size: 766 Bytes

Contents

# encoding: utf-8

module InvoiceBar
  
  # Module that provides +search_for+ method on a model class.
  # It expects +searchable_fields+ to be defined.
  # +searchable_fields+ contains the table fields of the model
  # (attributes) that should be searched.
  module Searchable
    extend ActiveSupport::Concern
    
    included do      
      def self.search_for(query)
        where(self.build_search_query, :query => "%#{query}%")
      end
      
      protected

        def self.build_search_query()
          @query = ''

          self.searchable_fields.each do |field|
            unless @query.blank?
              @query += ' OR '
            end

            @query += "#{field} LIKE :query"
          end

          @query
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
invoice_bar-0.0.6 lib/searchable.rb
invoice_bar-0.0.5 lib/searchable.rb
invoice_bar-0.0.4 lib/searchable.rb
invoice_bar-0.0.3 lib/searchable.rb
invoice_bar-0.0.2 lib/searchable.rb
invoice_bar-0.0.1 lib/searchable.rb