hi, guys, I would like to define a common method in the application controller because the other resources will use this method. 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. I''m just testing the syntax call so, please don''t look at this from a process design point of view :) application_controller.rb ================ class ApplicationController < ActionController::Base protect_from_forgery def is_owner_or_admin(obj) @part = Part.find(params[:id]) @current_user = get_current_user if ( @current_user == nil ) flash[:notice] = ''Access denied'' redirect_to root_url return false elsif ( @current_user.is_admin or (@current_user.login =@part.created_by) ) return true else flash[:notice] = ''Access denied'' redirect_to root_url return false end end end I''m trying to call this method in each of the before_filter in the resources (ie. Posts, Coupons). 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) respond_to do |format| format.html # show.html.erb format.xml { render :xml => @post } end end ..... end Anyway, when I run the rails server or run "ruby -w posts_controller.rb", I get the following error: /home/ct9a/projects/port_over_to_rails3/app/controllers/ posts_controller.rb:20: syntax error, unexpected ''('', expecting keyword_end :is_owner_or_admin(@post) ^ -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 1 June 2011 11:36, ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 <pavl...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 June 2011 11:36, ct9a <anexi...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Jun-01 11:58 UTC
Re: Re: Creating common method in application ontroller
On 1 June 2011 12:36, ct9a <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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.well, you''ve done that already - any method you write in application_controller is available to any class that inherits from it.> 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).But what happens when you''re not using a controller; maybe when you''re at the console, or in a script? - then these methods wouldn''t be available. They relate to the objects - so they should (on balance) live there. If you think they should be in the application_controller (or any individual controller) that''s probably the old procedural programming habits showing their heads :-)> 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.Devise is for authentication (logging in), Aegis & CanCan are for authorization (can a user, logged in or not, do this thing...) - they''re very different problem domains. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2011-Jun-01 12:10 UTC
Re: Re: Creating common method in application ontroller
> > > But what happens when you''re not using a controller; maybe when you''re > at the console, or in a script? - then these methods wouldn''t be > available. They relate to the objects - so they should (on balance) > live there. If you think they should be in the application_controller > (or any individual controller) that''s probably the old procedural > programming habits showing their heads :-) > >yeah, in some ways, that has shown some procedural programming trait (my day job sees me coding in both procedural and OO environments and my arguments to totally be rid of procedural programming have been knocked back strongly :( ). Yes, that is a valid point but given that the resources I deal with can only work in the web domain as the app i have here is very simple albeit I do deal with applications that have deal with data on and offline (xml file transfers and etc) :)> Devise is for authentication (logging in), Aegis & CanCan are for > authorization (can a user, logged in or not, do this thing...) - > they''re very different problem domains. >Yep, you''re right, Michael :) For my own app, I have just simplified it to have 2 types of users (ie. ''admin'' and ''regular'' types). Hence, I done away with using authorization gems as it''s a simple application :) Sweet. Nice discussion. Thank you :) Loving rails since ver 2.3.x and finding sanity in it, Gordon Yeong :) -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Jun-01 12:36 UTC
Re: Re: Creating common method in application ontroller
On 1 June 2011 13:10, Gordon Yeong <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> my arguments > to totally be rid of procedural programming have been knocked back strongly > :( ).You don''t fix that by still using procedural methods when you''re working in Rails ;-) Demonstrate the efficiencies of following OO methods, and there''s no argument. There''s very few occasions when a procedural approach is better. The smaller the app, the more important following the design standards is, so you can change it quickly later - no client likes hearing "we''ll have to rewrite the whole thing to do that..." when they ask for a little extra functionality to a ''simple'' site (I know... I''ve had to say it in the past! :-D Regards, -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2011-Jun-01 12:48 UTC
Re: Re: Creating common method in application ontroller
Yep, agreed. Using rails and domolicious just reinstates my sanity (ie OO methodology) which I work with after my day job. Seems what I have done in my rails 2.3.8 application was right all along as in, i declared the method for each resource and called the method within the resource''s object (ie. post.is_owner_or_admin and coupon.is_owner_or_admin) :) Cheers :D On 1 June 2011 22:36, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 1 June 2011 13:10, Gordon Yeong <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > my arguments > > to totally be rid of procedural programming have been knocked back > strongly > > :( ). > > You don''t fix that by still using procedural methods when you''re > working in Rails ;-) > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Out of curiosity what should one do if one wants to have a method inherited by all models? Does rails already have a file for adding methods to the model base class? Or do we just ram our methods into ActiveModel and call it a day? I have run into almost the opposite scenario as the thread starter. Mainly with certain integrity checks I''ve needed to perform. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/dmlfeUdaRFN1U2NK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Jun-03 17:41 UTC
Re: Re: Creating common method in application ontroller
On 3 June 2011 18:34, Kevin <darkintent-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Out of curiosity what should one do if one wants to have a method inherited > by all models?You can monkey patch Object or Class if you really want... # patch in environment.rb class Object def your_method puts "my method is here" end end # console>> o = AnyObject.new=> #<AnyObject id: etc>>> o.your_methodmy method is here => nil>> String.your_methodmy method is here => nil -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2011-Jun-03 22:38 UTC
Re: Re: Creating common method in application ontroller
Monkey patches are evil. On Jun 4, 2011 3:42 AM, "Michael Pavling" <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3 June 2011 18:34, Kevin <darkintent-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Out of curiosity what should one do if one wants to have a methodinherited>> by all models? > > You can monkey patch Object or Class if you really want... > > # patch in environment.rb > class Object > def your_method > puts "my method is here" > end > end > > # console >>> o = AnyObject.new > => #<AnyObject id: etc> >>> o.your_method > my method is here > => nil >>> String.your_method > my method is here > => nil > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email torubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org> For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.>-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 3 June 2011 18:34, Kevin <darkintent-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Out of curiosity what should one do if one wants to have a method inherited > by all models? Does rails already have a file for adding methods to the > model base class? Or do we just ram our methods into ActiveModel and call > it a day? I have run into almost the opposite scenario as the thread > starter. Mainly with certain integrity checks I''ve needed to perform.Can one derive a class for ActiveModel, put the common methods in there, and then derive the models from that? I have not tried it. Colin -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2011-Jun-04 11:26 UTC
Re: Re: Creating common method in application ontroller
On 3 June 2011 23:38, Gordon Yeong <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jun 4, 2011 3:42 AM, "Michael Pavling" <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> You can monkey patch Object or Class if you really want... > > Monkey patches are evil."Evil" is a bit strong :-) They can have a place. I''ve found it invaluable in the past to add a patch for ActiveRecord::Errors to merge two error objects'' messages together. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Google seems to indicate that one may simply mixin (Is that the correct Ruby terminology.) parts of ActiveModel into any class as one sees fit. For example:http://yehudakatz.com/2010/01/10/activemodel-make-any-ruby-object-feel-like-activerecord/ If memory about the way inheritance works in Ruby serves me we should be able to do something like that, without even needing to derive from ActiveModel directly. Though in the case of a Rails application one would probably just create a stub class that derives from ActiveRecord::Base stick the common methods in there and then subclass that class since ActiveModel does not include the database related functionality. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/SGFNOFFjZGpKOTBK. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Gordon Yeong
2011-Jun-05 00:05 UTC
Re: Re: Creating common method in application ontroller
hi, Michael:) they are evil when you have to decommission some gems that are no longer supported and change from one rails 2.3.x-1 to rails 2.3.x. I had that in the past about a year plus... Madness lol On 4 June 2011 21:26, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 3 June 2011 23:38, Gordon Yeong <anexiole-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > On Jun 4, 2011 3:42 AM, "Michael Pavling" <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> You can monkey patch Object or Class if you really want... > > > > Monkey patches are evil. > > "Evil" is a bit strong :-) > > They can have a place. I''ve found it invaluable in the past to add a > patch for ActiveRecord::Errors to merge two error objects'' messages > together. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.