Class: Dbhero::Dataclip
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Dbhero::Dataclip
- Defined in:
- app/models/dbhero/dataclip.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#q_result ⇒ Object
readonly
Returns the value of attribute q_result.
Instance Method Summary collapse
- #cached? ⇒ Boolean
- #csv_string ⇒ Object
- #description_without_title ⇒ Object
- #query_result ⇒ Object
- #refresh_cache ⇒ Object
- #set_token ⇒ Object
- #title ⇒ Object
- #to_param ⇒ Object
- #total_rows ⇒ Object
Instance Attribute Details
#q_result ⇒ Object (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
39 40 41 |
# File 'app/models/dbhero/dataclip.rb', line 39 def cached? @cached ||= Rails.cache.fetch("dataclip_#{self.token}").present? end |
#csv_string ⇒ Object
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_title ⇒ Object
31 32 33 |
# File 'app/models/dbhero/dataclip.rb', line 31 def description_without_title description.split("\n")[1..-1].join("\n") end |
#query_result ⇒ Object
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.) end raise ActiveRecord::Rollback end end |
#refresh_cache ⇒ Object
14 15 16 |
# File 'app/models/dbhero/dataclip.rb', line 14 def refresh_cache Rails.cache.delete("dataclip_#{self.token}") end |
#set_token ⇒ Object
19 20 21 |
# File 'app/models/dbhero/dataclip.rb', line 19 def set_token self.token = SecureRandom.uuid unless self.token end |
#title ⇒ Object
27 28 29 |
# File 'app/models/dbhero/dataclip.rb', line 27 def title description.split("\n")[0] end |
#to_param ⇒ Object
23 24 25 |
# File 'app/models/dbhero/dataclip.rb', line 23 def to_param self.token end |
#total_rows ⇒ Object
35 36 37 |
# File 'app/models/dbhero/dataclip.rb', line 35 def total_rows @total_rows ||= @q_result.rows.length end |