Class: ReddeFormBuilder

Inherits:
ActionView::Helpers::FormBuilder
  • Object
show all
Defined in:
app/form_builders/redde_form_builder.rb

Instance Method Summary collapse

Instance Method Details

#empty_wrap(*args, &block) ⇒ Object



96
97
98
99
100
# File 'app/form_builders/redde_form_builder.rb', line 96

def empty_wrap(*args, &block)
  options = args.extract_options!
  content = block_given? ? capture(&block) : args[0]
  ( :tr, tag( :td, class: ['redde-form__cell', options[:cell]].flatten.compact ) + ( :td, content, class: ['redde-form__cell', options[:cell]] ), class: ['redde-form__row', options[:wrap]].flatten.compact )
end

#error_messages(attrs = {}) ⇒ Object



90
91
92
93
94
# File 'app/form_builders/redde_form_builder.rb', line 90

def error_messages(attrs = {})
  if object.errors.full_messages.any?
    render 'admin/redde/validate', { f: self, attrs: attrs }
  end
end

#fieldset(name, *args, &block) ⇒ Object



108
109
110
111
112
113
114
# File 'app/form_builders/redde_form_builder.rb', line 108

def fieldset(name, *args, &block)
  options = args.extract_options!
  (:tbody, class: ['redde-form__fieldset', options[:class]].flatten.compact) do
    concat (:tr, (:th, name, colspan: 2, class: 'redde-form__thead'))
    concat capture(&block)
  end
end

#redde_check_box(name, *args) ⇒ Object



32
33
34
35
36
37
38
# File 'app/form_builders/redde_form_builder.rb', line 32

def redde_check_box(name, *args)
  options = args.extract_options!
   :tr, class: options[:wrapper_class] do
    concat tag :td
    concat  :td, check_box(name, options) + " " + smart_label(name),  class: 'redde-form__cell'
  end
end

#redde_date_time(name, *args) ⇒ Object



27
28
29
30
# File 'app/form_builders/redde_form_builder.rb', line 27

def redde_date_time(name, *args)
  options = args.extract_options!
  wrap(name, datetime_select(name, options), options)
end

#redde_field(name, *args) ⇒ Object

delegate :debug, :render, :content_tag, :tag, :link_to, :concat, :capture, to: :@template



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'app/form_builders/redde_form_builder.rb', line 5

def redde_field(name, *args)
  label(name)
  case object.class.columns.detect { |column| column.name == name.to_s }.try(:type)
  when :text then redde_text_area(name, *args)
  when :boolean then redde_check_box(name, *args)
  when :time then redde_date_time(name, *args)
  when :datetime then redde_date_time(name, *args)
  else
    if object.send(name).class.superclass.name == 'CarrierWave::Uploader::Base'
      redde_image_field(name, *args)
    else
      redde_text_field(name, *args)
    end
  end
end

#redde_file_field(name, *args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'app/form_builders/redde_form_builder.rb', line 54

def redde_file_field(name, *args)
  options = args.extract_options!
  wrap(name, options) do
    (:span, class: ['redde-form__file rfile jsr-file', options[:class]]) do
      concat  :span, raw('Загрузить') + (:span, file_field(name, options.merge(class: 'rfile__inp')), class: 'rfile__wrap jsr-file--wrap'), class: 'rfile__btn btn icon-upload'
      concat (:span, '', class: 'rfile__name jsr-file--name')
      concat (:span, raw(' '), title: 'удалить', class: 'rfile__del jsr-file--del', hidden: "")
    end
  end
end

#redde_image_field(name, *args) ⇒ Object



46
47
48
49
50
51
52
# File 'app/form_builders/redde_form_builder.rb', line 46

def redde_image_field(name, *args)
  html = redde_file_field(name, *args)
  if object.send(name).present?
    html = empty_wrap(image_tag(object.send(name), class: 'redde-form__img-preview') + check_box("remove_#{name}") + label("remove_#{name}")).concat(html)
  end
  html
end

#redde_select(name, choices, opts = {}, *args) ⇒ Object



21
22
23
24
25
# File 'app/form_builders/redde_form_builder.rb', line 21

def redde_select(name, choices, opts = {}, *args)
  options = args.extract_options!
  options[:class] = assign_class(['sel', 'redde-form__sel'], options[:class])
  wrap(name, select(name, choices, opts, options.except(:wrap, :cell)), options)
end

#redde_submit(text, opts) ⇒ Object



71
72
73
74
75
76
# File 'app/form_builders/redde_form_builder.rb', line 71

def redde_submit(text, opts)
  css_class = ['sbm']
  css_class << opts[:class] if opts[:class].present?
  css_class << '_save' if text == 'Сохранить'
  button(text, class: css_class, value: text, name: :commit)
end

#redde_submits(*args, &block) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'app/form_builders/redde_form_builder.rb', line 78

def redde_submits *args, &block
   :tfoot do
     :tr do
       :td, colspan: 2, class: 'redde-form__actions' do
        concat redde_submit('Сохранить', class: 'redde-form__sbm')
        concat redde_submit('Применить', class: 'redde-form__sbm')
        concat capture(&block) if block_given?
      end
    end
  end
end

#redde_text_area(name, *args) ⇒ Object



65
66
67
68
69
# File 'app/form_builders/redde_form_builder.rb', line 65

def redde_text_area(name, *args)
  options = args.extract_options!
  options[:class] = assign_class(['txtr', 'redde-form__txtr'], options[:class])
  wrap(name, text_area(name, options), options)
end

#redde_text_field(name, *args) ⇒ Object



40
41
42
43
44
# File 'app/form_builders/redde_form_builder.rb', line 40

def redde_text_field(name, *args)
  options = args.extract_options!
  options[:class] = assign_class(['inp', 'redde-form__inp'], options[:class])
  wrap(name, text_field(name, options), options)
end

#wrap(name = nil, *args, &block) ⇒ Object



102
103
104
105
106
# File 'app/form_builders/redde_form_builder.rb', line 102

def wrap(name = nil, *args, &block)
  options = args.extract_options!
  content = block_given? ? capture(&block) : args[0]
  ( :tr, ( :td, smart_label(name), class: ['redde-form__cell', '_label', options[:cell]].flatten.compact ) + ( :td, content, class: ['redde-form__cell', options[:cell]] ), class: ['redde-form__row', ('_error' if object.errors[name].any?), options[:wrap]].flatten.compact )
end