Module: Ditty::Helpers::Views
- Defined in:
- lib/ditty/helpers/views.rb
Instance Method Summary collapse
- #delete_form(entity, label = 'Delete') ⇒ Object
- #delete_form_tag(url, options = {}, &block) ⇒ Object
- #display(value, type = :string) ⇒ Object
- #edit_form_tag(url, options = {}, &block) ⇒ Object
- #filter_control(filter, opts = {}) ⇒ Object
- #flash_messages(key = :flash) ⇒ Object
- #form_control(name, model, opts = {}) ⇒ Object
- #form_tag(url, options = {}, &block) ⇒ Object
- #layout ⇒ Object
- #new_form_tag(url, options = {}, &block) ⇒ Object
- #pagination(list, base_path, qp = {}) ⇒ Object
- #query_string(add = {}) ⇒ Object
- #with_layout(url) ⇒ Object
Instance Method Details
#delete_form(entity, label = 'Delete') ⇒ Object
66 67 68 69 |
# File 'lib/ditty/helpers/views.rb', line 66 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
71 72 73 74 |
# File 'lib/ditty/helpers/views.rb', line 71 def delete_form_tag(url, = {}, &block) [:form_verb] = :delete form_tag(url, , &block) end |
#display(value, type = :string) ⇒ Object
108 109 110 111 112 113 114 115 116 |
# File 'lib/ditty/helpers/views.rb', line 108 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
76 77 78 79 |
# File 'lib/ditty/helpers/views.rb', line 76 def edit_form_tag(url, = {}, &block) [:form_verb] = :put form_tag(url, , &block) end |
#filter_control(filter, opts = {}) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/ditty/helpers/views.rb', line 41 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) } end |
#flash_messages(key = :flash) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/ditty/helpers/views.rb', line 51 def (key = :flash) return '' if flash(key).empty? id = (key == :flash ? 'flash' : "flash_#{key}") = flash(key).collect do || " <div class='alert alert-#{[0]} alert-dismissable' role='alert'>#{[1]}</div>\n" end "<div id='#{id}'>\n" + .join + '</div>' end |
#form_control(name, model, opts = {}) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ditty/helpers/views.rb', line 22 def form_control(name, model, opts = {}) label = opts.delete(:label) || name.to_s.titlecase klass = opts.delete(:class) || 'form-control' unless opts[:type] == 'file' group = opts.delete(:group) || model.class.to_s.demodulize.underscore field = opts.delete(:field) || name default = opts.delete(:default) || 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 } end |
#form_tag(url, options = {}, &block) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/ditty/helpers/views.rb', line 86 def form_tag(url, = {}, &block) [:form_verb] ||= :post [:attributes] ||= {} [:attributes] = { 'class': 'form-horizontal' }.merge [:attributes] [:url] = [:form_verb].to_sym == :get ? url : with_layout(url) haml :'partials/form_tag', locals: .merge(block: block) end |
#layout ⇒ Object
8 9 10 11 |
# File 'lib/ditty/helpers/views.rb', line 8 def layout return :embedded if request.params['layout'] == 'embedded' :layout end |
#new_form_tag(url, options = {}, &block) ⇒ Object
81 82 83 84 |
# File 'lib/ditty/helpers/views.rb', line 81 def new_form_tag(url, = {}, &block) [:form_verb] = :post form_tag(url, , &block) end |
#pagination(list, base_path, qp = {}) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/ditty/helpers/views.rb', line 94 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
60 61 62 63 64 |
# File 'lib/ditty/helpers/views.rb', line 60 def query_string(add = {}) qs = params.clone.merge(add) qs.delete('captures') Rack::Utils.build_query qs end |
#with_layout(url) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/ditty/helpers/views.rb', line 13 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 |