Class: Basepack::Forms::Import

Inherits:
Base
  • Object
show all
Defined in:
lib/basepack/forms/import.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#association_chain, #factory, #groups, #nested_in, #partial, #resource, #resource_class, #view

Instance Method Summary collapse

Methods inherited from Base

#build_from_factory, #chain, #chain_with_class, #configure, #content_for_field, #default_group, #field, #field_names, #fields, #fields_hash, #group, #has_field?, #hide_field, #hide_fields, #inspect, #inverse_of_nested_in?, #label, #label_plural, #new_form, #new_record?, #permit_params, #render_field, #render_field!, #sanitize_params, #show_fields, #show_only_fields, #translate, #visible_field, #visible_fields, #visible_groups, #with_resource

Constructor Details

#initialize(factory, chain, options = {}) ⇒ Import

Returns a new instance of Import.



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
# File 'lib/basepack/forms/import.rb', line 11

def initialize(factory, chain, options = {})
  super(factory, chain, options)

  @action_name = options[:action_name] || :import

  @builder_default_options = {
    as:       :import,
    html:     { multipart: true, class: 'form-horizontal denser' },
    defaults: { input_html: { class: 'span6'} }
  }

  if options[:show_form]
    @show_form = options[:show_form]
  end

  if options[:edit_form]
    @edit_form = options[:edit_form]
    @edit_form.builder_default_options[:as] = :import
    @edit_form.content_for_form do |form, opt = {}, &block|
      form.render_form!(opt) do
        view.safe_concat form.render_fields
        view.safe_concat(view.(:div, class: "pull-right") do
          form.view.render("forms/buttons/submit_create")
        end)
      end
    end
  end

  if options[:list_form]
    @list_form = options[:list_form]
    @list_form.content_for_row do |form, &block|
      view.(:tr, class: @show_form.resource.id == form.resource.id ? "success" : nil, &block)
    end
    @list_form.content_for_actions do |form|
      result = ''.html_safe
      result << form.render_action("Zobrazit", path(import_id: form.resource.id), "icon-eye-open",
                                   class: @show_form.resource.id == form.resource.id ? 'btn btn-mini disabled' : 'btn btn-mini',
                                  )
      result << form.render_action("Smazat", path(delete_id: form.resource.id), "icon-trash",
                                   class: 'btn btn-mini btn-danger',
                                   method: :delete,
                                   data: { confirm: I18n.t('admin.form.confirmation') })
      result
    end
  end
end

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



8
9
10
# File 'lib/basepack/forms/import.rb', line 8

def action_name
  @action_name
end

#builderObject (readonly)

Returns the value of attribute builder.



7
8
9
# File 'lib/basepack/forms/import.rb', line 7

def builder
  @builder
end

#builder_default_optionsObject

Returns the value of attribute builder_default_options.



9
10
11
# File 'lib/basepack/forms/import.rb', line 9

def builder_default_options
  @builder_default_options
end

#edit_formObject (readonly)

Returns the value of attribute edit_form.



4
5
6
# File 'lib/basepack/forms/import.rb', line 4

def edit_form
  @edit_form
end

#list_formObject (readonly)

Returns the value of attribute list_form.



6
7
8
# File 'lib/basepack/forms/import.rb', line 6

def list_form
  @list_form
end

#show_formObject (readonly)

Returns the value of attribute show_form.



5
6
7
# File 'lib/basepack/forms/import.rb', line 5

def show_form
  @show_form
end

Instance Method Details

#configuration_params(&block) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/basepack/forms/import.rb', line 108

def configuration_params(&block)
  if block
    @configuration_params = block
  else
    if @configuration_params
      @configuration_params.(self, @show_form.resource)
    elsif @show_form.resource.respond_to? :configuration_params
      @show_form.resource.configuration_params
    else
      nil
    end
  end
end

#default_partialObject



81
82
83
# File 'lib/basepack/forms/import.rb', line 81

def default_partial
  'forms/import'
end

#field_nested_name(field) ⇒ Object



85
86
87
# File 'lib/basepack/forms/import.rb', line 85

def field_nested_name(field)
  field.form.nested_in ? "#{field_nested_name(field.form.nested_in)}[#{field.method_name}]" : field.method_name
end

#fields_for_import_as_select_optionsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/basepack/forms/import.rb', line 89

def fields_for_import_as_select_options
  @normal = [["Žádný", '']]
  @association = []

  visible_fields.each do |field|
    if !field.association?
      @normal << [field.label.to_s, field.method_name.to_s]
    elsif nform = field.nform
      # nested form
      next if field.multiple? or !field.nested_form
      nform.visible_fields.each do |f|
        @association << [f.nested_label, field_nested_name(f)]
      end
    end
  end

  @normal + @association
end

#path(params = {}) ⇒ Object



58
59
60
# File 'lib/basepack/forms/import.rb', line 58

def path(params = {})
  view.polymorphic_path([@action_name, association_chain, resource_class].flatten, params)
end

#view=(view) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/basepack/forms/import.rb', line 71

def view=(view)
  super
  if @edit_form
    @edit_form.view = view
    @edit_form.path = path
  end
  @show_form.view = view if @show_form
  @list_form.view = view if @list_form
end

#with_builder(builder, &block) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/basepack/forms/import.rb', line 62

def with_builder(builder, &block)
  @builder = builder
  begin
    yield(self)
  ensure
    @builder = nil
  end
end