Class: Backend::FormBuilder

Inherits:
SimpleForm::FormBuilder
  • Object
show all
Defined in:
app/helpers/backend/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#abilities_list(attribute_name = :abilities_list, options = {}) ⇒ Object


217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/helpers/backend/form_builder.rb', line 217

def abilities_list(attribute_name = :abilities_list, options = {})
  prefix = @lookup_model_names.first +
           @lookup_model_names[1..-1].collect { |x| "[#{x}]" }.join +
           "[#{attribute_name}][]"
  input(attribute_name, options) do
    data_lists = {}
    @template.(:span, class: 'control-group abilities-list') do
      abilities_for_select = Nomen::Ability.list.sort { |a, b| a.human_name <=> b.human_name }.map do |a|
        attrs = { value: a.name }
        if a.parameters
          a.parameters.each do |parameter|
            if parameter == :variety
              data_lists[parameter] ||= Nomen::Variety.selection
            elsif parameter == :issue_nature
              data_lists[parameter] ||= Nomen::IssueNature.selection
            else
              raise "Unknown parameter type for an ability: #{parameter.inspect}"
            end
          end
          attrs[:data] = { ability_parameters: a.parameters.join(', ') }
        end
        @template.(:option, a.human_name, attrs)
      end.join.html_safe
      widget = @template.(:span, class: 'abilities') do
        if list = @object.send(attribute_name)
          list.collect do |a|
            ar = a.to_s.split(/[\(\,\s\)]+/).compact
            ability = Nomen::Ability[ar.shift]
            @template.(:div, data: { ability: ability.name }, class: :ability) do
              html = @template.label_tag ability.human_name
              html << @template.hidden_field_tag(prefix, a, class: 'ability-value')
              ar.each_with_index do |p, index|
                html << @template.select_tag(nil, @template.options_for_select(data_lists[ability.parameters[index]], p), data: { ability_parameter: index })
              end
              html << @template.link_to('#', data: { remove_closest: '.ability' }) do
                @template.(:i)
              end
              html
            end
          end.join.html_safe
        end
      end
      attrs = {}
      widget << @template.select_tag('ability-creator', abilities_for_select, name: nil)
      widget << ' '.html_safe
      widget << @template.link_to(:add.tl, '#ability-creator', data: { add_ability: prefix })
      data_lists.each do |key, selection|
        widget << @template.(:datalist, @template.options_for_select(selection), data: { ability_parameter_list: key })
      end
      widget
    end
  end
end

#access_control_list(name = :rights) ⇒ Object


583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'app/helpers/backend/form_builder.rb', line 583

def access_control_list(name = :rights)
  prefix = @lookup_model_names.first + @lookup_model_names[1..-1].collect { |x| "[#{x}]" }.join
  html = ''.html_safe
  reference = (@object.send(name) || {}).with_indifferent_access
  for resource, rights in Ekylibre::Access.resources.sort { |a, b| Ekylibre::Access.human_resource_name(a.first) <=> Ekylibre::Access.human_resource_name(b.first) }
    resource_reference = reference[resource] || []
    html << @template.(:div, class: 'control-group booleans') do
      @template.(:label, class: 'control-label') do
        Ekylibre::Access.human_resource_name(resource)
      end +
        @template.(:div, class: 'controls') do
          rights.collect do |interaction, right|
            checked = resource_reference.include?(interaction.to_s)
            attributes = { class: "chk-access chk-access-#{interaction}", data: { access: "#{interaction}-#{resource}" } }
            if right.dependencies
              attributes[:data][:need_accesses] = right.dependencies.join(' ')
            end
            attributes[:class] << ' active' if checked
            @template.(:label, attributes) do
              @template.check_box_tag("#{prefix}[#{name}][#{resource}][]", interaction, checked) +
                ERB::Util.h(Ekylibre::Access.human_interaction_name(interaction).strip)
            end
          end.join.html_safe
        end
    end
  end
  html
end

#actionsObject


616
617
618
619
620
621
622
623
624
625
626
627
628
629
# File 'app/helpers/backend/form_builder.rb', line 616

