Module: Card::Set::All::Form::HtmlFormat

Extended by:
Card::Set::AbstractFormat
Defined in:
tmpsets/set/mod023-edit/all/form.rb

Instance Method Summary collapse

Instance Method Details

#add_junction_classObject

FIELD VIEWS



257
258
259
260
261
# File 'tmpsets/set/mod023-edit/all/form.rb', line 257

def add_junction_class
  return unless card.name.junction?

  class_up "card-editor", "RIGHT-#{card.name.tag_name.safe_key}"
end

#button_formgroupObject



38
39
40
41
42
# File 'tmpsets/set/mod023-edit/all/form.rb', line 38

def button_formgroup
  wrap_with :div, class: classy("form-group") do
    wrap_with :div, yield
  end
end

#card_form(action, opts = {}) ⇒ Object



205
206
207
208
209
210
211
212
# File 'tmpsets/set/mod023-edit/all/form.rb', line 205

def card_form action, opts={}
  @form_root = true
  hidden = hidden_form_tags action, opts
  form_for card, card_form_opts(action, opts) do |cform|
    @form = cform
    hidden + output(yield(cform))
  end
end

#card_form_html_opts(action, opts = {}) ⇒ Object



230
231
232
233
234
235
# File 'tmpsets/set/mod023-edit/all/form.rb', line 230

def card_form_html_opts action, opts={}
  add_class opts, "card-form"
  add_class opts, "slotter" unless opts[:redirect] || opts[:no_slotter]
  add_class opts, "autosave" if action == :update
  opts
end

#card_form_opts(action, opts = {}) ⇒ Object

Parameters:

  • action (Symbol)

    :create or :update

  • opts (Hash) (defaults to: {})

    html options

Options Hash (opts):

  • :redirect (Boolean) — default: false

    if true form is no "slotter"



222
223
224
225
226
227
228
# File 'tmpsets/set/mod023-edit/all/form.rb', line 222

def card_form_opts action, opts={}
  url, action = card_form_url_and_action action
  html_opts = card_form_html_opts action, opts
  form_opts = { url: url, html: html_opts }
  form_opts[:remote] = true unless html_opts.delete(:redirect)
  form_opts
end

#card_form_url_and_action(action) ⇒ Object



237
238
239
240
241
242
243
244
245
# File 'tmpsets/set/mod023-edit/all/form.rb', line 237

def card_form_url_and_action action
  case action
  when Symbol then [path(action: action), action]
  when Hash   then [path(action), action[:action]]
    # for when non-action path args are required
  else
    raise Card::Error, "unsupported #card_form_url action: #{action}"
  end
end

#content_field(skip_rev_id = false) ⇒ Object



68
69
70
71
72
73
74
# File 'tmpsets/set/mod023-edit/all/form.rb', line 68

def content_field skip_rev_id=false
  with_nest_mode :normal do
    # by changing nest mode to normal, we ensure that editors (eg image
    # previews) can render core views.
    output [content_field_revision_tracking(skip_rev_id), _render_editor]
  end
end

#content_field_revision_tracking(skip_rev_id) ⇒ Object

SAMPLE editor view for override view :editor do text_area :content, rows: 5, class: "d0-card-content" end



81
82
83
84
85
86
# File 'tmpsets/set/mod023-edit/all/form.rb', line 81

def content_field_revision_tracking skip_rev_id
  card.last_action_id_before_edit = card.last_action_id
  return if !card || card.new_card? || skip_rev_id

  hidden_field :last_action_id_before_edit, class: "current_revision_id"
end

#edit_fields?Boolean

override and return true to optimize

Returns:

  • (Boolean)


111
112
113
# File 'tmpsets/set/mod023-edit/all/form.rb', line 111

def edit_fields?
  edit_fields.present?
end

#edit_in_form_prefixObject



183
184
185
# File 'tmpsets/set/mod023-edit/all/form.rb', line 183

def edit_in_form_prefix
  "#{parent.form_prefix}[subcards][#{card.name.from form_context.card.name}]"
end

#edit_slotObject



88
89
90
91
92
93
94
95
# File 'tmpsets/set/mod023-edit/all/form.rb', line 88

def edit_slot
  case
  when inline_nests_editor?  then _render_core
  when multi_card_editor?    then multi_card_edit(true)
  when in_multi_card_editor? then editor_in_multi_card
  else                            single_card_edit_field
  end
end

#editor_in_multi_cardObject



129
130
131
132
133
134
135
# File 'tmpsets/set/mod023-edit/all/form.rb', line 129

def editor_in_multi_card
  add_junction_class
  formgroup render_title,
            editor: "content", help: true, class: classy("card-editor") do
    [content_field, (form.hidden_field(:type_id) if card.new_card?)]
  end
end

#editor_wrap(type = nil) ⇒ Object



247
248
249
250
251
252
253
# File 'tmpsets/set/mod023-edit/all/form.rb', line 247

def editor_wrap type=nil
  html_class = "editor"
  html_class << " #{type}-editor" if type
  wrap_with :div, class: html_class do
    yield
  end
end

#explicit_form_prefixObject



187
188
189
# File 'tmpsets/set/mod023-edit/all/form.rb', line 187

def explicit_form_prefix
  inherit :explicit_form_prefix
end

#formObject

