Module: Card::Model::SaveHelper

Includes:
SaveArguments, SaveHelperHelper
Included in:
Cardio::Migration
Defined in:
card/lib/card/model/save_helper.rb,
card/lib/card/model/save_helper/save_arguments.rb,
card/lib/card/model/save_helper/save_helper_helper.rb

Overview

API to create and update cards. It is intended as a helper for “external” scripts (seeding, testing, migrating, etc) and not for internal application code. The general pattern is: All methods use the ActiveRecord !-methods that throw exceptions if somethings fails. All !-methods in this module rename existing cards to resolve name conflicts)

Defined Under Namespace

Modules: SaveArguments, SaveHelperHelper

Constant Summary

Constants included from SaveHelperHelper

SaveHelperHelper::CARDTYPE_METHOD_REGEXP

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Card::Model::SaveHelper::SaveHelperHelper

Instance Method Details

#add_script(name, opts = {}) ⇒ Object

TODO: this is too specific for this



123
124
125
126
127
128
129
130
# File 'card/lib/card/model/save_helper.rb', line 123

def add_script name, opts={}
  name.sub!(/^script:?\s?/, "") # in case name is given with prefix
  # remove it so that we don't double it

  add_coderule_item name, "script",
                    opts[:type_id] || Card::CoffeeScriptID,
                    opts[:to] || "*all+*script"
end

#create_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: create



19
20
21
# File 'card/lib/card/model/save_helper.rb', line 19

def create_card name_or_args, content_or_args=nil
  Card.create! create_args(name_or_args, content_or_args)
end

#create_card!(name_or_args, content_or_args = nil) ⇒ Object Also known as: create!

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



24
25
26
27
# File 'card/lib/card/model/save_helper.rb', line 24

def create_card! name_or_args, content_or_args=nil
  args = standardize_args name_or_args, content_or_args
  create_card args.reverse_merge(rename_if_conflict: :old)
end

#create_or_update_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: create_or_update



40
41
42
43
44
45
46
47
48
49
50
# File 'card/lib/card/model/save_helper.rb', line 40

def create_or_update_card name_or_args, content_or_args=nil
  name = name_from_args name_or_args

  if Card[name]
    args = standardize_update_args name_or_args, content_or_args
    update_card(name, args)
  else
    args = standardize_args name_or_args, content_or_args
    create_card(args)
  end
end

#create_or_update_card!(name_or_args, content_or_args = nil) ⇒ Object Also known as: create_or_update!



117
118
119
120
# File 'card/lib/card/model/save_helper.rb', line 117

def create_or_update_card! name_or_args, content_or_args=nil
  args = standardize_args name_or_args, content_or_args
  create_or_update args.reverse_merge(rename_if_conflict: :new)
end

#delete_card(name) ⇒ Object Also known as: delete



52
53
54
55
56
# File 'card/lib/card/model/save_helper.rb', line 52

def delete_card name
  return unless Card.exist?(name)

  Card[name].delete!
end

#delete_code_card(name) ⇒ Object



58
59
60
61
62
63
64
# File 'card/lib/card/model/save_helper.rb', line 58

def delete_code_card name
  return unless delete_code_card? name

  card = Card[name]
  card.update! codename: nil
  card.delete!
end

#ensure_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: ensure

create if card doesn’t exist updates existing card only if given attributes are different except the name

Examples:

if a card with name “under_score” exists

ensure_card "Under Score"                 # => no change
ensure_card "Under Score", type: :pointer # => changes the type to pointer
                                          #    but not the name


73
74
75
76
# File 'card/lib/card/model/save_helper.rb', line 73

def ensure_card name_or_args, content_or_args=nil
  name, args = standardize_ensure_args name_or_args, content_or_args
  ensure_card_simplified name, args
end

#ensure_card!(name_or_args, content_or_args = nil) ⇒ Object Also known as: ensure!

create if card doesn’t exist updates existing card only if given attributes are different including the name For example if a card with name “under_score” exists then ensure_card "Under Score" renames it to “Under Score”



91
92
93
94
# File 'card/lib/card/model/save_helper.rb', line 91

def ensure_card! name_or_args, content_or_args=nil
  name, args = standardize_ensure_args name_or_args, content_or_args
  ensure_card_simplified name, add_name(name, args)
end

#ensure_code_card(name_or_args, content_or_args = nil) ⇒ Object

like ensure_card but derives codename from name if no codename is given. The derived codename is all lower case with underscores; “*” and “:” are removed



80
81
82
83
84
# File 'card/lib/card/model/save_helper.rb', line 80

def ensure_code_card name_or_args, content_or_args=nil
  name, args = standardize_ensure_args name_or_args, content_or_args
  args[:codename] = codename_from_name(name) unless args[:codename]
  ensure_card_simplified name, args
end

#ensure_trait(name, codename, args = {}) ⇒ Object

Creates or updates a trait card with codename and right rules. Content for rules that are pointer cards by default is converted to pointer format.

Examples:

ensure_trait "*a_or_b", :a_or_b,
             default: { type_id: Card::PointerID },
             options: ["A", "B"],
             input: "radio"


104
105
106
107
108
109
# File 'card/lib/card/model/save_helper.rb', line 104

def ensure_trait name, codename, args={}
  ensure_card name, codename: codename
  args.each do |setting, value|
    ensure_trait_rule name, setting, value
  end
end

#ensure_trait_rule(trait, setting, value) ⇒ Object



111
112
113
114
115
# File 'card/lib/card/model/save_helper.rb', line 111

def ensure_trait_rule trait, setting, value
  validate_setting setting
  card_args = normalize_trait_rule_args setting, value
  ensure_card [trait, :right, setting], card_args
end

#update_card(name, content_or_args) ⇒ Object Also known as: update



29
30
31
32
33
# File 'card/lib/card/model/save_helper.rb', line 29

def update_card name, content_or_args
  args = standardize_update_args name, content_or_args
  resolve_name_conflict args
  Card[name]&.update! args
end

#update_card!(name, content_or_args) ⇒ Object Also known as: update!



35
36
37
38
# File 'card/lib/card/model/save_helper.rb', line 35

def update_card! name, content_or_args
  args = standardize_update_args name, content_or_args
  update_card name, args.reverse_merge(rename_if_conflict: :new)
end

#with_user(user_name, &block) ⇒ Object



15
16
17
# File 'card/lib/card/model/save_helper.rb', line 15

def with_user user_name, &block
  Card::Auth.with(current_id: Card.fetch_id(user_name), &block)
end