PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Developing Rails plugin: need some metaprog.
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Developing Rails plugin: need some metaprog.

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 13h00   #1
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Developing Rails plugin: need some metaprog.

Hi all

I'm developing an authorization plugin that looks like the following so
far:

module Incense
module Authorization
def self.included(base_class)
base_class.extend(ClassMethods)
end

module ClassMethods
def performs_authorization(options = {})
before_filter :prepare_user

include(InstanceMethods)
end
end
end

module InstanceMethods
private
def prepare_user
session[:user] ||= Member.find_by_id(2) # When there's no user_id
in the session, then use #2 for Guest
...
end

Because I extracted this functionality from an application, in
prepare_user() there's still a hardwired "Member" model used. But in
another application I need this to be a "User" model, so I'd like to be
able to specify the model to use in the options hash of the
performs_authorization() method. So how can I achieve this? Do I have to
use define_method() in the context of performs_authorization? This would
work, I guess, but I don't like it very much... Isn't there another way?

Thanks
Josh
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 13h01   #2
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Developing Rails plugin: need some metaprog.

Maybe I can pass arguments to include() somehow?

include(InstanceMethods, model_to_use)

?
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 22/11/2007, 17h45   #3
Giles Bowkett
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Developing Rails plugin: need some metaprog.

> Maybe I can pass arguments to include() somehow?
>
> include(InstanceMethods, model_to_use)


That should be fine. Until you try to access it, it's just a variable.
You can have

variable.do_something()

and then later assign different objects to that variable. e.g.:

def sound(beast)
beast.make_noise
end

and then

sound(Cow) # => "moo"
sound(Dog) # => "woof"
sound(Rabbit) # => "squee"

--
Giles Bowkett

Podcast: http://hollywoodgrit.blogspot.com
Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org
Tumblelog: http://giles.tumblr.com

  Réponse avec citation
Vieux 23/11/2007, 19h59   #4
Jean-François Trân
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Developing Rails plugin: need some metaprog.

Hi Joshua,

> I'm developing an authorization plugin that looks like the
> following so far:
>
> module Incense
> module Authorization
> def self.included(base_class)
> base_class.extend(ClassMethods)
> end
>
> module ClassMethods
> def performs_authorization(options =3D {})
> before_filter :prepare_user
>
> include(InstanceMethods)
> end
> end
> end
>
> module InstanceMethods
> private
> def prepare_user
> session[:user] ||=3D Member.find_by_id(2) # When there's no user_id
> in the session, then use #2 for Guest
> ...
> end
>
> Because I extracted this functionality from an application, in
> prepare_user() there's still a hardwired "Member" model used.
> But in another application I need this to be a "User" model, so
> I'd like to be able to specify the model to use in the options
> hash of the performs_authorization() method. So how can I
> achieve this? Do I have to use define_method() in the context of
> performs_authorization? This would work, I guess, but I don't like
> it very much... Isn't there another way?


A simple way consists in storing the options hash as a class
variable, you can then create instance and class method
to get the class from the options hash :

def performs_authorization(options =3D {})
before_filter :prepare_user

cattr_accessor ptions
self.options =3D options

include(InstanceMethods)
end

# give the name you want
def user_model
options[:user_model]
end

module InstanceMethods
private
def user_model
self.class.user_model
end

def prepare_user
session[:user] ||=3D user_model.find_by_id(2)
...
end
end

HTH,

-- Jean-Fran=E7ois.

  Réponse avec citation
Vieux 26/11/2007, 09h50   #5
Joshua Muheim
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Developing Rails plugin: need some metaprog.

Thank you, Jean-François! But is this good style? I'm always looking for
well written patterns, and maybe I should dig deeper into Rails for
having a better overwiew about how they do things. :-)
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h44.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,60226 seconds with 13 queries