Class: Iord::OutputHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/iord/sort.rb,
lib/iord/search.rb,
lib/iord/iterable.rb,
lib/iord/paginate.rb,
lib/iord/output_helper.rb

Direct Known Subclasses

JsonOutput

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view_context) ⇒ OutputHelper

Returns a new instance of OutputHelper.



5
6
7
# File 'lib/iord/output_helper.rb', line 5

def initialize(view_context)
  @v = view_context
end

Instance Attribute Details

#vObject

Returns the value of attribute v.



3
4
5
# File 'lib/iord/output_helper.rb', line 3

def v
  @v
end

Instance Method Details

#display(object, attrs) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/iord/output_helper.rb', line 9

def display(object, attrs)
  v.(:dl, nil, nil, false) do
    attrs.collect do |attr|
      v.(:dt, v.field_name(object, attr).to_s.humanize) +
        v.(:dd, v.field_value(object, attr))
    end.join.html_safe
  end
end

#display_array(array, attrs) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/iord/output_helper.rb', line 18

def display_array(array, attrs)
  v.(:ul, nil, nil, false) do
    array.collect do |el|
      v.(:li, display(el, attrs))
    end.join.html_safe
  end
end

#fieldset(title, attrs, form, predicate = nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/iord/output_helper.rb', line 26

def fieldset(title, attrs, form, predicate = nil)
  v.(:fieldset, nil, nil, false) do
    c = v.(:legend, title)
    c += attrs.collect do |attr|
      if predicate.nil? or predicate.call(form, attr)
        v.field_form(form, attr)
      end
    end.compact.join.html_safe
    c += yield.to_s.html_safe if block_given?
  end
end

#image(url, hsh) ⇒ Object



94
95
96
97
# File 'lib/iord/output_helper.rb', line 94

def image(url, hsh)
  hsh ||= {}
  v.image_tag(url, hsh)
end

#input(label, input, errors) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/iord/output_helper.rb', line 38

def input(label, input, errors)
  v.(:div, {'class' => :input}, nil, false) do
    c = label.html_safe
    c += input.html_safe
    c += v.(:div, errors.join('<br />').html_safe, {'class' => :error}, false)
  end
end

#iterate_formObject



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
# File 'lib/iord/iterable.rb', line 46

