app/models/unidom/common/concerns/model_extension.rb in unidom-common-1.5 vs app/models/unidom/common/concerns/model_extension.rb in unidom-common-1.6
- old
+ new
@@ -1,9 +1,14 @@
module Unidom::Common::Concerns::ModelExtension
extend ActiveSupport::Concern
+ include Unidom::Common::Concerns::ArgumentValidation
+ include Unidom::Common::Concerns::NotationColumn
+ include Unidom::Common::Concerns::ExactColumn
+ include Unidom::Common::Concerns::SecureColumn
+
included do |includer|
validates :state, presence: true, length: { is: columns_hash['state'].limit }
scope :included_by, ->(inclusion) { where id: inclusion }
@@ -15,18 +20,20 @@
scope :valid_duration, ->(range) { where "(#{includer.table_name}.opened_at BETWEEN ? AND ?) OR (#{includer.table_name}.closed_at <= ? AND #{includer.table_name}.closed_at >= ?)", range.min, range.max, range.max, range.min }
scope :alive, ->(living: true) { where defunct: !living }
scope :dead, ->(defunct: true) { where defunct: defunct }
+=begin
scope :notation_column_where, ->(name, operator, value) do
operation = :like==operator ? { operator: 'ILIKE', value: "%#{value}%" } : { operator: operator.to_s, value: value }
where "#{table_name}.notation -> 'columns' ->> '#{name}' #{operation[:operator]} :value", value: operation[:value]
end
scope :notation_boolean_column_where, ->(name, value) do
where "(#{table_name}.notation -> 'columns' ->> '#{name}')::boolean = :value", value: (value ? true : false)
end
+=end
if columns_hash['ordinal'].present?&&:integer==columns_hash['ordinal'].type
validates :ordinal, presence: true, numericality: { integer_only: true, greater_than: 0 }
scope :ordinal_is, ->(ordinal) { where ordinal: ordinal }
end
@@ -134,22 +141,24 @@
else
::SecureRandom.uuid
end
end
+=begin
def assert_present!(name, value)
raise ArgumentError.new("The #{name} argument is required.") if value.blank?
end
+=end
end
module ClassMethods
def to_id(model)
model.respond_to?(:id) ? model.id : model
end
-
+=begin
def notation_column(*names)
names.each do |name|
name = name.to_s
instance_eval do
define_method(name) do
@@ -175,11 +184,13 @@
notation['columns'][name] = value
end
end
end
end
+=end
+=begin
def exact_column(*names)
names.each do |name|
name = name.to_s
instance_eval do
scope :"#{name}_is", ->(value) { where "#{name}_exact_signature" => exact_signature(self, name, value) }
@@ -187,18 +198,23 @@
send "#{name}_exact_signature=", self.class.exact_signature(self.class, name, send(name))
end
end
end
end
+=end
+=begin
def assert_present!(name, value)
raise ArgumentError.new("The #{name} argument is required.") if value.blank?
end
+=end
+=begin
def exact_signature(klass, name, value)
text = "#{Rails.application.secrets[:secret_key_base]}@#{Rails.root}/#{klass.table_name}##{name}=#{value}"
"#{Digest::MD5.digest(text)}#{Digest::SHA512.digest(text)}"
end
+=end
end
end