Class: Dbhero::Dataclip

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#q_resultObject (readonly)

Returns the value of attribute q_result.



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

def q_result
  @q_result
end

Instance Method Details

#csv_stringObject



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

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



25
26
27
# File 'app/models/dbhero/dataclip.rb', line 25

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

#query_resultObject



33
34
35
36
37
38
39
40
41
42
# File 'app/models/dbhero/dataclip.rb', line 33

def query_result
  Dataclip.transaction do
    begin
      @q_result ||= ActiveRecord::Base.connection.select_all(self.raw_query)
    rescue => e
      self.errors.add(:base, e.message)
    end
    raise ActiveRecord::Rollback
  end
end

#set_tokenObject



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

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

#titleObject



21
22
23
# File 'app/models/dbhero/dataclip.rb', line 21

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

#to_paramObject



17
18
19
# File 'app/models/dbhero/dataclip.rb', line 17

def to_param
  self.token
end

#total_rowsObject



29
30
31
# File 'app/models/dbhero/dataclip.rb', line 29

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