lib/espresso/model.rb in espresso-0.1.12 vs lib/espresso/model.rb in espresso-0.2.1
- old
+ new
@@ -1,80 +1,36 @@
-require 'searchlogic'
-require 'will_paginate'
+require 'espresso'
+require 'active_support/core_ext/class/inheritable_attributes'
+require 'active_support/core_ext/string'
module Espresso
- # @author Alexander Semyonov
module Model
- def self.included(model)
- super
- model.class_eval do
- extend ClassMethods
+ extend Espresso::Concern
- class_inheritable_accessor :model_attrs, :name_field, :per_page
+ included do
+ class_inheritable_accessor :name_field, :model_modifiers
- self.model_attrs = []
- self.name_field = :name
- self.per_page = 10
-
- named_scope :for_index, :conditions => '1 = 1'
- named_scope :for_feed, :order => 'created_at DESC'
- named_scope :for_show, :conditions => '1 = 1'
- named_scope :recent, lambda {
- {:limit => per_page}
- }
- end
+ self.name_field = :name
+ self.model_modifiers = []
end
- # String representation of model, based on Model’s name_field
- def to_s
- send(self.class.name_field).to_s
- end
-
- # Model’s classes, based on Model.model_attrs
- def model_class
- main_class = "#{::Espresso::MODEL_PREFIX}-#{self.class.name.underscore}"
- classes = [main_class] +
- self.class.model_attrs.inject([]) do |collection, attribute|
- if send("#{attribute}?")
- collection << " #{main_class}_#{attribute}"
- end
- collection
- end
- classes.join(' ')
- 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 = {}, simple_query = nil)
- query ||= {}
- query.merge!(self.parse_simple_query(simple_query)) if simple_query
- searchlogic = search(query)
- results = searchlogic.paginate(:page => page)
- [searchlogic, results]
+ # Make a slug from object‘s #name_field
+ # @param [ActiveRecord::Base] model object, which slug is making
+ # @return [String] slug made from model’s #name_field
+ def make_slug(model)
+ model.send(name_field).parameterize
end
- alias_method :search_results, :paginate_found
+ 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}
+ module InstanceMethods
+ # String representation of model, based on Model’s #name_field
+ def to_s
+ send(self.class.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
end
+end
+
+if defined?(InheritedResources)
+ require 'espresso/model/inherited_resources'
end