Module: Ekylibre::Record::Acts::Numbered::ClassMethods

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

Instance Method Summary collapse

Instance Method Details

#acts_as_numbered(*args) ⇒ Object

Use preference to select preferred sequence to attribute number in column


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
# File 'lib/ekylibre/record/acts/numbered.rb', line 11

def acts_as_numbered(*args)
  options = args[-1].is_a?(Hash) ? args.delete_at(-1) : {}
  column = args.shift || :number

  unless columns_definition[column]
    Rails.logger.fatal "Method #{column.inspect} must be an existent column of the table #{table_name}"
  end

  options = { start: '00000001' }.merge(options)

  main_class = self
  while main_class.superclass != Ekylibre::Record::Base && main_class.superclass != ActiveRecord::Base
    main_class = superclass
  end
  class_name = main_class.name

  usage = options[:usage] || class_name.tableize
  unless Sequence.usage.values.include?(usage)
    raise "Usage #{usage} must be defined in Sequence usages"
  end

  last  = "#{class_name}.where('#{column} IS NOT NULL').reorder('LENGTH(#{column}) DESC, #{column} DESC').first"

  code  = ''

  code << "attr_readonly :#{column}\n" unless options[:readonly].is_a? FalseClass

  code << "validates :#{column}, presence: true, uniqueness: true\n"

  code << "before_validation(:load_unique_predictable_#{column}, on: :create)\n"
  code << "after_validation(:load_unique_reliable_#{column}, on: :create)\n"

  code << "def load_unique_predictable_#{column}\n"
  code << "  unless self.#{column}.present?\n" if options[:force].is_a?(FalseClass)
  code << "    last = #{last}\n"
  code << "    self.#{column} = (last.nil? ? #{options[:start].inspect} : last.#{column}.blank? ? #{options[:start].inspect} : last.#{column}.succ)\n"
  code << "    while #{class_name}.find_by(#{column}: self.#{column}) do\n"
  code << "      self.#{column}.succ!\n"
  code << "    end\n"
  code << "  end\n" if options[:force].is_a?(FalseClass)
  code << "  return true\n"
  code << "end\n"

  code << "def load_unique_reliable_#{column}\n"
  code << "  unless self.#{column}\n" if options[:force].is_a?(FalseClass)
  code << "    if sequence = Sequence.of('#{usage}')\n"
  code << "      self.#{column} = sequence.next_value\n"
  code << "      while #{class_name}.find_by(#{column}: self.#{column}) do\n"
  code << "        self.#{column} = sequence.next_value\n"
  code << "      end\n"
  code << "    else\n"
  code << "      last = #{last}\n"
  code << "      self.#{column} = (last.nil? ? #{options[:start].inspect} : last.#{column}.blank? ? #{options[:start].inspect} : last.#{column}.succ)\n"
  code << "      while #{class_name}.find_by(#{column}: self.#{column}) do\n"
  code << "        self.#{column}.succ!\n"
  code << "      end\n"
  code << "    end\n"
  code << "  end\n" if options[:force].is_a?(FalseClass)
  code << "  return true\n"
  code << "end\n"
  # puts code
  class_eval code
end