my $query_base = "SELECT ACL.id from ACL, Groups, Principals, CachedGroupMembers WHERE " . "(ACL.RightName = 'SuperUser' OR ACL.RightName = '$right') " # Never find disabled groups. . "AND Principals.Disabled = 0 " . "AND CachedGroupMembers.Disabled = 0 " # We always grant rights to Groups . "AND Principals.id = Groups.id " . "AND Principals.PrincipalType = 'Group' " # See if the principal is a member of the group recursively or _is the rightholder_ # never find recursively disabled group members # also, check to see if the right is being granted _directly_ to this principal, # as is the case when we want to look up group rights . "AND Principals.id = CachedGroupMembers.GroupId " . "AND CachedGroupMembers.MemberId = ". $self->Id ." " # Make sure the rights apply to the entire system or to the object in question . "AND ($check_objects) "; # The groups query does the query based on group membership and individual user rights my $groups_query = $query_base # limit the result set to groups of types ACLEquivalence (user), # UserDefined, SystemInternal and Personal. All this we do # via (ACL.PrincipalType = 'Group') condition . "AND ACL.PrincipalId = Principals.id " . "AND ACL.PrincipalType = 'Group' "; $self->_Handle->ApplyLimits( \$groups_query, 1 ); #only return one result my $hitcount = $self->_Handle->FetchResult($groups_query); return 1 if $hitcount; # get out of here if success # The roles query does the query based on roles my $roles_query = $query_base . "AND ACL.PrincipalType = Groups.Type " . "AND ($check_roles) "; $self->_Handle->ApplyLimits( \$roles_query, 1 ); #only return one result
un ORM en ruby
développé pour jouer la couche Modèle de Ruby on Rails
utilisable indépendamment de RoR
pas de XML
réflexivité et extension dynamique au chargement
magique n'est pas une insulte
utilisez du SQL dans les cas tordus et/ou pour la performance
pourquoi dupliquer la description de vos données, votre base la connait
def self.up add_column :inscriptions, :confirmation, :boolean drop_table :asupprimer end
% rake db:migrate (in /home/nc/travail/rails/fpw2006/test) == ModificationSchema: migrating ============================================== -- add_column(:inscriptions, :confirmation, :boolean) -> 0.1418s -- drop_table(:asupprimer) -> 0.0148s == ModificationSchema: migrated (0.2641s) =====================================
class Rando < ActiveRecord::Base end
randos
» :
CREATE TABLE "randos" ( "id" integer NOT NULL PRIMARY KEY, "jour" date NOT NULL, "titre" varchar(250) NOT NULL, "distance" integer NOT NULL, "rythme" varchar(1) NOT NULL, "publication" date NOT NULL, "descriptif" text NOT NULL );
class Animateur < ActiveRecord::Base belongs_to :adherent has_and_belongs_to_many :randos has_many :filleuls, :class_name => "Animateur", :foreign_key => "parrain_id" belongs_to :parrain, :class_name => "Animateur", :foreign_key => "parrain_id" end
>> Adherent.find(:all).each { |a| puts a.nom } Lagaffe Le Rouge Prunelle
>> Adherent.find_by_nom_and_prenom("Lagaffe", "Gaston", :select => "nom") => #<Adherent:0xb74cfb8c @attributes={"nom"=>"Lagaffe"}>
>> Adherent.find(:all, :conditions => ["echeance > ? and naissance < ?", Time.now, Time.local(1970,1,1)]) => [#<Adherent:0xb74a934c @attributes={"nom"=>"Lagaffe"...
>> a = Adherent.find_by_nom("Lagaffe") => #<Adherent:0xb75f1330 ... "naissance"=>"2004-04-06"
>> a.naissance="06-01-1966" => "06-01-1966"
>> a.save => true
>> Adherent.find_by_nom("Lagaffe").naissance.to_s => "1966-01-06"
>> a = Adherent.find_by_nom("Le Rouge") => #<Adherent:0xb73dc84c ...
>> a.destroy
>> Adherent.find_by_nom("Le Rouge") => nil
>> Rando.find(1).animateurs << Animateur.find(1) => [#<Animateur:0xb745f648 @attributes={"id"=>"1", ...
>> Rando.find(1).animateurs => [#<Animateur:0xb7445040 @attributes={"id"=>"1", "adherent_id"=>"3", "rando_id"=>"1", "animateur_id"=>"1", "parrain_id"=>nil, "date_diplome"=>"2000-04-01 00:00:00"}>]
>> Rando.find(1).animateurs.each { |a| puts a.adherent.nom } Prunelle
class Rando < ActiveRecord::Base validates_presence_of :jour, :titre, :rythme validates_length_of :titre, :maximum => 250 validates_length_of :nom, :in => 3..50 validates_format_of :nom, :with => /^\w+$/ validates_numericality_of :distance validates_inclusion_of :rythme, :in %w{L M S R} end
class Rando < ActiveRecord::Base def validate ... end end
before_validation, before_validation_on_create, before_validation_on_update after_validation, before_save, before_create, after_save, after_update, after_create, before_destroy, after_destroy
class Adherent < ActiveRecord::Base def before_create self.adhesion ||= Time.now end
def after_create logger.info "Adhérent #{adherent.id} modifié" end end