Module: AbAdmin::Views::AdminHelpers

Defined in:
lib/ab_admin/views/admin_helpers.rb

Instance Method Summary collapse

Instance Method Details

#admin_commentsObject


85
86
87
# File 'lib/ab_admin/views/admin_helpers.rb', line 85

def admin_comments
  render 'admin/admin_comments/comments'
end

#admin_editable(item, attr, options = nil) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ab_admin/views/admin_helpers.rb', line 24

def admin_editable(item, attr, options=nil)
  options = {} unless options.is_a?(Hash)
  options[:type] ||= 'select' if options[:source]
  
  case attr.to_s
    when /_at$/
      options[:type] ||= 'date'
    when /^is_/
      options[:type] ||= 'select'
      options[:source] ||= {1 => 'yes', 0 => 'no'}
      options[:value] ||= item[attr] ? 1 : 0
      options[:title] ||= item[attr] ? 'yes' : 'no'
    when /description|body|content/
      options[:type] ||= 'textarea'
    else
      options[:type] ||= 'text'
  end

  data = {
      type: options[:type],
      source: options[:source].try(:to_json),
      model: options[:model] || item.class.model_name.singular,
      url: options[:url] || "/admin/#{item.class.model_name.plural}/#{item.id}",
      name: attr,
      value: options[:value] || item[attr],
      title: options[:title] || item[attr]
  }
  link_to admin_pretty_data(data[:title]).html_safe, '#', class: "editable #{options[:class]}", data: data.update(options[: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)
  options = args.extract_options!
  options[:remote] = true if request.xhr?
  options[:html] ||= {}
  options[:html][:class] ||= 'form-horizontal'
  options[:builder] ||= ::AbAdmin::Views::FormBuilder
  options[:html]['data-id'] = record.id
  if controller_name == 'manager' && resource_class == Array(object).last.class
    options[:url] ||= object.new_record? ? collection_path : resource_path
  end
  if options.delete(:nested)
    simple_nested_form_for([:admin, object].flatten, *(args << options), &block)
  else
    simple_form_for([:admin, object].flatten, *(args << options), &block)
  end
end

#admin_layout_cssObject


67
68
69
70
71
72
73
# File 'lib/ab_admin/views/admin_helpers.rb', line 67

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


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ab_admin/views/admin_helpers.rb', line 97

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_titleObject


75
76
77
78
79
# File 'lib/ab_admin/views/admin_helpers.rb', line 75

def admin_title
  base = @breadcrumbs ? @breadcrumbs.map_val(:name).reverse : []
  base << @page_title || 'Ab Admin'
  base.join(' - ')
end

#admin_tree(items) ⇒ Object


62
63
64
65
# File 'lib/ab_admin/views/admin_helpers.rb', line 62

def admin_tree(items)
  return if items.blank?
  items.map { |item| admin_tree_item(item) }.join.html_safe
end

#admin_tree_item(item) ⇒ Object


58
59
60
# File 'lib/ab_admin/views/admin_helpers.rb', line 58

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


153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ab_admin/views/admin_helpers.rb', line 153

def call_method_or_proc_on(obj, symbol_or_proc, options = {})
  exec = options[:exec].nil? ? true : options[:exec]
  case symbol_or_proc
    when Symbol, String
      obj.send(symbol_or_proc.to_sym)
    when Proc
      if exec
        instance_exec(obj, &symbol_or_proc)
      else
        symbol_or_proc.call(obj)
      end
  end
end

#color_bool(val, success_class = 'badge-success') ⇒ Object


89
90
91
# File 'lib/ab_admin/views/admin_helpers.rb', line 89

def color_bool(val, success_class='badge-success')
  %(<span class="badge #{success_class if val}">#{val ? '+' : '-'}</span>).html_safe
end

#ha(attr) ⇒ Object


149
150
151
# File 'lib/ab_admin/views/admin_helpers.rb', line 149

def ha(attr)
  resource_class.han(attr)
end

#icon(name, white = false) ⇒ Object


93
94
95
# File 'lib/ab_admin/views/admin_helpers.rb', line 93

def icon(name, white=false)
  "<i class='icon-#{name} #{'icon-white' if white}'></i> ".html_safe
end

#include_fvObject


81
82
83
# File 'lib/ab_admin/views/admin_helpers.rb', line 81

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


142
143
144
145
146
147
# File 'lib/ab_admin/views/admin_helpers.rb', line 142

def input_set(title, options={}, &block)
  options.reverse_merge!(class: "inputs well well-small clearfix #{options.delete(:legend_class) || 'do_sort'}", id: options.delete(:legend_id))
  html = (:label, title, class: "input_set label #{options.delete(:label_class)}")
  html.concat(capture(&block)) if block_given?
  (:div, html, options)
end

#item_image(item, assoc = :photo, size = :thumb) ⇒ Object


133
134
135
# File 'lib/ab_admin/views/admin_helpers.rb', line 133

def item_image(item, assoc=:photo, size=:thumb)
  image_tag_if(item.send(assoc).try(:url, size))
end

118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ab_admin/views/admin_helpers.rb', line 118

def item_image_link(item, options={})
  options.reverse_merge!(assoc: :picture)
  options[:url] ||= resource_path(item)
  image = item.send(options[:assoc])
  return nil unless image
  version = options[:version] || image.class.thumb_size
  image_url_method = options[:image_url_method] || :url
  popover_content = "<img class='image_link_popover popover_#{options[:assoc]}' src='#{image.send(image_url_method, options[:full_version])}'></img>"
  popover_data = {content: popover_content, title: AbAdmin.display_name(item)}

  html_options = options.delete(:html_options) || {}
  html_options.reverse_merge!(rel: 'popover', remote: options[:remote], data: popover_data)
  link_to image_tag(image.send(image_url_method, version)), options[:url], html_options
end

#options_for_ckeditor(options = {}) ⇒ Object


54
55
56
# File 'lib/ab_admin/views/admin_helpers.rb', line 54

def options_for_ckeditor(options = {})
  {width: 930, height: 200, toolbar: 'VeryEasy', namespace: ''}.update(options)
end

#per_page_variantsObject


137
138
139
# File 'lib/ab_admin/views/admin_helpers.rb', line 137

def per_page_variants
  AbAdmin.per_page_variants.map{|variant| [variant, max_per_page].min }.uniq
end

#pretty_data(object) ⇒ Object


114
115
116
# File 'lib/ab_admin/views/admin_helpers.rb', line 114

def pretty_data(object)
  AbAdmin.pretty_data(object)
end