Class: Record

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Conversion, ActiveModel::Dirty, ActiveModel::Model, ActiveModel::Validations, Turbo::Broadcastable
Defined in:
app/models/record.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Record

TODO: more validation



25
26
27
28
29
30
31
32
33
34
# File 'app/models/record.rb', line 25

def initialize(attributes = {})
  super
  if name.blank?
    self.name = "@"
  end

  if ttl.blank?
    self.ttl = 300
  end
end

Instance Attribute Details

#_id=(value) ⇒ Object (writeonly)

Sets the attribute _id

Parameters:

  • value

    the value to set the attribute _id to.



15
16
17
# File 'app/models/record.rb', line 15

def _id=(value)
  @_id = value
end

#_persisted=(value) ⇒ Object (writeonly)

Sets the attribute _persisted

Parameters:

  • value

    the value to set the attribute _persisted to.



15
16
17
# File 'app/models/record.rb', line 15

def _persisted=(value)
  @_persisted = value
end

#contentObject

Returns the value of attribute content.



11
12
13
# File 'app/models/record.rb', line 11

def content
  @content
end

#domain_idObject

Returns the value of attribute domain_id.



11
12
13
# File 'app/models/record.rb', line 11

def domain_id
  @domain_id
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'app/models/record.rb', line 11

def name
  @name
end

#priorityObject

Returns the value of attribute priority.



11
12
13
# File 'app/models/record.rb', line 11

def priority
  @priority
end

#ttlObject

Returns the value of attribute ttl.



11
12
13
# File 'app/models/record.rb', line 11

def ttl
  @ttl
end

#typeObject

Returns the value of attribute type.



11
12
13
# File 'app/models/record.rb', line 11

def type
  @type
end

Class Method Details

.allObject

standard:disable all



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'app/models/record.rb', line 125

def self.all
  Rails.cache.fetch "records", expires_in: 2.minutes do
    dnsimple_records = client.zones.all_zone_records(Rails.application.credentials.dnsimple., Rails.application.config.domain).data.select { |record| !record.system_record }

    records = []
    for r in dnsimple_records
      if !r.name.blank?
        record = dnsimple_to_record(r)

        if record.domain_id
          records.push(record)
        end
      end
    end

    records

  end
end

.create(attributes = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'app/models/record.rb', line 36

def self.create(attributes = {})
  obj = new(attributes)
  obj.validate!
  obj.save # standard:disable all

  obj
end

.destroy_all_host!(host) ⇒ Object



116
117
118
119
120
# File 'app/models/record.rb', line 116

def self.destroy_all_host!(host)
  where_host(host).each do |r|
    r.destroy!
  end
end

.dnsimple_to_record(obj) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/record.rb', line 62

def self.dnsimple_to_record(obj)
  re = /^((.*)\.)?(.*)$/
  cap = re.match(obj.name)
  subdomain = cap[2]
  domain = cap[3]
  domain_obj = Domain.find_by(host: domain)

  Record.new(
    _id: obj.id,
    _persisted: true,
    name: subdomain,
    content: obj.content,
    priority: obj.priority,
    ttl: obj.ttl,
    type: obj.type,
    domain_id: domain_obj&.id
  )
end

.filter_dnsimple_host(host, domains) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/models/record.rb', line 44

def self.filter_dnsimple_host(host, domains)
  domain = Domain.find_by(host: host)
  Rails.cache.fetch([domain, "records"], expires_in: 1.week) do
    records = []
    domains.each do |r|
      if r.domain_id == domain.id
        records.push(r)
      end
    end

    records
  end
end

.find(id) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/record.rb', line 145

def self.find(id)
  id = id.to_i
  found = nil
  for r in all
    if r.id == id
      found = r
      break
    end
  end

  found
end

.where_host(host) ⇒ Object



58
59
60
# File 'app/models/record.rb', line 58

def self.where_host(host)
  filter_dnsimple_host(host, all)
end

Instance Method Details

#destroy!Object



111
112
113
114
# File 'app/models/record.rb', line 111

def destroy!
  destroy_record
  broadcast_remove_to(domain)
end

#idObject



107
108
109
# File 'app/models/record.rb', line 107

def id
  @_id
end

#persisted?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/record.rb', line 103

def persisted?
  @_persisted
end

#saveObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/record.rb', line 81

def save
  validate!

  if persisted?
    update_record
    broadcast_replace_to(domain, partial: "records/record")
  else
    persist
    broadcast_append_to(domain, partial: "records/record")
  end

  changes_applied

  Rails.cache.delete("records")
  domain.update!(updated_at: Time.now) # standard:disable all
end

#update(attributes = {}) ⇒ Object



98
99
100
101
# File 'app/models/record.rb', line 98

def update(attributes = {})
  assign_attributes(attributes)
  save # standard:disable all
end