Module: Card::ActiveRecordHelper

Included in:
Migration
Defined in:
lib/card/active_record_helper.rb

Instance Method Summary collapse

Instance Method Details

#create_card(args) ⇒ Object



3
4
5
6
# File 'lib/card/active_record_helper.rb', line 3

def create_card args
  resolve_name_conflict args
  Card.create! args
end

#create_card!(args) ⇒ Object

if card with same name exists move it out of the way



9
10
11
# File 'lib/card/active_record_helper.rb', line 9

def create_card! args
  create_card args.reverse_merge(rename_if_conflict: :old)
end

#create_or_update(name_or_args, args = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/card/active_record_helper.rb', line 22

def create_or_update name_or_args, args={}
  if name_or_args.is_a?(Hash)
    name = name_or_args.delete :name
    args = name_or_args
  else
    name = name_or_args
  end
  if Card[name]
    update_card name, args
  else
    create_card args.merge(name: name)
  end
end

#create_or_update!(name_or_args, args = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/card/active_record_helper.rb', line 36

def create_or_update! name_or_args, args=nil
  if name_or_args.is_a?(Hash)
    name = name_or_args.delete(:name)
    args = name_or_args
  else
    name = name_or_args
    args ||= {}
  end
  create_or_update name, args.reverse_merge(rename_if_conflict: :new)
end

#resolve_name_conflict(args) ⇒ Object



47
48
49
50
51
# File 'lib/card/active_record_helper.rb', line 47

def resolve_name_conflict args
  rename = args.delete :rename_if_conflict
  return unless rename
  args[:name] = Card.uniquify_name args[:name], rename
end

#update_card(name, args) ⇒ Object



13
14
15
16
# File 'lib/card/active_record_helper.rb', line 13

def update_card name, args
  resolve_name_conflict args
  Card[name].update_attributes! args
end

#update_card!(args) ⇒ Object



18
19
20
# File 'lib/card/active_record_helper.rb', line 18

def update_card! args
  update_card args.reverse_merge(rename_if_conflict: :new)
end