Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow scoping finds and creates by non parent #2

Open
2collegebums opened this issue Aug 17, 2009 · 0 comments
Open

allow scoping finds and creates by non parent #2

2collegebums opened this issue Aug 17, 2009 · 0 comments

Comments

@2collegebums
Copy link

we often use the pattern of scoping all of our controllers by objects that are not explicitly set as parents. This is a bit like the begin_of_association_chain method in inherited_resources (found: http://github.com/josevalim/inherited_resources/tree/master ).

a quick solution that we implemented is as follows:

in nested.rb

module ResourceController
 module Helpers
   module Nested
     protected 
      ....
    def scoped?
      scoping_object.present?
    end

    def scope_association
      @scope_association ||= scoping_object.send(model_name.to_s.pluralize.to_sym)
    end

    def end_of_association_chain
      return scope_association if scoped?
      parent? ? parent_association : model
    end
...
end

and then in the controller it is used as follows:

def scoping_object
   current_facility
end

This will scope all finds using current_facility.associations.find and create using
current_facility.associations.new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant