Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Some code extracted from croconize

  • Loading branch information...
commit 6eec88eeee77a1f10a78e8663513a5ff9eace784 1 parent 1c781e5
Alexander Semyonov authored
View
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Alexander Semyonov
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
4 init.rb
@@ -0,0 +1,4 @@
+require 'activerecord'
+require 'espresso/model'
+
+ActiveRecord::Base.send(:include, Espresso::Model)
View
24 lib/espresso/controller.rb
@@ -0,0 +1,24 @@
+module Espresso
+ class Controller < InheritedResources::Base
+ def new
+ new! do |format|
+ format.html { render 'edit' }
+ end
+ end
+
+ def create
+ create! do |success, failure|
+ failure.html { render 'edit' }
+ end
+ end
+
+ protected
+ def collection
+ unless (result = get_collection_ivar).present?
+ @search, result = end_of_association_chain.paginate_found(params[:page], params[:query], params[:q])
+ set_collection_ivar(result)
+ end
+ result
+ end
+ end
+end
View
46 lib/espresso/model.rb
@@ -0,0 +1,46 @@
+module Espresso
+ 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
+ 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
+
+ def parse_simple_query(query)
+ {:"#{name_field}_like" => query}
+ end
+
+ def name_field(new_name_field = nil)
+ if new_name_field.present?
+ @@name_field = new_name_field
+ end
+ @@name_field
+ end
+
+ 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
Please sign in to comment.
Something went wrong with that request. Please try again.