6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/xhive/active_record_extensions.rb', line 6
def mount_page(attr)
define_method(attr.to_s) do
site = Xhive::Site.first
begin
page = Xhive::Mapper.page_for(site, self.class.name.downcase, attr.to_s, id)
rescue ActiveRecord::RecordNotFound
page = nil
end
return page
end
define_method("#{attr}=") do |value|
fail RecordNotPersistedError, "Cannot assign a page to an unsaved #{self.class.name}" if new_record?
site = Xhive::Site.first
page = value.is_a?(Xhive::Page) ? value : site.pages.find(value)
Xhive::Mapper.map_resource(site, page, self.class.name.downcase, attr.to_s, id)
end
define_method("#{attr}_content") do
self.send(attr.to_sym).present_content(self.class.name.downcase.to_sym => self)
end
end
|