Class: BetterRecord::Base

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ModelConcerns::HasProtectedPassword, ModelConcerns::HasValidatedAvatar
Defined in:
app/models/better_record/base.rb

Direct Known Subclasses

AttachmentValidation, LoggedAction, TableSize

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.full_table_nameObject



122
123
124
# File 'app/models/better_record/base.rb', line 122

def self.full_table_name
  "#{schema_qualified[:schema_name]}.#{schema_qualified[:table_name]}"
end

.gender_enum(col) ⇒ Object



57
58
59
# File 'app/models/better_record/base.rb', line 57

def self.gender_enum(col)
  enum col, BetterRecord::Gender::ENUM
end

.get_hashed_string(str) ⇒ Object



61
62
63
64
65
# File 'app/models/better_record/base.rb', line 61

def self.get_hashed_string(str)
  ct = Time.now.to_i
  cq = ActiveRecord::Base.sanitize_sql_array(["hash_password(?) as hashed_cert_#{t}", str])
  select(cq).limit(1).first[:"hashed_cert_#{t}"]
end

.reset_all_schemasObject



67
68
69
70
71
72
# File 'app/models/better_record/base.rb', line 67

def self.reset_all_schemas
  ActiveRecord::Base.descendants.each do |desc|
    desc.reset_qualified_schema rescue nil
  end
  true
end

.reset_qualified_schemaObject



74
75
76
# File 'app/models/better_record/base.rb', line 74

def self.reset_qualified_schema
  @schema_qualified = nil
end

.saved_qualified_schemaObject



78
79
80
# File 'app/models/better_record/base.rb', line 78

def self.saved_qualified_schema
  @schema_qualified
end

.schema_qualifiedObject



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
# File 'app/models/better_record/base.rb', line 82

def self.schema_qualified
  return @schema_qualified if @schema_qualified.present?
  if table_name =~ /.+\..+/
    tmp_name = table_name.split('.')
    @schema_qualified = {
      schema_name: tmp_name[0],
      table_name: tmp_name[1]
    }
  else
    paths = (connection.execute("show search_path").first || {})['search_path']
    paths = (paths.presence || "public").to_s.split(",")
    paths.each do |schema_path|
      row = (
        connection.execute <<-SQL.cleanup_production
          SELECT c.oid,
            n.nspname,
            c.relname
          FROM pg_catalog.pg_class c
            LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
          WHERE c.relname = '#{table_name}'
            AND n.nspname = '#{schema_path.strip}'
          ORDER BY 2, 3;
        SQL
      ).first

      if row.present?
        @schema_qualified = {
          schema_name: row['nspname'],
          table_name: row['relname']
        }
        break
      end
    end
    return @schema_qualified ||= {
      schema_name: "public",
      table_name: table_name
    }
  end
end

.set_audit_methods!Object

Class Methods ========================================================



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
# File 'app/models/better_record/base.rb', line 26

def self.set_audit_methods!
  begin
    t_name = BetterRecord::LoggedAction.table_name.sub('_view', '')
    connection.execute(%Q(SELECT 1 FROM #{t_name}_#{self.table_name_only} LIMIT 1))

    self.const_set(:LoggedAction, Class.new(ApplicationRecord))
    self.const_get(:LoggedAction).table_name = "#{t_name}_#{self.table_name_only}"
    self.const_get(:LoggedAction).primary_key = :event_id
  rescue ActiveRecord::StatementInvalid
    self.const_set(:LoggedAction, BetterRecord::LoggedAction)
  end

  self.has_many self.audit_relation_name,
    class_name: "#{self.to_s}::LoggedAction",
    primary_type: :full_table_name,
    foreign_key: :row_id,
    foreign_type: :full_name,
    as: self.audit_relation_name

  self.has_many :"#{self.audit_relation_name}_full_table",
    class_name: "#{self.to_s}::LoggedAction",
    primary_type: :table_name_only,
    foreign_key: :row_id,
    foreign_type: :table_name,
    as: self.audit_relation_name

  self
rescue ActiveRecord::NoDatabaseError
  self
end

.table_name_onlyObject



126
127
128
# File 'app/models/better_record/base.rb', line 126

def self.table_name_only
  schema_qualified[:table_name]
end

Instance Method Details

#dup(allow_full_dup = false) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'app/models/better_record/base.rb', line 141

def dup(allow_full_dup = false)
  if !allow_full_dup && self.class.const_defined?(:NON_DUPABLE_KEYS)
    super().tap do |r|
      r.class::NON_DUPABLE_KEYS.each {|k| r[k] = nil }
    end
  else
    super()
  end
end

#get_hashed_string(str) ⇒ Object

Instance Methods =====================================================



133
134
135
# File 'app/models/better_record/base.rb', line 133

def get_hashed_string(str)
  self.class.get_hashed_string(str)
end

#indifferent_attributesObject



137
138
139
# File 'app/models/better_record/base.rb', line 137

def indifferent_attributes
  attributes.with_indifferent_access
end