def actions
  return nil unless @actions.any?
  @template.form_actions do
    html = ''.html_safe
    for action in @actions
      html += if action[:type] == :block
                action[:content].html_safe
              else
                @template.send(action[:type], *action[:args])
              end
    end
    html
  end
end

#add(type = :block, *args, &block) ⇒ Object


631
632
633
634
635
636
637
638
639
640
# File 'app/helpers/backend/form_builder.rb', line 631

def add(type = :block, *args, &block)
  @actions ||= []
  if type == :block
    @actions << { type: type, content: @template.capture(&block) }
  else
    type = { submit: :submit_tag, link: :link_to }[type] || type
    @actions << { type: type, args: args }
  end
  true
end

#attachmentsObject


110
111
112
# File 'app/helpers/backend/form_builder.rb', line 110

def attachments
  nested_association :attachments
end

#attachments_field_setObject


104
105
106
107
108
# File 'app/helpers/backend/form_builder.rb', line 104

def attachments_field_set
  @template.field_set(:attachments) do
    attachments
  end
end

#backend_fields_for(*args, &block) ⇒ Object


420
421
422
423
424
425
426
427
428
429
430
431
# File 'app/helpers/backend/form_builder.rb', line 420

def backend_fields_for(*args, &block)
  options = args.extract_options!
  options[:wrapper] = self.options[:wrapper] if options[:wrapper].nil?
  options[:defaults] ||= self.options[:defaults]

  options[:builder] ||= if self.class < ActionView::Helpers::FormBuilder
                          self.class
                        else
                          Backend::FormBuilder
                        end
  fields_for(*(args << options), &block)
end

#clear_actions!Object


642
643
644
# File 'app/helpers/backend/form_builder.rb', line 642

def clear_actions!
  @actions = []
end

#custom_fieldsObject

Adds custom fields


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/helpers/backend/form_builder.rb', line 81

def custom_fields
  return nil unless @object.customizable?
  custom_fields = @object.class.custom_fields
  return nil unless custom_fields.any?
  @template.(:div, class: 'custom-fields') do
    simple_fields_for(:custom_fields, OpenStruct.new(@object.custom_fields)) do |cff|
      custom_fields.map do |custom_field|
        options = { as: custom_field.nature.to_sym, required: custom_field.required?, label: custom_field.name }
        if custom_field.choice?
          options[:collection] = custom_field.choices.collect { |c| [c.name, c.value] }
        end

        if custom_field.nature.to_sym == :number
          options[:as] = :string
          options[:input_html] = { pattern: '[0-9]+([.][0-9]+)?' }
        end

        cff.input(custom_field.column_name, options)
      end.join.html_safe
    end
  end
end

#date_range(start_attribute_name = :started_on, stop_attribute_name = :stopped_on, *args) ⇒ Object


396
397
398
399
400
401
402
403
404
405
# File 'app/helpers/backend/form_builder.rb', line 396

def date_range(start_attribute_name = :started_on, stop_attribute_name = :stopped_on, *args)
  options = args.extract_options!
  attribute_name = args.shift || options[:name] || :period
  input(attribute_name, options.merge(wrapper: :append)) do
    @template.(:span, :from.tl, class: 'add-on') +
      input(start_attribute_name, wrapper: :simplest) +
      @template.(:span, :to.tl, class: 'add-on') +
      input(stop_attribute_name, wrapper: :simplest)
  end
end

#datetime_range(*args) ⇒ Object


383
384
385
386
387
388
389
390
391
392
393
394
# File 'app/helpers/backend/form_builder.rb', line 383

def datetime_range(*args)
  options = args.extract_options!
  start_attribute_name = args.shift || :started_at
  stop_attribute_name = args.shift || :stopped_at
  attribute_name = args.shift || options[:name] || :period
  input(attribute_name, options.merge(wrapper: :append)) do
    @template.(:span, :from.tl, class: 'add-on') +
      input_field(start_attribute_name, options[:start_input_html] || options[:input_html]) +
      @template.(:span, :to.tl, class: 'add-on') +
      input_field(stop_attribute_name, options[:stop_input_html] || options[:input_html])
  end
