Sha256: cd4f286e30ab8a85d4c10938e43da0fe078e665618f32c22bf27875e9567ea4d

Contents?: true

Size: 1.99 KB

Versions: 5

Compression:

Stored size: 1.99 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
        self.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

5 entries across 5 versions & 2 rubygems

Version Path
krasivotokak-espresso-0.0.2 lib/espresso/model.rb
krasivotokak-espresso-0.0.3 lib/espresso/model.rb
krasivotokak-espresso-0.0.4 lib/espresso/model.rb
espresso-0.0.4 lib/espresso/model.rb
espresso-0.0.3 lib/espresso/model.rb