savage-beast 0.1.0
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/.gitignore +2 -0
- data/README.rdoc +42 -0
- data/Rakefile +25 -0
- data/VERSION +1 -0
- data/app/controllers/application.rb +6 -0
- data/app/controllers/beast_application_controller.rb +3 -0
- data/app/controllers/forums_controller.rb +64 -0
- data/app/controllers/moderators_controller.rb +10 -0
- data/app/controllers/monitorships_controller.rb +22 -0
- data/app/controllers/posts_controller.rb +134 -0
- data/app/controllers/topics_controller.rb +110 -0
- data/app/helpers/count_helper.rb +13 -0
- data/app/helpers/forums_helper.rb +15 -0
- data/app/helpers/moderators_helper.rb +2 -0
- data/app/helpers/monitorships_helper.rb +2 -0
- data/app/helpers/posts_helper.rb +3 -0
- data/app/helpers/topics_helper.rb +3 -0
- data/app/models/forum.rb +26 -0
- data/app/models/moderatorship.rb +6 -0
- data/app/models/monitorship.rb +5 -0
- data/app/models/monitorships_sweeper.rb +9 -0
- data/app/models/post.rb +33 -0
- data/app/models/posts_sweeper.rb +12 -0
- data/app/models/topic.rb +94 -0
- data/app/views/forums/_form.html.erb +21 -0
- data/app/views/forums/edit.html.erb +12 -0
- data/app/views/forums/index.html.erb +77 -0
- data/app/views/forums/new.html.erb +10 -0
- data/app/views/forums/show.html.erb +94 -0
- data/app/views/layouts/_head.html.erb +34 -0
- data/app/views/layouts/_post.rss.builder +9 -0
- data/app/views/layouts/application.html.erb +29 -0
- data/app/views/layouts/beast.html.erb +29 -0
- data/app/views/monitorships/create.js.rjs +4 -0
- data/app/views/monitorships/destroy.js.rjs +4 -0
- data/app/views/posts/_edit.html.erb +38 -0
- data/app/views/posts/edit.html.erb +14 -0
- data/app/views/posts/edit.js.rjs +6 -0
- data/app/views/posts/index.html.erb +53 -0
- data/app/views/posts/index.rss.builder +20 -0
- data/app/views/posts/monitored.html.erb +55 -0
- data/app/views/posts/monitored.rss.builder +15 -0
- data/app/views/posts/update.js.rjs +3 -0
- data/app/views/topics/_form.html.erb +28 -0
- data/app/views/topics/edit.html.erb +10 -0
- data/app/views/topics/index.html.erb +2 -0
- data/app/views/topics/new.html.erb +18 -0
- data/app/views/topics/show.html.erb +183 -0
- data/app/views/topics/show.rss.builder +16 -0
- data/config/routes.rb +18 -0
- data/generators/beast/beast_generator.rb +88 -0
- data/generators/beast/templates/migrations/create_savage_tables.rb +73 -0
- data/generators/beast/templates/public/images/clearbits/_readme.txt +12 -0
- data/generators/beast/templates/public/images/clearbits/add.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/addressbook.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/alert.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/apple.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_e.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_n.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_ne.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_nw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_s.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_se.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_sw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow1_w.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_e.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_n.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_ne.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_nw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_s.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_se.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_sw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow2_w.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_e.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_n.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_ne.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_nw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_s.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_se.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_sw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/arrow3_w.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/ascii.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/back.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_blank.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_circle.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_rounded.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_rounded_ne.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_rounded_nw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_rounded_se.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bg_rounded_sw.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bigsmile.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/binary.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/blah.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/bstop.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/buy.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/calday.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/calendar.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/camera.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/cart.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/cd.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/cellphone.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/chat.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/check.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/close.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/comment.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/cube.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/day.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/denied.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/document.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/download.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/edit.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/eject.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/equalizer.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/first.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/flag.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/flash.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/folder.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/forward.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/frown.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/ftp.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/graph.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/heart.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/home.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/html.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/ipod.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/last.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/lock.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/loop.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/mail.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/man.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/manman.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/music.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/mute.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/mute_centered.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/newwindow.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/next.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/night.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/open.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/pause.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/phone.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/play.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/previous.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/quicktime.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/redo.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/reload.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/sad.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/save.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/scream.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/search.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/seconds.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/smile.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/smirk.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/star.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/stop.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/subtract.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/switch.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/target.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/tcp.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/time.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/toggle.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/tongue.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/tools.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/trackback.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/trash.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/tv.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/type.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/undo.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/unlock.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/upload.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/user.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/video.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/volume_high.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/volume_low.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/wifi.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/window.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/woman.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/womanman.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/work.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/zoomin.gif +0 -0
- data/generators/beast/templates/public/images/clearbits/zoomout.gif +0 -0
- data/generators/beast/templates/public/images/feed-icon.png +0 -0
- data/generators/beast/templates/public/images/rails.png +0 -0
- data/generators/beast/templates/public/images/reply_background.png +0 -0
- data/generators/beast/templates/public/images/small_circle.gif +0 -0
- data/generators/beast/templates/public/images/spinner.gif +0 -0
- data/generators/beast/templates/public/images/spinner_black.gif +0 -0
- data/generators/beast/templates/public/images/spinner_bounce.gif +0 -0
- data/generators/beast/templates/public/javascripts/beast.js +80 -0
- data/generators/beast/templates/public/stylesheets/display.css +941 -0
- data/lang/en.yml +206 -0
- data/lib/beast.rb +79 -0
- data/lib/savage_beast/application_helper.rb +91 -0
- data/lib/savage_beast/authentication_system.rb +47 -0
- data/lib/savage_beast/user_init.rb +79 -0
- data/lib/tasks/capistrano.rake +90 -0
- data/lib/tasks/deploy_edge.rake +44 -0
- data/lib/tasks/savage_beast.rake +133 -0
- data/po/beast.pot +712 -0
- data/po/nl/beast.po +700 -0
- data/tested_plugins/acts_as_list/README +23 -0
- data/tested_plugins/acts_as_list/init.rb +3 -0
- data/tested_plugins/acts_as_list/lib/active_record/acts/list.rb +256 -0
- data/tested_plugins/acts_as_list/test/list_test.rb +332 -0
- data/tested_plugins/gibberish/LICENSE +18 -0
- data/tested_plugins/gibberish/README +118 -0
- data/tested_plugins/gibberish/Rakefile +14 -0
- data/tested_plugins/gibberish/init.rb +3 -0
- data/tested_plugins/gibberish/lang/es.yml +3 -0
- data/tested_plugins/gibberish/lang/fr.yml +3 -0
- data/tested_plugins/gibberish/lib/gibberish.rb +8 -0
- data/tested_plugins/gibberish/lib/gibberish/localize.rb +88 -0
- data/tested_plugins/gibberish/lib/gibberish/string_ext.rb +17 -0
- data/tested_plugins/gibberish/test/gibberish_test.rb +203 -0
- data/tested_plugins/gibberish/test/lang/es.yml +1 -0
- data/tested_plugins/gibberish/test/lang/fr.yml +1 -0
- data/tested_plugins/mislav-will_paginate/CHANGELOG.rdoc +110 -0
- data/tested_plugins/mislav-will_paginate/LICENSE +18 -0
- data/tested_plugins/mislav-will_paginate/README.rdoc +107 -0
- data/tested_plugins/mislav-will_paginate/Rakefile +53 -0
- data/tested_plugins/mislav-will_paginate/examples/apple-circle.gif +0 -0
- data/tested_plugins/mislav-will_paginate/examples/index.haml +69 -0
- data/tested_plugins/mislav-will_paginate/examples/index.html +92 -0
- data/tested_plugins/mislav-will_paginate/examples/pagination.css +90 -0
- data/tested_plugins/mislav-will_paginate/examples/pagination.sass +91 -0
- data/tested_plugins/mislav-will_paginate/init.rb +1 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate.rb +78 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/array.rb +16 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/collection.rb +146 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/core_ext.rb +32 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/finder.rb +264 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/named_scope.rb +170 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/named_scope_patch.rb +37 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/version.rb +9 -0
- data/tested_plugins/mislav-will_paginate/lib/will_paginate/view_helpers.rb +402 -0
- data/tested_plugins/mislav-will_paginate/test/boot.rb +21 -0
- data/tested_plugins/mislav-will_paginate/test/collection_test.rb +143 -0
- data/tested_plugins/mislav-will_paginate/test/console +8 -0
- data/tested_plugins/mislav-will_paginate/test/database.yml +22 -0
- data/tested_plugins/mislav-will_paginate/test/finder_test.rb +476 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/admin.rb +3 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/developer.rb +14 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/developers_projects.yml +13 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/project.rb +15 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/projects.yml +6 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/replies.yml +29 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/reply.rb +7 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/schema.rb +38 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/topic.rb +10 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/topics.yml +30 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/user.rb +2 -0
- data/tested_plugins/mislav-will_paginate/test/fixtures/users.yml +35 -0
- data/tested_plugins/mislav-will_paginate/test/helper.rb +40 -0
- data/tested_plugins/mislav-will_paginate/test/lib/activerecord_test_case.rb +43 -0
- data/tested_plugins/mislav-will_paginate/test/lib/activerecord_test_connector.rb +75 -0
- data/tested_plugins/mislav-will_paginate/test/lib/load_fixtures.rb +11 -0
- data/tested_plugins/mislav-will_paginate/test/lib/view_test_process.rb +178 -0
- data/tested_plugins/mislav-will_paginate/test/tasks.rake +59 -0
- data/tested_plugins/mislav-will_paginate/test/view_test.rb +365 -0
- data/tested_plugins/white_list/README +29 -0
- data/tested_plugins/white_list/Rakefile +22 -0
- data/tested_plugins/white_list/init.rb +2 -0
- data/tested_plugins/white_list/lib/white_list_helper.rb +97 -0
- data/tested_plugins/white_list/test/white_list_test.rb +132 -0
- data/tested_plugins/white_list_formatted_content/init.rb +27 -0
- metadata +322 -0
@@ -0,0 +1,264 @@
|
|
1
|
+
require 'will_paginate/core_ext'
|
2
|
+
|
3
|
+
module WillPaginate
|
4
|
+
# A mixin for ActiveRecord::Base. Provides +per_page+ class method
|
5
|
+
# and hooks things up to provide paginating finders.
|
6
|
+
#
|
7
|
+
# Find out more in WillPaginate::Finder::ClassMethods
|
8
|
+
#
|
9
|
+
module Finder
|
10
|
+
def self.included(base)
|
11
|
+
base.extend ClassMethods
|
12
|
+
class << base
|
13
|
+
alias_method_chain :method_missing, :paginate
|
14
|
+
# alias_method_chain :find_every, :paginate
|
15
|
+
define_method(:per_page) { 30 } unless respond_to?(:per_page)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# = Paginating finders for ActiveRecord models
|
20
|
+
#
|
21
|
+
# WillPaginate adds +paginate+, +per_page+ and other methods to
|
22
|
+
# ActiveRecord::Base class methods and associations. It also hooks into
|
23
|
+
# +method_missing+ to intercept pagination calls to dynamic finders such as
|
24
|
+
# +paginate_by_user_id+ and translate them to ordinary finders
|
25
|
+
# (+find_all_by_user_id+ in this case).
|
26
|
+
#
|
27
|
+
# In short, paginating finders are equivalent to ActiveRecord finders; the
|
28
|
+
# only difference is that we start with "paginate" instead of "find" and
|
29
|
+
# that <tt>:page</tt> is required parameter:
|
30
|
+
#
|
31
|
+
# @posts = Post.paginate :all, :page => params[:page], :order => 'created_at DESC'
|
32
|
+
#
|
33
|
+
# In paginating finders, "all" is implicit. There is no sense in paginating
|
34
|
+
# a single record, right? So, you can drop the <tt>:all</tt> argument:
|
35
|
+
#
|
36
|
+
# Post.paginate(...) => Post.find :all
|
37
|
+
# Post.paginate_all_by_something => Post.find_all_by_something
|
38
|
+
# Post.paginate_by_something => Post.find_all_by_something
|
39
|
+
#
|
40
|
+
# == The importance of the <tt>:order</tt> parameter
|
41
|
+
#
|
42
|
+
# In ActiveRecord finders, <tt>:order</tt> parameter specifies columns for
|
43
|
+
# the <tt>ORDER BY</tt> clause in SQL. It is important to have it, since
|
44
|
+
# pagination only makes sense with ordered sets. Without the <tt>ORDER
|
45
|
+
# BY</tt> clause, databases aren't required to do consistent ordering when
|
46
|
+
# performing <tt>SELECT</tt> queries; this is especially true for
|
47
|
+
# PostgreSQL.
|
48
|
+
#
|
49
|
+
# Therefore, make sure you are doing ordering on a column that makes the
|
50
|
+
# most sense in the current context. Make that obvious to the user, also.
|
51
|
+
# For perfomance reasons you will also want to add an index to that column.
|
52
|
+
module ClassMethods
|
53
|
+
# This is the main paginating finder.
|
54
|
+
#
|
55
|
+
# == Special parameters for paginating finders
|
56
|
+
# * <tt>:page</tt> -- REQUIRED, but defaults to 1 if false or nil
|
57
|
+
# * <tt>:per_page</tt> -- defaults to <tt>CurrentModel.per_page</tt> (which is 30 if not overridden)
|
58
|
+
# * <tt>:total_entries</tt> -- use only if you manually count total entries
|
59
|
+
# * <tt>:count</tt> -- additional options that are passed on to +count+
|
60
|
+
# * <tt>:finder</tt> -- name of the ActiveRecord finder used (default: "find")
|
61
|
+
#
|
62
|
+
# All other options (+conditions+, +order+, ...) are forwarded to +find+
|
63
|
+
# and +count+ calls.
|
64
|
+
def paginate(*args)
|
65
|
+
options = args.pop
|
66
|
+
page, per_page, total_entries = wp_parse_options(options)
|
67
|
+
finder = (options[:finder] || 'find').to_s
|
68
|
+
|
69
|
+
if finder == 'find'
|
70
|
+
# an array of IDs may have been given:
|
71
|
+
total_entries ||= (Array === args.first and args.first.size)
|
72
|
+
# :all is implicit
|
73
|
+
args.unshift(:all) if args.empty?
|
74
|
+
end
|
75
|
+
|
76
|
+
WillPaginate::Collection.create(page, per_page, total_entries) do |pager|
|
77
|
+
count_options = options.except :page, :per_page, :total_entries, :finder
|
78
|
+
find_options = count_options.except(:count).update(:offset => pager.offset, :limit => pager.per_page)
|
79
|
+
|
80
|
+
args << find_options
|
81
|
+
# @options_from_last_find = nil
|
82
|
+
pager.replace(send(finder, *args) { |*a| yield(*a) if block_given? })
|
83
|
+
|
84
|
+
# magic counting for user convenience:
|
85
|
+
pager.total_entries = wp_count(count_options, args, finder) unless pager.total_entries
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# Iterates through all records by loading one page at a time. This is useful
|
90
|
+
# for migrations or any other use case where you don't want to load all the
|
91
|
+
# records in memory at once.
|
92
|
+
#
|
93
|
+
# It uses +paginate+ internally; therefore it accepts all of its options.
|
94
|
+
# You can specify a starting page with <tt>:page</tt> (default is 1). Default
|
95
|
+
# <tt>:order</tt> is <tt>"id"</tt>, override if necessary.
|
96
|
+
#
|
97
|
+
# See {Faking Cursors in ActiveRecord}[http://weblog.jamisbuck.org/2007/4/6/faking-cursors-in-activerecord]
|
98
|
+
# where Jamis Buck describes this and a more efficient way for MySQL.
|
99
|
+
def paginated_each(options = {})
|
100
|
+
options = { :order => 'id', :page => 1 }.merge options
|
101
|
+
options[:page] = options[:page].to_i
|
102
|
+
options[:total_entries] = 0 # skip the individual count queries
|
103
|
+
total = 0
|
104
|
+
|
105
|
+
begin
|
106
|
+
collection = paginate(options)
|
107
|
+
with_exclusive_scope(:find => {}) do
|
108
|
+
# using exclusive scope so that the block is yielded in scope-free context
|
109
|
+
total += collection.each { |item| yield item }.size
|
110
|
+
end
|
111
|
+
options[:page] += 1
|
112
|
+
end until collection.size < collection.per_page
|
113
|
+
|
114
|
+
total
|
115
|
+
end
|
116
|
+
|
117
|
+
# Wraps +find_by_sql+ by simply adding LIMIT and OFFSET to your SQL string
|
118
|
+
# based on the params otherwise used by paginating finds: +page+ and
|
119
|
+
# +per_page+.
|
120
|
+
#
|
121
|
+
# Example:
|
122
|
+
#
|
123
|
+
# @developers = Developer.paginate_by_sql ['select * from developers where salary > ?', 80000],
|
124
|
+
# :page => params[:page], :per_page => 3
|
125
|
+
#
|
126
|
+
# A query for counting rows will automatically be generated if you don't
|
127
|
+
# supply <tt>:total_entries</tt>. If you experience problems with this
|
128
|
+
# generated SQL, you might want to perform the count manually in your
|
129
|
+
# application.
|
130
|
+
#
|
131
|
+
def paginate_by_sql(sql, options)
|
132
|
+
WillPaginate::Collection.create(*wp_parse_options(options)) do |pager|
|
133
|
+
query = sanitize_sql(sql.dup)
|
134
|
+
original_query = query.dup
|
135
|
+
# add limit, offset
|
136
|
+
add_limit! query, :offset => pager.offset, :limit => pager.per_page
|
137
|
+
# perfom the find
|
138
|
+
pager.replace find_by_sql(query)
|
139
|
+
|
140
|
+
unless pager.total_entries
|
141
|
+
count_query = original_query.sub /\bORDER\s+BY\s+[\w`,\s]+$/mi, ''
|
142
|
+
count_query = "SELECT COUNT(*) FROM (#{count_query})"
|
143
|
+
|
144
|
+
unless ['oracle', 'oci'].include?(self.connection.adapter_name.downcase)
|
145
|
+
count_query << ' AS count_table'
|
146
|
+
end
|
147
|
+
# perform the count query
|
148
|
+
pager.total_entries = count_by_sql(count_query)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
def respond_to?(method, include_priv = false) #:nodoc:
|
154
|
+
case method.to_sym
|
155
|
+
when :paginate, :paginate_by_sql
|
156
|
+
true
|
157
|
+
else
|
158
|
+
super(method.to_s.sub(/^paginate/, 'find'), include_priv)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
protected
|
163
|
+
|
164
|
+
def method_missing_with_paginate(method, *args) #:nodoc:
|
165
|
+
# did somebody tried to paginate? if not, let them be
|
166
|
+
unless method.to_s.index('paginate') == 0
|
167
|
+
if block_given?
|
168
|
+
return method_missing_without_paginate(method, *args) { |*a| yield(*a) }
|
169
|
+
else
|
170
|
+
return method_missing_without_paginate(method, *args)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
# paginate finders are really just find_* with limit and offset
|
175
|
+
finder = method.to_s.sub('paginate', 'find')
|
176
|
+
finder.sub!('find', 'find_all') if finder.index('find_by_') == 0
|
177
|
+
|
178
|
+
options = args.pop
|
179
|
+
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
|
180
|
+
options = options.dup
|
181
|
+
options[:finder] = finder
|
182
|
+
args << options
|
183
|
+
|
184
|
+
paginate(*args) { |*a| yield(*a) if block_given? }
|
185
|
+
end
|
186
|
+
|
187
|
+
# Does the not-so-trivial job of finding out the total number of entries
|
188
|
+
# in the database. It relies on the ActiveRecord +count+ method.
|
189
|
+
def wp_count(options, args, finder)
|
190
|
+
excludees = [:count, :order, :limit, :offset, :readonly]
|
191
|
+
excludees << :from unless ActiveRecord::Calculations::CALCULATIONS_OPTIONS.include?(:from)
|
192
|
+
|
193
|
+
# we may be in a model or an association proxy
|
194
|
+
klass = (@owner and @reflection) ? @reflection.klass : self
|
195
|
+
|
196
|
+
# Use :select from scope if it isn't already present.
|
197
|
+
options[:select] = scope(:find, :select) unless options[:select]
|
198
|
+
|
199
|
+
if options[:select] and options[:select] =~ /^\s*DISTINCT\b/i
|
200
|
+
# Remove quoting and check for table_name.*-like statement.
|
201
|
+
if options[:select].gsub('`', '') =~ /\w+\.\*/
|
202
|
+
options[:select] = "DISTINCT #{klass.table_name}.#{klass.primary_key}"
|
203
|
+
end
|
204
|
+
else
|
205
|
+
excludees << :select # only exclude the select param if it doesn't begin with DISTINCT
|
206
|
+
end
|
207
|
+
|
208
|
+
# count expects (almost) the same options as find
|
209
|
+
count_options = options.except *excludees
|
210
|
+
|
211
|
+
# merge the hash found in :count
|
212
|
+
# this allows you to specify :select, :order, or anything else just for the count query
|
213
|
+
count_options.update options[:count] if options[:count]
|
214
|
+
|
215
|
+
# forget about includes if they are irrelevant (Rails 2.1)
|
216
|
+
if count_options[:include] and
|
217
|
+
klass.private_methods.include?('references_eager_loaded_tables?') and
|
218
|
+
!klass.send(:references_eager_loaded_tables?, count_options)
|
219
|
+
count_options.delete :include
|
220
|
+
end
|
221
|
+
|
222
|
+
# we may have to scope ...
|
223
|
+
counter = Proc.new { count(count_options) }
|
224
|
+
|
225
|
+
count = if finder.index('find_') == 0 and klass.respond_to?(scoper = finder.sub('find', 'with'))
|
226
|
+
# scope_out adds a 'with_finder' method which acts like with_scope, if it's present
|
227
|
+
# then execute the count with the scoping provided by the with_finder
|
228
|
+
send(scoper, &counter)
|
229
|
+
elsif finder =~ /^find_(all_by|by)_([_a-zA-Z]\w*)$/
|
230
|
+
# extract conditions from calls like "paginate_by_foo_and_bar"
|
231
|
+
attribute_names = $2.split('_and_')
|
232
|
+
conditions = construct_attributes_from_arguments(attribute_names, args)
|
233
|
+
with_scope(:find => { :conditions => conditions }, &counter)
|
234
|
+
else
|
235
|
+
counter.call
|
236
|
+
end
|
237
|
+
|
238
|
+
count.respond_to?(:length) ? count.length : count
|
239
|
+
end
|
240
|
+
|
241
|
+
def wp_parse_options(options) #:nodoc:
|
242
|
+
raise ArgumentError, 'parameter hash expected' unless options.respond_to? :symbolize_keys
|
243
|
+
options = options.symbolize_keys
|
244
|
+
raise ArgumentError, ':page parameter required' unless options.key? :page
|
245
|
+
|
246
|
+
if options[:count] and options[:total_entries]
|
247
|
+
raise ArgumentError, ':count and :total_entries are mutually exclusive'
|
248
|
+
end
|
249
|
+
|
250
|
+
page = options[:page] || 1
|
251
|
+
per_page = options[:per_page] || self.per_page
|
252
|
+
total = options[:total_entries]
|
253
|
+
[page, per_page, total]
|
254
|
+
end
|
255
|
+
|
256
|
+
private
|
257
|
+
|
258
|
+
# def find_every_with_paginate(options)
|
259
|
+
# @options_from_last_find = options
|
260
|
+
# find_every_without_paginate(options)
|
261
|
+
# end
|
262
|
+
end
|
263
|
+
end
|
264
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
module WillPaginate
|
2
|
+
# This is a feature backported from Rails 2.1 because of its usefullness not only with will_paginate,
|
3
|
+
# but in other aspects when managing complex conditions that you want to be reusable.
|
4
|
+
module NamedScope
|
5
|
+
# All subclasses of ActiveRecord::Base have two named_scopes:
|
6
|
+
# * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
|
7
|
+
# * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly: <tt>Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)</tt>
|
8
|
+
#
|
9
|
+
# These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
|
10
|
+
# intermediate values (scopes) around as first-class objects is convenient.
|
11
|
+
def self.included(base)
|
12
|
+
base.class_eval do
|
13
|
+
extend ClassMethods
|
14
|
+
named_scope :scoped, lambda { |scope| scope }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module ClassMethods
|
19
|
+
def scopes
|
20
|
+
read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
|
21
|
+
end
|
22
|
+
|
23
|
+
# Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
|
24
|
+
# such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
|
25
|
+
#
|
26
|
+
# class Shirt < ActiveRecord::Base
|
27
|
+
# named_scope :red, :conditions => {:color => 'red'}
|
28
|
+
# named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# The above calls to <tt>named_scope</tt> define class methods <tt>Shirt.red</tt> and <tt>Shirt.dry_clean_only</tt>. <tt>Shirt.red</tt>,
|
32
|
+
# in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
|
33
|
+
#
|
34
|
+
# Unlike Shirt.find(...), however, the object returned by <tt>Shirt.red</tt> is not an Array; it resembles the association object
|
35
|
+
# constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
|
36
|
+
# <tt>Shirt.red.find(:all, :conditions => {:size => 'small'})</tt>. Also, just
|
37
|
+
# as with the association objects, name scopes acts like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>,
|
38
|
+
# <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really were an Array.
|
39
|
+
#
|
40
|
+
# These named scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce all shirts that are both red and dry clean only.
|
41
|
+
# Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
|
42
|
+
# for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
|
43
|
+
#
|
44
|
+
# All scopes are available as class methods on the ActiveRecord::Base descendent upon which the scopes were defined. But they are also available to
|
45
|
+
# <tt>has_many</tt> associations. If,
|
46
|
+
#
|
47
|
+
# class Person < ActiveRecord::Base
|
48
|
+
# has_many :shirts
|
49
|
+
# end
|
50
|
+
#
|
51
|
+
# then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
|
52
|
+
# only shirts.
|
53
|
+
#
|
54
|
+
# Named scopes can also be procedural.
|
55
|
+
#
|
56
|
+
# class Shirt < ActiveRecord::Base
|
57
|
+
# named_scope :colored, lambda { |color|
|
58
|
+
# { :conditions => { :color => color } }
|
59
|
+
# }
|
60
|
+
# end
|
61
|
+
#
|
62
|
+
# In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
|
63
|
+
#
|
64
|
+
# Named scopes can also have extensions, just as with <tt>has_many</tt> declarations:
|
65
|
+
#
|
66
|
+
# class Shirt < ActiveRecord::Base
|
67
|
+
# named_scope :red, :conditions => {:color => 'red'} do
|
68
|
+
# def dom_id
|
69
|
+
# 'red_shirts'
|
70
|
+
# end
|
71
|
+
# end
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
#
|
75
|
+
# For testing complex named scopes, you can examine the scoping options using the
|
76
|
+
# <tt>proxy_options</tt> method on the proxy itself.
|
77
|
+
#
|
78
|
+
# class Shirt < ActiveRecord::Base
|
79
|
+
# named_scope :colored, lambda { |color|
|
80
|
+
# { :conditions => { :color => color } }
|
81
|
+
# }
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
# expected_options = { :conditions => { :colored => 'red' } }
|
85
|
+
# assert_equal expected_options, Shirt.colored('red').proxy_options
|
86
|
+
def named_scope(name, options = {})
|
87
|
+
name = name.to_sym
|
88
|
+
scopes[name] = lambda do |parent_scope, *args|
|
89
|
+
Scope.new(parent_scope, case options
|
90
|
+
when Hash
|
91
|
+
options
|
92
|
+
when Proc
|
93
|
+
options.call(*args)
|
94
|
+
end) { |*a| yield(*a) if block_given? }
|
95
|
+
end
|
96
|
+
(class << self; self end).instance_eval do
|
97
|
+
define_method name do |*args|
|
98
|
+
scopes[name].call(self, *args)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
class Scope
|
105
|
+
attr_reader :proxy_scope, :proxy_options
|
106
|
+
|
107
|
+
[].methods.each do |m|
|
108
|
+
unless m =~ /(^__|^nil\?|^send|^object_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty\?|respond_to\?)/
|
109
|
+
delegate m, :to => :proxy_found
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
delegate :scopes, :with_scope, :to => :proxy_scope
|
114
|
+
|
115
|
+
def initialize(proxy_scope, options)
|
116
|
+
[options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
|
117
|
+
extend Module.new { |*args| yield(*args) } if block_given?
|
118
|
+
@proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
|
119
|
+
end
|
120
|
+
|
121
|
+
def reload
|
122
|
+
load_found; self
|
123
|
+
end
|
124
|
+
|
125
|
+
def first(*args)
|
126
|
+
if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
|
127
|
+
proxy_found.first(*args)
|
128
|
+
else
|
129
|
+
find(:first, *args)
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def last(*args)
|
134
|
+
if args.first.kind_of?(Integer) || (@found && !args.first.kind_of?(Hash))
|
135
|
+
proxy_found.last(*args)
|
136
|
+
else
|
137
|
+
find(:last, *args)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def empty?
|
142
|
+
@found ? @found.empty? : count.zero?
|
143
|
+
end
|
144
|
+
|
145
|
+
def respond_to?(method, include_private = false)
|
146
|
+
super || @proxy_scope.respond_to?(method, include_private)
|
147
|
+
end
|
148
|
+
|
149
|
+
protected
|
150
|
+
def proxy_found
|
151
|
+
@found || load_found
|
152
|
+
end
|
153
|
+
|
154
|
+
private
|
155
|
+
def method_missing(method, *args)
|
156
|
+
if scopes.include?(method)
|
157
|
+
scopes[method].call(self, *args)
|
158
|
+
else
|
159
|
+
with_scope :find => proxy_options do
|
160
|
+
proxy_scope.send(method, *args) { |*a| yield(*a) if block_given? }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def load_found
|
166
|
+
@found = find(:all)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
ActiveRecord::Associations::AssociationProxy.class_eval do
|
2
|
+
protected
|
3
|
+
def with_scope(*args)
|
4
|
+
@reflection.klass.send(:with_scope, *args) { |*a| yield(*a) if block_given? }
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
[ ActiveRecord::Associations::AssociationCollection,
|
9
|
+
ActiveRecord::Associations::HasManyThroughAssociation ].each do |klass|
|
10
|
+
klass.class_eval do
|
11
|
+
protected
|
12
|
+
alias :method_missing_without_scopes :method_missing_without_paginate
|
13
|
+
def method_missing_without_paginate(method, *args)
|
14
|
+
if @reflection.klass.scopes.include?(method)
|
15
|
+
@reflection.klass.scopes[method].call(self, *args) { |*a| yield(*a) if block_given? }
|
16
|
+
else
|
17
|
+
method_missing_without_scopes(method, *args) { |*a| yield(*a) if block_given? }
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Rails 1.2.6
|
24
|
+
ActiveRecord::Associations::HasAndBelongsToManyAssociation.class_eval do
|
25
|
+
protected
|
26
|
+
def method_missing(method, *args)
|
27
|
+
if @target.respond_to?(method) || (!@reflection.klass.respond_to?(method) && Class.respond_to?(method))
|
28
|
+
super
|
29
|
+
elsif @reflection.klass.scopes.include?(method)
|
30
|
+
@reflection.klass.scopes[method].call(self, *args)
|
31
|
+
else
|
32
|
+
@reflection.klass.with_scope(:find => { :conditions => @finder_sql, :joins => @join_sql, :readonly => false }) do
|
33
|
+
@reflection.klass.send(method, *args) { |*a| yield(*a) if block_given? }
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end if ActiveRecord::Base.respond_to? :find_first
|