end

#fields(partial = 'form') ⇒ Object


612
613
614
# File 'app/helpers/backend/form_builder.rb', line 612

def fields(partial = 'form')
  @template.(:div, @template.render(partial, f: self), class: 'form-fields')
end

#geolocation(attribute_name = :geolocation, options = {}) ⇒ Object


329
330
331
# File 'app/helpers/backend/form_builder.rb', line 329

def geolocation(attribute_name = :geolocation, options = {})
  point(attribute_name, options)
end

#geolocation_field(attribute_name = :geolocation, options = {}) ⇒ Object


333
334
335
# File 'app/helpers/backend/form_builder.rb', line 333

def geolocation_field(attribute_name = :geolocation, options = {})
  point_field(attribute_name, options)
end

#geometry(attribute_name, options = {}) ⇒ Object


283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'app/helpers/backend/form_builder.rb', line 283

def geometry(attribute_name, options = {})
  editor = options[:editor] || {}
  editor[:controls] ||= {}
  editor[:controls][:draw] ||= {}
  editor[:controls][:draw][:draw] = options[:draw] || {}
  editor[:controls][:importers] ||= { formats: [:gml, :kml, :geojson], title: :import.tl, okText: :import.tl, cancelText: :close.tl }
  editor[:controls][:importers][:content] ||= @template.importer_form(editor[:controls][:importers][:formats])

  geom = @object.send(attribute_name)
  if geom
    editor[:edit] = Charta.new_geometry(geom).to_json_object
    editor[:view] = { center: Charta.new_geometry(geom).centroid, zoom: 16 }
  else
    if sibling = @object.class.where("#{attribute_name} IS NOT NULL").first
      editor[:view] = { center: Charta.new_geometry(sibling.send(attribute_name)).centroid }
    elsif zone = CultivableZone.first
      editor[:view] = { center: zone.shape_centroid }
    end
  end
  show = options.delete(:show)
  unless show.is_a?(FalseClass)
    show ||= @object.class.where.not(attribute_name => nil)
    union = Charta.empty_geometry
    if show.any?
      union = show.geom_union(attribute_name)
      editor[:show] = union.to_json_object unless union.empty?
    end
  end
  editor[:back] ||= MapBackground.availables.collect(&:to_json_object)

  input(attribute_name, options.deep_merge(input_html: { data: { map_editor: editor } }))
end

#input(attribute_name, options = {}, &block) ⇒ Object

Updates default input method


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/helpers/backend/form_builder.rb', line 156

def input(attribute_name, options = {}, &block)
  options[:input_html] ||= {}
  if options[:show]
    options[:input_html]['data-show'] = clean_targets(options.delete(:show))
  end
  if options[:hide]
    options[:input_html]['data-hide'] = clean_targets(options.delete(:hide))
  end
  autocomplete = options[:autocomplete]
  if autocomplete
    autocomplete = {} if autocomplete.is_a?(TrueClass)
    autocomplete[:column] ||= attribute_name.to_s
    autocomplete[:action] ||= :autocomplete
    autocomplete[:format] ||= :json
    options[:input_html]['data-autocomplete'] = @template.url_for(autocomplete)
  end
  super(attribute_name, options, &block)
end

#items_list(attribute_name, options = {}) ⇒ Object


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/helpers/backend/form_builder.rb', line 186

def items_list(attribute_name, options = {})
  prefix = @lookup_model_names.first +
           @lookup_model_names[1..-1].collect { |x| "[#{x}]" }.join +
           "[#{attribute_name}][]"
  if options[:selection]
    selection = options[:selection]
  elsif options[:of]
    selection = []
    if options[:of].to_s =~ /\#/
      array = options[:of].to_s.split('#')
      selection = Nomen[array.first].property_natures[array.second].selection
    else
      selection = Nomen[options[:of]].selection
    end
  else
    raise 'Need selection'
  end
  list = @object.send(attribute_name) || []
  input(attribute_name, options) do
    @template.(:span, class: 'control-group symbol-list') do
      selection.collect do |human_name, name|
        checked = list.include?(name.to_sym)
        @template.label_tag(nil, class: "nomenclature-item#{' checked' if checked}") do
          @template.check_box_tag(prefix, name, checked) +
            @template.(:span, human_name)
        end
      end.join.html_safe
    end
  end