If you use subfield cards to render a form for a new card then the subfield cards should be created on the new card not the existing card that build the form



157
158
159
# File 'tmpsets/set/mod023-edit/all/form.rb', line 157

def form
  @form ||= inherit(:form) || new_form
end

#form_contextObject



191
192
193
# File 'tmpsets/set/mod023-edit/all/form.rb', line 191

def form_context
  form_root? || !form_root ? self : parent
end

#form_prefixObject



170
171
172
173
174
175
176
177
# File 'tmpsets/set/mod023-edit/all/form.rb', line 170

def form_prefix
  case
  when explicit_form_prefix          then explicit_form_prefix # configured
  when simple_form?                  then "card"               # simple form
  when parent.card.name == card.name then parent.form_prefix   # card nests self
  else                                    edit_in_form_prefix
  end
end

#form_rootObject



199
200
201
202
203
# File 'tmpsets/set/mod023-edit/all/form.rb', line 199

def form_root
  return self if @form_root

  parent ? parent.form_root : nil
end

#form_root?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'tmpsets/set/mod023-edit/all/form.rb', line 195

def form_root?
  @form_root == true
end

#hidden_form_tags(_action, opts) ⇒ Object



214
215
216
217
# File 'tmpsets/set/mod023-edit/all/form.rb', line 214

def hidden_form_tags _action, opts
  success = opts.delete :success
  success_tags success
end

#in_multi_card_editor?Boolean

test: are we already within a multi-card form?

Returns:

  • (Boolean)


116
117
118
# File 'tmpsets/set/mod023-edit/all/form.rb', line 116

def in_multi_card_editor?
  @in_multi_card_editor.present?
end

#inline_nests_editor?Boolean

test: render nests within a normal rendering of the card's content? (as opposed to a standardized form)

Returns:

  • (Boolean)


99
100
101
# File 'tmpsets/set/mod023-edit/all/form.rb', line 99

def inline_nests_editor?
  voo.editor == :inline_nests
end

#multi_card_edit(fields_only = false) ⇒ Object



137
138
139
140
141
# File 'tmpsets/set/mod023-edit/all/form.rb', line 137

def multi_card_edit fields_only=false
  nested_cards_for_edit(fields_only).map do |name, options|
    nest name, options || {}
  end.join "\n"
end

#multi_card_editor?Boolean

test: are we opening a new multi-card form?

Returns:

  • (Boolean)


104
105
106
107
108
# File 'tmpsets/set/mod023-edit/all/form.rb', line 104

def multi_card_editor?
  voo.structure || voo.edit_structure || # structure configured in voo
    card.structure ||                    # structure in card rule
    edit_fields?                         # list of fields in card rule
end

#name_fieldObject



44
45
46
47
# File 'tmpsets/set/mod023-edit/all/form.rb', line 44

def name_field
  # value needed because otherwise gets wrong value if there are updates
  text_field :name, value: card.name, autocomplete: "off"
end

#new_formObject



161
162
163
164
# File 'tmpsets/set/mod023-edit/all/form.rb', line 161

def new_form
  @form_root = true unless parent&.form_root
  instantiate_builder(form_prefix, card, {})
end

#process_edit_fields(fields) ⇒ Object

cards or a hash with the fields as keys and a hash with nest options as values

Parameters:

  • fields (Hash|Array)

    either an array with field names and/or field



146
147
148
149
150
# File 'tmpsets/set/mod023-edit/all/form.rb', line 146

def process_edit_fields fields
  fields.map do |field, opts|
    field_nest field, opts
  end.join "\n"
end

#reset_formObject



166
167
168
# File 'tmpsets/set/mod023-edit/all/form.rb', line 166

def reset_form
  @form = new_form
end

#simple_form?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'tmpsets/set/mod023-edit/all/form.rb', line 179

def simple_form?
  form_root? || !form_root || !parent
end

#single_card_edit_fieldObject



120
121
122
123
124
125
126
127
# File 'tmpsets/set/mod023-edit/all/form.rb', line 120

def single_card_edit_field
  if voo.show?(:type_formgroup) || voo.show?(:name_formgroup)
    # display content field in formgroup for consistency with other fields
    formgroup("content", editor: :content, help: false) { content_field }
  else
    editor_wrap(:content) { content_field }
  end
end

#type_field(args = {}) ⇒ Object



49
50
51
52
53
54
55
# File 'tmpsets/set/mod023-edit/all/form.rb', line 49

def type_field args={}
  typelist = Auth.createable_types
  current_type = type_field_current_value args, typelist
  options = options_from_collection_for_select typelist, :to_s, :to_s,
                                               current_type
  template.select_tag "card[type]", options, args
end

#type_field_current_value(args, typelist) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'tmpsets/set/mod023-edit/all/form.rb', line 57

def type_field_current_value args, typelist
  return if args.delete :no_current_type

  if !card.new_card? && !typelist.include?(card.type_name)
    # current type should be an option on existing cards,
    # regardless of create perms
    typelist.push(card.type_name).sort!
  end
  card.type_name_or_default
end

#wrap_type_formgroupObject



32
33
34
35
36
# File 'tmpsets/set/mod023-edit/all/form.rb', line 32

def wrap_type_formgroup
  formgroup "type", editor: "type", class: "type-formgroup", help: false do
    yield
  end
end