Module: Sibu::PagesHelper

Defined in:
app/helpers/sibu/pages_helper.rb

Instance Method Summary collapse

Instance Method Details

#available_docsObject



26
27
28
# File 'app/helpers/sibu/pages_helper.rb', line 26

def available_docs
  options_from_collection_for_select(Sibu::Document.for_user(sibu_user), :file_url, :file_name, @element["value"])
end


22
23
24
# File 'app/helpers/sibu/pages_helper.rb', line 22

def available_links
  options_from_collection_for_select(@site.pages.order(:name), :id, :name, @element["value"])
end

#current_elt(elt) ⇒ Object



271
272
273
# File 'app/helpers/sibu/pages_helper.rb', line 271

def current_elt(elt)
  (elt.is_a?(Hash) ? elt : (select_element(elt) || {})).except('elements')
end

#default_content(type) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'app/helpers/sibu/pages_helper.rb', line 275

def default_content(type)
  case type
  when "text"
    {"text" => Sibu::DEFAULT_TEXT}
  when "link"
    {"value" => "", "text" => Sibu::DEFAULT_TEXT}
  when "paragraph"
    {"text" => Sibu::DEFAULT_PARAGRAPH}
  when "media"
    {"src" => Sibu::DEFAULT_IMG}
  when "embed"
    {"code" => '<p>Contenu HTML personnalisé (iframe, etc...)</p>'}
  end
end

#elements(id = nil) ⇒ Object



118
119
120
121
# File 'app/helpers/sibu/pages_helper.rb', line 118

def elements(id = nil)
  items = id ? select_element(id)["elements"] : @sb_entity.find_or_init(*@sb_section)["elements"]
  items.blank? ? [{"id" => "el#{Time.current.to_i}"}] : items
end

#elt_id(elt) ⇒ Object



267
268
269
# File 'app/helpers/sibu/pages_helper.rb', line 267

def elt_id(elt)
  elt.is_a?(Hash) ? (elt["data-id"] || elt["id"]) : elt
end

#elts(id) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
# File 'app/helpers/sibu/pages_helper.rb', line 216

def elts(id)
  items = []
  element_id = elt_id(id)
  elemnts = @sb_entity.elements(*(@sb_section + element_id.split("|")).uniq)
  if elemnts
    elemnts.each do |e|
      e["data-id"] = [element_id, e["id"]].join("|")
      items << e
    end
  end
  items.blank? ? [{"id" => element_id.split("|").last, "data-id" => [element_id, "#{element_id}0"].join("|")}] : items
end

#embed(elt, opts = {}) ⇒ Object



169
170
171
172
173
174
# File 'app/helpers/sibu/pages_helper.rb', line 169

def embed(elt, opts = {})
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt, "code" => '<p>Contenu HTML personnalisé (iframe, etc...)</p>'}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  opts.merge!({data: {id: elt_id(elt), type: "embed"}}) if action_name != 'show'
  (:div, (:p, raw(content["code"]).html_safe), opts)
end

#empty_tag(elt, tag, type, opts = {}) ⇒ Object



148
149
150
151
152
153
154
# File 'app/helpers/sibu/pages_helper.rb', line 148

def empty_tag(elt, tag, type, opts = {})
  repeat = opts.delete(:repeat)
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt}.merge!(default_content(type))
  opts = defaults.merge(opts)
  opts.merge!({data: {id: elt_id(elt), type: type, repeat: repeat}}) if action_name != 'show'
  (tag, nil, opts)
end

#form_label(elt, html_opts = {}, &block) ⇒ Object



156
157
158
159
160
161
# File 'app/helpers/sibu/pages_helper.rb', line 156

def form_label(elt, html_opts = {}, &block)
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt, "text" => Sibu::DEFAULT_TEXT}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  html_opts.merge!({data: {id: elt_id(elt), type: "text"}}) if action_name != 'show'
  (:label, raw(content["text"]).html_safe, html_opts)
end

#grp(elt, opts = {}, &block) ⇒ Object



138
139
140
141
142
143
144
145
146
# File 'app/helpers/sibu/pages_helper.rb', line 138

