krasivotokak-espresso 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/espresso.gemspec +2 -2
- data/lib/espresso/controller.rb +10 -1
- data/lib/espresso/model.rb +21 -1
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.2
|
data/espresso.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{espresso}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alexander Semyonov"]
|
12
|
-
s.date = %q{2009-08-
|
12
|
+
s.date = %q{2009-08-17}
|
13
13
|
s.description = %q{Useful templates for controller and model functions}
|
14
14
|
s.email = %q{rotuka@rotuka.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/espresso/controller.rb
CHANGED
@@ -1,13 +1,17 @@
|
|
1
1
|
require 'inherited_resources'
|
2
2
|
|
3
3
|
module Espresso
|
4
|
-
class
|
4
|
+
class ObjectsController < InheritedResources::Base
|
5
|
+
# Same as default InheritedResources::Base#new, but render 'edit' view,
|
6
|
+
# other than 'new'
|
5
7
|
def new
|
6
8
|
new! do |format|
|
7
9
|
format.html { render 'edit' }
|
8
10
|
end
|
9
11
|
end
|
10
12
|
|
13
|
+
# Same as default InheritedResources::Base#create, but render 'edit' view,
|
14
|
+
# other than 'new'
|
11
15
|
def create
|
12
16
|
create! do |success, failure|
|
13
17
|
failure.html { render 'edit' }
|
@@ -15,6 +19,11 @@ module Espresso
|
|
15
19
|
end
|
16
20
|
|
17
21
|
protected
|
22
|
+
|
23
|
+
# Find collection of objects with pagination.
|
24
|
+
# Also made Searchlogic object @search
|
25
|
+
#
|
26
|
+
# @return [WillPaginate::Collection] collection of objects
|
18
27
|
def collection
|
19
28
|
unless (result = get_collection_ivar).present?
|
20
29
|
@search, result = end_of_association_chain.paginate_found(params[:page], params[:query], params[:q])
|
data/lib/espresso/model.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'searchlogic'
|
2
2
|
|
3
3
|
module Espresso
|
4
|
+
# @author Alexander Semyonov
|
4
5
|
module Model
|
5
6
|
def self.included(model)
|
6
7
|
model.extend ClassMethods
|
@@ -13,6 +14,14 @@ module Espresso
|
|
13
14
|
end
|
14
15
|
|
15
16
|
module ClassMethods
|
17
|
+
# Paginates search results
|
18
|
+
#
|
19
|
+
# @param [Integer] page number of results’ page
|
20
|
+
# @param [Hash] query searchlogic fields (proc’ed named scopes’ names with values)
|
21
|
+
# @param [String] simple_query params for simple «LIKE '%something%'» searches (e.g. /people?q=Alexander)
|
22
|
+
# @return [Array] searchlogic object and collection of results
|
23
|
+
#
|
24
|
+
# @todo Add an options to paginating
|
16
25
|
def paginate_found(page = nil, query = nil, simple_query = nil)
|
17
26
|
query ||= {}
|
18
27
|
query.merge(parse_simple_query(simple_query)) if simple_query.present?
|
@@ -21,17 +30,28 @@ module Espresso
|
|
21
30
|
[@search, @results]
|
22
31
|
end
|
23
32
|
|
33
|
+
# Make searchlogic query from simple query option
|
34
|
+
# Needed to be reimplemented in subclasses
|
35
|
+
#
|
36
|
+
# @param [String] simple query string
|
37
|
+
# @return [Hash] searchlogic query
|
24
38
|
def parse_simple_query(query)
|
25
39
|
{:"#{name_field}_like" => query}
|
26
40
|
end
|
27
41
|
|
42
|
+
# «NameField» is a main field, used to represent model in to_s method and in simple queries
|
43
|
+
# @param [Symbol, String] new_name_field new field name
|
44
|
+
# @return [Symbol] field name
|
28
45
|
def name_field(new_name_field = nil)
|
29
46
|
if new_name_field.present?
|
30
|
-
@@name_field = new_name_field
|
47
|
+
@@name_field = new_name_field.to_sym
|
31
48
|
end
|
32
49
|
@@name_field
|
33
50
|
end
|
34
51
|
|
52
|
+
# Make a slug from object’s NameField
|
53
|
+
# @param [ActiveRecord::Base] object, which slug is making
|
54
|
+
# @return [String] slug
|
35
55
|
def make_slug(object)
|
36
56
|
object.send(name_field).parameterize
|
37
57
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: krasivotokak-espresso
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Semyonov
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-08-
|
12
|
+
date: 2009-08-17 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|