Module: Ditty::Helpers::Views

Defined in:
lib/ditty/helpers/views.rb

Instance Method Summary collapse

Instance Method Details

#delete_form(entity, label = 'Delete') ⇒ Object



75
76
77
78
# File 'lib/ditty/helpers/views.rb', line 75

def delete_form(entity, label = 'Delete')
  locals = { delete_label: label, entity: entity }
  haml :'partials/delete_form', locals: locals
end

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



80
81
82
83
# File 'lib/ditty/helpers/views.rb', line 80

def delete_form_tag(url, options = {}, &block)
  options[:form_verb] = :delete
  form_tag(url, options, &block)
end

#display(value, type = :string) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/ditty/helpers/views.rb', line 118

def display(value, type = :string)
  if [true, false].include?(value) || type.to_sym == :boolean
    value ? 'Yes' : 'No'
  elsif value.nil? || type.to_sym == :nil
    '(Empty)'
  else
    value
  end
end

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



85
86
87
88
# File 'lib/ditty/helpers/views.rb', line 85

def edit_form_tag(url, options = {}, &block)
  options[:form_verb] = :put
  form_tag(url, options, &block)
end

#filter_control(filter, opts = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ditty/helpers/views.rb', line 47

def filter_control(filter, opts = {})
  meth = "#{filter[:name]}_options".to_sym
  return unless respond_to? meth

  haml :'partials/filter_control', locals: {
    name: filter[:name],
    label: opts[:label] || filter[:name].to_s.titlecase,
    options: send(meth),
    total_filters: opts[:filters]
  }
end

#flash_messages(key = :flash) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/ditty/helpers/views.rb', line 59

def flash_messages(key = :flash)
  return '' if flash(key).empty?

  id = (key == :flash ? 'flash' : "flash_#{key}")
  messages = flash(key).collect do |message|
    "  <div class='alert alert-#{message[0]} alert-dismissable' role='alert'>#{message[1]}</div>\n"
  end
  "<div id='#{id}'>\n#{messages.join}</div>"
end

#form_control(name, model, opts = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ditty/helpers/views.rb', line 25

def form_control(name, model, opts = {})
  label = opts.delete(:label) || name.to_s.titlecase
  klass = opts.delete(:class) || 'form-control' unless opts[:type] == 'file'
  klass = "#{klass} is-invalid" if model.errors[name]
  group = opts.delete(:group) || model.class.to_s.demodulize.underscore
  field = opts.delete(:field) || name
  default = opts.delete(:default) || nil
  help_text = opts.delete(:help_text) || nil

  attributes = { type: 'text', id: name, name: "#{group}[#{name}]", class: klass }.merge(opts)
  haml :'partials/form_control', locals: {
    model: model,
    label: label,
    attributes: attributes,
    name: name,
    group: group,
    field: field,
    default: default,
    help_text: help_text
  }
end

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



95
96
97
98
99
100
101
# File 'lib/ditty/helpers/views.rb', line 95

def form_tag(url, options = {}, &block)
  options[:form_verb] ||= :post
  options[:attributes] ||= {}
  options[:attributes] = { class: 'form-horizontal' }.merge options[:attributes]
  options[:url] = options[:form_verb].to_sym == :get ? url : with_layout(url)
  haml :'partials/form_tag', locals: options.merge(block: block)
end

#layoutObject



9
10
11
12
13
# File 'lib/ditty/helpers/views.rb', line 9

def layout
  return :embedded if request.params['layout'] == 'embedded'

  :layout
end


135
136
137
138
139
140
141
# File 'lib/ditty/helpers/views.rb', line 135

def link_to(name = nil, options = nil, html_options = {})
  html_options[:href] ||= url_for(options)

  capture_haml do
    haml_tag :a, name, html_options
  end
end

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



90
91
92
93
# File 'lib/ditty/helpers/views.rb', line 90

def new_form_tag(url, options = {}, &block)
  options[:form_verb] = :post
  form_tag(url, options, &block)
end

#pagination(list, base_path, qp = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ditty/helpers/views.rb', line 103

def pagination(list, base_path, qp = {})
  return unless list.respond_to?(:pagination_record_count) || list.respond_to?(:total_entries)

  list = ::Ditty::Services::PaginationWrapper.new(list)
  locals = {
    first_link: "#{base_path}?" + query_string(qp.merge(page: 1)),
    next_link: list.last_page? ? '#' : "#{base_path}?" + query_string(qp.merge(page: list.next_page)),
    prev_link: list.first_page? ? '#' : "#{base_path}?" + query_string(qp.merge(page: list.prev_page)),
    last_link: "#{base_path}?" + query_string(qp.merge(page: list.page_count)),
    base_path: base_path,
    list: list
  }
  haml :'partials/pager', locals: locals
end

#query_string(add = {}) ⇒ Object



69
70
71
72
73
# File 'lib/ditty/helpers/views.rb', line 69

def query_string(add = {})
  qs = params.clone.merge(add)
  qs.delete('captures')
  Rack::Utils.build_query(qs.delete_if { |_k, v| v == '' })
end

#sort_icon(field) ⇒ Object



154
155
156
157
158
159
# File 'lib/ditty/helpers/views.rb', line 154

def sort_icon(field)
  return 'fa-sort' unless params[:sort] == field.to_s
  return 'fa-sort-up' if params[:order] == 'asc'

  'fa-sort-down'
end

#sort_query(field) ⇒ Object



147
148
149
150
151
152
# File 'lib/ditty/helpers/views.rb', line 147

def sort_query(field)
  query_string(
    order: params[:sort] == field.to_s && params[:order] == 'asc' ? 'desc' : 'asc',
    sort: field,
  )
end

#sort_ui(field) ⇒ Object



143
144
145
# File 'lib/ditty/helpers/views.rb', line 143

def sort_ui(field)
  haml :'partials/sort_ui', locals: { field: field }
end

#static_headers(view) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/ditty/helpers/views.rb', line 161

def static_headers(view)
  view_path = "views/#{view}.haml"
  return unless File.exist?(view_path)

  last_modified File.mtime(view_path)
  etag Digest::SHA2.hexdigest(File.read(view_path))
end

#url_for(options = nil) ⇒ Object



128
129
130
131
132
133
# File 'lib/ditty/helpers/views.rb', line 128

def url_for(options = nil)
  return options if options.is_a? String
  return request.env['HTTP_REFERER'] if options == :back && request.env['HTTP_REFERER']

  raise 'Unimplemented'
end

#with_layout(url) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/ditty/helpers/views.rb', line 15

def with_layout(url)
  uri = URI.parse(url)
  # Don't set the layout if there's none. Don't set the layout for external URIs
  return url if params['layout'].nil? || uri.host

  qs = { 'layout' => params['layout'] }.merge(uri.query ? CGI.parse(uri.query) : {})
  uri.query = Rack::Utils.build_query qs
  uri.to_s
end