def grp(elt, opts = {}, &block)
  wrapper = opts.delete(:wrapper) || :div
  repeat = opts.delete(:repeat)
  children = opts.delete(:children)
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt}
  opts = defaults.merge(opts)
  opts.merge!({data: {id: elt_id(elt), type: "group", repeat: repeat, children: children}}) if action_name != 'show'
  (wrapper, capture(*elts(elt), &block), opts)
end

#img(elt, opts = {}) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/helpers/sibu/pages_helper.rb', line 123

def img(elt, opts = {})
  wrapper = opts.delete(:wrapper)
  repeat = opts.delete(:repeat)
  size = opts.delete(:size)
  defaults = {"id" => elt.is_a?(Hash) ? elt["id"] : elt, "src" => Sibu::DEFAULT_IMG}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  if action_name == 'show'
    content["src"] = ("/#{conf[:deployment_path]}" + content["src"]) if @online && conf[:deployment_path]
  else
    opts.merge!({data: {id: elt_id(elt), type: "media", repeat: repeat, size: size}})
  end
  wrapper ? (wrapper, (:img, nil, content.except("id")), opts)
      : (:img, nil, content.except("id").merge(opts.stringify_keys) {|k, old, new| k == 'class' ? [old, new].join(' ') : new})
end

#interactive_map(elt, html_opts = {}) ⇒ Object



260
261
262
263
264
265
# File 'app/helpers/sibu/pages_helper.rb', line 260

def interactive_map(elt, html_opts = {})
  defaults = {"data-lat" => "46.1988027", "data-lng" => "5.1748288", "data-title" => Sibu::DEFAULT_TEXT}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {"id" => elt}))
  html_opts.merge!({data: {id: elt_id(elt), type: "map"}}) if action_name != 'show'
  (:div, nil, content.merge(html_opts))
end

#is_current_page(link_val) ⇒ Object



10
11
12
# File 'app/helpers/sibu/pages_helper.rb', line 10

def is_current_page(link_val)
  /\d+/.match?(link_val) && link_val.to_i == @page.id
end

#join_tokens(tokens, suffix) ⇒ Object



98
99
100
# File 'app/helpers/sibu/pages_helper.rb', line 98

def join_tokens(tokens, suffix)
  (tokens + [suffix]).select {|t| !t.blank?}.join("|")
end


229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'app/helpers/sibu/pages_helper.rb', line 229

def link(elt, html_opts = {}, &block)
  repeat = html_opts.delete(:repeat)
  children = html_opts.delete(:children)
  defaults = {"id" => elt_id(elt), "value" => "", "text" => Sibu::DEFAULT_TEXT}
  link_elt = current_elt(elt)
  content = defaults.merge(link_elt)
  val = content.delete("value") || ""
  text = content.delete("text")
  html_opts.merge!({data: {id: elt_id(elt), type: "link", repeat: repeat, children: children}}) if action_name != 'show'
  if val.to_s.start_with?('http')
    content["href"] = val
  elsif val.to_s.start_with?('#')
    content["href"] = val
    content.delete("target")
  elsif val.to_s.start_with?('/')
    content["href"] = val
  elsif val.to_s.include?('@')
    content["href"] = 'mailto:' + val
  else
    content["href"] = @links.keys.include?(val.to_s) ? (action_name == 'show' ? link_path(val) : site_page_edit_content_path(@site.id, val)) : '#'
  end
  if block_given?
    @sb_section = (@sb_section || []) + [elt_id(elt)]
    html_output = (:a, capture(link_elt, elts(elt), &block), content.merge(html_opts).except("elements"))
    @sb_section -= [elt_id(elt)]
    html_output
  else
    (:a, raw(text), content.merge(html_opts).except("elements"))
  end
end


5
6
7
8
# File 'app/helpers/sibu/pages_helper.rb', line 5

def link_path(page_id)
  p = @site.page_by_id(page_id)
  p ? (request.host == conf[:host] ? site_page_path(@site.id, p.id) : (conf[:deployment_path] ? "/#{conf[:deployment_path]}/#{p.path}" : "/#{p.path}")) : "#"
end


30
31
32
33
34
35
36
37
38
39
40
# File 'app/helpers/sibu/pages_helper.rb', line 30

def link_type(val)
  if val.blank? || /^\d+$/.match(val.to_s)
    'internal'
  elsif Sibu::Document.for_user(sibu_user).map {|d| d.file_url}.include?(val)
    'document'
  elsif val.to_s.start_with?('http', '#', '/')
    'external'
  else
    'email'
  end
end

#nested_elements(elt_or_id) ⇒ Object

Note : the best option is probably a “section” helper that is instantiated from the section hash and that provides all the logic for elements manipulation (ex: section.h3(), section.elements, section.elements(nested_id), etc…) That will also remove the entity type references (site / page) from the partials (but is still has to be figured out by Sibu) Note : add “each_with_elements” and “elements” on section/elts enumerables



106
107
108
109
110
111
112
# File 'app/helpers/sibu/pages_helper.rb', line 106

def nested_elements(elt_or_id)
  element_id = elt_id(elt_or_id)
  element_id_tokens = (@sb_section + element_id.split("|")).uniq
  nested_elts = @sb_entity.elements(*element_id_tokens)
  nested_elts.blank? ? [{"id" => element_id.split("|").last, "data-id" => join_tokens(element_id_tokens[1..-1], "#{element_id}0")}] :
      nested_elts.map {|e| e.merge({"data-id" => join_tokens(element_id_tokens[1..-1], e['id'])})}
end

#render_page_section(s) ⇒ Object



191
192
193
194
195
196
# File 'app/helpers/sibu/pages_helper.rb', line 191

def render_page_section(s)
  @sb_section = [s['id']]
  @sb_entity = @page
  render partial: "shared/#{@site.section_template(s)}",
         locals: {sibu: self, sibu_section: s, sibu_attrs: sibu_attributes(s).html_safe}
end

#sbObject



94
95
96
# File 'app/helpers/sibu/pages_helper.rb', line 94

def sb
  self
end

#sb_pageObject Also known as: page

Note : see ActionView::OutputBuffer



177
178
179
180
# File 'app/helpers/sibu/pages_helper.rb', line 177

def sb_page
  @sb_entity = @page
  self
end

#sb_siteObject Also known as: site



184
185
186
187
# File 'app/helpers/sibu/pages_helper.rb', line 184

def sb_site
  @sb_entity = @site
  self
end

#section(id, tag, html_opts = {}, &block) ⇒ Object



210
211
212
213
214
# File 'app/helpers/sibu/pages_helper.rb', line 210

def section(id, tag, html_opts = {}, &block)
  @sb_section = [id]
  opts = action_name != 'show' ? html_opts.merge({"data-sb-id" => id, "data-sb-entity" => @sb_entity == @site ? 'site' : 'page'}) : html_opts
  (tag, capture(self, &block), opts)
end

#sections_templatesObject



14
15
16
# File 'app/helpers/sibu/pages_helper.rb', line 14

def sections_templates
  @site.site_template.available_sections
end

#select_element(id) ⇒ Object



114
115
116
# File 'app/helpers/sibu/pages_helper.rb', line 114

def select_element(id)
  @sb_entity.element(*@sb_section, id)
end

#sibu_attributes(section) ⇒ Object

Page sections attrs



199
200
201
# File 'app/helpers/sibu/pages_helper.rb', line 199

def sibu_attributes(section)
  action_name != 'show' ? ('data-sb-id="' + section['id'] + '" data-sb-entity="page"') : ''
end

#sibu_attrs(section_id) ⇒ Object

Site sections attrs



204
205
206
207
208
# File 'app/helpers/sibu/pages_helper.rb', line 204

def sibu_attrs(section_id)
  @sb_section = [section_id]
  @sb_entity = @site
  action_name != 'show' ? ('data-sb-id="' + section_id + '" data-sb-entity="site"').html_safe : ''
end

#site_imagesObject



18
19
20
# File 'app/helpers/sibu/pages_helper.rb', line 18

def site_images
  Sibu::Image.for_user(sibu_user).uniq
end

#widget(elt, widget_type, opts = {}, &block) ⇒ Object



163
164
165
166
167
# File 'app/helpers/sibu/pages_helper.rb', line 163

def widget(elt, widget_type, opts = {}, &block)
  content = elt.is_a?(Hash) ? elt : (select_element(elt) || {})
  opts.merge!({data: {id: elt_id(elt), type: "widget_#{widget_type.to_s.split('::').last.underscore}"}}) if action_name != 'show'
  (:div, capture(widget_type.new(content), &block), opts)
end