end

#linestring(attribute_name, options = {}) ⇒ Object


279
280
281
# File 'app/helpers/backend/form_builder.rb', line 279

def linestring(attribute_name, options = {})
  geometry(attribute_name, options.merge(draw: { polygon: false, polyline: true }))
end

#money(attribute_name, *args) ⇒ Object


373
374
375
376
377
378
379
380
381
# File 'app/helpers/backend/form_builder.rb', line 373

def money(attribute_name, *args)
  options = args.extract_options!
  currency_attribute_name = args.shift || options[:currency_attribute] || :currency
  input(attribute_name, options.merge(wrapper: :append)) do
    html = input_field(attribute_name)
    html << input_field(currency_attribute_name, collection: Nomen::Currency.items.values.collect { |c| [c.human_name, c.name.to_s] }.sort)
    html
  end
end

#nested_association(association, *args) ⇒ Object

Adds nested association support


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/helpers/backend/form_builder.rb', line 47

def nested_association(association, *args)
  options = args.extract_options!
  reflection = find_association_reflection(association)
  raise "Association #{association.inspect} not found" unless reflection
  if block_given?
    ActiveSupport::Deprecation.warn "Nested association don't take code block anymore. Use partial '#{association.to_s.singularize}_fields' instead."
  end
  # raise ArgumentError.new("Reflection #{reflection.name} must be a has_many") if reflection.macro != :has_many
  item = association.to_s.singularize
  partial = options[:partial] || item + '_fields'
  options[:locals] ||= {}
  html = simple_fields_for(association) do |nested|
    @template.render(partial, options[:locals].merge(f: nested))
  end
  html_options = { id: "#{association}-field", class: "nested-#{association} nested-association" }
  if reflection.macro == :has_many
    unless options[:new].is_a?(FalseClass)
      html << @template.(:div, class: 'links') do
        @template.link_to_add_association(options[:button_label] || "labels.add_#{item}".t, self, association, 'data-no-turbolink' => true, partial: partial, render_options: { locals: options[:locals] }, class: "nested-add add-#{item}")
      end
    end
    if options[:minimum]
      html_options[:data] ||= {}
      html_options[:data][:association_insertion_minimum] = options[:minimum]
    end
    if options[:maximum]
      html_options[:data] ||= {}
      html_options[:data][:association_insertion_maximum] = options[:maximum]
    end
  end
  @template.(:div, html, html_options)
end

#picture(attribute_name = :picture, options = {}) ⇒ Object


175
176
177
178
179
180
181
182
183
184
# File 'app/helpers/backend/form_builder.rb', line 175

def picture(attribute_name = :picture, options = {})
  format = options.delete(:format) || :thumb
  input(attribute_name, options) do
    html = file_field(attribute_name)
    if @object.send(attribute_name).file?
      html << @template.(:div, @template.image_tag(@object.send(attribute_name).url(format)), class: 'preview picture')
    end
    html
  end
end

#point(attribute_name, options = {}) ⇒ Object


337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'app/helpers/backend/form_builder.rb', line 337

def point(attribute_name, options = {})
  marker = {}
  if geom = @object.send(attribute_name)
    marker[:marker] = Charta.new_geometry(geom).to_json_object['coordinates'].reverse
    marker[:view] = { center: marker[:marker] }
  else
    if sibling = @object.class.where("#{attribute_name} IS NOT NULL").first
      marker[:view] = { center: Charta.new_geometry(sibling.send(attribute_name)).centroid }
    elsif zone = CultivableZone.first
      marker[:view] = { center: zone.shape_centroid }
    end
    marker[:marker] = marker[:view][:center] if marker[:view]
  end
  marker[:background] ||= MapBackground.availables.collect(&:to_json_object)
  input(attribute_name, options.merge(input_html: { data: { map_marker: marker } }))
end

