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



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

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

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



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

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



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

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



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

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



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

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

#update_card!(args) ⇒ Object



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

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