hi there, Michael, Good evening :)
I'm just trying to figure out how to make functions in the base class (application controller) such that resources/objects inheriting from the base class will be able to use them. 1. I've actually got an existing application which has the method implemented individually (ie. is_owner_or_admin() is being implemented in posts_controller.rb and coupons_controller.rb). 2. the purpose of this method is to return true if the current object is being accessed by its owner/creator OR by an admin user. Hence, I wanted to extract that functionality into a base class (ie. the application controller) and pass that method an object to evaluate. 3. I am trying to port over a rails 2.3.8 application into rails 3. In rails 2.3.8, i was using authlogic but in my attempt to port it over to rails 3, i am using devise. I do not think device would have any functionality as described in the previous point (2) but i might just look at Aegis, CanCan and some others for reference. 4. Your reply on "symbols aren't method calls" is so right :) I think I might have figured out how to solve my initial problem :) Thank you :) Gordon Yeong On Jun 1, 8:50 pm, Michael Pavling <[email protected]> wrote: > On 1 June 2011 11:36, ct9a <[email protected]> wrote: > > > Scenario: I have 2 resources: Posts (for blogs) and Coupons. > > > Common method: is_owner_or_admin( object ) > > > I have defined this method within the application controller as I > > figured it's akin the base class that the controllers for the > > resources will inherit from. > > You might find it makes more sense as a method on the objects > themselves - it's a bit more in the spirit of OO this way too, whereas > the "common method" in application_controller is a bit imperative. > > So you can do stuff like: > coupon = Coupon.new > if coupon.is_owned_or_administered_by(current_user) # no need to > put the result of "get_common_user" in an instance variable - and > "common_user" is a bit more commonly used > # do your stuff here... > end > > > So, in posts_controller.rb , I do: > > > class PostsController < ApplicationController > > before_filter :authenticate_user!, > > :except => [:index, :show, :send_message, :get_subcategories] > > > def show > > @post = Post.find(params[:id]) > > :is_owner_or_admin(@post) > > Looks like you're reinventing authorization processes - it would save > you some time if you looked at Aegis, CanCan and the like... > > > /home/ct9a/projects/port_over_to_rails3/app/controllers/ > > posts_controller.rb:20: syntax error, unexpected '(', expecting > > keyword_end > > :is_owner_or_admin(@post) > > well yes - symbols aren't method calls. > > You should really be looking at either doing: > @post.is_owner_or_admined_by(current_user) > or > current_user.owns_or_administers(@post) > which way round is your choice, and either way can be utilised in the > permissions model of your chosen authentication gem. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

