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

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

Instance Method Summary collapse

Instance Method Details

#all_columns_namesObject


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

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


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

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

#all_modelsObject


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

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

#all_translated_attribute_namesObject


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

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

#friendly_find(*args) ⇒ Object


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

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

#full_truncate(with_destroy = true) ⇒ Object


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

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


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

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


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

def han(attr)
  human_attribute_name(attr)
end

#max_timeObject


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

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


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

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


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

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

#update_counter_column(col, assoc_method) ⇒ Object


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
83
84
# File 'lib/ab_admin/concerns/utilities.rb', line 58

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


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

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