#point_field(attribute_name, options = {}) ⇒ Object


354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'app/helpers/backend/form_builder.rb', line 354

def point_field(attribute_name, options = {})
  marker = {}
  if geom = @object.send(attribute_name)
    coordinates = Charta.new_geometry(geom).to_json_object['coordinates']
    marker[:marker] = coordinates.reverse if coordinates
    marker[:view] = { center: marker[:marker] }
  else
    if sibling = @object.class.where("#{attribute_name} IS NOT NULL").first
      marker[:view] = { center: Charta.new_geometry(sibling.send(attribute_name)).centroid }
    elsif zone = CultivableZone.first
      marker[:view] = { center: zone.shape_centroid }
    end
    marker[:marker] = marker[:view][:center] if marker[:view]
  end
  marker[:background] ||= MapBackground.availables.collect(&:to_json_object).first
  marker[:background] &&= MapBackground.by_default.to_json_object
  input_field(attribute_name, options.merge(data: { map_marker: marker }))
end

#polygon(attribute_name, options = {}) ⇒ Object


275
276
277
# File 'app/helpers/backend/form_builder.rb', line 275

def polygon(attribute_name, options = {})
  geometry(attribute_name, options)
end

#product_formObject


433
434
435
436
437
438
439
440
# File 'app/helpers/backend/form_builder.rb', line 433

def product_form
  model = object.class
  until @template.lookup_context.exists?("backend/#{model.name.tableize}/form", [], true)
    model = model.superclass
    break if model == ActiveRecord::Base || model == Ekylibre::Record::Base
  end
  @template.render "backend/#{model.name.tableize}/form", f: self
end

#product_form_frame(options = {}, &block) ⇒ Object

Build a frame for all product _forms


443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'app/helpers/backend/form_builder.rb', line 443

def product_form_frame(options = {}, &block)
  html = ''.html_safe

  variant = @object.variant
  unless variant
    variant_id = @template.params[:variant_id]
    variant = ProductNatureVariant.where(id: variant_id.to_i).first if variant_id
  end
  options[:input_html] ||= {}
  options[:input_html][:class] ||= ''

  if variant
    @object.nature ||= variant.nature
    whole_indicators = variant.whole_indicators
    # Add product type selector
    form = @template.field_set options[:input_html] do
      fs = input(:variant_id, value: variant.id, as: :hidden)
      # Add name
      fs << input(:name)
      # Add work number
      fs << input(:work_number) unless options[:work_number].is_a?(FalseClass)
      # Add variant selector
      fs << variety(scope: variant)

      fs << input(:born_at)
      fs << input(:dead_at)

      # error message for indicators
      if Rails.env.development?
        fs << @object.errors.inspect if @object.errors.any?
      end

      # Adds owner fields
      if @object.initializeable?
        fs << @template.render(partial: 'backend/shared/initial_values_form', locals: { f: self })
      end

      # Add custom fields
      fs << custom_fields

      fs << attachments

      fs
    end
    html << form

    # Add form body
    html += if block_given?
              @template.capture(&block)
            else
              @template.render(partial: 'backend/shared/default_product_form', locals: { f: self })
            end

    # Add first indicators
    indicators = variant.variable_indicators.delete_if do |i|
      whole_indicators.include?(i) || [:geolocation, :shape].include?(i.name.to_sym)
    end
    if object.new_record? && indicators.any?

      indicators.each do |indicator|
        @object.readings.build(indicator_name: indicator.name)
      end if @object.readings.empty?

      html << @template.field_set(:indicators) do
        fs = ''.html_safe
        @object.readings.each do |reading|
          indicator = reading.indicator
          # error message for indicators
          if Rails.env.development?
            fs << reading.errors.inspect if reading.errors.any?
          end
          fs << backend_fields_for(:readings, reading) do |indfi|
            indfi.input(:product_id, as: :hidden) + indfi.reading
          end
        end
        fs
      end
    end

  else
    clear_actions!
    variants = ProductNatureVariant.of_variety(@object.class.name.underscore)
    if variants.any?
      html << @template.field_set(:choose_a_type_of_product) do
        #           buttons = ''.html_safe
        #           for variant in ProductNatureVariant.of_variety(@object.class.name.underscore)
        #             buttons << @template.link_to(variant.name, { action: :new, variant_id: variant.id }, class: 'btn')
        #           end
        #           buttons
        choices = {}
        choices[:action] ||= :unroll
        choices[:controller] ||= :product_nature_variants

        new_url = {}
        new_url[:controller] ||= @object.class.name.underscore.pluralize.downcase
        new_url[:action] ||= :new

        choices[:scope] = { of_variety: @object.class.name.underscore.to_sym } unless @object.class.name.blank?

        input_id = :variant_id

        html_options = {}
        html_options[:data] ||= {}
        @template.(:div, class: 'control-group') do
          @template.(:label, Product.human_attribute_name(:variant), class: 'control-label') +
            @template.(:div, class: 'controls') do
              input_field(:variant, html_options.deep_merge(as: :string, id: input_id, data: { selector: @template.url_for(choices), redirect_on_change_url: @template.url_for(new_url) }))
            end
        end
      end
    end

  end

  html
