Class: BookingTemplate
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- BookingTemplate
- Defined in:
- app/models/booking_template.rb
Direct Known Subclasses
Class Method Summary collapse
- .build_booking(code, params = {}) ⇒ Object
- .create_booking(code, params = {}) ⇒ Object
-
.import(struct) ⇒ Object
Importer.
Instance Method Summary collapse
- #booking_parameters(params = {}) ⇒ Object
-
#build_booking(params = {}) ⇒ Object
Factory methods.
- #create_booking(params = {}) ⇒ Object
-
#to_s(format = :default) ⇒ Object
Standard methods.
Class Method Details
.build_booking(code, params = {}) ⇒ Object
75 76 77 |
# File 'app/models/booking_template.rb', line 75 def self.build_booking(code, params = {}) find_by_code(code).try(:build_booking, params) end |
.create_booking(code, params = {}) ⇒ Object
79 80 81 |
# File 'app/models/booking_template.rb', line 79 def self.create_booking(code, params = {}) find_by_code(code).try(:create_booking, params) end |
.import(struct) ⇒ Object
Importer
84 85 86 87 88 89 90 91 |
# File 'app/models/booking_template.rb', line 84 def self.import(struct) templates = self.all.inject([]) do |found, template| puts "matcher: " + template.matcher puts 'text: ' + struct.text found << template if not Regexp.new(template.matcher).match(struct.text).eql?nil end puts templates.inspect end |
Instance Method Details
#booking_parameters(params = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/models/booking_template.rb', line 29 def booking_parameters(params = {}) # Prepare parameters set by template booking_params = attributes.reject!{|key, value| !["title", "comments", "credit_account_id", "debit_account_id"].include?(key)} # Calculate amount booking_amount = BigDecimal.new(attributes['amount'] || '0') params.stringify_keys! # Lookup reference reference = params['reference'] unless reference ref_type = params['reference_type'] ref_id = params['reference_id'] if ref_type.present? and ref_id.present? reference = ref_type.constantize.find(ref_id) end end if reference case self.amount_relates_to when 'reference_amount' booking_amount *= reference.amount unless reference.amount.nil? when 'reference_balance' booking_amount *= reference.balance unless reference.balance.nil? when 'reference_amount_minus_balance' booking_amount *= reference.amount - reference.balance unless (reference.amount.nil? or reference.balance.nil?) end end booking_amount = booking_amount.try(:round, 2) booking_params['amount'] = booking_amount # Override by passed in parameters booking_params.merge!(params) end |
#build_booking(params = {}) ⇒ Object
Factory methods
67 68 69 |
# File 'app/models/booking_template.rb', line 67 def build_booking(params = {}) Booking.new(booking_parameters(params)) end |
#create_booking(params = {}) ⇒ Object
71 72 73 |
# File 'app/models/booking_template.rb', line 71 def create_booking(params = {}) Booking.create(booking_parameters(params)) end |
#to_s(format = :default) ⇒ Object
Standard methods
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'app/models/booking_template.rb', line 10 def to_s(format = :default) case format when :short "%s / %s CHF %s" % [ credit_account ? credit_account.to_s(:short) : '?', debit_account ? debit_account.to_s(:short) : '?', amount ? "%0.2f" % amount.to_f : '?', ] else "%s an %s CHF %s, %s (%s)" % [ credit_account ? credit_account.to_s : '?', debit_account ? debit_account.to_s : '?', amount ? "%0.2f" % amount.to_f : '?', title.present? ? title : '?', comments.present? ? comments : '?' ] end end |