Module: AbAdmin::Concerns::AdminAddition

Extended by:
ActiveSupport::Concern
Included in:
Structure, User
Defined in:
lib/ab_admin/concerns/admin_addition.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#admin_comments_count_non_zeroObject


48
49
50
# File 'lib/ab_admin/concerns/admin_addition.rb', line 48

def admin_comments_count_non_zero
  self[:admin_comments_count].to_i.zero? ? nil : self[:admin_comments_count]
end

#for_input_tokenObject


22
23
24
# File 'lib/ab_admin/concerns/admin_addition.rb', line 22

def for_input_token
  {id: id, text: AbAdmin.safe_display_name(self).to_s}
end

#hanObject


26
27
28
# File 'lib/ab_admin/concerns/admin_addition.rb', line 26

def han
  "#{self.class.model_name.human(count: 1)} ##{self.id} #{AbAdmin.safe_display_name(self)}"
end

#new_changesObject


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ab_admin/concerns/admin_addition.rb', line 34

def new_changes
  excluded_attrs = [:updated_at]
  excluded_attrs += translated_attribute_names if self.class.translates?
  all_changes = changes.except(*excluded_attrs).map { |k, v| [k, v[1]] }.to_h
  if self.class.translates?
    globalize.dirty.each do |attr, changes|
      changes.each do |change|
        all_changes["#{attr}_#{change[0]}"] = send("#{attr}_#{change[0]}")
      end
    end
  end
  all_changes
end

#next_prev_by_url(scope, url, prev = false) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ab_admin/concerns/admin_addition.rb', line 76

def next_prev_by_url(scope, url, prev=false)
  predicates = {'>' => '<', '<' => '>', 'desc' => 'asc', 'asc' => 'desc'}
  query = Rack::Utils.parse_nested_query(URI.parse(url).query).symbolize_keys
  query[:q] ||= {}
  order_str = query[:q]['s'] || 'id desc'
  order_col, order_mode = order_str.split
  quoted_order_col = self.class.quote_column(order_col)
  if prev
    query[:q]['s'] = ["#{order_col} #{predicates[order_mode]}", 'id desc']
    predicate = order_mode == 'desc' ? '>' : '<'
    id_predicate = '<'
  else
    query[:q]['s'] = ["#{order_col} #{order_mode}", 'id']
    predicate = order_mode == 'desc' ? '<' : '>'
    id_predicate = '>'
  end
  sql = "(#{quoted_order_col} #{predicate} :val OR (#{quoted_order_col} = :val AND #{self.class.quote_column('id')} #{id_predicate} #{id}))"
  scope.where(sql, val: send(order_col)).ransack(query[:q]).result(distinct: true).first
end

#token_data(method, options = {}) ⇒ Object


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ab_admin/concerns/admin_addition.rb', line 52

def token_data(method, options={})
  assoc = self.class.reflect_on_association(method)
  scope = send(method)
  scope = scope.reorder("#{assoc.options[:through]}.position") if options[:sortable]
  records = Array(scope)
  data = records.map(&:for_input_token)
  data = {
      pre: data.to_json,
      class: assoc.klass.name,
      multi: assoc.collection?,
      c: options.delete(:c),
      sortable: options.delete(:sortable)
  }
  if options[:geo_order]
    data[:c] ||= {}
    singular = self.class.model_name.singular
    data[:c].reverse_deep_merge!({with: {lat: "#{singular}_lat", lon: "#{singular}_lon"}})
  end
  if data[:c] && !data[:c].is_a?(String)
    data[:c] = data[:c].to_json
  end
  options.reverse_deep_merge!(class: 'fancy_select', data: data, value: records.map(&:id).join(','))
end

#translated_any(attr) ⇒ Object


30
31
32
# File 'lib/ab_admin/concerns/admin_addition.rb', line 30

def translated_any(attr)
  send(attr).presence || translations.detect { |r| r.send(attr).present? }.try!(attr)
end