This commit is contained in:
22
app/models/leafable.rb
Normal file
22
app/models/leafable.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
module Leafable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
TYPES = %w[ Page Section Picture ]
|
||||
|
||||
included do
|
||||
has_one :leaf, as: :leafable, inverse_of: :leafable, touch: true
|
||||
has_one :book, through: :leaf
|
||||
|
||||
delegate :title, to: :leaf
|
||||
end
|
||||
|
||||
class_methods do
|
||||
def leafable_name
|
||||
@leafable_name ||= ActiveModel::Name.new(self).singular.inquiry
|
||||
end
|
||||
end
|
||||
|
||||
def leafable_name
|
||||
self.class.leafable_name
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user