Module: Card::Model::SaveHelper
- Included in:
- Card::Migration
- Defined in:
- lib/card/model/save_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)
Instance Method Summary collapse
- #add_coderule_item(name, prefix, type_id, to) ⇒ Object
- #add_name(name, content_or_args) ⇒ Object
- #add_script(name, opts = {}) ⇒ Object
- #add_style(name, opts = {}) ⇒ Object
- #create_card(name_or_args, content_or_args = nil) ⇒ Object (also: #create)
-
#create_card!(name_or_args, content_or_args = nil) ⇒ Object
(also: #create!)
if card with same name exists move it out of the way.
- #create_or_update_card(name_or_args, content_or_args = nil) ⇒ Object (also: #create_or_update)
- #create_or_update_card!(name_or_args, content_or_args = nil) ⇒ Object (also: #create_or_update!)
- #delete_card(name) ⇒ Object (also: #delete)
- #delete_code_card(name) ⇒ Object
- #ensure_attributes(card, args) ⇒ Object
-
#ensure_card(name_or_args, content_or_args = nil) ⇒ Object
(also: #ensure)
create if card doesn't exist updates existing card only if given attributes are different except the name.
-
#ensure_card!(name_or_args, content_or_args = nil) ⇒ Object
(also: #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". - #name_from_args(name_or_args) ⇒ Object
- #resolve_name_conflict(args) ⇒ Object
-
#standardize_args(name_or_args, content_or_args) ⇒ Object
Args.
- #standardize_update_args(name_or_args, content_or_args) ⇒ Object
- #update_card(name, content_or_args) ⇒ Object (also: #update)
- #update_card!(name, content_or_args) ⇒ Object (also: #update!)
Instance Method Details
#add_coderule_item(name, prefix, type_id, to) ⇒ Object
166 167 168 169 170 171 172 173 |
# File 'lib/card/model/save_helper.rb', line 166 def add_coderule_item name, prefix, type_id, to codename = "#{prefix}_#{name.tr(' ', '_').underscore}" name = "#{prefix}: #{name}" ensure_card name, type_id: type_id, codename: codename Card[to].add_item! name end |
#add_name(name, content_or_args) ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/card/model/save_helper.rb', line 116 def add_name name, content_or_args if content_or_args.is_a?(String) { content: content_or_args, name: name } else content_or_args.reverse_merge name: name end end |
#add_script(name, opts = {}) ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/card/model/save_helper.rb', line 156 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 |
#add_style(name, opts = {}) ⇒ Object
147 148 149 150 151 152 153 154 |
# File 'lib/card/model/save_helper.rb', line 147 def add_style name, opts={} name.sub!(/^style\:?\s?/, '') # in case name is given with prefix # remove it so that we don't double it add_coderule_item name, "style", opts[:type_id] || Card::ScssID, opts[:to] || "*all+*style" end |
#create_card(name_or_args, content_or_args = nil) ⇒ Object Also known as: create
12 13 14 15 16 |
# File 'lib/card/model/save_helper.rb', line 12 def create_card name_or_args, content_or_args=nil args = standardize_args name_or_args, content_or_args resolve_name_conflict args Card.create! 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
79 80 81 82 |
# File 'lib/card/model/save_helper.rb', line 79 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
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/card/model/save_helper.rb', line 24 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!
89 90 91 92 |
# File 'lib/card/model/save_helper.rb', line 89 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
36 37 38 39 |
# File 'lib/card/model/save_helper.rb', line 36 def delete_card name return unless (card = Card[name]) card.delete! end |
#delete_code_card(name) ⇒ Object
41 42 43 44 |
# File 'lib/card/model/save_helper.rb', line 41 def delete_code_card name update name, codename: nil delete name end |
#ensure_attributes(card, args) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/card/model/save_helper.rb', line 130 def ensure_attributes card, args args = args.with_indifferent_access subcards = card.extract_subcard_args! args update_args = args.select do |key, value| if key =~ /^\+/ subfields[key] = value false else card.send(key) != value end end return if update_args.empty? && subcards.empty? # FIXME: use ensure_attributes for subcards card.update_attributes! update_args.merge(subcards: subcards) 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
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/card/model/save_helper.rb', line 53 def ensure_card name_or_args, content_or_args=nil args = standardize_args name_or_args, content_or_args name = args.delete(:name) if (card = Card[name]) ensure_attributes card, args card else Card.create! args.merge(name: name) end 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"
69 70 71 72 73 74 75 76 |
# File 'lib/card/model/save_helper.rb', line 69 def ensure_card! name_or_args, content_or_args=nil args = standardize_args name_or_args, content_or_args if (card = Card[args[:name]]) ensure_attributes card, args else Card.create! args end end |
#name_from_args(name_or_args) ⇒ Object
112 113 114 |
# File 'lib/card/model/save_helper.rb', line 112 def name_from_args name_or_args name_or_args.is_a?(Hash) ? name_or_args[:name] : name_or_args end |
#resolve_name_conflict(args) ⇒ Object
124 125 126 127 128 |
# File 'lib/card/model/save_helper.rb', line 124 def resolve_name_conflict args rename = args.delete :rename_if_conflict return unless args[:name] && rename args[:name] = Card.uniquify_name args[:name], rename end |
#standardize_args(name_or_args, content_or_args) ⇒ Object
Returns args.
95 96 97 98 99 100 101 |
# File 'lib/card/model/save_helper.rb', line 95 def standardize_args name_or_args, content_or_args if name_or_args.is_a?(Hash) name_or_args else add_name name_or_args, content_or_args || {} end end |
#standardize_update_args(name_or_args, content_or_args) ⇒ Object
103 104 105 106 107 108 109 110 |
# File 'lib/card/model/save_helper.rb', line 103 def standardize_update_args name_or_args, content_or_args return name_or_args if name_or_args.is_a?(Hash) if content_or_args.is_a?(String) { content: content_or_args } else content_or_args end end |
#update_card(name, content_or_args) ⇒ Object Also known as: update
18 19 20 21 22 |
# File 'lib/card/model/save_helper.rb', line 18 def update_card name, content_or_args args = standardize_update_args name, content_or_args resolve_name_conflict args Card[name].update_attributes! args end |
#update_card!(name, content_or_args) ⇒ Object Also known as: update!
84 85 86 87 |
# File 'lib/card/model/save_helper.rb', line 84 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 |