-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move extra functionalities to optional installer
- Loading branch information
Matteo La Cognata [fabbricadigitale]
committed
Aug 4, 2018
1 parent
d3237a3
commit 7f9b80d
Showing
31 changed files
with
811 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::ExcelController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
include ActionController::MimeResponds | ||
|
||
def download | ||
records = related ? | ||
instance.send(related) | ||
: klass_search_scope.where(allowed_filter) | ||
send_data(records.to_xlsx) | ||
rescue StandardError => e | ||
render_error e.message | ||
end | ||
|
||
private | ||
|
||
# params[:resource] the model name we want to download records from | ||
def klass | ||
params[:resource].tableize.singularize.camelize.constantize | ||
end | ||
|
||
# filter[:q] is a free-text search | ||
def klass_search_scope | ||
filter[:q].present? ? | ||
klass.search(filter[:q]) | ||
: klass.all | ||
end | ||
|
||
# params[:related] controls whether to send | ||
# false: noop | ||
# true: all records related to the id of the object received | ||
# ex. all posts of a certain author | ||
def related | ||
params[:related] ? | ||
params[:related].tableize : nil | ||
end | ||
|
||
# params[:id] required when requesting related records | ||
def instance | ||
params[:id] ? | ||
klass.find(params[:id]) | ||
: nil | ||
end | ||
|
||
# params[:filter] filters applied to the search records | ||
def filter | ||
params[:filter] && params[:filter].permit! || {} | ||
end | ||
|
||
def allowed_filter | ||
filter.except(:q) | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::PdfController < ApplicationController | ||
before_action :authenticate_user! | ||
|
||
include ActionController::MimeResponds | ||
|
||
def print | ||
send_file( | ||
instance.to_pdf, | ||
filename: instance.name, | ||
type: :pdf ) | ||
rescue StandardError => e | ||
render_error e.message | ||
end | ||
|
||
private | ||
|
||
def klass | ||
params[:resource].singularize.camelize.constantize | ||
end | ||
|
||
def instance | ||
klass.find(params[:id]) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationJob < ActiveJob::Base | ||
discard_on(StandardError) do |job, exception| | ||
puts(exception) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# frozen_string_literal: true | ||
|
||
class ApplicationMailer < ActionMailer::Base | ||
layout 'mailer' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'roo' | ||
require 'roo-xls' | ||
|
||
module ActsAsSpreadsheet | ||
module Spreadsheet | ||
extend self | ||
|
||
def open(file_path) | ||
open_spreadsheet(File.open(file_path)) | ||
end | ||
|
||
def open_spreadsheet(file) | ||
case File.extname(file) | ||
when '.csv' then Roo::CSV.new(file.path, csv_options: { col_sep: ';' }) | ||
when '.ods' then Roo::OpenOffice.new(file.path) | ||
when '.xls' then Roo::Excel.new(file.path) | ||
when '.xlsx' then Roo::Excelx.new(file.path) | ||
else raise "Unknown file type: #{file}" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
module HasExchangeRate | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
def has_exchange_rate(options = {}); end | ||
end | ||
|
||
included do | ||
|
||
default_value_for :auto_update_exchange_rate, | ||
true | ||
|
||
default_value_for :currency_code, ENV['CURRENCY'] | ||
|
||
validates :exchange_rate, | ||
presence: true, | ||
numericality: true, | ||
unless: proc { |r| r.auto_update_exchange_rate? } | ||
end | ||
|
||
def effective_exchange_rate | ||
auto_update_exchange_rate ? | ||
(ENV['CURRENCY'] === currency_code ? 1 : OpenExchangeRate.get_exchange_rate(currency_code, ENV['CURRENCY'])) : exchange_rate | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# frozen_string_literal: true | ||
# ## Schema Information | ||
# | ||
# Table name: `countries` | ||
# | ||
# ### Columns | ||
# | ||
# Name | Type | Attributes | ||
# --------------------------- | ------------------ | --------------------------- | ||
# **`id`** | `uuid` | `not null, primary key` | ||
# **`name`** | `citext` | `not null` | ||
# **`alpha2`** | `string` | | ||
# **`alpha3`** | `string` | `not null` | ||
# **`region`** | `string` | | ||
# **`subregion`** | `string` | | ||
# **`world_region`** | `string` | | ||
# **`country_code`** | `string` | | ||
# **`international_prefix`** | `string` | | ||
# **`currency_code`** | `string` | | ||
# **`currency_name`** | `string` | | ||
# **`currency_symbol`** | `string` | | ||
# **`timezones`** | `string` | `is an Array` | ||
# **`timezones_offsets`** | `string` | `is an Array` | ||
# **`created_at`** | `datetime` | `not null` | ||
# **`updated_at`** | `datetime` | `not null` | ||
# | ||
# ### Indexes | ||
# | ||
# * `index_countries_on_name` (_unique_): | ||
# * **`name`** | ||
# | ||
|
||
class Country < ApplicationRecord | ||
include HasFulltextSearch | ||
include SpreadsheetArchitect | ||
|
||
validates_uniqueness_of :name, | ||
case_sensitive: false | ||
|
||
validates :alpha3, | ||
inclusion: { in: ISO3166::Country.all.map(&:alpha3) }, | ||
presence: true | ||
|
||
def iso3166_country | ||
ISO3166::Country.find_country_by_alpha3(alpha3) | ||
end | ||
|
||
def tz_info | ||
TZInfo::Country.get(iso3166_country.alpha2) | ||
end | ||
|
||
before_save :set_or_update_country_info | ||
def set_or_update_country_info | ||
self.alpha2 = iso3166_country.alpha2 | ||
self.region = iso3166_country.continent | ||
self.subregion = iso3166_country.subregion | ||
self.world_region = iso3166_country.world_region | ||
self.international_prefix = iso3166_country.international_prefix | ||
self.country_code = iso3166_country.country_code | ||
end | ||
|
||
before_save :set_or_update_currency_info | ||
def set_or_update_currency_info | ||
return if iso3166_country.currency.nil? | ||
self.currency_code = iso3166_country.currency.iso_code | ||
self.currency_symbol = iso3166_country.currency.symbol | ||
self.currency_name = iso3166_country.currency.name | ||
end | ||
|
||
before_save :set_or_update_timezones_info | ||
def set_or_update_timezones_info | ||
timezones_offsets = [] | ||
tz_info.zone_identifiers.each do |time_zone| | ||
timezones_offsets.push(Time.now.in_time_zone(time_zone).formatted_offset) | ||
end | ||
self.timezones = tz_info.zone_identifiers | ||
self.timezones_offsets = timezones_offsets | ||
rescue StandardError # TZInfo::InvalidCountryCode: Invalid country code | ||
end | ||
|
||
after_commit :flush_cache, on: [:update, :destroy] | ||
|
||
def flush_cache | ||
Rails.cache.delete(["Country", self.id]) | ||
end | ||
|
||
def self.find_in_cache(id) | ||
Rails.cache.fetch(["Country", id], expires_in: 1.day) do find(id) end | ||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'money/bank/open_exchange_rates_bank' | ||
|
||
class OpenExchangeRate | ||
def initialize | ||
@moe = Money::Bank::OpenExchangeRatesBank.new | ||
@moe.cache = 'tmp/rates' | ||
@moe.app_id = ENV['OPEN_EXCHANGE_RATE_SECRET'] | ||
end | ||
|
||
def update_rates | ||
@moe.update_rates | ||
@moe.save_rates | ||
Money.default_bank = @moe | ||
end | ||
|
||
def self.update_rates | ||
new.update_rates if Money.default_bank.rates.empty? | ||
end | ||
|
||
def self.exists_exchange_rate_for_currency?(currency_code) | ||
result = get_exchange_rate_usd(currency_code) | ||
return false if result.nil? || result == 'Unknown Currency' | ||
true | ||
end | ||
|
||
def self.get_exchange_rate_usd(to_currency_code) | ||
return 'Unknown Currency' unless Money::Currency.table.map { |t| t[1][:iso_code] }.include?(to_currency_code) | ||
update_rates | ||
Money.default_bank.get_rate 'USD', | ||
to_currency_code | ||
end | ||
|
||
def self.get_exchange_rate(from_currency_code, to_currency_code) | ||
exchange_rate_usd = get_exchange_rate_usd(to_currency_code) | ||
return exchange_rate_usd if from_currency_code === 'USD' | ||
exchange_rate_from = get_exchange_rate_usd(from_currency_code) | ||
return nil unless exchange_rate_from || exchange_rate_from == 'Unknown Currency' | ||
exchange_rate_usd / exchange_rate_from | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | ||
</head> | ||
<body> | ||
<%= yield %> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= yield %> |
Oops, something went wrong.