Class: Dbhero::Dataclip

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/dbhero/dataclip.rb

Direct Known Subclasses

DataclipRead

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#q_resultObject (readonly)

Returns the value of attribute q_result.



12
13
14
# File 'app/models/dbhero/dataclip.rb', line 12

def q_result
  @q_result
end

Instance Method Details

#cached?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'app/models/dbhero/dataclip.rb', line 39

def cached?
  @cached ||= Rails.cache.fetch("dataclip_#{self.token}").present?
end

#csv_stringObject



56
57
58
59
60
61
62
63
# File 'app/models/dbhero/dataclip.rb', line 56

def csv_string
  query_result
  csv_string = CSV.generate(force_quotes: true, col_sep: Dbhero.csv_delimiter) do |csv|
    csv << @q_result.columns
    @q_result.rows.each { |row| csv << row }
  end
  csv_string
end

#description_without_titleObject



31
32
33
# File 'app/models/dbhero/dataclip.rb', line 31

def description_without_title
  description.split("\n")[1..-1].join("\n")
end

#query_resultObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/dbhero/dataclip.rb', line 43

def query_result
  DataclipRead.transaction do
    begin
      @q_result ||= Rails.cache.fetch("dataclip_#{self.token}", expires_in: (::Dbhero.cached_query_exp||10.minutes)) do
        DataclipRead.connection.select_all(self.raw_query)
      end
    rescue => e
      self.errors.add(:base, e.message)
    end
    raise ActiveRecord::Rollback
  end
end

#refresh_cacheObject



14
15
16
# File 'app/models/dbhero/dataclip.rb', line 14

def refresh_cache
  Rails.cache.delete("dataclip_#{self.token}")
end

#set_tokenObject



19
20
21
# File 'app/models/dbhero/dataclip.rb', line 19

def set_token
  self.token = SecureRandom.uuid unless self.token
end

#titleObject



27
28
29
# File 'app/models/dbhero/dataclip.rb', line 27

def title
  description.split("\n")[0]
end

#to_paramObject



23
24
25
# File 'app/models/dbhero/dataclip.rb', line 23

def to_param
  self.token
end

#total_rowsObject



35
36
37
# File 'app/models/dbhero/dataclip.rb', line 35

def total_rows
  @total_rows ||= @q_result.rows.length
end