GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — sql-cms/app/models/validation.rb at master · evilbuck/sql-cms Expand file tree/ Copy pathvalidation.rbMore file actions 147 lines (116 loc) · 4.88 KB / Copy pathvalidation.rbFile metadata and controls147 lines (116 loc) · 4.88 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 # frozen_string_literal: true # == Schema Information # # Table name: validations # # id :integer not null, primary key # name :string not null # immutable :boolean default(FALSE), not null # sql :text not null # created_at :datetime not null # updated_at :datetime not null # # Indexes # # index_validations_on_lowercase_name (lower((name)::text)) UNIQUE # class Validation < ApplicationRecord # Validations are SQL Data Quality Checks run after Transforms with which they are associated, and validate that the transformed data isn't corrupt # A Validation returns the ID(s) of any record(s) that fail the validation auto_normalize # Validations validates :sql, presence: true validates :name, presence: true, uniqueness: { case_sensitive: false } # Callbacks include Concerns::ImmutableCallbacks immutable :update, :destroy # Associations has_many :transform_validations, inverse_of: :validation, dependent: :destroy has_many :transforms, through: :transform_validations # Instance Methods def usage_count transform_validations.count end def used? usage_count > 0 end # Class Methods class << self def non_null @non_null ||= where(name: 'Column :table_name.:column_name IS NOT NULL').first_or_create!( immutable: true, sql: 'SELECT id FROM :table_name WHERE :column_name IS NULL' ) end def presence @presence ||= where(name: 'Column :table_name.:column_name is Present').first_or_create!( immutable: true, sql: "SELECT id FROM :table_name WHERE (TRIM(:column_name) <> '') IS NOT TRUE" ) end def uniqueness @uniqueness ||= where(name: 'Column :table_name.:column_name is Unique').first_or_create!( immutable: true, sql: 'SELECT :table_name.* FROM :table_name JOIN :table_name AS t ON :table_name.:column_name = t.:column_name AND :table_name.id != t.id' ) end def fk @fk ||= where(name: 'Column :fk_table_name.:fk_column_name is a Valid FK to Column :pk_table_name.:pk_column_name').first_or_create!( immutable: true, sql: "SELECT id FROM :fk_table_name WHERE :fk_column_name IS NOT NULL AND NOT EXISTS (SELECT 1 FROM :pk_table_name WHERE :pk_table_name.:pk_column_name = :fk_table_name.:fk_column_name)" ) end def inclusion @inclusion ||= where(name: 'Column :table_name.:column_name is Included in :allowed_values').first_or_create!( immutable: true, sql: 'SELECT id FROM :table_name WHERE :column_name NOT IN (:allowed_values)' ) end DIGIT_REGEXP = '^-?[[:digit:]]+$' def integer @integer ||= where(name: 'Column :table_name.:column_name is an Integer').first_or_create!( immutable: true, sql: "SELECT id FROM :table_name WHERE :column_name !~ '#{DIGIT_REGEXP}'" ) end def integer_with_additional @integer_with_additional ||= where(name: 'Column :table_name.:column_name is an Integer or :extras').first_or_create!( immutable: true, sql: "SELECT id FROM :table_name WHERE :column_name !~ '#{DIGIT_REGEXP}' AND :column_name NOT IN (:extras)" ) end def greater_than @greater_than ||= where(name: 'Column :table_name.:column_name is Greater Than :value').first_or_create!( immutable: true, sql: 'SELECT id from :table_name WHERE :column_name <= :value' ) end def less_than @less_than ||= where(name: 'Column :table_name.:column_name is Less Than :value').first_or_create!( immutable: true, sql: 'SELECT id from :table_name WHERE :column_name >= :value' ) end def non_overlapping @non_overlapping ||= where(name: "Table :table_name's Columns :low_column_name & :high_column_name Denote a Non-Overlapping Range").first_or_create!( immutable: true, sql: "SELECT first_alias.id FROM :table_name first_alias, :table_name second_alias WHERE first_alias.id <> second_alias.id AND ((first_alias.:low_column_name - second_alias.:high_column_name) * (second_alias.:low_column_name - first_alias.:high_column_name)) >= 0" ) end def percentage @percentage ||= where(name: "Column :table_name.:column_name is a valid percent").first_or_create!( immutable: true, sql: "SELECT id FROM :table_name WHERE :column_name < 0 OR :column_name > 100" ) end def between @between ||= where(name: 'Column :table_name.:column_name is Between :min_value and :max_value').first_or_create!( immutable: true, sql: 'SELECT id from :table_name WHERE :column_name < :min_value OR :column_name > :max_value' ) end def flush_cache @non_null = @presence = @uniqueness = @fk = @inclusion = @integer = @integer_with_additional = @greater_than = @less_than = @non_overlapping = @percentage = @between = nil end end end |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |