Sha256: 202fc23ed2e279f217aab16f937d16d8e17cab7b1a1c93878b3dc9166241971a

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

require 'searchlogic'

module Espresso
  # @author Alexander Semyonov
  module Model
    def self.included(model)
      model.extend ClassMethods
      model.class_eval do
        include InstanceMethods

        attr_accessor :name_field
        @name_field = :name
      end
    end

    module ClassMethods
      # Paginates search results
      #
      # @param [Integer] page number of results’ page
      # @param [Hash] query searchlogic fields (proc’ed named scopes’ names with values)
      # @param [String] simple_query params for simple «LIKE '%something%'» searches (e.g. /people?q=Alexander)
      # @return [Array] searchlogic object and collection of results
      #
      # @todo Add an options to paginating
      def paginate_found(page = nil, query = nil, simple_query = nil)
        query ||= {}
        query.merge(parse_simple_query(simple_query)) if simple_query.present?
        @search = search(query)
        @results = @search.paginate(:page => page)
        [@search, @results]
      end

      # Make searchlogic query from simple query option
      # Needed to be reimplemented in subclasses
      #
      # @param [String] simple query string
      # @return [Hash] searchlogic query
      def parse_simple_query(query)
        {:"#{name_field}_like" => query}
      end

      # «NameField» is a main field, used to represent model in to_s method and in simple queries
      # @param [Symbol, String] new_name_field new field name
      # @return [Symbol] field name
      def name_field(new_name_field = nil)
        if new_name_field.present?
          @@name_field = new_name_field.to_sym
        end
        @@name_field
      end

      # Make a slug from object’s NameField
      # @param [ActiveRecord::Base] object, which slug is making
      # @return [String] slug
      def make_slug(object)
        object.send(name_field).parameterize
      end
    end

    module InstanceMethods
      def to_s
        send(@@name_field).to_s
      end

    protected
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
krasivotokak-espresso-0.0.5 lib/espresso/model.rb
krasivotokak-espresso-0.0.6 lib/espresso/model.rb
espresso-0.0.6 lib/espresso/model.rb
espresso-0.0.5 lib/espresso/model.rb