12_generate_custom_csv_file #58

Open
philipp wants to merge 59 commits from 12_generate_custom_csv_file into develop
26 changed files with 75 additions and 78 deletions
Showing only changes of commit 4ff44aed4c - Show all commits

View file

@ -14,23 +14,23 @@ class AppleBar
def group_bar_state
if apples >= 100
'success'
else
if FoodsoftConfig[:stop_ordering_under].present? and
apples >= FoodsoftConfig[:stop_ordering_under]
elsif FoodsoftConfig[:stop_ordering_under].present? &&
(apples >= FoodsoftConfig[:stop_ordering_under])
'warning'
else
'danger'
end
end
end
# Use apples as percentage, but show at least 10 percent
def group_bar_width
@ordergroup.apples < 2 ? 2 : @ordergroup.apples
[@ordergroup.apples, 2].max
end
def mean_order_amount_per_job
(1 / @global_avg).round rescue 0
(1 / @global_avg).round
rescue
0
end
def apples

View file

@ -16,7 +16,7 @@ class ArticlesCsv < RenderCSV
Article.human_attribute_name(:unit_quantity),
'',
'',
Article.human_attribute_name(:article_category),
Article.human_attribute_name(:article_category)
]
end
@ -36,7 +36,7 @@ class ArticlesCsv < RenderCSV
o.unit_quantity,
'',
'',
o.article_category.try(:name),
o.article_category.try(:name)
]
end
end

View file

@ -8,9 +8,7 @@ class BankAccountConnector
nil
end
def text
@text
end
attr_reader :text
end
class TextField
@ -24,13 +22,7 @@ class BankAccountConnector
nil
end
def name
@name
end
def value
@value
end
attr_reader :name, :value
def label
@label || @name.to_s
@ -73,17 +65,7 @@ class BankAccountConnector
@bank_account.iban
end
def auto_submit
@auto_submit
end
def controls
@controls
end
def count
@count
end
attr_reader :auto_submit, :controls, :count
def text(data)
@controls += [TextItem.new(data)]
@ -142,11 +124,9 @@ class BankAccountConnector
@bank_account.save!
end
def load(data)
end
def load(data); end
def dump
end
def dump; end
def t(key, args = {})
return t(".fields.#{key}") unless key.is_a? String

View file

@ -1,7 +1,7 @@
class BankTransactionReference
# parses a string from a bank transaction field
def self.parse(data)
m = /(^|[^\w\.])FS(?<group>\d+)(\.(?<user>\d+))?(?<parts>([A-Za-z]+\d+(\.\d+)?)+)([^\w\.]|$)/.match(data)
m = /(^|[^\w.])FS(?<group>\d+)(\.(?<user>\d+))?(?<parts>([A-Za-z]+\d+(\.\d+)?)+)([^\w.]|$)/.match(data)
return unless m
parts = {}
@ -13,7 +13,7 @@ class BankTransactionReference
ret = { group: m[:group].to_i, parts: parts }
ret[:user] = m[:user].to_i if m[:user]
return ret
ret
end
def self.js_code_for_user(user)

View file

@ -27,12 +27,20 @@ module DateTimeAttributeValidate
define_method("#{attribute}_date_value=") do |val|
self.instance_variable_set("@#{attribute}_is_set", true)
self.instance_variable_set("@#{attribute}_date_value", val)
self.send("#{attribute}_date=", val) rescue nil
begin
self.send("#{attribute}_date=", val)
rescue
nil
end
end
define_method("#{attribute}_time_value=") do |val|
self.instance_variable_set("@#{attribute}_is_set", true)
self.instance_variable_set("@#{attribute}_time_value", val)
self.send("#{attribute}_time=", val) rescue nil
begin
self.send("#{attribute}_time=", val)
rescue
nil
end
end
# fallback to field when values are not set
@ -48,11 +56,19 @@ module DateTimeAttributeValidate
# validate date and time
define_method("#{attribute}_datetime_value_valid") do
date = self.instance_variable_get("@#{attribute}_date_value")
unless date.blank? || (Date.parse(date) rescue nil)
unless date.blank? || begin
Date.parse(date)
rescue
nil
end
errors.add(attribute, "is not a valid date") # @todo I18n
end
time = self.instance_variable_get("@#{attribute}_time_value")
unless time.blank? || (Time.parse(time) rescue nil)
unless time.blank? || begin
Time.parse(time)
rescue
nil
end
errors.add(attribute, "is not a valid time") # @todo I18n
end
end