end

#reading(options = {}) ⇒ Object


114
115
116
117
# File 'app/helpers/backend/form_builder.rb', line 114

def reading(options = {})
  indicator = Nomen::Indicator.find!(@object.indicator_name)
  @template.render(partial: 'backend/shared/reading_form', locals: { f: self, indicator: indicator, hidden: (options[:as] == :hidden) })
end

#referenced_association(association, options = {}) ⇒ Object

Display a selector with “new” button


32
33
34
35
36
# File 'app/helpers/backend/form_builder.rb', line 32

def referenced_association(association, options = {})
  return self.association(association, options) if options[:as] == :hidden
  options = referenced_association_input_options(association, options)
  input(options[:reflection].foreign_key, options)
end

#referenced_association_field(association, options = {}) ⇒ Object

Display a selector with “new” button


39
40
41
42
43
44
# File 'app/helpers/backend/form_builder.rb', line 39

def referenced_association_field(association, options = {})
  return self.association(association, options) if options[:as] == :hidden
  options = referenced_association_input_options(association, options)
  html_options = options[:input_html].merge(options.slice(:as, :reflection))
  input_field(options[:reflection].foreign_key, html_options)
end

#referenced_nomenclature(association, options = {}) ⇒ Object

Raises:

  • (ArgumentError)

22
23
24
25
26
27
28
29
# File 'app/helpers/backend/form_builder.rb', line 22

def referenced_nomenclature(association, options = {})
  klass = @object.class
  reflection = klass.nomenclature_reflections[association]
  raise ArgumentError, "Invalid nomenclature reflection: #{association}" unless reflection
  options[:collection] ||= reflection.klass.selection
  options[:label] ||= klass.human_attribute_name(association)
  input(reflection.foreign_key, options)
end

#shape(attribute_name = :shape, options = {}) ⇒ Object


271
272
273
# File 'app/helpers/backend/form_builder.rb', line 271

def shape(attribute_name = :shape, options = {})
  geometry(attribute_name, options)
end

#shape_field(attribute_name = :shape, options = {}) ⇒ Object


316
317
318
319
320
321
322
323
324
325
326
327
# File 'app/helpers/backend/form_builder.rb', line 316

def shape_field(attribute_name = :shape, options = {})
  raise @object.send(attribute_name)
  geometry = Charta.new_geometry(@object.send(attribute_name) || Charta.empty_geometry)
  options[:input_html] ||= {}
  options[:input_html][:data] ||= {}
  options[:input_html][:data][:map_editor] ||= {}
  options[:input_html][:data][:map_editor] ||= {}
  options[:input_html][:data][:map_editor][:back] ||= MapBackground.availables.collect(&:to_json_object)

  # return self.input(attribute_name, options.merge(input_html: {data: {spatial: geometry.to_json_object}}))
  input_field(attribute_name, options.merge(input_html: { data: { map_editor: { edit: geometry.to_json_object } } }))
end

#subset(name, options = {}, &block) ⇒ Object

