Module: AbAdmin::Views::AdminHelpers
- Defined in:
- lib/ab_admin/views/admin_helpers.rb
Instance Method Summary collapse
- #admin_comments ⇒ Object
- #admin_editable(item, attr, opts = nil) ⇒ Object
- #admin_form_for(object, *args, &block) ⇒ Object
- #admin_layout_css ⇒ Object
- #admin_pretty_data(object) ⇒ Object
- #admin_site_name ⇒ Object
- #admin_title ⇒ Object
- #admin_tree(items) ⇒ Object
- #admin_tree_item(item) ⇒ Object
- #call_method_or_proc_on(obj, symbol_or_proc, options = {}) ⇒ Object
- #color_bool(val, options = {}) ⇒ Object
- #editable_bool(item, attr, label: nil) ⇒ Object
- #ha(attr) ⇒ Object
- #icon(name, white = false) ⇒ Object
- #include_fv ⇒ Object
-
#input_set(title, options = {}, &block) ⇒ Object
input_set ‘title’, legend_class: ‘do_sort’, label_class: ‘label-info’ do.
- #item_image(item, assoc = :photo, size = :thumb) ⇒ Object
- #item_image_link(item, options = {}) ⇒ Object
- #options_for_ckeditor(options = {}) ⇒ Object
- #per_page_variants ⇒ Object
- #pretty_data(object) ⇒ Object
Instance Method Details
#admin_comments ⇒ Object
115 116 117 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 115 def admin_comments render 'admin/admin_comments/comments' end |
#admin_editable(item, attr, opts = nil) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 31 def admin_editable(item, attr, opts=nil) opts = {} unless opts.is_a?(Hash) if opts[:title] title = opts[:title] else assoc_name = attr.to_s.remove('_id') title = item.class.reflect_on_association(assoc_name) ? AbAdmin.display_name(item.send(assoc_name)) : item[attr] end html_title = admin_pretty_data(title).to_s.html_safe return html_title unless can?(:update, item) if opts[:collection] if opts[:collection].is_a?(Hash) opts[:source] = opts[:collection] elsif opts[:collection].is_a?(Array) opts[:source] = opts[:collection].first.respond_to?(:id) ? opts[:collection].map {|r| [r.id, AbAdmin.display_name(r)]}.to_h : opts[:collection].map {|v| [v, v]}.to_h end end unless opts[:type] if opts[:source] opts[:type] = 'select' else case attr.to_s when /_at$/ opts[:type] ||= 'date' opts[:title] ||= html_title when /^is_/ opts[:type] ||= 'select' opts[:source] ||= {1 => 'yes', 0 => 'no'} opts[:value] ||= item[attr] ? 1 : 0 opts[:title] ||= item[attr] ? 'yes' : 'no' when /description|body|content/ opts[:type] ||= 'textarea' else opts[:type] ||= 'text' end end end data = { type: opts[:type], source: opts[:source].try(:to_json), model: opts[:model] || item.class.model_name.singular, accept: opts[:accept], url: opts[:url] || "/admin/#{item.class.model_name.plural}/#{item.id}", name: attr, value: opts[:value] || item[attr], title: opts[:title] || item[attr] } link_to html_title, '#', class: "editable #{opts[:class]}", data: data.update(opts[:data] || {}) end |
#admin_form_for(object, *args, &block) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 5 def admin_form_for(object, *args, &block) record = Array(object).last record.fallbacks_for_empty_translations = false if record.respond_to?(:fallbacks_for_empty_translations) = args. [:remote] = true if request.xhr? [:html] ||= {} [:html][:class] ||= 'form-horizontal' [:builder] ||= ::AbAdmin::Views::FormBuilder [:html]['data-id'] = record.id if controller_name == 'manager' && resource_class == Array(object).last.class [:url] ||= object.new_record? ? collection_path : resource_path end if .delete(:nested) simple_nested_form_for([:admin, object].flatten, *(args << ), &block) else simple_form_for([:admin, object].flatten, *(args << ), &block) end end |
#admin_layout_css ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 97 def admin_layout_css css = [] css << 'content_with_sidebar' if settings[:sidebar] || content_for?(:sidebar) css << 'well' if settings[:well] && current_index_view != 'tree' css << "#{current_index_view}_view" css end |
#admin_pretty_data(object) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 130 def admin_pretty_data(object) case object when String, Integer, BigDecimal, Float object when TrueClass, FalseClass color_bool(object) when Date, DateTime, Time, ActiveSupport::TimeWithZone I18n.l(object, format: :long) when NilClass '' when ActiveRecord::Base admin_show_link(object) else AbAdmin.safe_display_name(object) || object end end |
#admin_site_name ⇒ Object
186 187 188 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 186 def admin_site_name AbAdmin.site_name.is_a?(String) ? AbAdmin.site_name : AbAdmin.site_name.call end |
#admin_title ⇒ Object
105 106 107 108 109 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 105 def admin_title base = @breadcrumbs ? @breadcrumbs.map_val(:name).reverse : [] base << @page_title || 'Ab Admin' base.join(' - ') end |
#admin_tree(items) ⇒ Object
92 93 94 95 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 92 def admin_tree(items) return if items.blank? items.map { |item| admin_tree_item(item) }.join.html_safe end |
#admin_tree_item(item) ⇒ Object
88 89 90 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 88 def admin_tree_item(item) render 'tree_item', item: item, child_tree: admin_tree(item.cached_children) end |
#call_method_or_proc_on(obj, symbol_or_proc, options = {}) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 190 def call_method_or_proc_on(obj, symbol_or_proc, = {}) exec = [:exec].nil? ? true : [:exec] case symbol_or_proc when String ActiveSupport::Deprecation.warn('`call_method_or_proc_on` don\'t accept method name as String, use Symbol instead') if symbol_or_proc =~ /\A\w+\z/ symbol_or_proc when Symbol obj.send(symbol_or_proc.to_sym) when Proc exec ? instance_exec(obj, &symbol_or_proc) : symbol_or_proc.call(obj) end end |
#color_bool(val, options = {}) ⇒ Object
119 120 121 122 123 124 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 119 def color_bool(val, ={}) .reverse_merge!(true_css: 'badge-success', false_css: nil, nil_css: nil) css = ["#{val.inspect}_css".to_sym] text = val.nil? ? '?' : (val ? '+' : '-') %(<span class="badge #{css}">#{text}</span>).html_safe end |
#editable_bool(item, attr, label: nil) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 24 def editable_bool(item, attr, label: nil) url = "/admin/#{item.class.model_name.plural}/#{item.id}.json" html = check_box_tag("#{item.class.model_name.singular}[#{attr}]", '1', item.send(attr), class: 'js-auto-submit-checkbox', data: {url: url}) html = content_tag(:label, "#{label} #{html}".html_safe) if label content_tag :div, html, class: 'auto-submit-checkbox-wrap white-space-nowrap' end |
#ha(attr) ⇒ Object
182 183 184 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 182 def ha(attr) resource_class.han(attr) end |
#icon(name, white = false) ⇒ Object
126 127 128 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 126 def icon(name, white=false) "<i class='icon-#{name} #{'icon-white' if white}'></i> ".html_safe end |
#include_fv ⇒ Object
111 112 113 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 111 def include_fv "<script type='text/javascript'>window.fv = #{fv.to_h.to_json}</script>".html_safe end |
#input_set(title, options = {}, &block) ⇒ Object
input_set ‘title’, legend_class: ‘do_sort’, label_class: ‘label-info’ do
175 176 177 178 179 180 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 175 def input_set(title, ={}, &block) .reverse_merge!(class: "inputs well well-small clearfix #{.delete(:legend_class) || 'do_sort'}", id: .delete(:legend_id)) html = title ? content_tag(:label, title, class: "input_set label #{.delete(:label_class)}") : ''.html_safe html.concat(capture(&block)) if block_given? content_tag(:div, html, ) end |
#item_image(item, assoc = :photo, size = :thumb) ⇒ Object
166 167 168 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 166 def item_image(item, assoc=:photo, size=:thumb) image_tag_if(item.send(assoc).try(:url, size)) end |
#item_image_link(item, options = {}) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 151 def item_image_link(item, ={}) .reverse_merge!(assoc: :picture) [:url] ||= resource_path(item) image = item.send([:assoc]) return nil unless image version = [:version] || image.class.thumb_size image_url_method = [:image_url_method] || :url popover_content = "<img class='image_link_popover popover_#{[:assoc]}' src='#{image.send(image_url_method, [:full_version])}'></img>" popover_data = {content: popover_content, title: AbAdmin.display_name(item)} = .delete(:html_options) || {} .reverse_merge!(rel: 'popover', remote: [:remote], data: popover_data) link_to image_tag(image.send(image_url_method, version)), [:url], end |
#options_for_ckeditor(options = {}) ⇒ Object
84 85 86 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 84 def ( = {}) {width: 930, height: 200, toolbar: 'VeryEasy', namespace: ''}.update() end |
#per_page_variants ⇒ Object
170 171 172 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 170 def per_page_variants AbAdmin.per_page_variants.map{|variant| [variant, max_per_page].min }.uniq end |
#pretty_data(object) ⇒ Object
147 148 149 |
# File 'lib/ab_admin/views/admin_helpers.rb', line 147 def pretty_data(object) AbAdmin.pretty_data(object) end |