View file

@ -54,8 +54,8 @@ module Foodsoft
# @param options [Hash<String, String>] Extra variables to expand
# @return [String] Expanded string
def self.expand(str, options = {})
str.gsub /{{([._a-zA-Z0-9]+)}}/ do
options[$1] || self.get($1)
str.gsub(/{{([._a-zA-Z0-9]+)}}/) do
options[::Regexp.last_match(1)] || self.get(::Regexp.last_match(1))
end
end

View file

@ -44,6 +44,8 @@ class FoodsoftConfig
# @return [ActiveSupport::HashWithIndifferentAccess] Current configuration from configuration file.
mattr_accessor :config
mattr_accessor :default_config
# Configuration file location.
# Taken from environment variable +FOODSOFT_APP_CONFIG+,
# or else +config/app_config.yml+.
@ -189,7 +191,7 @@ class FoodsoftConfig
# @return [Hash] Full configuration.
def to_hash
keys.to_h { |k| [k, self[k]] }
keys.index_with { |k| self[k] }
end
# for using active_model_serializer in the api/v1/configs controller
@ -216,7 +218,6 @@ class FoodsoftConfig
# end
#
# @return [Hash] Default configuration values
mattr_accessor :default_config
private

View file

@ -6,7 +6,11 @@ module FoodsoftDateUtil
schedule = IceCube::Schedule.new(start)
schedule.add_recurrence_rule rule_from(options[:recurr])
# @todo handle ical parse errors
occ = (schedule.next_occurrence(from).to_time rescue nil)
occ = begin
schedule.next_occurrence(from).to_time
rescue
nil
end
end
if options && options[:time] && occ
occ = occ.beginning_of_day.advance(seconds: Time.parse(options[:time]).seconds_since_midnight)
@ -17,9 +21,10 @@ module FoodsoftDateUtil
# @param p [String, Symbol, Hash, IceCube::Rule] What to return a rule from.
# @return [IceCube::Rule] Recurring rule
def self.rule_from(p)
if p.is_a? String
case p
when String
IceCube::Rule.from_ical(p)
elsif p.is_a? Hash
when Hash
IceCube::Rule.from_hash(p)
else
p

View file

@ -19,7 +19,7 @@ class FoodsoftMailReceiver < MidiSmtpServer::Smtpd
private
def on_rcpt_to_event(ctx, rcpt_to)
def on_rcpt_to_event(_ctx, rcpt_to)
recipient = rcpt_to.gsub(/^\s*<\s*(.*)\s*>\s*$/, '\1')
@handlers << self.class.find_handler(recipient)
rcpt_to
@ -29,7 +29,6 @@ class FoodsoftMailReceiver < MidiSmtpServer::Smtpd
end
def on_message_data_event(ctx)
begin
@handlers.each do |handler|
handler.call(ctx[:message][:data])
end
@ -39,10 +38,9 @@ class FoodsoftMailReceiver < MidiSmtpServer::Smtpd
ensure
@handlers.clear
end
end
def self.find_handler(recipient)
m = /(?<foodcoop>[^@\.]+)\.(?<address>[^@]+)(@(?<hostname>[^@]+))?/.match recipient
m = /(?<foodcoop>[^@.]+)\.(?<address>[^@]+)(@(?<hostname>[^@]+))?/.match recipient
raise "recipient is missing or has an invalid format" if m.nil?
raise "Foodcoop '#{m[:foodcoop]}' could not be found" unless FoodsoftConfig.allowed_foodcoop? m[:foodcoop]
@ -51,7 +49,7 @@ class FoodsoftMailReceiver < MidiSmtpServer::Smtpd
@@registered_classes.each do |klass|
if match = klass.regexp.match(m[:address])
handler = klass.new match
return lambda { |data| handler.received(data) }
return ->(data) { handler.received(data) }
end
end

View file

@ -32,7 +32,7 @@ class InvoicesCsv < RenderCSV
t.deposit,
t.deposit_credit,
t.paid_on,
t.note,
t.note
]
end
end

View file

