page_content.rb
5.02 KB
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
require 'kanjai/template_generator'
module Kanjai
class PageContent < ActiveRecord::Base
belongs_to :page
belongs_to :page_datum
has_many :page_content_markers, dependent: :destroy
accepts_nested_attributes_for :page_content_markers
def self.edit_template(content_type)
case content_type
when 'rte_editor'
return ['kanjai/admin/pages/content_types/rte_editor', {}]
when 'plugin'
return ['kanjai/admin/pages/content_types/plugin', {}]
else
template_part = TemplatePart.find_by_code(content_type)
if (template_part.part_type == 'content')
return ['kanjai/admin/pages/content_types/content', {template_part: template_part}]
end
end
end
def build_markers(type_content)
if type_content != 'rte_editor' and type_content != self.type_content
self.update_column(:type_content, type_content)
self.page_content_markers.each do |item|
item.destroy
end
page_template = self.page.page_template
if page_template
subpart = TemplatePart.find_by_code(type_content)
if subpart
#find repeat block
repeat_element = subpart.field_options.select{|item| item[:name] == 'repeat' }.first
count_repeat = nil
if repeat_element and repeat_element[:attributes]['count'].present?
count_repeat = repeat_element[:attributes]['count'].to_i
end
subpart.field_options.each do |item|
if item[:name] != 'repeat'
marker_obj = self.page_content_markers.find_by_marker(item[:name])
unless marker_obj
if item[:attributes]['repeat'].to_s == 'true'
if count_repeat.to_i > 0
(1..count_repeat).each do |index|
self.page_content_markers.create({
marker: item[:name],
row_item: index
})
end
end
else
self.page_content_markers.create({
marker: item[:name]
})
end
end
end
end
end
end
end
end
def build_markers_rows(type_content)
row_item = nil
if type_content != 'rte_editor'
page_template = self.page.page_template
if page_template
subpart = TemplatePart.find_by_code(type_content)
if subpart
#find current max row_item
row_item = self.page_content_markers.collect(&:row_item).max
row_item = 0 if row_item.nil?
row_item += 1
subpart.field_options.each do |item|
if item[:name] != 'repeat'
if item[:attributes]['repeat'].to_s == 'true'
self.page_content_markers.create({
marker: item[:name],
row_item: row_item
})
end
end
end
end
end
end
return row_item
end
def delete_markers_row(row_item)
self.page_content_markers.where(row_item: row_item).delete_all
end
def get_content
if self.type_content == 'rte_editor'
I18n.t('admin.content_types.rte_editor')
elsif self.type_content == 'plugin'
"#{I18n.t('admin.content_types.plugin')}: #{controller_name}.#{action_name}"
else
self.type_content
end
end
def get_content_frontend
if self.type_content == 'rte_editor'
self.text_html.to_s
elsif self.type_content == 'plugin'
controller = self.controller_name.constantize.new
controller.send self.action_name
controller_variables = controller.instance_variables
hash = {}
controller_variables.each do |variable|
hash[variable.to_s[1..-1]] = controller.instance_variable_get(variable.to_s)
end
self.controller_name.constantize.render self.action_name.to_sym, assigns: hash
else
page_template = self.page.page_template
subpart = TemplatePart.find_by_code(self.type_content)
html = Kanjai::TemplateGenerator.subpart_generate(page_template, 'content', subpart.elements, self.marker_hash, self.id)
html
end
end
def get_simple_content
subpart = TemplatePart.find_by_code(self.type_content)
subpart.source
end
def marker_hash
result = {}
self.page_content_markers.order(:row_item).each do |item|
result[item.row_item] ||= {}
result[item.row_item][item.marker] = item.text_value
end
result
end
end
end