Load a partial


408
409
410
411
412
413
414
415
416
417
418
# File 'app/helpers/backend/form_builder.rb', line 408

def subset(name, options = {}, &block)
  options[:id] ||= name
  if options[:depend_on]
    options['data-depend-on'] = options.delete(:depend_on)
  end
  if block_given?
    return @template.(:div, capture(&block), options)
  else
    return @template.(:div, @template.render(partial: "#{name}_form", locals: { f: self, object: @object }), options)
  end
end

#variant_quantifier_of(association, *args, &block) ⇒ Object


119
120
121
122
123
# File 'app/helpers/backend/form_builder.rb', line 119

def variant_quantifier_of(association, *args, &block)
  input("#{association}_quantifier", wrapper: :append) do
    variant_quantifier_of_field(association, *args, &block)
  end
end

#variant_quantifier_of_field(association, *args) ⇒ Object


125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/helpers/backend/form_builder.rb', line 125

def variant_quantifier_of_field(association, *args)
  options = args.extract_options!
  unless reflection = find_association_reflection(association)
    raise "Association #{association.inspect} not found"
  end
  indicator_column = options[:indicator_column] || "#{association}_indicator"
  unit_column = options[:unit_column] || "#{association}_unit"
  html_options = { data: { variant_quantifier: "#{@object.class.name.underscore}_#{reflection.foreign_key}" } }
  # Adds quantifier
  [:population, :working_duration].each do |quantifier|
    html_options[:data]["quantifiers_#{quantifier}".to_sym] = true if options[quantifier]
  end
  # Specify scope
  html_options[:data][:use_closest] = options[:closest] if options[:closest]
  option_tags = nil
  if variant = @object.send(association)
    quantifier_id = "#{@object.send(indicator_column)}-#{@object.send(unit_column)}"
    option_tags = variant.unified_quantifiers(options.slice(:population, :working_duration)).map do |quantifier|
      # Please update app/views/backend/product_nature_variants/quantifiers view if you change something here
      attrs = { value: "#{quantifier[:indicator][:name]}-#{quantifier[:unit][:name]}", data: { indicator: quantifier[:indicator][:name], unit: quantifier[:unit][:name], unit_symbol: quantifier[:unit][:symbol] } }
      attrs[:selected] = true if attrs[:value] == quantifier_id
      @template.(:option, :unit_and_indicator.tl(indicator: quantifier[:indicator][:human_name], unit: quantifier[:unit][:human_name]), attrs)
    end.join.html_safe
  end
  html = @template.select_tag("#{association}_quantifier", option_tags, html_options)
  html << input_field(indicator_column, as: :hidden, class: 'quantifier-indicator')
  html << input_field(unit_column, as: :hidden, class: 'quantifier-unit')
  html
end

#variety(options = {}) ⇒ Object


560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
# File 'app/helpers/backend/form_builder.rb', line 560

def variety(options = {})
  scope = options[:scope]
  varieties         = Nomen::Variety.selection(scope ? scope.variety : nil)
  @object.variety ||= (scope ? scope.variety : varieties.first ? varieties.first.last : nil)
  if options[:derivative_of] || (scope && scope.derivative_of)
    derivatives = Nomen::Variety.selection(scope ? scope.derivative_of : nil)
    @object.derivative_of ||= (scope ? scope.derivative_of : derivatives.first ? derivatives.first.last : nil)
    return input(:variety, wrapper: :append, class: :inline) do
      field = ('<span class="add-on">' <<
               ERB::Util.h(:x_of_y.tl(x: '{@@@@[email protected]@@@}', y: '{@@@@[email protected]@@@}')) <<
               '</span>')
      field.gsub!('{@@', '</span>')
      field.gsub!('@@}', '<span class="add-on">')
      field.gsub!('<span class="add-on"></span>', '')
      field.gsub!('@@[email protected]@', input_field(:variety, as: :select, collection: varieties))
      field.gsub!('@@[email protected]@', input_field(:derivative_of, as: :select, collection: derivatives))
      field.html_safe
    end
  else
    return input(:variety, collection: varieties)
  end
end