Created
January 29, 2011 01:59
-
-
Save mrrooijen/801418 to your computer and use it in GitHub Desktop.
Inherited Resources - Mongoid collection hack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Option 1 (Probably best) | |
module MongoidActions | |
def collection | |
get_collection_ivar || set_collection_ivar(end_of_association_chain.all) | |
end | |
end | |
InheritedResources::Base.send :include, MongoidActions | |
## | |
# Option 2 | |
class ApplicationController < ActionController::Base | |
protect_from_forgery | |
def self.set_collection(collection_object) | |
define_method :collection do | |
instance_variable_set( | |
"@#{collection_object}", | |
end_of_association_chain.paginate(:page => params[:page]) | |
) | |
end | |
end | |
end | |
class PostsController < ApplicationController | |
inherit_resources | |
set_collection :posts | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Surely a silly question, but where would you place the first option? In a mongoid_actions.rb file within lib/ ?