Sha256: 5364ebc34766aa302aa2c9f2fca8b37d96793ea25f4a52e737e3252b66cefb0b

Contents?: true

Size: 1.87 KB

Versions: 8

Compression:

Stored size: 1.87 KB

Contents

module Lipsiadmin
  module DataBase
    # This Module provides named scope for:
    # 
    # - Search records in a extjs way (but can reusable)
    # - Paginate records in a extjs way (but can be reusable)
    # - Add association to the model (used for search in dependents tables)
    # 
    #   Examples:
    #   
    #     invoices = current_account.store.invoices.with(:order).ext_search(params)
    #     invoices_count = invoices.size
    #     invoices_paginated = invoices.ext_paginate(params)
    # 
    module UtilityScopes
      def self.included(base)#:nodoc:
        base.class_eval do 
          named_scope :ext_search, lambda { |params|
            order = params[:sort].blank? && params[:dir].blank? ? nil : "#{params[:sort]} #{params[:dir]}"
            conditions = nil

            if !params[:query].blank? && !params[:fields].blank?
              filters = params[:fields].split(",").collect { |f| "#{f} LIKE ?" }.compact
              conditions = [filters.join(" OR ")].concat((1..filters.size).collect { "%#{params[:query]}%" })
            end

            { :conditions => conditions }
          }
          named_scope :ext_paginate, lambda { |params|
            order = params[:sort].blank? && params[:dir].blank? ? nil : "#{params[:sort]} #{params[:dir]}"
            { :order => order, :limit => params[:limit], :offset => params[:start] }
          }
          named_scope :with, lambda { |*associations| { :include => associations } }
          
          # You or your plugins (ex: will_paginate) now can override the search/paginate
          # at the moment we can't remove them for backward compatibility.
          (class << self; self end).instance_eval do
            %w(search paginate).each do |name|
              define_method name do |*args|
                send("ext_#{name}", *args)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
lipsiadmin-5.1.6 lib/data_base/utility_scopes.rb
lipsiadmin-5.1.5 lib/data_base/utility_scopes.rb
lipsiadmin-5.1.4 lib/data_base/utility_scopes.rb
lipsiadmin-5.1.3 lib/data_base/utility_scopes.rb
lipsiadmin-5.1.2 lib/data_base/utility_scopes.rb
lipsiadmin-5.1.1 lib/data_base/utility_scopes.rb
lipsiadmin-5.1.0 lib/data_base/utility_scopes.rb
lipsiadmin-5.0.9 lib/data_base/utility_scopes.rb