def iterate_form
  v.form_tag(v.iterate_url, method: :get, class: 'iterate') do
    html = ''
    if v.iord_features.include?(:search)
      html +=
        search_term +
        search_operator +
        search_value
      html += '<br/>'
    end

    html += %Q[<label for="order_by">#{v.t('iord.text.order_by')}</label>]
    html += %q[<select name="order_by" id="order_by">]
    v.resource_attribute_names.each do |attr|
      html += %Q[<option value="#{attr}"]
      html += %q[ selected="selected"] if attr == v.order_by
      html += %Q[>#{attr.humanize}</option>] if attr != '_id'
      html += %q[>ID</option>] if attr == '_id'
    end
    html += %q[</select>]
    html += '<br/>'

    html += %Q[<label for="sort_mode">#{v.t('iord.text.sort_mode')}</label>]
    html += %q[<select name="sort_mode" id="sort_mode">]
    %i(asc desc).each do |sort_mode|
      html += %Q[<option value="#{sort_mode}"]
      html += %q[ selected="selected"] if sort_mode == v.sort_mode
      html += %Q[>#{v.t("iord.order_by.#{sort_mode}")}</option>]
    end
    html += %q[</select>]
    html += '<br/>'

    html += %Q[<label for="edit">#{v.t('iord.text.edit')}</label>]
    html += %q[<input type="checkbox" value="true" name="edit" id="edit" />]
    html += '<br/>'

    html += %Q[<input type="hidden" name="pos" value="0" />]
    html += v.submit_tag(v.t('iord.buttons.iterate'), name: '')

    html.html_safe
  end
end


90
91
92
# File 'lib/iord/output_helper.rb', line 90

def link_to(label, url, hsh)
  v.link_to(label, url, hsh)
end


54
55
56
# File 'lib/iord/output_helper.rb', line 54

def link_to_add(form, attr_name, attr)
  form.link_to_add(v.t('iord.buttons.add', model: attr_name), attr, class: 'btn btn-default')
end


66
67
68
69
# File 'lib/iord/output_helper.rb', line 66

def link_to_collection()
  v.link_to(v.t('iord.buttons.back'), v.collection_url,
                       class: 'btn btn-default')
end


58
59
60
61
62
63
64
# File 'lib/iord/output_helper.rb', line 58

def link_to_create()
  if v.new_resource_url?
    v.link_to(v.t('iord.buttons.create'),
              v.new_resource_url,
              class: 'btn btn-default')
  end
end


84
85
86
87
88
# File 'lib/iord/output_helper.rb', line 84

def link_to_destroy(object = nil)
  v.link_to(v.t('iord.buttons.destroy'), v.resource_url(object),
                       data: {method: 'delete', confirm: v.t('iord.alert.destroy')},
                       class: 'btn btn-default')
end


76
77
78
79
80
81
82
# File 'lib/iord/output_helper.rb', line 76

def link_to_edit(object = nil)
  if v.edit_resource_url?
    v.link_to(v.t('iord.buttons.edit'),
              v.edit_resource_url(object),
              class: 'btn btn-default')
  end
end


9
10
11
12
# File 'lib/iord/iterable.rb', line 9

def link_to_iterate
  v.link_to(v.t('iord.buttons.iterate'), v.iterate_url(true),
            class: 'btn btn-default')
end


19
20
21
22
# File 'lib/iord/iterable.rb', line 19

def link_to_iterate_edit
  v.link_to(v.t('iord.buttons.edit'), v.iterate_url(edit: true),
            class: 'btn btn-default')
end


5
6
7
# File 'lib/iord/iterable.rb', line 5

def link_to_iterate_if_enabled
  link_to_iterate if v.iord_features.include?(:iterable)
end


24
25
26
27
28
# File 'lib/iord/iterable.rb', line 24

def link_to_iterate_next_if_possible
  return if v.pos >= v.collection_count - 1
  v.link_to(v.t('iord.buttons.next'), v.iterate_url(pos: v.pos + 1),
            class: 'btn btn-default')
end


30
31
32
33
34
# File 'lib/iord/iterable.rb', line 30

def link_to_iterate_previous_if_possible
  return if v.pos <= 0
  v.link_to(v.t('iord.buttons.previous'), v.iterate_url(pos: v.pos - 1),
            class: 'btn btn-default')
end


14
15
16
17
# File 'lib/iord/iterable.rb', line 14

def link_to_iterate_show
  v.link_to(v.t('iord.buttons.show'), v.iterate_url(edit: false),
            class: 'btn btn-default')
end


50
51
52
# File 'lib/iord/output_helper.rb', line 50

def link_to_remove(form, attr_name)
  form.link_to_remove(v.t('iord.buttons.remove', model: attr_name), class: 'btn btn-default')
end


71
72
73
74
# File 'lib/iord/output_helper.rb', line 71

def link_to_show(object = nil)
  v.link_to(v.t('iord.buttons.show'), v.resource_url(object),
                       class: 'btn btn-default')
end

#order_by(attribute) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/iord/sort.rb', line 9

def order_by(attribute)
  content = String.new
  if v.resource_class.attribute_names.include? attribute.to_s
    content += v.link_to sort_desc_symbol, v.collection_url(order_by: attribute, sort_mode: :desc)
    content += "&nbsp;"
    content += v.link_to sort_asc_symbol, v.collection_url(order_by: attribute, sort_mode: :asc)
  end
  return content.html_safe
end

#page(i) ⇒ Object



39
40
41
# File 'lib/iord/paginate.rb', line 39

def page(i)
  %Q[<span class="page">#{i + 1}</span>].html_safe
end

#paginate_if_enabledObject



5
6
7
# File 'lib/iord/paginate.rb', line 5

def paginate_if_enabled
  pagination if v.iord_features.include? :paginate
end

#paginationObject



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
# File 'lib/iord/paginate.rb', line 9

def pagination
  limit = v.limit
  offset = v.offset
  count = v.count
  page_div = %q[<p class="page">]
  page_div += %Q[#{v.t('iord.text.page')} ]

  if offset - limit + 1 > 0
    page_div += v.link_to "<", v.collection_url(offset: offset - limit)
    page_div += "&nbsp;"
  end

  (count / limit + 1).ceil.times do |i|
    page_div += "&nbsp;" unless i == 0
    if (i * limit) == offset
      page_div += page(i)
    else
      page_div += v.link_to page(i), v.collection_url(offset: i * limit)
    end
  end

  if offset < (count - limit)
    page_div += "&nbsp;"
    page_div += v.link_to ">", v.collection_url(offset: offset + limit)
  end

  page_div += %Q[</p>]
  return page_div.html_safe
end

#search_formObject



9
10
11
12
13
14
15
16
# File 'lib/iord/search.rb', line 9

def search_form
  v.form_tag(v.collection_url, method: :get, class: 'search') do
    search_term +
      search_operator +
      search_value +
      search_submit
  end
end

#search_if_enabledObject



5
6
7
# File 'lib/iord/search.rb', line 5

def search_if_enabled
  search_form if v.iord_features.include?(:search)
end

#search_operatorObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/iord/search.rb', line 35

def search_operator
  html = %q[<select name="op" id="search_operator">]
  v.search_operators.each do |op|
    html += %Q[<option value="#{op}"]
    html += %q[ selected="selected"] if op == v.search_operator
    html += %Q[>#{v.t("iord.search.operator.#{op}")}</option>]
  end
  html += %q[</select>]
  return html.html_safe
end

#search_submitObject



50
51
52
# File 'lib/iord/search.rb', line 50

def search_submit
  v.submit_tag(v.t('iord.buttons.search'), name: '')
end

#search_termObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/iord/search.rb', line 18

def search_term
  html = %q[<select name="q" id="search_term">]
  if v.search_term.nil?
    html += %Q[<option value="">#{v.t('iord.text.select_field')}</option>]
  else
    html += %Q[<option value="">#{v.t('iord.text.reset')}</option>]
  end
  v.resource_attribute_names.each do |attr|
    html += %Q[<option value="#{attr}"]
    html += %q[ selected="selected"] if attr == v.search_term
    html += %Q[>#{attr.humanize}</option>] if attr != '_id'
    html += %q[>ID</option>] if attr == '_id'
  end
  html += %q[</select>]
  return html.html_safe
end

#search_valueObject



46
47
48
# File 'lib/iord/search.rb', line 46

def search_value
  v.text_field_tag('v', v.search_value, id: 'search_value')
end

#sort_asc_symbolObject



19
20
21
# File 'lib/iord/sort.rb', line 19

def sort_asc_symbol
  '\\/'
end

#sort_desc_symbolObject



23
24
25
# File 'lib/iord/sort.rb', line 23

def sort_desc_symbol
  '/\\'
end

#sort_if_enabled(attribute) ⇒ Object



5
6
7
# File 'lib/iord/sort.rb', line 5

def sort_if_enabled(attribute)
  order_by(attribute) if v.iord_features.include? :sort
end

#submit(form) ⇒ Object



46
47
48
# File 'lib/iord/output_helper.rb', line 46

def submit(form)
  v.(:div, form.button(nil, class: 'btn btn-default'), {'class' => :actions})
end

#submit_to_next_if_possible(form) ⇒ Object



36
37
38
39
# File 'lib/iord/iterable.rb', line 36

def submit_to_next_if_possible(form)
  return ''.html_safe if v.pos >= v.collection_count - 1
  %Q[<div class="actions">#{form.button v.t('iord.buttons.next'), value: v.iterate_url(pos: v.pos + 1), name: 'go_to', class: 'btn btn-default'}</div>].html_safe
end

#submit_to_previous_if_possible(form) ⇒ Object



41
42
43
44
# File 'lib/iord/iterable.rb', line 41

def submit_to_previous_if_possible(form)
  return ''.html_safe if v.pos <= 0
  %Q[<div class="actions">#{form.button v.t('iord.buttons.previous'), value: v.iterate_url(pos: v.pos - 1), name: 'go_to', class: 'btn btn-default'}</div>].html_safe
end