aboutsummaryrefslogtreecommitdiffstats
path: root/app/helpers/exports_helper.rb
blob: cd177b424b273dce0441bb6d95cd6fbe7d49e212 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
79
80
81
82
83
84
85
module ExportsHelper

  def fields_for_export_type(form)
    partial_name = "fields_#{form.object.type.underscore}"

    begin
      render :partial => partial_name, :locals => { :form => form }
    rescue ActionView::MissingTemplate
      ""
    end
  end

  @@export_references_type = {}
  def export_references_type(type)
    @@export_references_type[type] ||= ReferencesTypeHelper.new(type)
  end

  class ReferencesTypeHelper
    
    attr_accessor :type

    def initialize(type)
      @type = type.to_s
    end

    def relation_name
      Export.references_relation(type)
    end

    def input_id
      "export_reference_#{relation_name}_ids"
    end

    def input_class
      "export_#{relation_name}_reference_ids"
    end

  end

  def export_references_input(form, export, type)
    ReferencesInput.new form, export, export_references_type(type)
  end

  class ReferencesInput

    attr_accessor :form, :export, :type_helper
    def initialize(form, export, type_helper)
      @form, @export, @type_helper = form, export, type_helper
    end

    delegate :input_id, :type, :input_class, :to => :type_helper

    def current?
      export.references_type == type
    end

    def references_map
      references.collect { |child| { :id => child.id, :name => child.name } } 
    end

    def wrapper_html_options
      { 
        :class => "export_reference_ids", 
        :id => "#{input_id}_input", 
        :"data-type" => type, 
        :style => ("display: none" unless current?) 
      }
    end

    def input_html_options
      { 
        :id => input_id, 
        :class =>  input_class,
        :"data-pre" => ( references_map.to_json if current? ),
        :disabled => (true unless current?)
      }
    end
    
    def input
      form.input :reference_ids, :as => :text, :wrapper_html => wrapper_html_options, :input_html => input_html_options
    end

  end

end