Module: LookupTable::ClassMethods

Defined in:
lib/lookup_table/class_methods.rb

Overview

shared class methods for lookup tables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#card_columnObject (readonly)

Returns the value of attribute card_column.



4
5
6
# File 'lib/lookup_table/class_methods.rb', line 4

def card_column
  @card_column
end

#card_queryObject (readonly)

Returns the value of attribute card_query.



4
5
6
# File 'lib/lookup_table/class_methods.rb', line 4

def card_query
  @card_query
end

Instance Method Details

#create(cardish) ⇒ Object

TODO: change to create_for_card for consistency



20
21
22
# File 'lib/lookup_table/class_methods.rb', line 20

def create cardish
  new_for_card(cardish).refresh
end

#create!(cardish) ⇒ Object

Raises:

  • (ActiveRecord::RecordInvalid)


24
25
26
27
28
# File 'lib/lookup_table/class_methods.rb', line 24

def create! cardish
  lookup = new_for_card cardish
  raise ActiveRecord::RecordInvalid, lookup if lookup.invalid?
  lookup.refresh
end

#create_or_update(cardish, *fields) ⇒ Object



30
31
32
33
34
# File 'lib/lookup_table/class_methods.rb', line 30

def create_or_update cardish, *fields
  lookup = for_card(cardish) || new_for_card(cardish)
  fields = nil if lookup.new_record? # update all fields if record is new
  lookup.refresh(*fields)
end

#define_main_fetcherObject



84
85
86
# File 'lib/lookup_table/class_methods.rb', line 84

def define_main_fetcher
  define_fetch_method @card_column, :id
end

#delete_for_card(cardish) ⇒ Object



44
45
46
# File 'lib/lookup_table/class_methods.rb', line 44

def delete_for_card cardish
  for_card(cardish)&.destroy
end

#fetcher(*args) ⇒ Object

define standard lookup fetch methods.

Eg. fetcher(:company_id) defines #fetch_company_id to fetch from card.company_id

And fetcher(foo: :bar) defines #fetch_foo to fetch from card.bar



80
81
82
# File 'lib/lookup_table/class_methods.rb', line 80

def fetcher *args
  fetcher_hash(*args).each { |col, method| define_fetch_method col, method }
end

#for_card(cardish) ⇒ Object Also known as: find_for_card



7
8
9
10
# File 'lib/lookup_table/class_methods.rb', line 7

def for_card cardish
  card_id = Card.id cardish
  card_id ? where(card_column => card_id).take : nil
end

#new_for_card(cardish) ⇒ Object



13
14
15
16
17
# File 'lib/lookup_table/class_methods.rb', line 13

def new_for_card cardish
  new.tap do |lookup|
    lookup.card_id = Card.id cardish
  end
end

#refresh(ids = nil, *fields) ⇒ Object

Parameters:

  • ids (Integer, Array<Integer>) (defaults to: nil)

    card ids of metric answer cards



49
50
51
52
53
# File 'lib/lookup_table/class_methods.rb', line 49

def refresh ids=nil, *fields
  Array(ids).compact.each do |ma_id|
    refresh_entry fields, ma_id
  end
end

#refresh_all(fields = nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/lookup_table/class_methods.rb', line 66

def refresh_all fields=nil
  count = 0
  Card.where(card_query).pluck_in_batches(:id) do |batch|
    count += batch.size
    puts "#{batch.first} - #{count}"
    refresh(batch, *fields)
  end
end

#refresh_entry(fields, card_id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/lookup_table/class_methods.rb', line 55

def refresh_entry fields, card_id
  if Card.exists? card_id
    create_or_update card_id, *fields
  else
    delete_for_card card_id
  end
rescue StandardError => e
  raise e, "failed to refresh #{name} lookup table " \
           "for card id #{card_id}: #{e.message}"
end

#update_by_ids(ids, *fields) ⇒ Object

Parameters:

  • ids (Array<Integer>)

    ids of answers in the answer table (NOT card ids)



37
38
39
40
41
42
# File 'lib/lookup_table/class_methods.rb', line 37

def update_by_ids ids, *fields
  Array(ids).each do |id|
    next unless (entry = find_by_id(id))
    entry.refresh(*fields)
  end
end