Module: HTMLDecorator

Included in:
Page
Defined in:
lib/yodel/models/pages/html_decorator.rb

Overview

Classes including this module must implement this interface: path: path to the current page path_was: original path to the current page (if path has been updated) form_for_page must be called in a context where form_for is available

Instance Method Summary collapse

Instance Method Details

#delete_button(text, options = {}) ⇒ Object


Actions




22
23
24
25
26
27
28
29
30
31
# File 'lib/yodel/models/pages/html_decorator.rb', line 22

def delete_button(text, options={})
  attributes = ""
  if options[:confirm]
    attributes << " onsubmit='return confirm(\"#{options.delete(:confirm)}\")'"
  end
  attributes << options.collect {|name, value| "#{name}='#{value}'"}.join(' ')
  button_input = "<button>#{text}</button>"
  method_input = "<input type='hidden' name='_method' value='delete'>"
  "<form action='#{path}' method='post' #{attributes}>#{method_input}#{button_input}</form>"
end


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/yodel/models/pages/html_decorator.rb', line 33

def delete_link(text, options={})
  attributes = ""
  if options[:confirm]
    confirm = "if(confirm(\"#{options.delete(:confirm)}\"))"
  else
    confirm = ''
  end
  if options[:wrap]
    wrap_start = options[:wrap][0]
    wrap_end = options[:wrap][1]
    options.delete(:wrap)
  else
    wrap_start = ''
    wrap_end = ''
  end
  attributes << options.collect {|name, value| "#{name}='#{value}'"}.join(' ')
  delete_link = "#{wrap_start}<a #{attributes} onclick='#{confirm}submit()' href='#'>#{text}</a>#{wrap_end}"
  method_input = "<input type='hidden' name='_method' value='delete'>"
  "<form action='#{path}' method='post' class='delete'>#{method_input}#{delete_link}</form>"
end

#form_for_page(options = {}, &block) ⇒ Object


Forms




9
10
11
12
13
14
15
16
# File 'lib/yodel/models/pages/html_decorator.rb', line 9

def form_for_page(options={}, &block)
  # FIXME: record proxy page needs to implement form_for with 3 parameters, not 2
  if method(:form_for).arity == -3
    form_for(self, path_was, options, &block)
  else
    form_for(self, options, &block)
  end
end

#immediately(action, options = {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/yodel/models/pages/html_decorator.rb', line 54

def immediately(action, options={})
  action_record = options.delete(:record)
  action_path = options.delete(:path)
  
  # remaining options are field mutations
  fields = fields_to_json(options)
  
  # perform the action directly on a record
  if action_path.nil?
    action_record ||= self
    action_record.from_json(fields)
    action_record.save
    ''
  else
    Hpricot::Elem.new('script', {}, [
      Hpricot::Text.new(action_to_javascript(action, action_path, fields))
    ])
  end
end

#on_click(action, options = {}, &block) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/yodel/models/pages/html_decorator.rb', line 74

def on_click(action, options={}, &block)
  # on_click requires child elements
  return '' unless block_given?
  
  # determine the path and fields of the request
  action_record = options.delete(:record) || self
  action_path   = options.delete(:path) || action_record.path
  fields        = fields_to_json(options)
  
  Ember::Template.wrap_content_block(block) do |content|
    Hpricot::Elem.new('span', {
      'class' => 'yodel-remote-action',
      'data-action' => CGI.escape_html(action_to_javascript(action, action_path, fields))
    }, [Hpricot::Text.new(content.join)])
  end
end

#paragraph(index, options = {}) ⇒ Object


Formatters




119
120
121
122
123
124
125
126
127
128
# File 'lib/yodel/models/pages/html_decorator.rb', line 119

def paragraph(index, options={})
  text = self.send(options[:field] || :content)
  paragraphs = Hpricot(text).search('/p')
  return '' if paragraphs.nil? || paragraphs[index].nil?
  if options[:strip]
    paragraphs[index].search('//text()').join
  else
    paragraphs[index].inner_html
  end
end

#paragraphs_from(index, options = {}) ⇒ Object

TODO: determine whether the initial search (Hpricot(text).children) should be changed to Hpricot(text).search(‘/p’), i.e should paragraphs_from return only p tags, or all child tags. This is dependent on the WYSIWYG editor - does it wrap <ul>s etc. in <p>s first, or are they top level elements?



134
135
136
137
138
139
140
141
142
143
# File 'lib/yodel/models/pages/html_decorator.rb', line 134

def paragraphs_from(index, options={})
  text = self.send(options[:field] || :content)
  paragraphs = Hpricot(text).children
  return '' if paragraphs.nil? || paragraphs[index..-1].nil?
  if options[:strip]
    paragraphs[index..-1].collect {|p| p.search('//text()').join}.join
  else
    paragraphs[index..-1].collect {|p| p.to_s}.join
  end
end