@ -1,4 +1,4 @@
class OrderPdf < RenderPDF
class OrderPDF < RenderPDF
attr_reader :order
def initialize(order, options = {})
@ -55,7 +55,7 @@ class OrderPdf < RenderPDF
end
def group_order_article_quantity_with_tolerance(goa)
goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : "#{goa.quantity}"
goa.tolerance > 0 ? "#{goa.quantity} + #{goa.tolerance}" : goa.quantity.to_s
end
def group_order_article_result(goa)
@ -88,7 +88,7 @@ class OrderPdf < RenderPDF
.pluck('groups.name', 'SUM(group_orders.price)', 'ordergroup_id', 'SUM(group_orders.transport)')
result.map do |item|
[item.first || stock_ordergroup_name] + item[1..-1]
[item.first || stock_ordergroup_name] + item[1..]
end
end
@ -103,7 +103,7 @@ class OrderPdf < RenderPDF
def each_ordergroup_batch(batch_size)
offset = 0
while true
loop do
go_records = ordergroups(offset, batch_size)
break unless go_records.any?

View file

@ -1,5 +1,5 @@
class OrderTxt
def initialize(order, options = {})
def initialize(order, _options = {})
@order = order
end
@ -15,10 +15,10 @@ class OrderTxt
text += "****** " + I18n.t('orders.fax.to_address') + "\n\n"
text += "#{FoodsoftConfig[:name]}\n#{contact[:street]}\n#{contact[:zip_code]} #{contact[:city]}\n\n"
text += "****** " + I18n.t('orders.fax.articles') + "\n\n"
text += "%8s %8s %s\n" % [I18n.t('orders.fax.number'), I18n.t('orders.fax.amount'), I18n.t('orders.fax.name')]
text += format("%8s %8s %s\n", I18n.t('orders.fax.number'), I18n.t('orders.fax.amount'), I18n.t('orders.fax.name'))
# now display all ordered articles
@order.order_articles.ordered.includes([:article, :article_price]).each do |oa|
text += "%8s %8d %s\n" % [oa.article.order_number, oa.units_to_order.to_i, oa.article.name]
text += format("%8s %8d %s\n", oa.article.order_number, oa.units_to_order.to_i, oa.article.name)
end
text
end

View file

@ -14,9 +14,9 @@ class OrdergroupsCsv < RenderCSV
Ordergroup.human_attribute_name(:break_start),
Ordergroup.human_attribute_name(:break_end),
Ordergroup.human_attribute_name(:last_user_activity),
Ordergroup.human_attribute_name(:last_order),
Ordergroup.human_attribute_name(:last_order)
]
row + Ordergroup.custom_fields.map { |f| f[:label] }
row + Ordergroup.custom_fields.pluck(:label)
end
def data
@ -33,7 +33,7 @@ class OrdergroupsCsv < RenderCSV
o.break_start,
o.break_end,
o.last_user_activity,
o.last_order.try(:starts),
o.last_order.try(:starts)
]
yield row + Ordergroup.custom_fields.map { |f| o.settings.custom_fields[f[:name]] }
end

View file

@ -18,7 +18,7 @@ class RotatedCell < Prawn::Table::Cell::Text
(height + (border_top_width / 2.0) + (border_bottom_width / 2.0)) / tan_rotation
end
def styled_width_of(text)
def styled_width_of(_text)
options = @text_options.reject { |k| k == :style }
with_font { (@pdf.height_of(@content, options) + padding_top + padding_bottom) / tan_rotation }
end
@ -156,9 +156,10 @@ class RenderPDF < Prawn::Document
def pdf_add_page_breaks?(docid = nil)
docid ||= self.class.name.underscore
cfg = FoodsoftConfig[:pdf_add_page_breaks]
if cfg.is_a? Array
case cfg
when Array
cfg.index(docid.to_s).any?
elsif cfg.is_a? Hash
when Hash
cfg[docid.to_s]
else
cfg

View file

@ -21,8 +21,6 @@ class TokenVerifier < ActiveSupport::MessageVerifier
# return original message
if r.length > 2
r[2]
else
nil
end
end
@ -32,8 +30,6 @@ class TokenVerifier < ActiveSupport::MessageVerifier
class InvalidPrefix < ActiveSupport::MessageVerifier::InvalidSignature; end
protected
def self.secret
# secret_key_base for Rails 4, but Rails 3 initializer may still be used
Foodsoft::Application.config.secret_key_base || Foodsoft::Application.config.secret_token