Module RidaAlBarazi::SimplySearchable::ClassMethods
In: lib/simply_searchable.rb

Methods

Public Instance methods

Return records that matches the passed params, for example:

Post.list(:title => ‘abc’, :created_at => Date.today)

Will return the posts that contain ‘abc’ in their title and created today.

[Source]

    # File lib/simply_searchable.rb, line 56
56:       def list(options={})
57:         listings = self
58:         options.each_pair do |key, value|
59:           key = key.to_s
60:           listings = listings.send("where_#{key}".to_sym, value) unless value.blank? or !self.column_names.include?key
61:         end
62:         return Post.with_pagination ? listings.paginate(:page => options[:page], :per_page => options[:per_page]) : listings.all
63:       end

Defines named_scope methods for all attributes as following:

        Post
                id:integer
                title:string
                body:text
                created_at:datetime
                updated_at:datetime

For integer and datetime like id, created_at and updated_at, named_scope methods will expect a number and uses equality check to match.

For string and text attributes like title and body, named_scope methods will expect a text and wil match using ’%string%’

By default SimplySearchable uses will_paginate to disable will_paginate just pass: :will_paginate => false

  class Post < ActiveRecord::Base
    simply_searchable :will_paginate => false
  end

  class Post < ActiveRecord::Base
    simply_searchable :per_page => 20
  end

[Source]

    # File lib/simply_searchable.rb, line 35
35:       def simply_searchable(options = {})
36:         options.reverse_merge!(:per_page => 30, :with_pagination => true)
37:         class_inheritable_accessor :attrs, :with_pagination, :per_page
38:         self.with_pagination = options[:with_pagination]
39:         self.per_page = options[:per_page]
40:         self.attrs = self.columns.collect{|c| [c.name, c.type]}
41:         self.attrs.each do |attribute|
42:           case attribute[1]
43:           when :text, :string then 
44:             named_scope "where_#{attribute[0]}".to_sym, lambda {|value| { :conditions => ["#{attribute[0]} like ?", "%#{value}%"] }}          
45:           else  
46:             named_scope "where_#{attribute[0]}".to_sym, lambda {|value| { :conditions => ["#{attribute[0]} = ?", value] }}          
47:           end
48:         end
49:       end

[Validate]