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

Extended by:
Card::Set::AbstractFormat
Defined in:
card/tmpsets/set/mod012-card-mod-edit/all/form.rb,
card/tmpsets/set/mod011-card-mod-edit/all/form.rb

Instance Method Summary collapse

Instance Method Details

#add_junction_classObject

FIELD VIEWS



247
248
249
250
251
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 247

def add_junction_class
  return unless card.name.junction?

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

#button_formgroup(&block) ⇒ Object



45
46
47
48
49
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 45

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

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



195
196
197
198
199
200
201
202
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 195

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



220
221
222
223
224
225
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 220

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”



212
213
214
215
216
217
218
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 212

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



227
228
229
230
231
232
233
234
235
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 227

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_fieldObject



56
57
58
59
60
61
62
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 56

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

#edit_fields?Boolean

override and return true to optimize

Returns:

  • (Boolean)


92
93
94
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 92

def edit_fields?
  edit_fields.present?
end

#edit_in_form_prefixObject



173
174
175
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 173

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

#edit_slotObject

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



69
70
71
72
73
74
75
76
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 69

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



109
110
111
112
113
114
115
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 109

def editor_in_multi_card
  add_junction_class
  formgroup render_title,
            input: "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, &block) ⇒ Object



237
238
239
240
241
242
243
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 237

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



177
178
179
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 177

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



147
148
149
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 147

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

#form_contextObject



181
182
183
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 181

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

#form_prefixObject



160
161
162
163
164
165
166
167
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 160

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



189
190
191
192
193
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 189

def form_root
  return self if @form_root

  parent ? parent.form_root : nil
end

#form_root?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 185

def form_root?
  @form_root == true
end

#hidden_form_tags(_action, opts) ⇒ Object



204
205
206
207
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 204

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)


97
98
99
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 97

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)


80
81
82
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 80

def inline_nests_editor?
  voo.input_type == :inline_nests
end

#interpret_main_success_opts(opts) ⇒ Object



226
227
228
229
230
# File 'card/tmpsets/set/mod011-card-mod-edit/all/form.rb', line 226

def interpret_main_success_opts opts
  return unless (hash = opts.delete :main_success)

  opts["data-main-success"] = JSON hash
end

#multi_card_edit(fields_only = false) ⇒ Object



117
118
119
120
121
122
123
124
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 117

def multi_card_edit fields_only=false
  field_configs = edit_field_configs fields_only
  return structure_link if field_configs.empty?

  field_configs.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)


85
86
87
88
89
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 85

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



51
52
53
54
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 51

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



151
152
153
154
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 151

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



136
137
138
139
140
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 136

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

#reset_formObject



156
157
158
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 156

def reset_form
  @form = new_form
end

#simple_form?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 169

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

#single_card_edit_fieldObject



101
102
103
104
105
106
107
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 101

def single_card_edit_field
  if voo.show?(:type_formgroup) || voo.show?(:name_formgroup)
    _render_content_formgroup # use formgroup for consistency
  else
    editor_wrap(:content) { content_field }
  end
end


126
127
128
129
130
131
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 126

def structure_link
  # LOCALIZE
  structured = link_to_card card.structure_rule_card, "structured"
  "<label>Content</label>"\
  "<p><em>Uneditable; content is #{structured} without nests</em></p>"
end

#view: conflict_trackerObject



33
34
35
36
37
38
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 33

view :conflict_tracker, cache: :never, unknown: true do
  return unless card&.real?

  card.last_action_id_before_edit = card.last_action_id
  hidden_field :last_action_id_before_edit, class: "current_revision_id"
end

#view: content_formgroupObject

single card content formgroup, labeled with “Content”



23
24
25
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 23

view :content_formgroup, unknown: true, cache: :never do
  wrap_content_formgroup { content_field }
end

#view: content_formgroupsObject

sometimes multiple card formgroups, sometimes just one



12
13
14
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 12

view :content_formgroups, cache: :never do
  wrap_with :fieldset, edit_slot, class: classy("card-editor", "editor")
end

#view: edit_in_formObject



27
28
29
30
31
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 27

view :edit_in_form, cache: :never, perms: :update, unknown: true do
  reset_form
  @in_multi_card_editor = true
  edit_slot
end

#view: name_formgroupObject



16
17
18
19
20
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 16

view :name_formgroup do
  formgroup "Name", input: "name", help: false do
    raw name_field
  end
end

#wrap_content_formgroup(&block) ⇒ Object



40
41
42
43
# File 'card/tmpsets/set/mod012-card-mod-edit/all/form.rb', line 40

def wrap_content_formgroup
  formgroup("Content", input: :content, help: false,
                       class: classy("card-editor")) { yield }
end