Class: Rivendell::DB::Cart

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/rivendell/db/cart.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.duplicated(*fields) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rivendell/db/cart.rb', line 24

def self.duplicated(*fields)
  sql_fields = fields.join(", ")
  query = "select GROUP_CONCAT(NUMBER) as numbers, #{sql_fields} from CART group by #{sql_fields} having count(NUMBER) > 1;"

  repository(:default).adapter.select(query).map do |duplicated_group|
    field_values = fields.inject({}) { |map, field| map[field] = duplicated_group.send(field); map }
    numbers = duplicated_group.numbers.split(",").map(&:to_i)
    { :numbers => numbers, :fields => field_values }
  end
end

.scheduler_code(scheduler_code) ⇒ Object



54
55
56
# File 'lib/rivendell/db/cart.rb', line 54

def self.scheduler_code(scheduler_code)
  all('sched_codes.like' => "%#{scheduler_code.ljust(11)}%")
end

Instance Method Details

#scheduler_codesObject



37
38
39
40
41
42
43
# File 'lib/rivendell/db/cart.rb', line 37

def scheduler_codes
  if sched_codes
    sched_codes.scan(/.{11}/).map(&:strip)
  else
    []
  end
end

#scheduler_codes=(scheduler_codes) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/rivendell/db/cart.rb', line 45

def scheduler_codes=(scheduler_codes)
  self.sched_codes =
    if scheduler_codes and !scheduler_codes.empty?
      scheduler_codes.uniq.sort.map { |c| c.ljust(11) }.join + "."
    else
      ""
    end
end

#use_free_number(context = :default) ⇒ Object



20
21
22
# File 'lib/rivendell/db/cart.rb', line 20

def use_free_number(context = :default)
  self.number ||= group.free_cart_number if group
end