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

Extended by:
Card::Set::AbstractFormat
Defined in:
platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb

Instance Method Summary collapse

Instance Method Details

#add_junction_classObject

FIELD VIEWS



257
258
259
260
261
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 257

def add_junction_class
  return unless card.name.compound?

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

#button_formgroup(&block) ⇒ Object



45
46
47
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 45

def button_formgroup &block
  wrap_with :div, class: "form-group #{classy 'button-form-group'}", &block
end

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



200
201
202
203
204
205
206
207
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 200

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



225
226
227
228
229
230
231
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 225

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
  interpret_main_success_opts opts
  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”



217
218
219
220
221
222
223
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 217

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



239
240
241
242
243
244
245
246
247
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 239

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



54
55
56
57
58
59
60
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 54

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)


90
91
92
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 90

def edit_fields?
  edit_fields.present?
end

#edit_in_form_prefixObject



178
179
180
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 178

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



67
68
69
70
71
72
73
74
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 67

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



107
108
109
110
111
112
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 107

def editor_in_multi_card
  add_junction_class
  wrap_editor_in_multi_card do
    [content_field, (form.hidden_field(:type_id) if card.new_card?)]
  end
end

#editor_wrap(type = nil, &block) ⇒ Object



249
250
251
252
253
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 249

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

#explicit_form_prefixObject



182
183
184
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 182

def explicit_form_prefix
  inherit :explicit_form_prefix
end

#formObject

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



152
153
154
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 152

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

#form_contextObject



186
187
188
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 186

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

#form_prefixObject



165
166
167
168
169
170
171
172
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 165

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



194
195
196
197
198
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 194

def form_root
  return self if @form_root

  parent ? parent.form_root : nil
end

#form_root?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 190

def form_root?
  @form_root == true
end

#hidden_form_tags(_action, opts) ⇒ Object



209
210
211
212
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 209

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)


95
96
97
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 95

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)


78
79
80
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 78

def inline_nests_editor?
  voo.input_type == :inline_nests
end

#interpret_main_success_opts(opts) ⇒ Object



233
234
235
236
237
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 233

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



122
123
124
125
126
127
128
129
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 122

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)


83
84
85
86
87
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 83

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



49
50
51
52
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 49

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



156
157
158
159
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 156

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

#reset_formObject



161
162
163
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 161

def reset_form
  @form = new_form
end

#simple_form?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 174

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

#single_card_edit_fieldObject



99
100
101
102
103
104
105
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 99

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


131
132
133
134
135
136
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 131

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 'platypus/tmp/set/gem-defaults/mod011-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 'platypus/tmp/set/gem-defaults/mod011-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 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 12

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

#view: edit_in_formObject



27
28
29
30
31
# File 'platypus/tmp/set/gem-defaults/mod011-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 'platypus/tmp/set/gem-defaults/mod011-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 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 40

def wrap_content_formgroup &block
  formgroup "Content", input: :content, help: false,
                       class: classy("card-editor"), &block
end

#wrap_editor_in_multi_card(&block) ⇒ Object



114
115
116
117
118
119
120
# File 'platypus/tmp/set/gem-defaults/mod011-edit/all/form.rb', line 114

def wrap_editor_in_multi_card &block
  return yield if input_type == :hidden

  formgroup render_title,
            input: "content", help: true, class: classy("card-editor"),
            &block
end