Class: Decidim::FileAuthorizationHandler::CensusDatum
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Decidim::FileAuthorizationHandler::CensusDatum
- Defined in:
- app/models/decidim/file_authorization_handler/census_datum.rb
Class Method Summary collapse
-
.clear(organization) ⇒ Object
Clear all census data for a given organization.
-
.insert_all(organization, values, extra_headers = nil) ⇒ Object
Insert a collection of values.
-
.inside(organization) ⇒ Object
An organzation scope.
-
.normalize_and_encode_id_document(id_document) ⇒ Object
Normalizes a id document string (remove invalid characters) and encode it to conform with Decidim privacy guidelines.
-
.parse_date(string) ⇒ Object
Convert a date from string to a Date object.
-
.search_id_document(organization, id_document) ⇒ Object
Search for a specific document id inside a organization.
Class Method Details
.clear(organization) ⇒ Object
Clear all census data for a given organization
65 66 67 |
# File 'app/models/decidim/file_authorization_handler/census_datum.rb', line 65 def self.clear(organization) CensusDatum.inside(organization).delete_all end |
.insert_all(organization, values, extra_headers = nil) ⇒ Object
Insert a collection of values
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'app/models/decidim/file_authorization_handler/census_datum.rb', line 42 def self.insert_all(organization, values, extra_headers = nil) return if values.empty? table_name = CensusDatum.table_name columns = %w(id_document birthdate decidim_organization_id created_at).join(",") columns = "#{columns},extras" if extra_headers.present? now = Time.current values = values.map do |row| vals = "('#{row[0]}', '#{row[1]}', '#{organization.id}', '#{now}'" if extra_headers.present? extras = {} extra_headers.present? && extra_headers.each_with_index do |header, idx| extras[header.downcase] = row[2 + idx] end vals += ", '#{extras.to_json}'" end "#{vals})" end sql = "INSERT INTO #{table_name} (#{columns}) VALUES #{values.join(",")}" ActiveRecord::Base.connection.execute(sql) end |
.inside(organization) ⇒ Object
An organzation scope
9 10 11 |
# File 'app/models/decidim/file_authorization_handler/census_datum.rb', line 9 def self.inside(organization) where(decidim_organization_id: organization.id) end |
.normalize_and_encode_id_document(id_document) ⇒ Object
Normalizes a id document string (remove invalid characters) and encode it to conform with Decidim privacy guidelines.
23 24 25 26 27 28 29 30 31 32 |
# File 'app/models/decidim/file_authorization_handler/census_datum.rb', line 23 def self.normalize_and_encode_id_document(id_document) return "" unless id_document id_document = id_document.gsub(/[^A-z0-9]/, "").upcase return "" if id_document.blank? Digest::SHA256.hexdigest( "#{id_document}-#{Rails.application.secrets.secret_key_base}" ) end |
.parse_date(string) ⇒ Object
Convert a date from string to a Date object
35 36 37 38 39 |
# File 'app/models/decidim/file_authorization_handler/census_datum.rb', line 35 def self.parse_date(string) Date.strptime((string || "").strip, "%d/%m/%Y") rescue StandardError nil end |
.search_id_document(organization, id_document) ⇒ Object
Search for a specific document id inside a organization
14 15 16 17 18 19 |
# File 'app/models/decidim/file_authorization_handler/census_datum.rb', line 14 def self.search_id_document(organization, id_document) CensusDatum.inside(organization) .where(id_document: normalize_and_encode_id_document(id_document)) .order(created_at: :desc, id: :desc) .first end |