foreman/lib/core_extensions.rb @ 272811b9
8a65dff7 | Ohad Levy | ActiveRecord::Associations::HasManyThroughAssociation.class_eval do
|
|
def delete_records(records)
|
|||
klass = @reflection.through_reflection.klass
|
|||
records.each do |associate|
|
|||
klass.destroy_all(construct_join_attributes(associate))
|
|||
end
|
|||
end
|
|||
end
|
|||
1462d569 | Ohad Levy | ||
# Add an empty method to nil. Now no need for if x and x.empty?. Just x.empty?
|
|||
class NilClass
|
|||
def empty?
|
|||
true
|
|||
end
|
|||
end
|
|||
class ActiveRecord::Base
|
|||
def <=>(other)
|
|||
self.name <=> other.name
|
|||
end
|
|||
def update_single_attribute(attribute, value)
|
|||
connection.update(
|
|||
b304788d | Amos Benari | "UPDATE #{self.class.quoted_table_name} SET " +
|
|
"#{connection.quote_column_name(attribute.to_s)} = #{quote_value(value)} " +
|
|||
"WHERE #{self.class.quoted_primary_key} = #{quote_value(id)}",
|
|||
1462d569 | Ohad Levy | "#{self.class.name} Attribute Update"
|
|
)
|
|||
end
|
|||
017e1049 | Ohad Levy | ||
de3fb0d7 | Amos Benari | def update_multiple_attribute(attributes)
|
|
connection.update(
|
|||
b304788d | Amos Benari | "UPDATE #{self.class.quoted_table_name} SET " +
|
|
attributes.map{|key, value| " #{connection.quote_column_name(key.to_s)} = #{quote_value(value)} " }.join(', ') +
|
|||
"WHERE #{self.class.quoted_primary_key} = #{quote_value(id)}",
|
|||
de3fb0d7 | Amos Benari | "#{self.class.name} Attribute Update"
|
|
)
|
|||
end
|
|||
b304788d | Amos Benari | ||
017e1049 | Ohad Levy | # ActiveRecord Callback class
|
|
class EnsureNotUsedBy
|
|||
0dd21533 | Ohad Levy | attr_reader :klasses, :logger
|
|
017e1049 | Ohad Levy | def initialize *attribute
|
|
1462d569 | Ohad Levy | @klasses = attribute
|
|
017e1049 | Ohad Levy | @logger = Rails.logger
|
|
1462d569 | Ohad Levy | end
|
|
def before_destroy(record)
|
|||
0dd21533 | Ohad Levy | klasses.each do |klass|
|
|
record.send(klass.to_sym).each do |what|
|
|||
017e1049 | Ohad Levy | record.errors.add :base, "#{record} is used by #{what}"
|
|
1462d569 | Ohad Levy | end
|
|
end
|
|||
67799065 | Ohad Levy | if record.errors.empty?
|
|
true
|
|||
else
|
|||
0dd21533 | Ohad Levy | logger.error "You may not destroy #{record.to_label} as it is in use!"
|
|
1462d569 | Ohad Levy | false
|
|
end
|
|||
end
|
|||
end
|
|||
def id_and_type
|
|||
"#{id}-#{self.class.table_name.humanize}"
|
|||
end
|
|||
alias_attribute :to_label, :name
|
|||
alias_attribute :to_s, :to_label
|
|||
def self.unconfigured?
|
|||
first.nil?
|
|||
end
|
|||
272811b9 | Ohad Levy | def self.per_page
|
|
Setting.entries_per_page rescue 20
|
|||
end
|
|||
1462d569 | Ohad Levy | end
|
|
module ExemptedFromLogging
|
|||
def process(request, *args)
|
|||
logger.silence { super }
|
|||
end
|
|||
end
|
|||
class String
|
|||
def to_gb
|
|||
begin
|
|||
a5da258d | Jacob McCann | value,f,unit=self.match(/(\d+(\.\d+)?) ?(([KMGT]B?|B))$/i)[1..3]
|
|
1462d569 | Ohad Levy | case unit.to_sym
|
|
84fce85c | jmccann | when nil, :B, :byte then (value.to_f / (4**10))
|
|
when :TB, :T, :terabyte then (value.to_f * (2**10))
|
|||
1462d569 | Ohad Levy | when :GB, :G, :gigabyte then value.to_f
|
|
84fce85c | jmccann | when :MB, :M, :megabyte then (value.to_f / (2**10))
|
|
when :KB, :K, :kilobyte, :kB then (value.to_f / (3**10))
|
|||
1462d569 | Ohad Levy | else raise "Unknown unit: #{unit.inspect}!"
|
|
end
|
|||
rescue
|
|||
99a2326b | Tim Speetjens | raise "Unknown string: #{self.inspect}!"
|
|
1462d569 | Ohad Levy | end
|
|
end
|
|||
end
|
|||
30ae12bf | Ohad Levy | ||
class ActiveModel::Errors
|
|||
def are_all_conflicts?
|
|||
self[:conflict].count == self.count
|
|||
end
|
|||
a5da258d | Jacob McCann | end
|