foreman/lib/core_extensions.rb @ 9d43fc71
acfbc458 | Marek Hulan | require 'tsort'
|
|
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
|
|||
f2194f0a | Tim Wade | attr_reader :klasses
|
|
5f029ed6 | Daniel Lobato | def initialize(*attribute)
|
|
1462d569 | Ohad Levy | @klasses = attribute
|
|
end
|
|||
def before_destroy(record)
|
|||
08d4fc31 | Ori Rabin | klasses.each do |klass, klass_name = klass|
|
|
record.association(klass.to_sym).association_scope.each do |what|
|
|||
error_message = _("%{record} is used by %{what}")
|
|||
unless what.is_a? String
|
|||
authorized_associations = AssociationAuthorizer.authorized_associations(record.class.reflect_on_association(klass.to_sym).klass, klass_name, false)
|
|||
if !authorized_associations.respond_to?(:to_a) || authorized_associations.to_a.include?(what)
|
|||
what = what.to_label
|
|||
else
|
|||
what = _(what.class.name)
|
|||
error_message = _("%{record} is being used by a hidden %{what} resource")
|
|||
end
|
|||
end
|
|||
record.errors.add :base, error_message % { :record => record, :what => what }
|
|||
1462d569 | Ohad Levy | end
|
|
end
|
|||
66e83407 | Dominic Cleal | if record.errors.present?
|
|
f2194f0a | Tim Wade | Rails.logger.error "You may not destroy #{record.to_label} as it is in use!"
|
|
66e83407 | Dominic Cleal | throw :abort
|
|
1462d569 | Ohad Levy | end
|
|
end
|
|||
end
|
|||
acfbc458 | Marek Hulan | class EnsureNoCycle
|
|
include TSort
|
|||
def initialize(base, source, target)
|
|||
2d8b4fef | Daniel Lobato | @source, @target = source, target
|
|
acfbc458 | Marek Hulan | @base = base.map { |record| [record.send(@source), record.send(@target)] }
|
|
@nodes = @base.flatten.uniq
|
|||
@graph = Hash.new { |h, k| h[k] = [] }
|
|||
9d43fc71 | Michael Moll | @base.each { |s, t| @graph[s] << t }
|
|
acfbc458 | Marek Hulan | end
|
|
def tsort_each_node(&block)
|
|||
@nodes.each(&block)
|
|||
end
|
|||
def tsort_each_child(node, &block)
|
|||
@graph[node].each(&block)
|
|||
end
|
|||
def ensure(record)
|
|||
@record = record
|
|||
add_new_edges
|
|||
detect_cycle
|
|||
end
|
|||
private
|
|||
def add_new_edges
|
|||
edges = @graph[@record.send(@source) || 0]
|
|||
9d43fc71 | Michael Moll | edges << @record.send(@target) unless edges.include?(@record.send(@target))
|
|
acfbc458 | Marek Hulan | end
|
|
def detect_cycle
|
|||
if strongly_connected_components.any? { |component| component.size > 1 }
|
|||
@record.errors.add :base, _("Adding would cause a cycle!")
|
|||
raise ::Foreman::CyclicGraphException, @record
|
|||
else
|
|||
true
|
|||
end
|
|||
end
|
|||
end
|
|||
1462d569 | Ohad Levy | def id_and_type
|
|
"#{id}-#{self.class.table_name.humanize}"
|
|||
end
|
|||
12cc808b | Dmitri Dolguikh | alias_attribute :to_label, :name_method
|
|
1462d569 | Ohad Levy | alias_attribute :to_s, :to_label
|
|
def self.unconfigured?
|
|||
02e4c535 | Daniel Lobato | where(nil).reorder('').limit(1).pluck(self.base_class.primary_key).empty?
|
|
1462d569 | Ohad Levy | end
|
|
272811b9 | Ohad Levy | def self.per_page
|
|
e4fde4e3 | Ivan Nečas | # Foreman.in_rake? prevents the failure of db:migrate for postgresql
|
|
0cbf996c | Joseph Mitchell Magen | # don't query settings table if in rake
|
|
e4fde4e3 | Ivan Nečas | return 20 if Foreman.in_rake?
|
|
2e5f252a | Dominic Cleal | Setting[:entries_per_page] rescue 20
|
|
272811b9 | Ohad Levy | end
|
|
75bb05af | Martin Bačovský | ||
def self.audited(*args)
|
|||
# do not audit data changes during db migrations
|
|||
super
|
|||
self.auditing_enabled = false if Foreman.in_rake?('db:migrate')
|
|||
end
|
|||
b05ba938 | Ohad Levy | end
|
|
1462d569 | Ohad Levy | module ExemptedFromLogging
|
|
def process(request, *args)
|
|||
logger.silence { super }
|
|||
end
|
|||
end
|
|||
class String
|
|||
3a36bdf6 | Stephen Benjamin | def to_translation
|
|
_(self)
|
|||
end
|
|||
1462d569 | Ohad Levy | def to_gb
|
|
fc2fc7ac | Tomer Brisker | match_data = self.match(/^(\d+(\.\d+)?) ?(([KMGT]i?B?|B|Bytes))?$/i)
|
|
0771cec2 | Marek Hulan | if match_data.present?
|
|
value, _, unit = match_data[1..3]
|
|||
else
|
|||
raise "Unknown string: #{self.inspect}!"
|
|||
end
|
|||
68388bc2 | Michael Moll | unit ||= :byte # default to bytes if no unit given
|
|
fc2fc7ac | Tomer Brisker | ||
0771cec2 | Marek Hulan | case unit.downcase.to_sym
|
|
fc2fc7ac | Tomer Brisker | when :b, :byte, :bytes then (value.to_f / 1.gigabyte)
|
|
0771cec2 | Marek Hulan | when :tb, :tib, :t, :terabyte then (value.to_f * 1.kilobyte)
|
|
when :gb, :gib, :g, :gigabyte then value.to_f
|
|||
when :mb, :mib, :m, :megabyte then (value.to_f / 1.kilobyte)
|
|||
when :kb, :kib, :k, :kilobyte then (value.to_f / 1.megabyte)
|
|||
e7e5ef3e | Tomer Brisker | else raise "Unknown unit: #{unit.inspect}!"
|
|
1462d569 | Ohad Levy | end
|
|
end
|
|||
b5782137 | Shlomi Zadok | ||
def to_utf8
|
|||
self.encode('utf-8', :invalid => :replace, :undef => :replace, :replace => '_')
|
|||
end
|
|||
6a7e5074 | Tomer Brisker | ||
def contains_erb?
|
|||
self =~ /<%.*%>/
|
|||
end
|
|||
end
|
|||
class Object
|
|||
def contains_erb?
|
|||
false
|
|||
end
|
|||
1462d569 | Ohad Levy | end
|
|
30ae12bf | Ohad Levy | ||
class ActiveModel::Errors
|
|||
def are_all_conflicts?
|
|||
43c4bd72 | Marek Hulan | self[:conflict].count + self[:'interfaces.conflict'].count == self.count
|
|
30ae12bf | Ohad Levy | end
|
|
a5da258d | Jacob McCann | end
|