Ich habe gelesen, dass die Verwendung von Vorbildliche Bedenken um fette Modelle zu enthäuten und Ihre Modellcodes zu TROCKNEN. Hier ist eine Erklärung mit Beispielen:
1) Austrocknen von Modellcodes
Betrachten Sie ein Artikelmodell, ein Ereignismodell und ein Kommentarmodell. Ein Artikel oder ein Ereignis hat viele Kommentare. Ein Kommentar gehört entweder zu einem Artikel oder einem Ereignis.
Traditionell können die Modelle wie folgt aussehen:
Kommentar Modell:
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
Artikel Modell:
class Article < ActiveRecord::Base
has_many :comments, as: :commentable
def find_first_comment
comments.first(created_at DESC)
end
def self.least_commented
#return the article with least number of comments
end
end
Ereignis-Modell
class Event < ActiveRecord::Base
has_many :comments, as: :commentable
def find_first_comment
comments.first(created_at DESC)
end
def self.least_commented
#returns the event with least number of comments
end
end
Wie wir feststellen können, gibt es einen wichtigen Teil des Codes, der sowohl für Event als auch für Article gilt. Mit concerns können wir diesen gemeinsamen Code in einem separaten Modul Commentable extrahieren.
Erstellen Sie dazu eine kommentierbare.rb-Datei in app/models/concerns.
module Commentable
extend ActiveSupport::Concern
included do
has_many :comments, as: :commentable
end
# for the given article/event returns the first comment
def find_first_comment
comments.first(created_at DESC)
end
module ClassMethods
def least_commented
#returns the article/event which has the least number of comments
end
end
end
Und jetzt sehen Ihre Modelle so aus:
Kommentar Modell:
class Comment < ActiveRecord::Base
belongs_to :commentable, polymorphic: true
end
Artikel Modell:
class Article < ActiveRecord::Base
include Commentable
end
Ereignis-Modell:
class Event < ActiveRecord::Base
include Commentable
end
2) Skin-nizing Fat Models.
Betrachten Sie ein Ereignismodell. Eine Veranstaltung hat viele Teilnehmer und Kommentare.
Typischerweise könnte das Ereignismodell wie folgt aussehen
class Event < ActiveRecord::Base
has_many :comments
has_many :attenders
def find_first_comment
# for the given article/event returns the first comment
end
def find_comments_with_word(word)
# for the given event returns an array of comments which contain the given word
end
def self.least_commented
# finds the event which has the least number of comments
end
def self.most_attended
# returns the event with most number of attendes
end
def has_attendee(attendee_id)
# returns true if the event has the mentioned attendee
end
end
Modelle mit vielen Assoziationen haben die Tendenz, immer mehr Code anzuhäufen und unüberschaubar zu werden. Concerns bieten eine Möglichkeit, fette Module zu verschlanken, so dass sie modularer und leichter zu verstehen sind.
Das obige Modell kann mit Hilfe von Bedenken wie folgt umstrukturiert werden: Erstellen Sie eine attendable.rb
y commentable.rb
Datei im Ordner app/models/concerns/event
attendable.rb
module Attendable
extend ActiveSupport::Concern
included do
has_many :attenders
end
def has_attender(attender_id)
# returns true if the event has the mentioned attendee
end
module ClassMethods
def most_attended
# returns the event with most number of attendes
end
end
end
kommentierbar.rb
module Commentable
extend ActiveSupport::Concern
included do
has_many :comments
end
def find_first_comment
# for the given article/event returns the first comment
end
def find_comments_with_word(word)
# for the given event returns an array of comments which contain the given word
end
module ClassMethods
def least_commented
# finds the event which has the least number of comments
end
end
end
Wenn Sie nun Concerns verwenden, reduziert sich Ihr Ereignismodell auf
class Event < ActiveRecord::Base
include Commentable
include Attendable
end
* Bei der Verwendung von Konzernen ist es ratsam, die Gruppierung auf der Basis von "Domänen" und nicht auf der Basis von "technischen" Gruppierungen vorzunehmen. Bereichsbasierte Gruppierungen sind z.B. 'Kommentierbar', 'Fotografierbar', 'Teilnehmbar'. Technische Gruppierung bedeutet 'ValidationMethods', 'FinderMethods' usw.