Class: Workarea::Tax::Rate

Inherits:
Object
  • Object
show all
Includes:
ApplicationDocument
Defined in:
app/models/workarea/tax/rate.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApplicationDocument

#releasable?

Methods included from Sidekiq::Callbacks

add_worker, assert_valid_config!, async, caching_classes?, disable, enable, inline, #run_callbacks, workers, workers_list

Methods included from Mongoid::Document

#embedded_children

Class Method Details

.search(query) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/workarea/tax/rate.rb', line 32

def self.search(query)
  return all unless query.present?

  regex = /^#{::Regexp.quote(query)}/i
  country = Country[query]

  clauses = [{ region: regex }, { postal_code: regex }]
  clauses << { country: country } if country.present?

  any_of(clauses)
end

.sortsObject



44
45
46
# File 'app/models/workarea/tax/rate.rb', line 44

def self.sorts
  [Sort.country, Sort.region, Sort.postal_code, Sort.newest, Sort.modified]
end

Instance Method Details

#percentageObject



56
57
58
59
60
61
# File 'app/models/workarea/tax/rate.rb', line 56

def percentage
  percentage_field = super
  return percentage_field unless percentage_field.zero?

  country_percentage + region_percentage + postal_code_percentage
end

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
# File 'app/models/workarea/tax/rate.rb', line 48

def valid?(*)
  super.tap do
    self.region = nil if region.blank?
    self.postal_code = nil if postal_code.blank?
    self.percentage ||= 0
  end
end