Class: BeerDb::Model::Brand

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
TextUtils::ValueHelper
Defined in:
lib/beerdb/models/brand.rb,
lib/beerdb/models/forward.rb

Class Method Summary collapse

Class Method Details

.create_or_update_from_attribs(attribs, values) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/beerdb/models/brand.rb', line 36

def self.create_or_update_from_attribs( attribs, values )

  ## fix: add/configure logger for ActiveRecord!!!
  logger = LogKernel::Logger.root

  ## check for grades (e.g. ***/**/*) in titles (will add attribs[:grade] to hash)
  ## if grade missing; set default to 4; lets us update overwrite 1,2,3 values on update
  attribs[:grade] ||= 4

  rec = Brand.find_by_key( attribs[:key] )

  if rec.present?
    logger.debug "update Brand #{rec.id}-#{rec.key}:"
  else
    logger.debug "create Brand:"
    rec = Brand.new
  end

  logger.debug attribs.to_json

  rec.update_attributes!( attribs )
end

.create_or_update_from_title(title, more_attribs = {}) ⇒ Object

convenience helper Brand.create_or_update_from_title



30
31
32
33
# File 'lib/beerdb/models/brand.rb', line 30

def self.create_or_update_from_title( title, more_attribs = {} )
  values = [title]
  Brand.create_or_update_from_values( values, more_attribs )
end

.create_or_update_from_values(values, more_attribs = {}) ⇒ Object



22
23
24
25
26
27
# File 'lib/beerdb/models/brand.rb', line 22

def self.create_or_update_from_values( values, more_attribs = {} )
  attribs, more_values = find_key_n_title( values )
  attribs = attribs.merge( more_attribs )

  Brand.create_or_update_from_attribs( attribs, more_values )
end