Skip to content

Instantly share code, notes, and snippets.

@mrrooijen
Created January 29, 2011 01:59
Show Gist options
  • Save mrrooijen/801418 to your computer and use it in GitHub Desktop.
Save mrrooijen/801418 to your computer and use it in GitHub Desktop.
Inherited Resources - Mongoid collection hack
##
# 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
@olivierlacan
Copy link

Surely a silly question, but where would you place the first option? In a mongoid_actions.rb file within lib/ ?

@tbbooher
Copy link

i'm wondering the same thing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment