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



322
323
324
# File 'app/helpers/sibu/pages_helper.rb', line 322

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

#default_content(type) ⇒ Object



326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'app/helpers/sibu/pages_helper.rb', line 326

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



125
126
127
128
# File 'app/helpers/sibu/pages_helper.rb', line 125

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



318
319
320
# File 'app/helpers/sibu/pages_helper.rb', line 318

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

#elts(id) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
277
# File 'app/helpers/sibu/pages_helper.rb', line 266

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



219
220
221
222
223
224
# File 'app/helpers/sibu/pages_helper.rb', line 219

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



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

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_input(elt, html_opts = {}) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/helpers/sibu/pages_helper.rb', line 178

def form_input(elt, html_opts = {})
  repeat = html_opts.delete(:repeat)
  children = html_opts.delete(:children)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  opts = {"id" => t_id}.merge(html_opts.stringify_keys).merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  form_name = opts.delete('form_name') || 'contact'
  field_name = opts.delete('field_name') || 'name'
  name_attr = "#{form_name}[#{field_name}]"

  @sb_section = (@sb_section || []) + [t_id]
  opts.merge!({data: {id: @sb_section[1..-1].join('|'), type: "input", repeat: repeat, children: children}}) if action_name != 'show'

  case field_name
  when 'name', 'address'
    html_output = (:input, nil, opts.merge('type' => 'text', 'name' => name_attr))
  when 'email'
    html_output = (:input, nil, opts.merge('type' => 'email', 'name' => name_attr))
  when 'telephone'
    html_output = (:input, nil, opts.merge('type' => 'tel', 'name' => name_attr))
  when 'arrival_date', 'departure_date'
    html_output = (:input, nil, opts.merge('type' => 'date', 'name' => name_attr))
  when 'people'
    html_output = (:input, nil, opts.merge('type' => 'number', 'name' => name_attr))
  when 'message'
    html_output = (:textarea, nil, opts.merge('name' => name_attr))
  when 'newsletter'
    html_output = (:input, nil, opts.merge('type' => 'checkbox', 'name' => name_attr))
  else
    html_output = (:input, nil, opts.merge('type' => 'text', 'name' => name_attr))
  end

  @sb_section -= [t_id]
  html_output
end

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



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

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



149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/helpers/sibu/pages_helper.rb', line 149

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

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/sibu/pages_helper.rb', line 130

def img(elt, opts = {})
  wrapper = opts.delete(:wrapper)
  repeat = opts.delete(:repeat)
  size = opts.delete(:size)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  defaults = {"id" => t_id, "src" => Sibu::DEFAULT_IMG}
  content = defaults.merge(elt.is_a?(Hash) ? elt : (select_element(elt) || {}))
  @sb_section = (@sb_section || []) + [t_id]
  if action_name == 'show'
    content["src"] = ("/#{conf[:deployment_path]}" + content["src"]) if @online && conf[:deployment_path]
  else
    opts.merge!({data: {id: @sb_section[1..-1].join('|'), type: "media", repeat: repeat, size: size}})
  end
  html_output = 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})
  @sb_section -= [t_id]
  html_output
end

#input_categoriesObject



30
31
32
33
34
35
# File 'app/helpers/sibu/pages_helper.rb', line 30

def input_categories
  options_for_select([['Nom', 'name'], ['Adresse', 'address'], ['Email', 'email'], ['Téléphone', 'telephone'],
                      ['Arrivée', 'arrival_date'], ['Départ', 'departure_date'], ['Nombre de personnes', 'people'],
                      ['Message (texte libre)', 'message'], ['Inscription à la newsletter', 'newsletter']],
                     @element["field_name"])
end

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



311
312
313
314
315
316
# File 'app/helpers/sibu/pages_helper.rb', line 311

def interactive_map(elt, html_opts = {})
  defaults = {"data-lat" => "46.1988027", "data-lng" => "5.1748288"}
  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.stringify_keys))
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



105
106
107
# File 'app/helpers/sibu/pages_helper.rb', line 105

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


279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/helpers/sibu/pages_helper.rb', line 279

def link(elt, html_opts = {}, &block)
  repeat = html_opts.delete(:repeat)
  children = html_opts.delete(:children)
  t_id = elt.is_a?(Hash) ? elt["id"] : elt
  defaults = {"id" => t_id, "value" => "", "text" => Sibu::DEFAULT_TEXT}
  link_elt = current_elt(elt)
  content = defaults.merge(link_elt)
  val = content.delete("value") || ""
  text = content.delete("text")
  @sb_section = (@sb_section || []) + [t_id]
  html_opts.merge!({data: {id: @sb_section[1..-1].join('|'), 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?
    html_output = (:a, capture(link_elt, nested_elements(elt), &block), content.merge(html_opts).except("elements"))
  else
    html_output = (:a, raw(text), content.merge(html_opts).except("elements"))
  end
  @sb_section -= [t_id]
  html_output
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


37
38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/sibu/pages_helper.rb', line 37

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



113
114
115
116
117
118
119
# File 'app/helpers/sibu/pages_helper.rb', line 113

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



241
242
243
244
245
246
# File 'app/helpers/sibu/pages_helper.rb', line 241

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



101
102
103
# File 'app/helpers/sibu/pages_helper.rb', line 101

def sb
  self
end

#sb_pageObject Also known as: page

Note : see ActionView::OutputBuffer



227
228
229
230
# File 'app/helpers/sibu/pages_helper.rb', line 227

def sb_page
  @sb_entity = @page
  self
end

#sb_siteObject Also known as: site



234
235
236
237
# File 'app/helpers/sibu/pages_helper.rb', line 234

def sb_site
  @sb_entity = @site
  self
end

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



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

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



121
122
123
# File 'app/helpers/sibu/pages_helper.rb', line 121

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

#sibu_attributes(section) ⇒ Object

Page sections attrs



249
250
251
# File 'app/helpers/sibu/pages_helper.rb', line 249

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



254
255
256
257
258
# File 'app/helpers/sibu/pages_helper.rb', line 254

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



213
214
215
216
217
# File 'app/helpers/sibu/pages_helper.rb', line 213

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