Module: AbAdmin::Concerns::Utilities::ClassMethods

Defined in:
lib/ab_admin/concerns/utilities.rb

Instance Method Summary collapse

Instance Method Details

#all_columns_namesObject



97
98
99
100
101
102
103
104
# File 'lib/ab_admin/concerns/utilities.rb', line 97

def all_columns_names
  ActiveSupport::Deprecation.warn('#all_columns_names is deprecated without replacement')
  if translates?
    column_names + all_translated_attribute_names + translated_attribute_names.map(&:to_s)
  else
    column_names
  end
end

#all_idsObject



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

def all_ids
  select(:id).map(&:id)
end

#all_modelsObject



30
31
32
33
34
# File 'lib/ab_admin/concerns/utilities.rb', line 30

def all_models
  Dir.glob(Rails.root.to_s + '/app/models/**/*.rb').each { |file| require file }
  ActiveRecord::Base.descendants.find_all { |model| model.table_exists? }
  #ActiveRecord::Base.descendants.find_all { |model| model.descends_from_active_record? }
end

#all_translated_attribute_namesObject



87
88
89
90
91
92
93
94
95
# File 'lib/ab_admin/concerns/utilities.rb', line 87

def all_translated_attribute_names
  if translates?
    ::I18n.available_locales.map do |loc|
      translated_attribute_names.map { |attr| "#{attr}_#{loc}" }
    end.flatten
  else
    []
  end
end

#friendly_find(*args) ⇒ Object



113
114
115
# File 'lib/ab_admin/concerns/utilities.rb', line 113

def friendly_find(*args)
  find(*args)
end

#full_truncate(with_destroy = true) ⇒ Object



20
21
22
23
24
# File 'lib/ab_admin/concerns/utilities.rb', line 20

def full_truncate(with_destroy=true)
  destroy_all if with_destroy
  truncate!
  const_get(:Translation).truncate! if try!(:translates?)
end

#generate_token(column = :guid) ⇒ Object



106
107
108
109
110
111
# File 'lib/ab_admin/concerns/utilities.rb', line 106

def generate_token(column=:guid)
  loop do
    token = AbAdmin.friendly_token
    break token unless to_adapter.find_first({column => token})
  end
end

#han(attr) ⇒ Object



36
37
38
# File 'lib/ab_admin/concerns/utilities.rb', line 36

def han(attr)
  human_attribute_name(attr)
end

#max_timeObject



12
13
14
# File 'lib/ab_admin/concerns/utilities.rb', line 12

def max_time
  Rails.cache.fetch("by_class_#{name}", expires_in: 60) { unscoped.maximum(:updated_at).to_i }
end

#max_time_by_scope(scope) ⇒ Object



16
17
18
# File 'lib/ab_admin/concerns/utilities.rb', line 16

def max_time_by_scope(scope)
  Rails.cache.fetch("by_class_#{name}_#{scope}", expires_in: 60) { unscoped.send(scope).maximum(:updated_at).to_i }
end

#quote_column(col_name) ⇒ Object



40
41
42
# File 'lib/ab_admin/concerns/utilities.rb', line 40

def quote_column(col_name)
  "#{quoted_table_name}.#{connection.quote_column_name(col_name)}"
end

#update_counter_column(col, assoc_method) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ab_admin/concerns/utilities.rb', line 59

def update_counter_column(col, assoc_method)
  assoc = assoc_count = reflect_on_association(assoc_method)
  if assoc
    add_from = ''
    add_cond = ['1=1']
    if assoc.options[:through]
      assoc_count = reflect_on_association(assoc.options[:through])
      unless assoc.klass.table_name.to_s == table_name.to_s
        add_from = "INNER JOIN #{assoc.klass.quoted_table_name} ON #{assoc_count.klass.quoted_table_name}.#{assoc.foreign_key} = #{assoc.klass.quoted_table_name}.id"
      end
    end
    add_cond << "#{assoc.klass.quoted_table_name}.#{assoc.type} = '#{name}'" if assoc.type
    add_cond << assoc.klass.instance_exec(&assoc.scope).to_sql[/WHERE(.*?)(?:(?:ORDER|LIMIT).*)?$/, 1] if assoc.scope
    if assoc.klass.default_scopes.present?
      assoc.klass.default_scopes.each do |scope|
        add_cond << scope.call.to_sql[/WHERE(.*?)(?:(?:ORDER|LIMIT).*)?$/, 1]
      end
    end
    count_klass = assoc_count.klass
    query = <<-SQL
        UPDATE #{quoted_table_name} SET #{col} = (SELECT COUNT(#{count_klass.quoted_table_name}.id)
          FROM #{count_klass.quoted_table_name} #{add_from}
          WHERE #{quoted_table_name}.id = #{count_klass.quoted_table_name}.#{assoc_count.foreign_key} AND #{add_cond.reject(&:blank?).join(' AND ')})
    SQL
    connection.execute(query)
  end
end

#update_counter_columns(*args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ab_admin/concerns/utilities.rb', line 44

def update_counter_columns(*args)
  args.each do |counter_column|
    assoc = reflect_on_association(counter_column.to_s.sub(/_count$/, '').to_sym)
    if assoc
      count_klass = assoc.klass
      query = <<-SQL
        UPDATE #{quoted_table_name} SET #{counter_column} = (SELECT COUNT(#{count_klass.quoted_table_name}.id)
          FROM #{count_klass.quoted_table_name}
          WHERE #{quoted_table_name}.id = #{count_klass.quoted_table_name}.#{assoc.foreign_key})
      SQL
      connection.execute(query)
    end
  end
end