Module: Ekylibre::Record::Acts::Affairable::ClassMethods

Defined in:
lib/ekylibre/record/acts/affairable.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_affairable(*args) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/ekylibre/record/acts/affairable.rb', line 9

def acts_as_affairable(*args)
  options = args.extract_options!
  reflection = reflect_on_association(options[:reflection] || :affair)
  currency = options[:currency] || :currency
  options[:dealt_at] ||= :created_at
  options[:amount] ||= :amount
  options[:debit] = true unless options.key?(:debit)

  options[:third] ||= args.shift || :third
  options[:role] ||= options[:third].to_s
  options[:good] ||= :debit
  code = ''

  affair = :affair
  affair_id = :affair_id
  if reflection
    affair = reflection.name
    affair_id = reflection.foreign_key
  else
    unless columns_definition[affair_id]
      Rails.logger.fatal 'Unable to acts as affairable without affair column'
      # raise StandardError, "Unable to acts as affairable no affair column"
    end
    code << "belongs_to :#{affair}, inverse_of: :#{name.underscore.pluralize}\n"
  end
  # code << "has_many :affairs, as: :originator, dependent: :destroy\n"

  # default scope for affairable
  code << "scope :affairable, -> { where('#{affair_id} IN (SELECT id FROM affairs WHERE NOT closed)') }\n"

  # Need to log strange things on delegate
  { credit: 0,
    debit: 0,
    closed: false,
    balance: 0,
    status: 'stop' }.each do |meth, default_value|
    code << "def affair_#{meth}\n"
    code << "  unless affair\n"
    code << "    Rails.logger.warn 'No affair on ' + self.class.name + ' ID=' + self.id.to_s\n"
    code << "    return #{default_value.inspect}\n"
    code << "  end\n"
    code << "  affair.#{meth}\n"
    code << "end\n"
  end
  code << "alias affair_closed? affair_closed\n"

  # # Marks model as affairable
  # code << "def self.affairable_options\n"
  # code << "  return {reflection: :#{affair}, currency: :#{currency}, third: :#{options[:third]}, third_role: :#{options[:role]}}\n"
  # code << "end\n"

  # Refresh after each save
  code << "validate do\n"
  code << "  if self.#{affair}\n"
  code << "    unless self.#{affair}.currency == self.#{currency}\n"
  code << "      errors.add(:#{affair}, :invalid_currency, got: self.#{currency}, expected: self.#{affair}.currency)\n"
  code << "      errors.add(:#{affair_id}, :invalid_currency, got: self.#{currency}, expected: self.#{affair}.currency)\n"
  code << "    end\n"
  code << "  end\n"
  # code << "  return true\n"
  code << "end\n"

  # Updates affair if already given
  code << "after_create do\n"
  code << "  if self.#{affair}\n"
  code << "    self.#{affair}.refresh!\n"
  code << "  end\n"
  # code << "  return true\n"
  code << "end\n"

  # Create "empty" affair if missing before every save
  code << "after_save do\n"
  code << "  if self.#{affair}\n"
  code << "    self.affair.refresh!\n"
  code << "  else\n"
  code << "    #{affair} = Affair.create!(currency: self.#{currency}, third: self.deal_third, third_role: self.deal_third_role)\n"
  code << "    self.deal_with!(#{affair})\n"
  code << "  end\n"
  # code << "  return true\n"
  code << "end\n"

  # Refresh after each save
  code << "def deal_with!(affair, dones = [])\n"
  code << "  return self if self.#{affair_id} == affair.id\n"
  code << "  dones << self\n"
  code << "  if affair.currency != self.currency\n"
  code << "    raise ArgumentError, \"The currency (\#{self.currency}) is different of the affair currency(\#{affair.currency})\"\n"
  code << "  end\n"
  code << "  Ekylibre::Record::Base.transaction do\n"
  code << "    if old_affair = self.#{affair}\n"
  code << "      for deal in self.other_deals\n"
  code << "        deal.deal_with!(affair, dones) unless dones.include?(deal)\n"
  code << "      end\n"
  # code << "      old_affair.destroy!\n"
  code << "      Affair.destroy(old_affair.id) if Affair.find_by(id: old_affair.id)\n"
  code << "    end\n"
  code << "    self.update_column(:#{affair_id}, affair.id)\n"
  code << "    affair.refresh!\n"
  code << "  end\n"
  code << "  return self.reload\n"
  code << "end\n"

  code << "def undeal!(affair = nil)\n"
  code << "  if affair and affair.id != self.#{affair_id}\n"
  # code << "    puts self.inspect.red\n"
  # code << "    puts affair.inspect.blue\n"
  code << "    raise ArgumentError, \"Cannot undeal from this unknown affair #\#{affair.id}\"\n"
  code << "  end\n"
  code << "  Ekylibre::Record::Base.transaction do\n"
  code << "    old_affair = self.#{affair}\n"
  code << "    affair = Affair.create!(currency: self.currency, third: self.deal_third, third_role: self.deal_third_role)\n"
  code << "    self.update_column(:#{affair_id}, affair.id)\n"
  code << "    affair.refresh!\n"
  code << "    old_affair.refresh!\n"
  code << "    if old_affair.deals_count.zero?\n"
  code << "      old_affair.destroy!\n"
  code << "    end\n"
  code << "  end\n"
  code << "end\n"

  # Define if detachable
  code << "def detachable?\n"
  code << "  return self.other_deals.any?\n"
  code << "end\n"

  # # Create "empty" affair if missing before every save
  # code << "before_save do\n"
  # code << "  unless self.#{affair}\n"
  # code << "    self.build_#{affair}(currency: self.#{currency}, third: self.deal_third)\n"
  # code << "  end\n"
  # code << "  return true\n"
  # code << "end\n"

  # # Create "empty" affair if missing before every save
  # code << "before_save do\n"
  # code << "  unless self.#{affair}\n"
  # code << "    #{affair} = Affair.new\n"
  # code << "    #{affair}.currency = self.#{currency}\n"
  # code << "    #{affair}.third    = self.deal_third\n"
  # code << "    #{affair}.save!\n"
  # code << "    self.#{affair_id} = #{affair}.id\n"
  # code << "  end\n"
  # code << "  return true\n"
  # code << "end\n"

  # # Refresh after each save
  # code << "after_save do\n"
  # code << "  Affair.find(self.#{affair_id}).save!\n"
  # code << "  Affair.clean_deads\n"
  # code << "  return true\n"
  # code << "end\n"

  # Return if deal is a debit for us
  code << "def good_deal?\n"
  code += if options[:good] == :debit
            "  return self.deal_debit?\n"
          elsif options[:good] == :credit
            "  return self.deal_credit?\n"
          else
            "  return self.#{options[:good]}\n"
          end
  code << "end\n"

  # Return if deal is a debit for us
  code << "def deal_debit?\n"
  if options[:debit].is_a?(TrueClass)
    code << "  return true\n"
  elsif options[:debit].is_a?(FalseClass)
    code << "  return false\n"
  elsif options[:debit].is_a?(Symbol)
    code << "  return self.#{options[:debit]}\n"
  else
    raise ArgumentError, 'Option :debit must be boolean or Symbol'
  end
  code << "end\n"

  # Return if deal is a credit for us
  code << "def deal_credit?\n"
  if options[:debit].is_a?(TrueClass)
    code << "  return false\n"
  elsif options[:debit].is_a?(FalseClass)
    code << "  return true\n"
  elsif options[:debit].is_a?(Symbol)
    code << "  return !self.#{options[:debit]}\n"
  end
  code << "end\n"

  # Define which amount to take in account
  code << "alias_attribute :deal_amount, :#{options[:amount]}\n"

  # Define which date to take in account
  code << "alias_attribute :dealt_at, :#{options[:dealt_at]}\n"

  # Define the third of the deal
  code << "alias_attribute :deal_third, :#{options[:third]}\n"

  # Define debit amount
  code << "def deal_debit_amount\n"
  code << "  return (self.deal_debit? ? self.deal_amount : 0)\n"
  code << "end\n"

  # Define credit amount
  code << "def deal_credit_amount\n"
  code << "  return (self.deal_credit? ? self.deal_amount : 0)\n"
  code << "end\n"

  # Define credit amount
  code << "def deal_mode_amount(mode = :debit)\n"
  code << "  if mode == :credit\n"
  code << "    return (self.deal_credit? ? self.deal_amount : 0)\n"
  code << "  else\n"
  code << "    return (self.deal_debit?  ? self.deal_amount : 0)\n"
  code << "  end\n"
  code << "end\n"

  # Returns other deals
  code << "def other_deals\n"
  code << "  return self.#{affair}.deals.delete_if{|x| x == self}\n"
  code << "end\n"

  # Returns other deals
  code << "def other_deals_of_same_type\n"
  code << "  return self.#{affair}.deals.delete_if{|x| x == self or !x.is_a?(self.class)}\n"
  code << "end\n"

  code << "def self.deal_third\n"
  code << "  return self.reflect_on_association(:#{options[:third]})\n"
  code << "end\n"

  # Define the third of the deal
  if options[:taxes].is_a?(Symbol)
    code << "alias_attribute :deal_taxes, :#{options[:taxes]}\n"
  elsif ![TrueClass].include?(options[:taxes].class)
    # Computes based on opposite operation taxes
    code << "def deal_taxes(mode = :debit)\n"
    code << "  return [] if self.deal_mode_amount(mode).zero?\n"
    code << "  return [{amount: self.deal_mode_amount(mode)}]\n"
    code << "end\n"
  end

  # Define the third of the deal
  code << "def deal_third_role\n"
  code += if options[:role].is_a?(Symbol)
            "  return self.#{options[:role]}\n"
          else
            "  return #{options[:role].to_sym.inspect}\n"
          end
  code << "end\n"

  # code.split("\n").each_with_index{|x, i| puts((i+1).to_s.rjust(4).white + ": " + x.blue)}

  class_eval(code)
end