espresso 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/espresso.gemspec +63 -0
- data/init.rb +4 -0
- data/lib/espresso.rb +2 -0
- data/lib/espresso/controller.rb +35 -0
- data/lib/espresso/model.rb +68 -0
- data/test/espresso_test.rb +7 -0
- data/test/test_helper.rb +10 -0
- metadata +106 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Alexander Semyonov
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= espresso
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Alexander Semyonov. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "espresso"
|
8
|
+
gem.summary = %Q{Rails extender to simplify rails development}
|
9
|
+
gem.description = %Q{Useful templates for controller and model functions}
|
10
|
+
gem.email = "rotuka@rotuka.com"
|
11
|
+
gem.homepage = "http://github.com/krasivotokak/espresso"
|
12
|
+
gem.authors = ["Alexander Semyonov"]
|
13
|
+
gem.add_development_dependency "thoughtbot-shoulda"
|
14
|
+
gem.add_dependency('searchlogic', '>= 2.2.3')
|
15
|
+
gem.add_dependency('josevalim-inherited_resources', '>= 0.8.5')
|
16
|
+
gem.add_dependency('mislav-will_paginate', '>= 2.3.11')
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
rescue LoadError
|
20
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
21
|
+
end
|
22
|
+
|
23
|
+
require 'rake/testtask'
|
24
|
+
Rake::TestTask.new(:test) do |test|
|
25
|
+
test.libs << 'lib' << 'test'
|
26
|
+
test.pattern = 'test/**/*_test.rb'
|
27
|
+
test.verbose = true
|
28
|
+
end
|
29
|
+
|
30
|
+
begin
|
31
|
+
require 'rcov/rcovtask'
|
32
|
+
Rcov::RcovTask.new do |test|
|
33
|
+
test.libs << 'test'
|
34
|
+
test.pattern = 'test/**/*_test.rb'
|
35
|
+
test.verbose = true
|
36
|
+
end
|
37
|
+
rescue LoadError
|
38
|
+
task :rcov do
|
39
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
task :test => :check_dependencies
|
44
|
+
|
45
|
+
task :default => :test
|
46
|
+
|
47
|
+
require 'rake/rdoctask'
|
48
|
+
Rake::RDocTask.new do |rdoc|
|
49
|
+
if File.exist?('VERSION')
|
50
|
+
version = File.read('VERSION')
|
51
|
+
else
|
52
|
+
version = ""
|
53
|
+
end
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "espresso #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.3
|
data/espresso.gemspec
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{espresso}
|
8
|
+
s.version = "0.0.3"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Alexander Semyonov"]
|
12
|
+
s.date = %q{2009-08-17}
|
13
|
+
s.description = %q{Useful templates for controller and model functions}
|
14
|
+
s.email = %q{rotuka@rotuka.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"espresso.gemspec",
|
25
|
+
"init.rb",
|
26
|
+
"lib/espresso.rb",
|
27
|
+
"lib/espresso/controller.rb",
|
28
|
+
"lib/espresso/model.rb",
|
29
|
+
"test/espresso_test.rb",
|
30
|
+
"test/test_helper.rb"
|
31
|
+
]
|
32
|
+
s.homepage = %q{http://github.com/krasivotokak/espresso}
|
33
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
34
|
+
s.require_paths = ["lib"]
|
35
|
+
s.rubygems_version = %q{1.3.5}
|
36
|
+
s.summary = %q{Rails extender to simplify rails development}
|
37
|
+
s.test_files = [
|
38
|
+
"test/espresso_test.rb",
|
39
|
+
"test/test_helper.rb"
|
40
|
+
]
|
41
|
+
|
42
|
+
if s.respond_to? :specification_version then
|
43
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
44
|
+
s.specification_version = 3
|
45
|
+
|
46
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
47
|
+
s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
48
|
+
s.add_runtime_dependency(%q<searchlogic>, [">= 2.2.3"])
|
49
|
+
s.add_runtime_dependency(%q<josevalim-inherited_resources>, [">= 0.8.5"])
|
50
|
+
s.add_runtime_dependency(%q<mislav-will_paginate>, [">= 2.3.11"])
|
51
|
+
else
|
52
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
53
|
+
s.add_dependency(%q<searchlogic>, [">= 2.2.3"])
|
54
|
+
s.add_dependency(%q<josevalim-inherited_resources>, [">= 0.8.5"])
|
55
|
+
s.add_dependency(%q<mislav-will_paginate>, [">= 2.3.11"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
|
59
|
+
s.add_dependency(%q<searchlogic>, [">= 2.2.3"])
|
60
|
+
s.add_dependency(%q<josevalim-inherited_resources>, [">= 0.8.5"])
|
61
|
+
s.add_dependency(%q<mislav-will_paginate>, [">= 2.3.11"])
|
62
|
+
end
|
63
|
+
end
|
data/init.rb
ADDED
data/lib/espresso.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'inherited_resources'
|
2
|
+
|
3
|
+
module Espresso
|
4
|
+
class ObjectsController < InheritedResources::Base
|
5
|
+
# Same as default InheritedResources::Base#new, but render 'edit' view,
|
6
|
+
# other than 'new'
|
7
|
+
def new
|
8
|
+
new! do |format|
|
9
|
+
format.html { render 'edit' }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Same as default InheritedResources::Base#create, but render 'edit' view,
|
14
|
+
# other than 'new'
|
15
|
+
def create
|
16
|
+
create! do |success, failure|
|
17
|
+
failure.html { render 'edit' }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
protected
|
22
|
+
|
23
|
+
# Find collection of objects with pagination.
|
24
|
+
# Also made Searchlogic object @search
|
25
|
+
#
|
26
|
+
# @return [WillPaginate::Collection] collection of objects
|
27
|
+
def collection
|
28
|
+
unless (result = get_collection_ivar).present?
|
29
|
+
@search, result = end_of_association_chain.paginate_found(params[:page], params[:query], params[:q])
|
30
|
+
set_collection_ivar(result)
|
31
|
+
end
|
32
|
+
result
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'searchlogic'
|
2
|
+
|
3
|
+
module Espresso
|
4
|
+
# @author Alexander Semyonov
|
5
|
+
module Model
|
6
|
+
def self.included(model)
|
7
|
+
model.extend ClassMethods
|
8
|
+
model.class_eval do
|
9
|
+
include InstanceMethods
|
10
|
+
|
11
|
+
attr_accessor :name_field
|
12
|
+
self.name_field = :name
|
13
|
+
end
|
14
|
+
end
|
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
|
25
|
+
def paginate_found(page = nil, query = nil, simple_query = nil)
|
26
|
+
query ||= {}
|
27
|
+
query.merge(parse_simple_query(simple_query)) if simple_query.present?
|
28
|
+
@search = search(query)
|
29
|
+
@results = @search.paginate(:page => page)
|
30
|
+
[@search, @results]
|
31
|
+
end
|
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
|
38
|
+
def parse_simple_query(query)
|
39
|
+
{:"#{name_field}_like" => query}
|
40
|
+
end
|
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
|
45
|
+
def name_field(new_name_field = nil)
|
46
|
+
if new_name_field.present?
|
47
|
+
@@name_field = new_name_field.to_sym
|
48
|
+
end
|
49
|
+
@@name_field
|
50
|
+
end
|
51
|
+
|
52
|
+
# Make a slug from object’s NameField
|
53
|
+
# @param [ActiveRecord::Base] object, which slug is making
|
54
|
+
# @return [String] slug
|
55
|
+
def make_slug(object)
|
56
|
+
object.send(name_field).parameterize
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
module InstanceMethods
|
61
|
+
def to_s
|
62
|
+
send(@@name_field).to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
protected
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: espresso
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Semyonov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-08-17 00:00:00 +04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: thoughtbot-shoulda
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: searchlogic
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 2.2.3
|
34
|
+
version:
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: josevalim-inherited_resources
|
37
|
+
type: :runtime
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 0.8.5
|
44
|
+
version:
|
45
|
+
- !ruby/object:Gem::Dependency
|
46
|
+
name: mislav-will_paginate
|
47
|
+
type: :runtime
|
48
|
+
version_requirement:
|
49
|
+
version_requirements: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 2.3.11
|
54
|
+
version:
|
55
|
+
description: Useful templates for controller and model functions
|
56
|
+
email: rotuka@rotuka.com
|
57
|
+
executables: []
|
58
|
+
|
59
|
+
extensions: []
|
60
|
+
|
61
|
+
extra_rdoc_files:
|
62
|
+
- LICENSE
|
63
|
+
- README.rdoc
|
64
|
+
files:
|
65
|
+
- LICENSE
|
66
|
+
- README.rdoc
|
67
|
+
- Rakefile
|
68
|
+
- VERSION
|
69
|
+
- espresso.gemspec
|
70
|
+
- init.rb
|
71
|
+
- lib/espresso.rb
|
72
|
+
- lib/espresso/controller.rb
|
73
|
+
- lib/espresso/model.rb
|
74
|
+
- test/espresso_test.rb
|
75
|
+
- test/test_helper.rb
|
76
|
+
has_rdoc: true
|
77
|
+
homepage: http://github.com/krasivotokak/espresso
|
78
|
+
licenses: []
|
79
|
+
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options:
|
82
|
+
- --charset=UTF-8
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: "0"
|
90
|
+
version:
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: "0"
|
96
|
+
version:
|
97
|
+
requirements: []
|
98
|
+
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 1.3.5
|
101
|
+
signing_key:
|
102
|
+
specification_version: 3
|
103
|
+
summary: Rails extender to simplify rails development
|
104
|
+
test_files:
|
105
|
+
- test/espresso_test.rb
|
106
|
+
- test/test_helper.rb
|