app/models/unidom/common/concerns/model_extension.rb in unidom-common-1.9.2 vs app/models/unidom/common/concerns/model_extension.rb in unidom-common-1.10

- old
+ new

@@ -19,21 +19,10 @@ 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: { only_integer: true, greater_than: 0 } scope :ordinal_is, ->(ordinal) { where ordinal: ordinal } end @@ -140,85 +129,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 + ## + # 将模型对象或者 ID 转换成 ID 。如: + # to_id(person) # person.id + # to_id(person.id) # person.id def to_id(model) model.respond_to?(:id) ? model.id : model end def to_code(code) code.respond_to?(:code) ? code.code : code end - -=begin - def notation_column(*names) - names.each do |name| - name = name.to_s - instance_eval do - define_method(name) do - notation.try(:[], 'columns').try(:[], name) - end - define_method("#{name}=") do |value| - notation['columns'] ||= {} - notation['columns'][name] = value - end - end - end - end - - def notation_boolean_column(*names) - names.each do |name| - name = name.to_s - instance_eval do - define_method("#{name}?") do - notation.try(:[], 'columns').try(:[], name) - end - define_method("#{name}=") do |value| - notation['columns'] ||= {} - 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) } - before_save do - 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